Adding lines and polygons
To add a LineString or a Polygon geometry, the user will draw the desired shape by clicking on each vertex in turn. Appropriate feedback will be displayed as the user clicks on each vertex. For example, a LineString geometry would be displayed in the following way:
To draw the outline of a Polygon geometry, the user will once again click on each vertex in turn. This time, however, the polygon itself will be displayed to make the resulting shape clear, as the following image shows:
In both cases, the basic logic of clicking on each vertex and displaying appropriate feedback is the same.
QGIS includes a map tool named QgsMapToolCapture
, which handles exactly this behavior: it allows the user to draw a LineString or the outline of a Polygon geometry by clicking on each vertex in turn. Unfortunately, QgsMapToolCapture
is not available as part of the PyQGIS library, so we will have to re-implement it ourselves using Python.
Let's start by looking at the design of our QgsMapToolCapture...