I want to enable the snapping options for the the existing layers using python programming. How can I do this?
1 Answer
I found a post here of someone who developed an Azimuth and Distance plugin which involved using snapping. You could take a look into the source code, it might lead you to the right direction.
Here is the script from the link posted:
def canvasPressEvent(self,event):
pixels=event.pos()
snapper=QgsMapCanvasSnapper(self.canvas)
snapped=snapper.snapToBackgroundLayers(pixels)
if len(snapped[1])>0:
xy=snapped[1][0].afterVertex
else:
#transforming pixels to x,y
transform = self.canvas.getCoordinateTransform()
xy = transform.toMapCoordinates(pixels) #captures the clicked coordinate and transform
self.emit(SIGNAL("finished(PyQt_PyObject)"),xy) #return QgsPoint in a signal
-
Thank you. But i want to enable snapping options before callling this trigger action. qgis.utils.iface.actionSplitFeatures().trigger() i dont want to link up with any events. Is there any possible way to do ?– user99Commented Feb 11, 2015 at 9:34
-
When i am trying the following piece of code i am getting the following error: snapper = QgsMapCanvasSnapper(canvas) snapper.setSnapSettingsForLayer(aLayer,True,SnapToVertexAndSegment,pixels,0.0001,True) Traceback (most recent call last): File "<input>", line 1, in <module> AttributeError: 'QgsMapCanvasSnapper' object has no attribute 'setSnapSettingsForLayer' So how can i set the snapping to the layers?– user99Commented Feb 11, 2015 at 12:08
-
Apologies but I am not experienced in this enough, hopefully others will advise.– JosephCommented Feb 12, 2015 at 10:20