TransWikia.com

Getting progressive distance polyline and points in QGIS

Geographic Information Systems Asked by MASAY on April 15, 2021

In QGIS, how do I get the distance between a layer of points and a polyline layer? I have tried with a distance matrix but it does not seem to be working,
I have a layer of points and a polyline; the perpendicular to the polyline must be found and then from the point of intersection the distance to the previous vertex must be calculated not the next. Is possible to have distance automatically?

I have tried with a v.net.connect but not working as it serves me. any ideas? I post this photo. I have to find d1, d2 and d3, the distance of the vertex.

enter image description here

This is the example I have export the distance of all points from the left vertex. I have tried with v.net.connect but the distance are from point to point that intersect. Any ideas?

One Answer

You can use pyqgis and closestSegmentWithContext() which for example will return closest point, next vertex nr which you can use with .distance method:

pointlyr = QgsProject.instance().mapLayersByName("points")[0] #Change to match your layer name
linelyr = QgsProject.instance().mapLayersByName("lines")[0] #Change to match your layer name

#List all features
points = [f for f in pointlyr.getFeatures()] #List all features
lines = [f for f in linelyr.getFeatures()] #List all features

d = {}
for p in points:
    sqrdist, closest_pointxy, afterVertex, _ = min([line.geometry().closestSegmentWithContext(p.geometry().asPoint()) for line in lines], key=lambda x: x[0])
    vertex_before = [v for v in lines[0].geometry().vertices()][afterVertex-1]
    dist = QgsPoint(closest_pointxy).distance(vertex_before)
    print(round(dist,0))
    d[p.id()] = dist

#Use "d" dictionary to update attribute or whatever you want to do

enter image description here

Answered by BERA on April 15, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP