[Qgis-developer] raster algebra plugin II - the return!

Martin Dobias wonder.sk at gmail.com
Sat Nov 29 14:29:09 EST 2008

On Sat, Nov 29, 2008 at 7:13 PM, Barry Rowlingson
<b.rowlingson at lancaster.ac.uk> wrote:
> 2008/11/29 Martin Dobias <wonder.sk at gmail.com>:
>> This sounds good. I hope there are tools like flex/bison avaiable also
>> for python...
>  Yeah, I'm using 'pyparsing', which gives you a very easy way to
> specify a grammar. It's not included with python so it'll be an
> external requirement - along with numpy for doing the computations.
> I'm still leaning how to write these grammar things though.

When thinking about it once more, maybe not creating an own parser and
instead running the expression in a prepared python environment using
eval builtin or exec statement. You can modify the environment by
passing dictionaries of local and global variables.

You would define raster band class and override some operators like
__add__, __mul__ to emulate the arithmetics, see:

You would define some processing functions like:

def rasterMax(r1, r2):
  return (... something ...)

And then just evaluate the formula:

res = eval('R1+R2', [ 'R1' : RasterBand(...), 'R2' : RasterBand(...)
], [ 'max', rasterMax, ... ])

This way you could save the need of installing pyparsing module (which
supposedly only small amount of users will have before trying the

>> I guess these 'Rn' bands can be assigned from more raster layers, right?
>  Yeah, the idea is that you set Rn to be bands from raster layers,
> then you write an expression with Rn in it, and you get back a new
> raster.
>  There's a few aspects of the GUI I'm not sure of though. I also think
> the plugin will provide some useful python functions too for people to
> use in the python console if they need more complex processing
> requirements.

Can you give me an example of more complex requirements? In fact
allowing some raster algebra stuff into python could be done quite
easily - a matter of adding some variables to globals dictionary, so
users could run directly from console: RasterBand(...) *


More information about the Qgis-developer mailing list