1

I am trying to calculate the population count (raster geotiff) lying under a polygon (vector layer). The appropriate QGIS library zonalstatisticsfb sum returns NULL. My QGIS python code is:

processing.run("qgis:zonalstatisticsfb", { 'COLUMN_PREFIX' : '_', 'INPUT' : 'C:/Users/Benedikt/test/isos.shp', 'INPUT_RASTER' : 'C:/Users/Benedikt/test/BY-AT.tif', 'OUTPUT' : 'C:/Users/Benedikt/test/out.shp', 'RASTER_BAND' : 1, 'STATISTICS' : [1] })

Input files can be obtained here in googledrive

The isos.shp attributes already contain the population count from openrouteservice isochrone population count (which I want to imitate). Why does QGIS calculate NULL? BTW: The QGIS Plugin creates the same result --> NULL.

0
1

QGIS calculates NULL because both layers have not the same projection. Layer named isos has EPSG:4326 and BY-AT raster has EPSG:54009 (World_Mollweide-Projected). I got your same result by using these layers in this situation. However, when I clipped and projected raster to EPSG:4326, a NULL different result is obtained. For testing my approach, I used following script:

import processing

registry = QgsProject.instance()

isos = registry.mapLayersByName('isos')
BY_AT_4326 = registry.mapLayersByName('BY-AT_4326')

processing.runAndLoadResults("qgis:zonalstatisticsfb", 
                             { 'COLUMN_PREFIX' : '_', 
                             'INPUT' : isos[0], 
                             'INPUT_RASTER' : BY_AT_4326[0], 
                             'OUTPUT' : 'TEMPORARY_OUTPUT', 
                             'RASTER_BAND' : 1, 
                             'STATISTICS' : [1] })

Layers look as follow after running script. Observe that the attributes table of layer with zonal statistic has _sum field (3,300,885). It was added for each pixel below this vector layer the value of 22,455 (true population count); much lower than obtained value of 3,300,885. So, for obtaining adequate values of population you can use mean or maximum instead (minimum returns zero because shapefile "touch" some pixels with this value). These statistics correspond to 'STATISTICS' : [2,6] used as processing parameter.

enter image description here

1
  • gdalogr:cliprasterbymasklayer did it! However, sum is the right way to calculate total population. I was able to compute numbers very close to ORS population count. Thanx a lot! – Benedikt M. Apr 27 at 7:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.