I need to do calculations on raster layers which require the sum of all cells of the raster. I've used the zonal statistics tool for that but it gets a little annoying to do those extra steps (run the plugin, open the Attribute table, copy the sum...).

Is there a way I can calculate and use the values of raster statistics directly inside a raster calculator?

Even better would be to calculate those values from a raster created in the same step:

A*B*C / Sum (A*B*C)


A calculation like

A*B*C / Sum (A*B*C)

will perform the A*B*C operation twice. To avoid that duplicate effort instead do it in two steps

X = A*B*C
X / Sum(X)

Storing A*B*C has an immediate payoff. To implement the Sum operation, use a zonal summary operator with the entire raster as the zone. That requires placing a constant, non-null value at every non-null cell of the raster. A simple way to accomplish this is to equate the raster with itself, thus:

ZonalSum(X, X==X)

(The syntax for ZonalSum will depend on the platform and the version of the software.)

The full workflow therefore is

X = A*B*C
X / ZonalSum(X, X==X)
  • This sounds interesting, can you help me out with the "ZonalSum" tool? How can I use it/ where can I find more information? Would that only be possible writing an own script? I use QGIS 2.4 on Windows 8... – Gumbo Jul 31 '14 at 8:05
  • A search of our site turns up a lot of information about that. – whuber Jul 31 '14 at 12:27
  • Hey, my comment was a little quick, now I already researched quite a bit but i'm not familiar enough with python and programming in general that I could understand most of the posts. Thx anyway – Gumbo Jul 31 '14 at 13:04

Why not just open the Attribute Table (VAT) and multiply all the values times all the counts ?

  • This is a good solution when the raster actually has a VAT ( which occurs only when it has integral values) and when a value is not needed "directly inside a raster calculator," as requested in this thread. – whuber Jul 30 '14 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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