1

I would like to complete the following map tool application that allows only to add some point features, by other functionalities like prompting the user to fill attribute form while editing for each point and save or rollback changes, together with maintaining the same layer styling.

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)

Browse other questions tagged or ask your own question.