Tuesday, March 28, 2023

QGIS: How to Find the Nearest Features on Different Layers

QGIS is free and open source Geographic Information System (GIS) software. It is designed to allow users to view, edit, and analyze geospatial data. Today, I will show you how to find the nearest feature in other layers, and draw the line to connect to the features. For example, I am using earthquake and volcanos data in Sumatera, Indonesia, you can download it here. You can use other data, and the process remains the same.

Earthquake and Volcanos data

The red triangle represents volcanos in Indonesia, and the green dot represents earthquake. I want to find what the nearest volcano to any earthquake. Before we get started, make sure you have activated Processing Toolbox Plugins, go to Plugins -> Manage and Install Plugins and check Processing plugin. Then, go to Processing -> Toolbox to open the Toolbox window, and search for Join attributes by nearest.

Processing Toolbox Window

Fill input layer 1 with Earthquake data and input layer 2 with Volcano data. In this window, you can also set the maximum distance and the max number of nearest neighbors.

Join Attributes By Nearest Window

Click run and the process is completed. Now, we have a new layes named joined layer. Open the attribute tables from joined layer, and you might notice new columns in our earthquake data, such as volcano name, the coordinate where the nearest point is located, and the distance betwen the earthquake and the nearest volcano. For instance, the first row of the table shows that the Bukit Daun Volcano is the closest volcano with a distance of 0.77 degree to the first earthquake data on the table.

Joined Layer Attributes Table

Finally, to draw the line from earthquake data to the nearest volcano, we use the joined layer and open properties menu. Go to Symbology and add a new marker. Change the symbol type to Geometry Generator and LineString/MultiLineString geometry type. In the text field, add this line to draw the line from the current coordinates to nearest_x and nearest_y that we created in the process earlier:

MAKE_LINE($geometry, MAKE_POINT("nearest_x", "nearest_y"))

Joined Layer Geometry Generator Symbol

Apply and now you can see the line from any earthquake to it’s nearest volcano.

Final Result of Drawing Line

Simple isn’t it, it’s because QGIS has many built-in tools to process geospatial data, including finding the nearest point from any features.