1

I would like to construct a map tool application that allows only to add some point features.

How do I complete the following code to activate snap mode while editing a layer with PyQGIS 3?

from qgis.core import *
from qgis.gui import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class AddPointTool(QgsMapTool):
   def __init__(self, mapCanvas, layer):
       QgsMapTool.__init__(self, mapCanvas)
       self.layer=layer
       self.setCursor(Qt.CrossCursor)

   def canvasReleaseEvent(self, event):
       point=self.toLayerCoordinates(self.layer, event.pos())
       fields=self.layer.dataProvider().fields()
       feature=QgsFeature()
       feature.setFields(fields)
       feature.setGeometry(QgsGeometry.fromPointXY(point))

       self.layer.addFeature(feature)
       self.layer.updateExtents()
       self.layer.triggerRepaint()

canvas=iface.mapCanvas()
layer=iface.activeLayer()

layer.startEditing()
layer.triggerRepaint()
tool=AddPointTool(canvas,layer)
canvas.setMapTool(tool)
  • The following expression enable snapping for PyQGIS 2.0 : QgsProject.instance().setSnapSettingsForLayer(layer.id(), True, 2, 1, 10, True) What is the equivalent expression in PyQGIS 3.0? – ennine Sep 13 '18 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.