TransWikia.com

Creating point precisely on line

Geographic Information Systems Asked by sweet.sugar.cola on November 16, 2020

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 GRASS GIS). 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?

2 Answers

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.

Answered by Alexandre Neto on November 16, 2020

@sweet.sugar.cola answered to her own question

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!

Answered by Taras on November 16, 2020

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