1

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?

1
  • Welcome to GIS SE. As a new user, please take the Tour. We use a Focused question/Best answer model here, so Questions get the best Answers when they include details of what exactly has been attempted, and what about the result was problematic. Questions which ask "Is it possible" are of little use, since the real question really ought to be "How is it possible?" Rather than writing that a process doesn't seem to work, you ought to be be writing about the exact process, the exact result, and why that result did not meet your need.
    – Vince
    Aug 4, 2020 at 13:09

1 Answer 1

2

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

4
  • I have tried for my line and my points but it doesn't seem to work well. the distance appears to be incorrect. the meters checked are not correct. moreover on the same line it is possible to know the name of a point geographically located on the next vertex of the line but in a different layer? thanks
    – MASAY
    Aug 7, 2020 at 17:48
  • Whats the coordinate system you are using?
    – BERA
    Aug 7, 2020 at 18:59
  • the problem is not the coordinates. but the fact that this code sees the distance of a point from a line made of two vertices ... I have a polyline made of several vertices with scattered points. that is, point 1 is between the first and second vertex, point 3 between the second and third vertex, and point 2 between the sixth and seventh vertex. however I use the UTM N32 reference system. is there a way to calculate the distance automatically with reference to the right ids with the right vertices?
    – MASAY
    Aug 8, 2020 at 16:17
  • in software global mapper you can select the point, select the polyline click on the chainage option that create that distance. can qgis do the same?
    – MASAY
    Aug 8, 2020 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.