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

Implementing renderers in Python

If you need to choose symbols based on more complicated criteria than the built-in renderers will provide, you can write your own custom QgsFeatureRendererV2 subclass using Python. For example, the following Python code implements a simple renderer that alternates between odd and even symbols as point features are displayed:

class OddEvenRenderer(QgsFeatureRendererV2):
    def __init__(self):
        QgsFeatureRendererV2.__init__(self, "OddEvenRenderer")
        self.evenSymbol = QgsMarkerSymbolV2.createSimple({})
        self.evenSymbol.setColor(QColor("light gray"))
        self.oddSymbol = QgsMarkerSymbolV2.createSimple({})
        self.n = 0

    def clone(self):
        return OddEvenRenderer()

    def symbolForFeature(self, feature):
        self.n = self.n + 1
        if self.n % 2 == 0:
            return self.evenSymbol
            return self.oddSymbol

    def startRender(self, context...