What I'm trying to do:
- Extract median (or 50th quantile) by a single polygon from 558 GeoTIFF rasters stored in a folder
What I got so far:
Using Python
from within Qgis and Qgs.ZonalStatistics
I've managed to extract statistics by polygon from *.tiff files in a directory, and add each value to a new column in the above polygon shapefile (column name based on raster filename). The code looks like this:
import glob, os, qgis.analysis
vectorlayer = qgis.utils.iface.mapCanvas().currentLayer()
rasterfolder = 'D:/effect_test/'
os.chdir(rasterfolder)
for lyr in glob.glob("*.tiff"):
qgis.analysis.QgsZonalStatistics(vectorlayer, lyr, attributePrefix=lyr, rasterBand=1).calculateStatistics(None)
What's left to resolve:
- With the above code I'm getting statistics for average, rejects, max
and min while I need to extract the median (which is the same as
the 50th quantile). I've been trying to add
QgsZonalStatistics.Mean
suggested in this question and alsoQgsZonalStatistics::Mean
suggested in the QgsZonalStatistics documentation. But it fails telling me 'QgsZonalStatistics' is not defined. How to re-write above code to get median or the 50th quantile instead?