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

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.oddSymbol.setColor(QColor("black"))
        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
        else:
            return self.oddSymbol

    def startRender(self, context...