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. In this tutorial,wWe will explore the Distance to nearest hub and Distance matrix tools to carry out the nearest neighbor analysis.
Given the locations of all known significant earthquakes, find out the nearest populated place for each location where the earthquake happened.
We will use NOAA’s National Geophysical Data Center’s Significant Earthquake Database as our layer representing all major earthquakes. Download the tab-delimited earthquake data.
Natural Earth has a nice Populated Places dataset. Download the simple (less columns) dataset
For convenience, you may directly download a copy of both the datasets from the links below:
ne_10m_populated_places_simple.zip
Data Sources: [NGDC] [NATURALEARTH]
ne_10m_populated_places_simple.zip
file in the Browser panel and expand it. Drag the ne_10m_populated_places_simple.shp
file to the canvas.ne_10m_populated_places_simple
loaded in the Layers panel. This layer contains the points representing populated places. Now we will load the earthquakes layer. This layer comes as a Tab Serepated Values (TSV) text file. To load this file, click the Open Data Source Manager button on the Data Source Toolbar. You can also use Ctrl + L
keyboard shortcut.signif.txt
file. Once loaded, the File Format and Geometry Definition fields should be auto-populated with correct values. Click Add followed by Close.signif
layer contains many records without a valid geometry. These records were imported with a NULL geometry. So let’s remove these records first. Go to .signif
as the Input layer and click Run. Once the processing finishes, click Close.Non null geometries
loaded into the Layers panel. We will use this layer instead of the original signif
layer in further analysis. Un-check the signif
layer in the Layers panel to hide it. Now it is time to perform the nearest neighbor analysis. Search and locate the tool. Double-click to launch it.Note
If you need point layer as output, use the Distance to nearest hub (points) tool instead.
Non null geometries
as the Source points layer. Select ne_10m_populated_places_simple
as the Distination hubs layer. Select name
as the Hub layer name attribute. The tool will also compute straight-line distance between the populated place and the nearest earthquake. Set Kilometers
as the Measurement unit. Click Run. Once the processing finishes, click Close.Hub distance
loaded in the Layers panel. This layer has line features connecting each earthquake point to the nearest populated place. Right-click the Hub distance
layer and select Open Attribute Table.Hub distance
layer to hide it. Search and locate the tool.Non null geometries
as the Input point laeyer and I_D
as the Input unique ID field. Set ne_10m_populated_places_simple
as the Target point layer and name
as the Target unique ID field. Select Linear (N*k x 3) distance matrix
as the Output matrix type. The key here is to set the Use only the nearest (k) target points parameter to 1
- which will give you only the nearest neighbor in the output. Click Run to start the matrix calculation. Once the processing finishes, click Close.Distance matrix
will be loaded. Note that the output of this tool is a layer containin MultiPoint geometries. Each feature contains 2 points - source and target. Open the Attribute Table for the layer. You will see that each feature has attributes mapping the earthquake to its nearest populated place. Note that the distance here is in the layer’s CRS units (degrees).Distance matrix
layer and select Properties.Simple marker
sub-renderer and select Geometry generator
as the Symbol layer type. Set LineString / MultiLineString
as the Geometry type. Click the Expression button.make_line(point_n( $geometry, 1), point_n( $geometry, 2))
Distance matrix
layer now rendered with lines instead of points. Note that we did not have to create a new layer for this visualization. The layer still contains MultiPoint geometries, but it is dynamically rendered as lines based on the expression.This work is licensed under a Creative Commons Attribution 4.0 International License