GIS is very useful in analyzing spatial relationship between features. One such analysis is finding out which features are closest to a given feature. QGIS has a tool called Distance Matrix which helps with such analysis. In this tutorial, we will use 2 datasets and find out which points from one layer are closest to which point from the second layer.
Given the locations of all known significant earthquakes, find out the nearest populated place for each location where the earthquake happened.
For convenience, you may directly download a copy of both the datasets from the links below:
You may see some error messages as QGIS tries to import the file. These are valid errors and some rows from the file will not be imported. You can ignore the errors for the purpose of this tutorial.
EPSG: 4326. Verify that is the case in the bottom-right corner. Let’s also open the Populated Places layer. Go to .
ne_10m_populated_places_simple.zipfile and click Open.
signifas the Input point layer and the populated places
ne_10m_populated_places_simpleas the target layer. You also need to select a unique field from each of these layers which is how your results will be displayed. In this analysis, we are looking to get only
1nearest point, so check the Use only the nearest(k) target points, and enter 1. Name your output file
matrix.csv, and click OK. Once the processing finishes, click Close.
A useful thing to note is that you can even perform the analysis with only 1 layer. Select the same layer as both Input and Target. The result would be a nearest neighbor from the same layer instead of a different layer as we have used here.
matrix.csvfile in Notepad or any text editor. QGIS can import CSV files as well, so we will add it to QGIS and view it there. Go to .
matrix.csvfile. Since this file is just text columns, select No geometry (attribute only table) as the Geometry definition. Click OK.
Remember that the distance calculation will be done using the layers’ Coordinate Reference System. Here the distance will be in decimal degrees units because our source layer coordinates are in degrees. If you want distance in meters, reproject the layers before running the tool.
matrixas the Join layer` and
InputIDas the Join field. The Target field would be
I_D. Leave other options to their default values and click OK.
signiflayer by right-clicking and selecting Open Attribute Table.
signiflayer and select Save As....
earthquake_with_places.shp. Make sure the Add saved file to map box is checked and click OK.
signiflayer. As our dataset is quite large, we can run our visualization analysis on a subset of the data. QGIS has a neat feature where you can load a subset of features from a layer without having to export it to a new layer. Right-click the
earthquake_with_placeslayer and select Properties.
"COUNTRY" = 'MEXICO'
ne_10m_populated_places_simplelayer and select Properties.
"adm0name" = 'Mexico'
MMQGIS. Find and install the plugin. See Using Plugins for more details on how to work with plugins. Once you have the plugin installed, go to .
ne_10m_populated_places_simpleas the Hub Point Layer and
nameas the Hub ID Attribute. Similarly, select
earthquake_with_placesas the Spoke Point Layer and
matrix_Taras the Spoke Hub ID Attribute. The hub lines algorithm will go through each of earthquake points and create a line that will join it to the populated place which matches the attribute we specified. Click Browse and name the Output Shapefile as
earthquake_hub_lines.shp. Click OK to start the processing.
earthquake_hub_lineslayer loaded in QGIS. You can see that each earthquake point now has a line that connects it to the nearest populated place.
This work is licensed under a Creative Commons Attribution 4.0 International License