Select to view content in your preferred language

Zonal statistics in python console in qgis 3.8

1914
2
07-10-2019 03:26 PM
TilottamaGhosh
Emerging Contributor

Hi,

I am trying to use the Zonal statistics tool in QGIS  to extract the sum of several raster layers. I am using QGIS 3.8. It is best to use the python console as I have several raster layers. The codes which I got in some of the old discussion forums is not working. Here is an example of what I tried till now -

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)

I have to do this in qgis because the class I am going to teach does not have access to ArcGIS.

Thanks,

Tilottama

0 Kudos
2 Replies
Egge-Jan_Pollé
MVP Regular Contributor

Hi Tilottama Ghosh,

You might have a look at this post: pyqgis - Python Zonal Statistics Problems in QGIS - Geographic Information Systems Stack Exchange for a working example.

Alternatively, you can ask the students in your class to try to solve the issue themselves. Because the teacher (you, in this case) does not have to know everything, as long as he/she does provide a stimulating and inspiring learning environment to the audience.

HTH,

Egge-Jan

0 Kudos
TilottamaGhosh
Emerging Contributor

Thanks Egge. the code in the above link does not work for qgis 3.8

-Tilottama

0 Kudos