GIS is very useful in analyzing spatial relationship between features. One such analysis is finding out which features are closest to a given feature. There are multiple ways to do this analysis in QGIS. You can do a spatial join using the Join Attributes by Nearest or get the distances to all features from another layer using the Distance Matrix tool from the Processing Toolbox. In this tutorial, we will explore a tool named Distance to nearest hub from the Processing Toolbox that can not only find the distance to the closest feature but join it with a line to it for visualizing the results.
Given the locations of all known earthquakes between years 1900 and 2000, find out the nearest populated place for each location where the earthquake happened.
For this tutorial we will download a dataset of earthquakes between 1900-2000 from NOAA’s National Geophysical Data Center produces a great dataset of all significant earthquakes since 2150 BC. Visit the NOAA NCEI portal and enter Min as
1900and Max as
2000. This will return all earthquake incidents that occurred and were recorded by NOAA between those years. For other specific results, you can filter with different parameters. Click Search.
As a result, we got 2585 earthquake incidents. Click on the Download TSV icon.
Natural Earth는 훌륭한 인구밀집지역 Populated Places 데이터베이스를 가지고 있습니다. 단순 데이터셋`simple (less columns) dataset <http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/10m/cultural/ne_10m_populated_places_simple.zip>`_을 다운 받으십시오.
용이하게 하기 위해 두가지 데이터셋을 아래 링크로부터 바로 다운로드할 수 있습니다.
: guilabel :Browser 패널에서 다운로드 한``ne_10m_populated_places_simple.zip ‘’파일을 찾아 확장하십시오. ``ne_10m_populated_places_simple.shp ‘’파일을 캔버스로 드래그하십시오.
: guilabel :Layers 패널에 새로운 레이어``ne_10m_populated_places_simple ‘’이로드 된 것을 볼 수 있습니다. 이 레이어에는 채워진 장소를 나타내는 점이 포함됩니다. 이제 지진 레이어를로드하겠습니다. 이 계층은 * TSV (Tab Serepated Values) * 텍스트 파일로 제공됩니다. 이 파일을 불러 오려면 : guilabel :데이터 소스 도구 모음`에서 : guilabel :`데이터 소스 관리자 열기`버튼을 클릭하십시오. : kbd :`Ctrl + L 단축키를 사용할 수도 있습니다.
In the Data Source Manager dialog box, select Delimited Text.
Click the … button next to File name and browse to the downloaded
earthquakes-2021-11-25_13-39-30_+0530.tsvfile. Depending upon the operating system, you may not see the file at the downloaded directory. If that is the case, switch to All files (*; .) in the Choose a Delimited Text File to Open dialog. Once opened, select Custom delimiters in the File format section, and check
Tab. In the Geometry definition section, choose Point coordinates. By default X field and Y field values will be auto-populated with the appropriate fields in the input. In our case, they are
Latitude. You can leave the Geometry CRS to the default
EPSG:4326 - WGS 84CRS. If your file contains coordinates in a different CRS, you could select the appropriate CRS here. Click Add followed by Close.
Zoom around and explore both datasets. Each red point represents the location of an earthquake incidence, and each green point represents the location of a populated place. Our goal is to find out the nearest point from the populated places layer for each of the points in the earthquake layer. Let’s inspect the Attribute table of the earthquakes layer. Select the layer and click on Open Attribute Table icon in Toolbar.
2586features, but the data contains few entries with no latitude or longitude infomation. We have to remove that before proceeding further. Close the Attribute Table.
Go totool. Double-click to open it.
In the Remove Null Geometries dialog box, select
earthquakes-2021-11-25_13-39-30_+0530as the Input layer and check the box Also remove empty geometries. Click Run. Once the processing finishes, click Close.
A new layer
Non null geometrieswill be added to the Layers panel. For analysis we will use this layer instead of the original layer. Un-check the
earthquakes-2021-11-25_13-39-30_+0530layer in the Layers panel to hide it. Select the
Non null geometrieslayer and click the Open Attribute Table button from the Attributes Toolbar.
You will see a lower count for total features as all rows with empty latitude and longitude values were removed. Close the attribute table.
Now it is time to perform the nearest neighbor analysis. Search and locate thetool. Double-click to launch it.
We can also add a point layer as output, use the Distance to nearest hub (points) tool for that.
In the Distance to Nearest Hub (Line to Hub) dialog box, select
Non null geometriesas the Source points layer. Select
ne_10m_populated_places_simpleas the Destination hubs layer. Select
nameas the Hub layer name attribute. The tool will also compute the straight-line distance between the populated place and the nearest earthquake. Set
Kilometersas the Measurement unit. Click on
...in Hub Distance and click Save to File… to save the file as
earthquakes_with_nearest_city.gpkg. Click Run. Once the processing finishes, click Close.
Back in the main QGIS window, you will see a new line layer called
earthquakes_with_nearest_cityloaded in the Layers panel. This layer has line features connecting each earthquake point to the nearest populated place. Select the
earthquakes_with_nearest_citylayer and click Open Attribute Tabel icon in Toolbar.
Scroll right to the last columns, and you will see 2 new attributes called HubName and HubDist added to the original earthquake features. This is the name of the distance to the nearest neighbor from the populated places layer.