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

Creating custom map canvas items


A map canvas item is an item that is placed on top of the map canvas. Standard map canvas items include text annotations, vertex markers, and the visual highlighting of a feature. It is also possible to create your own custom map canvas items by subclassing QgsMapCanvasItem. To see how this works, let's create a map canvas item that draws a compass rose onto the map:

We'll start by creating the basic QgsMapCanvasItem subclass:

class CompassRoseItem(QgsMapCanvasItem):
    def __init__(self, canvas):
        QgsMapCanvasItem.__init__(self, canvas)
        self.center = QgsPoint(0, 0)
        self.size   = 100

    def setCenter(self, center):
        self.center = center

    def center(self):
        return self.center

    def setSize(self, size):
        self.size = size

    def size(self):
        return self.size

    def boundingRect(self):
        return QRectF(self.center.x() - self.size/2,
                      self.center.y() - self.size/2,
      ...