4

In QGIS I wanted to move points to the nearest lines. I checked the solutions here, but there are using tools which I am not familiar to yet(POSTGIS or GRASSGIS). I figured out my own way with using QGIS but it is giving me weird results.

My problem is that moved points are not exactly on the lines, there is 0.0000 length line beetwen them.

Working process: I connected the lines with points using v.net.connect, then I extracted only the lines beetween points and orignal lines layer (let's call them join). I created start and ending points on the join layer using small python code:

from qgis.core import * 
import qgis.utils

layers = iface.legendInterface().layers()

points = []

for i in layers[2].getFeatures():
    geom = i.geometry()
    a = geom.asPolyline()
    points.append(a)

vl = QgsVectorLayer("Point", "beg_end", "memory") 
pr = vl.dataProvider()

vl.startEditing()

for i in range(len(points)-1):
    fet = QgsFeature()
    fet2 = QgsFeature()
    fet.setGeometry(QgsGeometry.fromPoint(QgsPoint(points[i][0])))
    fet2.setGeometry(QgsGeometry.fromPoint(QgsPoint(points[i][len(points[i])-1])))
    pr.addFeatures([fet])
    pr.addFeatures([fet2])

vl.commitChanges()
vl.updateExtents()

QgsMapLayerRegistry.instance().addMapLayer(vl)

In the end I saved the end points of the join which are supposed to lie on my line layer (and they look like they are there). But if I use connect tool from networks plugin it turns out that it is creating 0.0000 length line between moved points and original lines to join them.

Why the points are not on the lines and what is the reason of this small differences? Is it possible to snap them to the lines and how?


I solved my problem, trying different approach suggested in answers (creating new vertex on a line, as it is not possible to create point directly on the line).

I use "connect" tool from Networks plugin. It join the point with closest line, as well as split the line into two parts. Then I create points at the end and beggining of the connection (using the python code above). Finally I save points which lie on original lines layer as a moved ones. Seems to work!

  • It is not mathematically possible for some point values to lie exactly on some line segments using binary computers. Research into floating-point representation will explain why this is the case. – Vince Jun 3 '16 at 10:46
1

Because of precision limitations, as stated in a commen, it might be impossible to make a point be exactly on top of a line segment. On the other hand, if that is mandatory ( for network analysis for example) you may want to do the opposite, create a new vertex for your line in the exact place on the point. That. Should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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