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://github.com/GeospatialPython/Learn/raw/master/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:
- In the QGIS Python console, we'll import the
random
module:import random
- Next, we'll load the
census
layer:src = "/qgis_data/qgis_data/census/GIS_CensusTract_poly.shp" tractLyr = QgsVectorLayer(src, "Census Tracts", "ogr")
- Then, we'll...