TransWikia.com

Split line at newly created point feature

Geographic Information Systems Asked by Vikram Aundhekar on February 23, 2021

I have one Point layer and Multiple line layers. I always wants to Maintain Topology between them. For which, If user adds any new feature in Point Layer, exactly snapped with line feature, the line feature should split at that point. I checked the manual way like ‘split feature’ tool available, where I need to split line first and place my point feature to that location. I checked Class: QgsSnappingUtils, to identify if the point is snapped to line before splitting it but not getting any work around. Please suggest how to proceed. Thank you.

One Answer

You can find below a way to get the nearest edge for a layer and verify the result is what you expect (by comparing input point and output point)

from qgis.core import (
    QgsFeature,
    QgsGeometry,
    QgsPointXY,
    QgsProject,
    QgsSnappingUtils,
    QgsVectorLayer,
)

# Current layer
yourlayer = iface.activeLayer()
# New snappingUtils
snappingUtils = QgsSnappingUtils()
# Locator for your layer
locator = snappingUtils.locatorForLayer(yourlayer)

# Input coords = like if you got the coordinates from mouse
point_coords = QgsPointXY(14.378, 35.962)
# Find nearest edge
match = locator.nearestEdge(point_coords, 5.0)
# Check
print(match.hasEdge())
print(match.point())

# Check it works visually
vl = QgsVectorLayer("Point", "temp", "memory")
pr = vl.dataProvider()

# Original point
f = QgsFeature()
f.setGeometry(QgsGeometry.fromPointXY(point_coords))
pr.addFeature(f)

# Matched point
f = QgsFeature()
f.setGeometry(QgsGeometry.fromPointXY(match.point()))
pr.addFeature(f)

vl.updateExtents()
QgsProject.instance().addMapLayer(vl)

Answered by ThomasG77 on February 23, 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