0

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.

0

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)
2
  • Thank you for the answer. Sorry for late update as I was little busy. I tried your code but I am getting error message saying "TypeError: QgsPointLocator.nearestEdge(): first argument of unbound method must have type 'QgsPointLocator' ". I checked api documentation, as per the document it should be QgsPointXY but not getting why this error occurred. – Vikram Aundhekar Jun 22 '20 at 12:23
  • Updated code to replace the incriminated line (an error on my side) – ThomasG77 Jun 22 '20 at 12:28

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.