Graphics Reference
In-Depth Information
if self.captureMode == CaptureTool.CAPTURE_POLYGON:
points.append(points[0]) # Close polygon.
return points
Finally, we have the geometryCaptured() method, which responds to the geometry
that is captured. This method creates a new geometry of the given type, adds it as a feature
to the map layer, and uses the onGeometryAdded callable object passed to the initial-
izer of our CaptureTool , to tell the rest of the application that a new geometry has
been added to the layer:
def geometryCaptured(self, layerCoords):
if self.captureMode == CaptureTool.CAPTURE_LINE:
geometry = QgsGeometry.fromPolyline(layerCoords)
elif self.captureMode ==
CaptureTool.CAPTURE_POLYGON:
geometry =
QgsGeometry.fromPolygon([layerCoords])
feature = QgsFeature()
feature.setGeometry(geometry)
self.layer.addFeature(feature)
self.layer.updateExtents()
self.onGeometryAdded()
While CaptureTool is complicated, it is a very powerful class that allows the user to
add new lines and polygons to a map layer. There are a few features we haven't implemen-
ted here (coordinate snapping, checking whether the resulting geometry is valid, and
adding support for inner rings that form "holes" within a polygon), but even as is, this is a
useful tool for adding new features to a map.
Search WWH ::




Custom Search