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

The Add Track map tool

Our first task is to let the user add a new track while in the track editing mode. This involves defining a new map tool, which we will call AddTrackTool. Before we start implementing the AddTrackTool class, however, we're going to create a mixin class that provides various helper methods for our map tools. We'll call this mixin class MapToolMixin.

Here is our initial implementation of the MapToolMixin class, which should be placed near the top of your module:

class MapToolMixin
    def setLayer(self, layer):
        self.layer = layer

    def transformCoordinates(self, screenPt):
        return (self.toMapCoordinates(screenPt),
                self.toLayerCoordinates(self.layer, screenPt))

    def calcTolerance(self, pos):
        pt1 = QPoint(pos.x(), pos.y())
        pt2 = QPoint(pos.x() + 10, pos.y())

        mapPt1,layerPt1 = self.transformCoordinates(pt1)
        mapPt2,layerPt2 = self.transformCoordinates(pt2)
        tolerance = layerPt2.x() ...