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
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Free Chapter
1
Getting Started with QGIS
Index

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
        self.setCursor(Qt.CrossCursor)

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

        feature = QgsFeature()
        feature.setGeometry(QgsGeometry.fromPoint(point))
        self.layer.addFeature(feature)
        self.layer.updateExtents()

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...