I'm trying to create a timeseries of raster data (one layer per year) by polygon (Administrative regions). Is there any way in QGIS to perform Zonal Statistics for multiple years (without having to perform the zonal statistics function each time)? Or something along the lines of the Point Sampling tool but for polygons?

  • Have you already tried to call the geoprocessing function from inside a for loop over years in Python console?
    – Detlev
    Commented Jun 10, 2015 at 17:28

2 Answers 2


I have something fairly close, although it only currently works for average and rejects max and min.

  1. Save all your raster layers in one folder with the same extension (e.g. .tif)
  2. Load shapefile (polygons) into QGIS
  3. Open Python (Plugins > Python console) and run the following

    import glob, os, qgis.analysis
    vectorlayer = qgis.utils.iface.mapCanvas().currentLayer()
    rasterfolder = 'C:/Users/…/Folder/'
    for lyr in glob.glob("*.tif"):
        qgis.analysis.QgsZonalStatistics(vectorlayer, lyr, attributePrefix=lyr, rasterBand=1).calculateStatistics(None)

Using the vector layer loaded in QGIS was an easy way to append the results to the attribute table. The code to loop through rasters in a folder was from here.

If there is a way to specify the statistic using QgsZonalStatistics.Mean that would be helpful to know.


For mean only use

for lyr in glob.glob("*.tif") :
    qgis.analysis.QgsZonalStatistics(vector, lyr, attributePrefix=lyr, rasterBand=1, stats=QgsZonalStatistics.Statistics(QgsZonalStatistics.Mean)).calculateStatistics(None)
  • 1
    You should provide a little more text to say how this answers, or part answers the question
    – nmtoken
    Commented Jan 2, 2018 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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