By Thomas

2013-01-05 13:10:12 8 Comments

I am using the Zonal Stats plugin in QGIS to extract raster statistics from overlaying polygons (I have shapefiles of species distribution and I want to extract environmental data from within each species' range). I have 300 or so files I need to get data from and so would like to write a script to run in the python console, however I am a complete novice with python and have no idea how to do this.


@Fitzroy Hogsflesh 2016-01-25 09:50:31

zoneStat = QgsZonalStatistics (polygonLayer, rasterFilePath, 'pre-', 1)

calculates by default just Count, Sum and Mean (as you can tell from Raster -> Zonal Statistics in QGIS Desktop, it can do a lot more).

If you, for instance, want to compute just the Mean you have to use:

zoneStat = QgsZonalStatistics (polygonLayer, rasterFilePath, 'pre-', 1, QgsZonalStatistics.Mean)

see API for all options.

@vinayan 2013-02-06 10:57:19

The below code worked for me QGis 1.8.0

You might modify this to accomodate multiple files with some loop..

from qgis.analysis import QgsZonalStatistics

#specify polygon shapefile vector
polygonLayer = QgsVectorLayer('F:/temp/zonalstat/zonePoly.shp', 'zonepolygons', "ogr") 

# specify raster filename
rasterFilePath = 'F:/temp/zonalstat/raster1.tif'

# usage - QgsZonalStatistics (QgsVectorLayer *polygonLayer, const QString &rasterFile, const QString &attributePrefix="", int rasterBand=1)
zoneStat = QgsZonalStatistics (polygonLayer, rasterFilePath, 'pre-', 1)

@Thomas 2013-02-06 15:36:14

Great. Thank you very much vinayan, that's exactly what I was after.

@Thomas 2013-02-06 16:16:25

Also see here for alternative solution using R

@vinayan 2013-02-06 17:33:27

glad it helped you!

@rudivonstaden 2013-03-26 20:19:53

@vinayan the QProgressDialog is useful for visual environments where you want to see how far the calculations have progressed. It has no use from the command line. You can use None as the parameter and it works fine. Then you don't need the from PyQt4.. line or the progressDialog = line. See similar post at…

@vinayan 2013-03-27 06:45:44

@rudivonstaden - that makes sense now..i updated the answer

@thadk 2015-05-04 22:54:07

This still works great from the qgis --code command line. I was not able to get it working from raw python however.

@Curlew 2013-01-06 21:21:40

Here is way to get what you want in SAGA GIS. This probably isn't the solution you want, but it works. I'll look into the reasons why my plugins fails and update it as soon as possible.

Install SAGA GIS (should also be available via apt-get or aptitudbe in your linux distribution).

  • Start SAGA, load in your Raster and vector shape (Menu Modules -> File -> GDAL/OGR import). You can see the process below.
  • Execute Module "Grid statistics for polygons" (Menu Modules -> Shape -> Grid -> Grid-values). Values are added directly to the table. The Dialog should look like thisenter image description here
  • Go to the tab "Data" in the workspace, rightclick on your vector layer and choose to "save as" to export the shape with the added attributes. You could also display the attribute table via rightclick and then click on table show

This works for the dataset you sent me. It is also possible to call SAGA modules in QGIS via SEXTANTE as a BATCH process. To do this simply activate the SAGA modules in the SEXTANTE options.

@Thomas 2013-01-07 13:58:23

thanks for the suggestion but I've already tried saga - the results it produced were inconsistent i.e. doing the same thing twice gave different results. I know the ZonalStats plugin in QGIS works, hence I'm after a way to automate ZonalStats.

@user99 2015-07-24 07:41:39

@vinayan i have the code which u have given for zonal statastics but it is creating the columns in the polygon vector layer but not updating the calculated values. Why is it so?

Related Questions

Sponsored Content

2 Answered Questions

[SOLVED] Robust Zonal stats in R and QGIS

2 Answered Questions

1 Answered Questions

[SOLVED] QGIS Python script for zonal statistics not running

2 Answered Questions

0 Answered Questions

Using zonal stats on lidar data returns unusual values

1 Answered Questions

[SOLVED] Python Script Zonal Stats as Table Loop Question

1 Answered Questions

[SOLVED] Zonal Statistics ArcMap 10.4

1 Answered Questions

0 Answered Questions

zonal statistics in Python crashes

Sponsored Content