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.
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.
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:
In the QGIS Python Console, we'll import the
random
module:import random
Next, we'll load the census layer:
src = "/Users/joellawhead/qgis_data/census/\ GIS_CensusTract_poly.shp" tractLyr = QgsVectorLayer(src, "Census Tracts", "ogr")
Then, we'll create our memory layer:
popLyr = QgsVectorLayer('Point?crs=epsg:4326', ...