Book Image

Building Mapping Applications with QGIS

By : Erik Westra
Book Image

Building Mapping Applications with QGIS

By: Erik Westra

Overview of this book

Table of Contents (16 chapters)
Building Mapping Applications with QGIS
About the Author
About the Reviewers
Free Chapter
Getting Started with QGIS

Adding Points

The following map tool allows the user to add a new Point feature to the given layer:

class AddPointTool(QgsMapTool):
    def __init__(self, canvas, layer):
        QgsMapTool.__init__(self, canvas)
        self.canvas = canvas
        self.layer  = layer

    def canvasReleaseEvent(self, event):
        point = self.toLayerCoordinates(self.layer, event.pos())

        feature = QgsFeature()

As you can see, this straightforward map tool sets the mouse cursor to a cross shape, and when the user releases the mouse over the map canvas, a new QgsGeometry object is created that represents a point at the current mouse position. This point is then added to the layer using layer.addFeature(), and the layer's extent is updated in case the newly added point is outside the layer's current extent.

Of course, this map tool...