Rasterlang is a plugin for Quantum GIS (QGIS) that performs mathematical operations on raster (grid) data.


There are some prerequisite python modules you should install to the python that Qgis is using:

The plugin is in my repository - add http://www.maths.lancs.ac.uk/~rowlings/Qgis/Plugins/plugins.xml to your repository list via the Plugin Installer and then use the Qgis Plugin Manager to activate it.


When run from the plugin menu or toolbar icon, rasterlang presents a dialog like this:

At the top is a list of loaded rasters, with rasters of the same size and position grouped. Operations can only be carried out on rasters within the same group - rasterlang does no resampling or interpolation.

Below this is a text entry line for the expression. As you enter an expression the system checks the syntax and updates the status line. When a valid expression is entered the OK button is enabled. Clicking that runs the calculation and a 'Save' dialog is presented. The resulting output is a GeoTIFF file with the same size and position as the input rasters. Currently no coordinate reference system data is respected. The file is then loaded as a Qgis layer.


Expressions in rasterlang are written in a simple LISP-style language. The basic form is an operator plus arguments enclosed in parentheses, separated by spaces: ( op arg1 arg2 ... ), but each argument can also be another expression. Simple arguments can be numbers or layers, referenced by the layer label. There are also two constants 'Pi' and 'e'. If there are layers named 'Pi' or 'e' then something bad will probably happen. So rename them.

Since layers in Qgis do not need to have unique names, and can be named things like '2.0' or '---', we need a unique alpha-numeric label for each layer. The second column in the rasterlang dialog gives this label. If there are no conflicts then it will be the same as the name of the layer, but if there are duplicates or characters that don't make sense in the rasterlang syntax a unique name will be generated.

Here are some example expressions given the above layer set:

(+ b0 10)Add 10 to layer b0
(+ b0 layer 12.34)Add b0 and b1.tiff and 12.34
(exp ogclogo)Take the exponential power of ogclogo
(^ e ogclogo)Take the exponential power of ogclogo
(* Pi (^ ogclogo 2 ) )'pi-r-squared' in rasterlang
(> layer-1 128)Return a raster of 1 where ogc1.tiff is over 128, 0 elsewhere.
(* layer-1 (> layer-1 128) )Like ogc1.tiff, but zero below 128
(band MandSLetters 0)Create a single band raster from first band (band zero)
(bind b0 (+ 3 b0) (* 2 b0))Create a three band raster from manipulations of the single band in b0
(bind (band MandSLetters 0) (* 1.3 (band MandSLetters 1)) (* 2.4 (band MandSLetters 2)))Create a 3-band raster with scaled versions of the input layer bands.

And here are some invalid expressions:

+ 1 b0Missing outer parentheses.
(+ 1 2)No rasters specified.
(+ b0 ogclogo)Rasters from different groups.
(+ b0 - 1)Syntax error.


The help button gives a brief introduction to the syntax and also lists the valid operators. For each operator a short description is given and the valid number of arguments it needs.



The code is maintained on my googlecode repository: browse the SVN. I'll try and keep the issues list up to date too. Contributions, such as new operators, are welcome.

I'd like to rewrite the syntax to something more pythonic. But this is the first time I've written a parser.

More Complex Raster Processing

If you need more complex raster processing then it's probably best done in Python. I have written some notes on how to do this.


Written by Barry Rowlingson <b.rowlingson@lancaster.ac.uk>