We have population data (total population, male, female, etc.) by polygon (Census tracts), but want a CSV file of the population densities for a regular grid of points at 1km spacing.
Below are the steps to do this using QGIS. In this example, we have already added a polygon layer called "Tracts" to the map, and will calculate the male population densities. (Repeat this process for other subpopulations.)
In order to calculate densities per square kilometer, we will to transform the tracts to a UTM (meters-based) coordinate system. For New York State tracts, we will use UTM zone 18N.
- In the layers panel, right-click "Tracts" > Save As...
- File name = 'tracts_utm.shp'
- CRS = EPSG:26918 (NAD83 / UTM zone 18N)
- Click OK
- When the process completes, right-click the new 'tracts_utm' layer > Set Project CRS from Layer
- Right-click "tracts_utm" > Open Attribute Table
- Click the "Field Calculator" icon (2nd from right in the toolbar)
- Create a new field:
- Output field name = male_dens
- Output field type = Decimal number (real)
- Output Precision = 3
- Expression = "MALES" / $area * 1000 * 1000 (this takes the total count of males in each tract, and converts it to a density per sq km)
- Raster menu > Conversion > Rasterize (Vector to Raster)
- Input file = tracts_utm
- Attribute field = male_dens
- Output = male_density.tif (be sure to click the "Select..." button to browse to specify the output location!)
- You'll see an alert that the output file doesn't exist. Click OK.
- Select "Raster resolution in map units per pixel"
- Horizontal = 1000
- Vertical = 1000
- To properly encode nodata pixels as -1, we need to edit the generated command:
- Click the pencil to edit the command
- Add " -a_nodata -1" immediate after the gdal_rasterize command, so that it starts like this:
gdal_rasterize -a_nodata -1 -a male_dens -tr 1000.0 1000.0 ...
- Raster menu > Conversion > Translate (Convert format)
- Input Layer = male_density
- Output file = male_denstiy.xyz (be sure to click the "Select..." button to browse to specify the output location!)
The lines of the .xyz file should look something like this:
626606.42132833065 4851489.903991037 10.925999641418457
627606.42132833065 4851489.903991037 6.8530001640319824
628606.42132833065 4851489.903991037 6.8530001640319824
629606.42132833065 4851489.903991037 6.8530001640319824
630606.42132833065 4851489.903991037 -1
The .xyz file is space-delimited, with 3 columns: x, y, and the raster value, which in this example is the male population density. Note that any lines with a -1 value can be discarded -- these are the nodata pixels that fell outside the original tract polygons.