Book Image

QGIS Python Programming Cookbook

Book Image

QGIS Python Programming Cookbook

Overview of this book

Table of Contents (16 chapters)
QGIS Python Programming Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating a graduated vector layer symbol renderer


A graduated vector layer symbol renderer is the vector equivalent of a raster color ramp. You can group features into similar ranges and use a limited set of colors to visually identify these ranges. In this recipe, we'll render a graduated symbol using a polygon shapefile.

Getting ready

You can download a shapefile containing a set of urban area polygons from https://geospatialpython.googlecode.com/files/MS_UrbanAnC10.zip.

Extract this file to a directory named ms in your qgis_data directory.

How to do it...

We will classify each urban area by population size using a graduated symbol, as follows:

  1. First, we import the QColor object to build our color range.

    from PyQt4.QtGui import QColor
    
  2. Next, we load our polygon shapefile as a vector layer:

    lyr = QgsVectorLayer("/qgis_data/ms/MS_UrbanAnC10.shp", "Urban Areas", "ogr")
    
  3. Now, we build some nested Python tuples that define the symbol graduation. Each item in the tuple contains a range label, range start...