# How to use ZonalStats plugin?

I am using QGIS 1.8.0 on a Windows Home Premium 7 machine. I have a single band TIFF that is 187 MB in size. My vector polygon shapefile represents a regular grid of a 147x112 36kmx36km grid cells. Both files have the same map projection parameters. I installed the NextGIS ZonalStats plugin, and tried running it. I have also tried the embedded zonal stats plugin. Finally, I have tried the following commands in the Python command window:

rasterfile = qgis.utils.iface.mapCanvas().currentLayer().source()
vectorlayer = qgis.utils.iface.mapCanvas().currentLayer()
zonalstats = qgis.analysis.QgsZonalStatistics(vectorlayer,rasterfile)
zonalstats.calculateStatistics(None)


In each case, the system fails to run to completion. Instead, I get the "Not Responding" error.

Sometimes Windows will ask me to kill the program. Other times, I have to kill it in the Task Manager.

Any help with this will be greatly appreciated as I am trying to move away from ArcGIS.

-
What is the CRS set to? –  Willy Jan 14 '13 at 0:11
In response to Willy, the two files have the following projection parameters: +proj=longlat +datum=WGS84 +no_defs The project projection parameters that I am using for display purposes are as follows: +proj=lcc +lat_1=33 +lat_2=45 +lat_0=40 +lon_0=-97 +x_0=0 +y_0=0 +a=6370997 +b=6370997 +units=m +no_defs –  James Jan 14 '13 at 15:43
What would be the EPSG codes of those projections? Can you check your vector for errors? And if you could put examples of the files on the web that might be a chance to troubleshoot. –  Willy Jan 16 '13 at 1:40
The EPSG is 4326. If you send me an email at terrakit@hotmail.com, I will post details on how to retrieve the data. –  James Jan 17 '13 at 1:27

This could be happening because the analysis tools first need to be imported with the following command before it can access the zonal stats tools (you can put it before any of the other commands):

import qgis.analysis


That said, I would expect it to show an error rather than not responding. Also, there is a sequence to the commands. You need to first select the TIFF layer in the table of contents on the left, then execute the rasterfile = ... command. Then you select the vector layer and execute the vectorlayer = ... command. Then you can execute the other two commands to generate the statistics.

-

Most likely the vector file is topologically broken. Try and repair it first, and then retry.

The link provides a short guide on how to accomplish it with grass toolbox.

-

Make sure that the two layers are in the same CRS and ensure that the topology is valid then try this:

from qgis.analysis import QgsZonalStatistics