14
qgis.spatialthoughts.com http://qgis.spatialthoughts.com/2013/04/tutorial-nearest-neighbor-analysis.html?pfstyle=wp Tutorial: Nearest Neighbor Analysis using QGIS GIS is very usef ul is analyzing spatial relationship between f eatures. One such analysis is f inding out which f eatures are closest to a given f eature. QGIS has a tool called ‘Distance Matrix’ which helps with such analysis. In this tutorial, we will use 2 datasets and f ind out which points f rom one layer are closest to which point f rom the second layer. The topics covered by this tutorial are Importing CSV f ile to QGIS. Understanding and using the Distance Matrix tool. Using table joins to merge the result of the analysis with the source data. Let’s get started. In this tutorial, we will walk through the process and answer this question. Given the locations of all known signif icant earthquakes, f ind out the nearest populated place f or each location where the earthquake happened. We will be using the Natural Earth Populated Places dataset along with NOAA’s National Geophysical Data Center’s Signif icant Earthquake Database. Follow the instructions in this tutorial to import the Signif icant Earthquake CSV f ile to QGIS. Also open the Natural Earth populated places layer using Layer → Add Vector Layer.

Qgis.spatialthoughts.com-Tutorial Nearest Neighbor Analysis Using QGIS

Embed Size (px)

Citation preview

Page 1: Qgis.spatialthoughts.com-Tutorial Nearest Neighbor Analysis Using QGIS

qgis.spat ialt ho ught s.co mhttp://qgis.spatialthoughts.com/2013/04/tutorial-nearest-neighbor-analysis.html?pfstyle=wp

Tutorial: Nearest Neighbor Analysis using QGIS

GIS is very usef ul is analyzing spatial relationship between f eatures. One such analysis is f inding out whichf eatures are closest to a given f eature. QGIS has a tool called ‘Distance Matrix’ which helps with suchanalysis. In this tutorial, we will use 2 datasets and f ind out which points f rom one layer are closest towhich point f rom the second layer.

The topics covered by this tutorial are

Importing CSV f ile to QGIS.

Understanding and using the Distance Matrix tool.

Using table joins to merge the result of the analysis with the source data.

Let’s get started. In this tutorial, we will walk through the process and answer this question. Given thelocations of all known signif icant earthquakes, f ind out the nearest populated place f or each locationwhere the earthquake happened.

We will be using the Natural Earth Populated Places dataset along with NOAA’s National Geophysical DataCenter ’s Signif icant Earthquake Database.

Follow the instructions in this tutorial to import the Signif icant Earthquake CSV f ile to QGIS. Also open theNatural Earth populated places layer using Layer → Add Vector Layer.

Page 2: Qgis.spatialthoughts.com-Tutorial Nearest Neighbor Analysis Using QGIS

In the screenshot, each green point represents the location of a signif icant earthquake and each blue pointrepresents the location of a populated place. We need a way to f ind out the nearest point f rom thepopulated places layer f or each of the points in the earthquake layer.

Page 3: Qgis.spatialthoughts.com-Tutorial Nearest Neighbor Analysis Using QGIS

We will use a tool called ‘Distance Matrix’ f or this analysis. Open the tool f rom Vector → Analysis Tools →Distance Matrix.

Page 4: Qgis.spatialthoughts.com-Tutorial Nearest Neighbor Analysis Using QGIS

Here select the earthquake layer as the Input point layer and the populated places as the target layer. Youalso need to select a unique f ield f rom each of these layers which is how your results will be displayed. Inthis analysis, we are looking to get only 1 nearest point, so check the box next to ‘Use only the nearest(k)target points, and enter 1. Name your output f ile ‘matrix.csv’, and click OK.

Page 5: Qgis.spatialthoughts.com-Tutorial Nearest Neighbor Analysis Using QGIS

Once your f ile is generated, you can view it in Notepad or any text editor. QGIS can import CSV f iles as well,so we will add it to QGIS and view it there. Click Layer → Vector Layer. Navigate to the path where you saved‘matrix.csv’ and click OK.

Page 6: Qgis.spatialthoughts.com-Tutorial Nearest Neighbor Analysis Using QGIS

You will the the CSV f ile loaded as a table.

Page 7: Qgis.spatialthoughts.com-Tutorial Nearest Neighbor Analysis Using QGIS

Right click on the table layer and select ‘Open Attribute Table’.

Page 8: Qgis.spatialthoughts.com-Tutorial Nearest Neighbor Analysis Using QGIS

Now you will be able to see the content of our results. The InputID f ield contains the f ield name f rom theEarthquake layer. The TargetID f ield contains the name of the f eature f rom the Populated Places layer thatwas the closest to the earthquake point. The Distance f ield is the distance between the 2 points.

Page 9: Qgis.spatialthoughts.com-Tutorial Nearest Neighbor Analysis Using QGIS

This is very close to the result we were looking f or. For some uses, this table would be suf f icient. I willdemonstrate, how we can use Table Joins to integrate this results in our original Earthquake layer. Look atthis tutorial f or more details on Table Joins. Right click on the Earthquake layer, and select Properties.

Page 10: Qgis.spatialthoughts.com-Tutorial Nearest Neighbor Analysis Using QGIS

Go to the Joins tab and click on the ‘+’ button.

Page 11: Qgis.spatialthoughts.com-Tutorial Nearest Neighbor Analysis Using QGIS

We want to join the data f rom our analysis result (matrix.csv) to this layer. We need to select a f ield f romeach of the layers that has the same values. Select the f ields as shown below.

You will see the join appear in the Joins tab. ClickOk.

Page 12: Qgis.spatialthoughts.com-Tutorial Nearest Neighbor Analysis Using QGIS

Now open the attribute table of the Earthquakes layer.

Page 13: Qgis.spatialthoughts.com-Tutorial Nearest Neighbor Analysis Using QGIS

You will see that f or every Earthquake f eature, we now have an attribute which is the nearest neighbor(closest populated place) and the distance to the nearest neighbor.

Page 14: Qgis.spatialthoughts.com-Tutorial Nearest Neighbor Analysis Using QGIS

A usef ul thing to note is that you can even perf orm the analysis with only 1 layer. Select the same layer asboth Input and Target. The result would be a nearest neighbor f rom the same layer instead of a dif f erentlayer as we used here.

Let me know in the comments how you have used this tool and what kind of cool applications you can thinkof using it.