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 dot density map


A dot density map uses point density to illustrate a field value within a polygon. We'll use this technique to illustrate population density in some US census bureau tracts.

Getting ready

You will need to download the census tract layer and extract it to a directory named census in your qgis_data directory from https://geospatialpython.googlecode.com/files/GIS_CensusTract.zip.

How to do it...

We will load the census layer, create a memory layer, loop through the features in the census layer, calculate a random point within the feature for every 100 people, and finally add the point to the memory layer. To do this, we need to perform the following steps:

  1. In the QGIS Python Console, we'll import the random module:

    import random
    
  2. Next, we'll load the census layer:

    src = "/Users/joellawhead/qgis_data/census/\
    GIS_CensusTract_poly.shp"
    tractLyr = QgsVectorLayer(src, "Census Tracts", "ogr")
    
  3. Then, we'll create our memory layer:

    popLyr =  QgsVectorLayer('Point?crs=epsg:4326', ...