[Qgis-user] Python Script for Multi-band zonal statistics
Leo Kris Palao
lk.palao at gmail.com
Mon Oct 21 20:06:36 PDT 2013
Dear QGIS Users,
Often we want to compute for band statistics for our stacked layers (i.e.,
bands 1 to 9). In QGIS, the available plugin for this is the Zonal
Statistics plugin but it only computes for one band at a time, and as we
suspect if our input is a stacked layer the plugin seems to get the average
values for all the stacked layers in a polygon.
So, I searched for some answers in the web, and I found a python script. I
used it and it seems to be working well, but this only works for one band,
So, what I did was to copy the script and change the band number for each
copy. However, this resulted in a very long script, and I think there is a
much better script than this. Can someone help me improve my script. I am
just new to Python. I just completed a basic Python training course.
import qgis.analysis
vectorlayer=qgis.utils.iface.mapCanvas().layer(0)
rasterfile = qgis.utils.iface.mapCanvas().layer(1).source()
zonalstats =
qgis.analysis.QgsZonalStatistics(vectorlayer,rasterfile,str(1),1)
zonalstats.calculateStatistics(None)
import qgis.analysis
vectorlayer=qgis.utils.iface.mapCanvas().layer(0)
rasterfile = qgis.utils.iface.mapCanvas().layer(1).source()
zonalstats =
qgis.analysis.QgsZonalStatistics(vectorlayer,rasterfile,str(2),2)
zonalstats.calculateStatistics(None)
import qgis.analysis
vectorlayer=qgis.utils.iface.mapCanvas().layer(0)
rasterfile = qgis.utils.iface.mapCanvas().layer(1).source()
zonalstats =
qgis.analysis.QgsZonalStatistics(vectorlayer,rasterfile,str(3),3)
zonalstats.calculateStatistics(None)
import qgis.analysis
vectorlayer=qgis.utils.iface.mapCanvas().layer(0)
rasterfile = qgis.utils.iface.mapCanvas().layer(1).source()
zonalstats =
qgis.analysis.QgsZonalStatistics(vectorlayer,rasterfile,str(4),4)
zonalstats.calculateStatistics(None)
import qgis.analysis
vectorlayer=qgis.utils.iface.mapCanvas().layer(0)
rasterfile = qgis.utils.iface.mapCanvas().layer(1).source()
zonalstats =
qgis.analysis.QgsZonalStatistics(vectorlayer,rasterfile,str(5),5)
zonalstats.calculateStatistics(None)
import qgis.analysis
vectorlayer=qgis.utils.iface.mapCanvas().layer(0)
rasterfile = qgis.utils.iface.mapCanvas().layer(1).source()
zonalstats =
qgis.analysis.QgsZonalStatistics(vectorlayer,rasterfile,str(6),6)
zonalstats.calculateStatistics(None)
import qgis.analysis
vectorlayer=qgis.utils.iface.mapCanvas().layer(0)
rasterfile = qgis.utils.iface.mapCanvas().layer(1).source()
zonalstats =
qgis.analysis.QgsZonalStatistics(vectorlayer,rasterfile,str(7),7)
zonalstats.calculateStatistics(None)
We used this tool most often to determine the average reflectance or
backscatter from our images per zone (polygon).
Thanks in advance,
-Leo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20131022/aedda229/attachment.html>
More information about the Qgis-user
mailing list