Graphics Reference
In-Depth Information
• If we are capturing a Polygon, the geometry will be closed when the user finishes
capturing. This means that we add an extra point to the geometry so that the out-
line begins and ends at the same point.
• When the user finishes capturing a geometry, the geometry will be added to the
layer, and a callback function will be used to tell the application that a new geo-
metry has been added.
Now that we know what we're doing, let's start implementing the CaptureTool class.
The first part of our class definition will look like the following:
class CaptureTool(QgsMapTool):
CAPTURE_LINE = 1
CAPTURE_POLYGON = 2
def __init__(self, canvas, layer, onGeometryAdded,
captureMode):
QgsMapTool.__init__(self, canvas)
self.canvas = canvas
self.layer = layer
self.onGeometryAdded = onGeometryAdded
self.captureMode = captureMode
self.rubberBand = None
self.tempRubberBand = None
self.capturedPoints = []
self.capturing = False
self.setCursor(Qt.CrossCursor)
At the top of our class, we define two constants, CAPTURE_LINE and
CAPTURE_POLYGON , which define the available capture modes. We then have the class
initializer, which will accept the following parameters:
canvas : This is the QgsMapCanvas this map tool will be part of.
layer : This is the QgsVectorLayer the geometry will be added to.
onGeometryAdded : This is a Python-callable object (that is, a method or func-
tion) that will be called when a new geometry has been added to the map layer.
captureMode : This indicates whether we are capturing a LineString or a Poly-
gon geometry.
Search WWH ::




Custom Search