Geographic Information Systems Asked on November 1, 2021
I have road network (lines) and points (Address) . With the help of closestSegmentWithContext function
, I join points to nearest lines in QGIS. Is there a way to to generate a unique ID at every joining point as the joining points are drawn manually in below image.
What I have researched so far:
The code for joining line segements to nearest points is given below and shape files are available here:
roadlyr = QgsProject.instance().mapLayersByName('TR_ROAD')[0]
addrlyr = QgsProject.instance().mapLayersByName('ADDRESS')[0]
#List all features
adresses = [f for f in addrlyr.getFeatures()]
roads = [f for f in roadlyr.getFeatures()]
#Create empty vector layer
vl = QgsVectorLayer("LineString?crs={}&index=yes".format(roadlyr.crs().authid()), "myLayer", "memory")
provider = vl.dataProvider()
for adress in adresses:
a = min([road.geometry().closestSegmentWithContext(adress.geometry().asPoint()) for road in roads], key=lambda x: x[0]) #For each road find closest address
gLine = QgsGeometry.fromPolyline([QgsPoint(adress.geometry().asPoint()), QgsPoint(a[1])])
f = QgsFeature()
f.setGeometry(gLine)
provider.addFeature(f)
QgsProject.instance().addMapLayer(vl)
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP