I'm looking for some Python + QGIS for the following:

import processing
for i in range (1,20):
    processing.runalg("qgis:zonalstatistics", "/folder/input/raster.tif",%i,
    "/folder/input/poly.shp","Var_%i",True, "/folder/output/zonalpoly_%i.shp"

The above BASH like example doesn't work, but if I substitute an integer in place of %i, the script runs correctly, so I know that the syntax is correct.

I'm a bit fuzzy on how to properly pass a variable into a string using Python and/or how to run a QGIS plugin from the built-in Python interpreter for QGIS.

Also, qgis:zonalstatistics calculates several summary statistics (mean, stddev, maximum, etc.) with no apparent means of specifying one or a few of these.

Is there a way of specifying just one, or a few?

It seems like a complete waste of processing time to calculate each if I'm only interested in one or a few measures.

closed as too broad by PolyGeo Aug 27 at 10:38

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.


1) string formatting:

It is a not a problem of QGIS or processing but a simple problem of string formatting in Python, look at Python String Format, for example.

print  "Var_{}".format(3)
# or old
print "Var_%s" % (1) 

Script example 1:

for i in range(1,20):
    processing.runalg("qgis:zonalstatistics", "/folder/input/raster.tif",i,"/folder/input/poly.shp","Var_{0}".format(i),True, "/folder/output/zonalpoly_{0}.shp".format(i))

Script example 2

for i in range(1,20):
     processing.runalg("qgis:zonalstatistics","/folder/input/raster.tif",i,"/folder/input/poly.shp", "Var_%s" %i ,True, "/folder/output/zonalpoly_%s.shp"%i

2) But QGIS actually has also zonal statistics methods built into the API with Python bindings (look at QGIS: How to calculate raster statistics for polygons?)

3) For the other question, no solution, look at Processing-Help: ZONAL STATISTICS:

processing.runalg("qgis:zonalstatistics", input_raster, band_number, input_vector, column_prefix, load_flag, output_layer)
  • Thanks! I've managed to get the first example running, although it was missing a right bracket at the end. Example two also seems to be missing a bracket. Unfortunately, running example two gets me a TypeError: unsupported operand type(s) for %: 'dict' and 'int' – Prophet60091 Apr 5 '14 at 0:32
  • Option 2) isn't really an option as I believe the built-in API version of zonal statistics doesn't allow for selecting an individual band within a file. If it does, it doesn't seem obvious to me how it's implemented. Good to know though. – Prophet60091 Apr 5 '14 at 0:36
  • Also missing a 0 in "Var_{}". Should be "Var_{0}" in the first example. Example 1 definitely works. Let me know if you can figure out the issue for Example 2. – Prophet60091 Apr 5 '14 at 0:54
  • Var_{0}in Python 2.6, Var_{}in Python 2.7 – gene Apr 6 '14 at 19:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.