Welcome to this tutorial on Nearest Neighbour Analysis in QGIS.
In this tutorial, we will learn about
Nearest Neighbour Analysis by Distance matrix method.
Statistics using Nearest Neighbour Analysis tool.
Here I am using, Ubuntu Linux OS version. 16.04 QGIS version 2.18
To follow this tutorial learner must be familiar with QGIS interface.
For pre-requisite QGIS tutorials, please use this link.
The files required to practise this tutorial are available in the Code files link.
Please download and extract the contents of the folder.
Here I have the folder with the required file to practise this tutorial.
Double-click to open the folder. Here you will find, Urban areas.shp and Volcanoes.shp.
Volcanoes.shp layer shows active volcanoes in the world.
Urban areas.shp shows populated urban areas of the world.
Open the two shape files in QGIS, select both the files.
Right-click and select Open with QGIS Desktop option from the context menu.
QGIS interface opens with two layers loaded in the layers panel.
Right-click on the volcanoes layer and select zoom to layer option.
On the canvas you will see a map with point features.
Let us label these point features.
Right-click on Volcanoes layer, from the sub menu click on Properties.
Layer Properties dialog-box opens.
Select Labels from the left panel.
Select Show labels for this layer option from the drop down.
In the Label with drop down select Name.
Here you will find various options to modify the label style.
Choose the required style and click on OK button.
On the canvas, points with the names is displayed.
Similarly let us label Urban areas.
On the canvas point features are labeled with their cities.
QGIS has tools to analyze spatial relationships between features.
One such tool is Nearest Neighbour Analysis.
Nearest Neighbour Analysis is used for the following analysis.
Finding distance between two Point features.
Finding features which are closest to a given feature.
First, we will create distance matrix for calculating the distances.
Let us open the attribute table for the Volcanoes layer.
Right-click on Volcanoes layer.
Select Open Attribute Table.
In the attribute table there are multiple columns.
Various attributes for the point features are listed here.
Names of the volcanoes and their locations are also listed here.
Close the attribute table.
Open the attribute table for the Urban areas layer.
Notice the various columns in the table.
You will find names of the cities, countries and other information in this table.
Close the attribute table.
Let us calculate the distance between the active volcanoes and nearest cities.
Click on Vector menu.
Select Analysis Tools.
Select Distance Matrix option from the sub-menu.
Distance Matrix dialog-box opens.
Please read the description about Distance matrix on the right-panel.
By default Parameters tab opens on the screen.
Select the Parameters as shown here.
Select Volcanoes as an Input Point Layer.
Select NAME as Input unique ID field
Select Urban Areas as Target Point Layer.
Select City as Target unique ID field.
Keep Output matrix type as Linear.
Let us find the distance from the volcano to two nearest cities.
Hence, select 2 in Use only the nearest target Points field.
Click on the 3 dots button next to Distance Matrix field.
From the drop-down menu, select Save to file. option.
In the dialog-box, give an appropriate name and location.
Choose Files of type, as CSV .
In the Encoding field choose System. Click on Save button.
In the Distance matrix dialog-box check the check-box for the following. Open output file after running algorithm.
Click on the Run button at the bottom-right corner of the dialog box.
The process will take few seconds.
A new csv layer named as Distance matrix is added in the Layers panel.
Open the attribute table for Distance matrix layer.
In the attribute table there are three columns. The last column is the distance between the volcano and nearest city.
Please note, here the distance is in meters.
This is because the layers are projected in WGS 84 UTM Zone 46N system.
Depending on the CRS, the distance can also be in layer units or in degrees.
Also observe that for each volcano, two nearest cities are listed.
Let us get some statistical analysis for the layers using Nearest neighbour tool.
We will run a nearest neighbour analysis to analyze the distribution of features.
The results will establish, the distribution as clustered, dispersed or random.
Close the attribute table.
Click on Vector menu.
Scroll down and click on the Analysis Tools.
From the sub-menu, select the Nearest Neighbour analysis.
Nearest Neighbour Analysis dialog-box opens.
Read the information given about Nearest neighbour analysis on the right-panel.
Select Volcanoes layer in the Points drop-down.
Click on Run button at the bottom-right corner.
Results window opens.
Some statistical parameters for the volcanoes layer are listed here.
Observed mean distance
Expected mean distance
Nearest neighbour index
Number of point features and Z-Score.
The Nearest Neighbour Index is expressed as the ratio of the Observed Mean Distance to the Expected Mean Distance.
If the index value is less than 1, the pattern exhibits clustering.
If the index value is greater than 1, the trend is towards dispersion.
Here the Nearest Neighbor Index value of 0.2 indicates clustering. Which means, the volcanoes are located close to each other.
Similarly a negative Z-Score also indicates clustering of point features.
Close the Results window.
Save the project using Project menu.
Let us summarize.
In this tutorial we have learnt about,
Nearest Neighbour Analysis by Distance Matrix method. Statistics using Nearest Neighbour Analysis tool.
As an assignment, Create Distance Matrix for nearest 5 volcanoes to urban areas. Hint: Use Urban Areas as Input and K as 5.
Your completed assignment should look as shown here.
The video at the following link summarizes the Spoken Tutorial project. Please download and watch it.
The Spoken Tutorial project team conducts workshops and gives certificates. For more details please write to us.
Please post your timed queries in this forum.
The Spoken Tutorial Project is funded by, MHRD Government of India.
This tutorial is contributed by Ambadas Maske from College of Engineering Pune, Snehalatha Kaliappan and Himanshi Karwanje from IIT Bombay. Thank you for joining.