2

I want to calculate the zonal statastics from multi band raster for a polygon layer. When I am using this snippet of code:

vectorlayer=qgis.utils.iface.mapCanvas().layer(0)
rasterfile = qgis.utils.iface.mapCanvas().layer(1).source()
zonalstats = qgis.analysis.QgsZonalStatistics(vectorlayer,rasterfile,"Zonal_")
zonalstats.calculateStatistics(None)

it is updating the field names but the values are NULL in the attribute table. So if I want to get the values to be updated, how can I do that?

1

Not sure why you're getting NULL values but the following code worked for me (note that I call the QGIS Zonal Statistics tool from the Processing plugin and I want to load the result):

vectorlayer = qgis.utils.iface.mapCanvas().layer(0)
rasterfile = qgis.utils.iface.mapCanvas().layer(1).source()
processing.runandload('qgis:zonalstatistics', rasterfile, 3, vectorlayer, "Zonal_", True, None)

The following is the help description for the parameters required which you can edit in your code:

>>>processing.alghelp("qgis:zonalstatistics")
    ALGORITHM: Zonal Statistics
        INPUT_RASTER <ParameterRaster>
        RASTER_BAND <ParameterNumber>
        INPUT_VECTOR <ParameterVector>
        COLUMN_PREFIX <ParameterString>
        GLOBAL_EXTENT <ParameterBoolean>
        OUTPUT_LAYER <OutputVector>

Hope this helps!

  • will the Zonal statastics work for multi band raster? – user99 Jul 24 '15 at 11:03
  • @user99 - I believe so yes since you can specify the number of Raster bands required (I used 3 in the code). – Joseph Jul 24 '15 at 11:07
  • is there any other way to calculate the raster statastics instead of Zonal? – user99 Jul 24 '15 at 11:16
  • @user99 - There's the Raster layer statistics tool which gives you the basic statistics: max, min, sum, mean, and std dev. Are there any specific statistics you are looking for? – Joseph Jul 24 '15 at 11:25

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.