Graphics Reference
In-Depth Information
Editing lines and polygons
The last major functionality we will examine is the ability to edit LineString and Polygon
features. Just as the
CaptureTool
allowed the user to click and drag to create new lines
and polygons, we will implement
EditTool
, which lets the user click and drag to move
the existing feature's vertices. The following image shows what the user will see when they
use this tool to move a vertex:
Our editing tool will also let the user add new vertices by double-clicking on a line seg-
ment, and delete vertices by right-clicking on the same line segment.
Let's define our
EditTool
class:
class EditTool(QgsMapTool):
def __init__(self, mapCanvas, layer, onGeometryChanged):
QgsMapTool.__init__(self, mapCanvas)
self.setCursor(Qt.CrossCursor)
self.layer = layer
self.onGeometryChanged = onGeometryChanged
self.dragging = False
self.feature = None
self.vertex = None
As you can see,
EditTool
is a subclass of
QgsMapTool
, and the initializer accepts
three parameters: the map canvas, the layer to be edited, and an
onGeometryChanged
callable object, which will be called when the user makes a change to a geometry.
Next, we want to define the
canvasPressEvent()
method. We'll start by identifying
the feature that the user clicked on: