By a_merko


2015-06-10 16:37:09 8 Comments

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?

2 comments

@Christina Kallimani 2018-01-02 11:53:04

For mean only use

os.chdir(rasterfolder)
for lyr in glob.glob("*.tif") :
    qgis.analysis.QgsZonalStatistics(vector, lyr, attributePrefix=lyr, rasterBand=1, stats=QgsZonalStatistics.Statistics(QgsZonalStatistics.Mean)).calculateStatistics(None)

@nmtoken 2018-01-02 12:21:09

You should provide a little more text to say how this answers, or part answers the question

@Sarracenia 2016-09-19 11:52:51

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/'
    os.chdir(rasterfolder)
    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.

Related Questions

Sponsored Content

0 Answered Questions

Zonal Statistics As Table with Very Small Polygon

1 Answered Questions

[SOLVED] Losing information using Zonal Statistics?

2 Answered Questions

[SOLVED] How does Zonal Statistics work exactly?

0 Answered Questions

Zonal Statistics calculator differences between QGIS Lyon and Wien

0 Answered Questions

QGIS Zonal Statistics for polygons is missing results?

1 Answered Questions

0 Answered Questions

How to Iterate ModelBuilder for Zonal Statistics?

0 Answered Questions

Reshape Matrix and perform Zonal Statistics in Matlab

Sponsored Content