PyQGIS: Measure the Distance Between Geometries


This tutorial will demonstrate how to measure the distance between two geometries in QGIS using Python. First we will measure the distance between points in the same layer. Then we will measure the distance between features in a points layer and a lines layer.

Start by defining names for the two layers newpoints is a point layer, and stream_order is a lines layer that represents streams. The first block of code opens the points layer from the QGIS table of contents. You can check out these blog posts the details on loading layers from a file and from the table of contents.

sLayerName = "newpoints"
tLayerName = "stream_order"

sLayers = QgsProject.instance().mapLayersByName(sLayerName)
sLayer = sLayers[0]

Once the layer is loaded, get the first two features and read their geometry. Check out this post for detailed info about reading geometries. The distance between a target and source geometry is calculated with the distance() function. Here sGeom is the source geometry and tGeom is the target geometry. The distance between the geometries is saved as dist_m.

# Distance between two features in the same layer
sFeat = sLayer.getFeature(0)
tFeat = sLayer.getFeature(1)
sGeom = sFeat.geometry()
tGeom = tFeat.geometry()
dist_m = sGeom.distance(tGeom)
print(dist_m)

Let’s calculate the distance between each point in the source layer (sLayer) and each line in the target Layer (tLayer). First load the target layer and get all the source features (sFeats).

# Distance between all features from two different layers
tLayers = QgsProject.instance().mapLayersByName(tLayerName)
tLayer = tLayers[0]
sFeats = sLayer.getFeatures()

Now loop through each of the source features and read its geometry (sgeom). We’re also going to read all the target layer features (tFeats) at this time. Next write a nested loop that loops through each target feature and reads the target feature’s geometry. Calculate the distance between the source feature and each target feature and print it to the console. When the distance has been measured to each target feature, print a message indicating that the measurements for the source feature are complete.

for sfeat in sFeats:
    sgeom = sfeat.geometry()
    tFeats = tLayer.getFeatures()
    for tfeat in tFeats:
        tgeom = tfeat.geometry()
        dist_pl = sgeom.distance(tgeom)
        print(sfeat.id(), tfeat.id(), dist_pl)
    print(sfeat.id(), 'done')

That’s it! You’ve now measured the distance between all the objects in the layers. See the code block below for the full script, and the video for a step-by step demonstration.

sLayerName = "newpoints"
tLayerName = "stream_order"

sLayers = QgsProject.instance().mapLayersByName(sLayerName)
sLayer = sLayers[0]

# Distance between two features in the same layer
sFeat = sLayer.getFeature(0)
tFeat = sLayer.getFeature(1)
sGeom = sFeat.geometry()
tGeom = tFeat.geometry()
dist_m = sGeom.distance(tGeom)

# Distance between all features from two different layers
tLayers = QgsProject.instance().mapLayersByName(tLayerName)
tLayer = tLayers[0]

sFeats = sLayer.getFeatures()

print(sLayer.featureCount())
for sfeat in sFeats:
    sgeom = sfeat.geometry()
    tFeats = tLayer.getFeatures()
    for tfeat in tFeats:
        tgeom = tfeat.geometry()
        dist_pl = sgeom.distance(tgeom)
        print(sfeat.id(), tfeat.id(), dist_pl)
    print(sfeat.id(), 'done')

Konrad Hafen

Konrad has a Master's Degree in Ecology and a Doctorate Degree in Water Resources and has been performing geospatial analysis and writing code (in multiple programming languages) for over a decade. He writes code to develop models and analysis workflows to predict and evaluate changes to landscapes and water resources. He has published multiple articles in prominent peer-reviewed, scientific journals. Konrad's code and workflow contribute to operational products that inform water and ecosystem management.

Latest Tutorials