ZonalStatistics, Neighbourhood analysis and more for LecoS

Since QGIS 2.0 stable was released just a while ago, i thought that it would be time to enhance my plugin LecoS a bit more.  Furthermore i also missed some functions, for instance i found no appropriate function to compute ZonalStatistics for a set of rasters of mine.  SAGA has a function to calculate some stats using a categorical and a zone raster layer. However it is lacking a raster output and specific stats. So i added  a new ZonalStatistics function to LecoS and i am sure that it will be of some use to Landscape ecologists and other GIS users out there. See a usecase below!

Furthermore i regularly use a lot of short python scripts to generate and query raster layers using a gdal+numpy backbone. Those custom functions of mine are a lot faster than any other plugin (all hail to numpy), which is why i also implemented some functions that are already available in QGIS through other plugins.

Here is the total changelog from the last LecoS version 1.8.2 to the new 1.9 (note that QGIS 1.8 won’t be supported anymore):

# Version 1.9
### Major Update: ###
- Added new tools to the Processing toolbox for use in complex models
    - Function to count Raster cells -> Output as table
    - Function to query raster values below a point layer
    - Function to intersect two landscape (raster) layers -> Output clipped raster
    - Function to creates a new random raster based on values from a statistical distribution -> Output raster
    - Function to conduct a Neighborhood analysis (analogous to r.neighbors. or Focal Statistics in ArcGis)
    - Function to conduct a connected component labeling of connected patches
    - Function to conduct ZonalStatistics based on two landscapes (ZonalStatistics with raster layers in ArcGIS)
- Improved the overall documentation for the Processing Toolbox and created new simple icons
- Fixed Bug: http://hub.qgis.org/issues/8810

I didn’t create any new graphical interfaces as i believe that sextante aka processing is the future. All new functions were therefore only added to the processing toolbox and not as seperate GUI. This also has the cool advantage that you could use all LecoS tools within more complex multi-algorithms models. The most visible difference to older LecoS versions is that i created a new icon for every function (make them distinguishable) and wrote documentary information.

All currently available processing functions

All currently available processing functions. Notice the new icons

The new function help

The new function help. Was a hell of work writing all this stuff

Click more to see a short tutorial demonstrating the functions using real data.

Due to the number of new functions here is a little showcase tutorial which demonstrate each new function:

  • Create random Landscape (Distribution)
    • Allows to create a random new raster layer based on a choosen statistical distribution. The user can specifiy the extent of the resulting raster and the input parameters for any distribution.
    • Screenshot:

Drawn from a random distribution. Could for instance be compared with a real landscape to test for significant effects in a model

  • Query raster values (Points)
    • Similar function to the already existing Pointsampling tool, however much faster (takes 1sec for around 3500 points) and simpler (minimal input).
    • Screenshot:
Result Raster point query

Result Raster point query

  • Count raster cells
    • Returns the number of cells per unique cell value inside a raster layer
    • Screenshot:
Number of cells

Number of cells

  • Neighbourhood Analysis (Moving Window)
    • Iterates over all cells of a given raster layer and calculates statistics for the surrounding raster cells. This is called moving window approach and is analogous to other similar tools like r.neighbors or ArcGis Focal Statistics.
    • Screenshot:
Displays the variety (number of landcover classes) for the country of Skane, Sweden.

Displays the variety (number of landcover classes) for the country of Skane, Sweden.

  • Label Landscape patches
    • Conducts a connected component labeling (chessboard structure) of all raster cells with a given value.
      The output is a rasterlayer containing all patches with an incremental number (zone). Here for instance i give all raster patches containing Coniferous forest an individual number. This function is quite good to generate a zones raster for zonal statistics.
    • Screenshot:
Labeled raster patches containing coniferous forest in Skane, Sweden.

Labeled raster patches containing coniferous forest in Skane, Sweden.

  • “Intersect Landscapes” and “Match two landscapes”
    • Those two new function are able to (1) make an intersection of two raster layers containing a smaller and a larger raster. The output raster layer contains the intersection of  the source raster layer (where the extent intersect with the extent of the target raster layer) and (2) equalize two raster layers in terms of spatial reprojection and resolution. The matching is done by reprojecting one raster to another. Missing cells are interpolated with a chosen method.
    • What it is useful for? I wrote these functions for data preparation for the Zonal statistics tool. If you for instance have two raster layers with different projections and where one of the raster layers is slightly bigger than the other you could use these tools to equalize them. Or you could use them to clip a larger raster (CORINE for europe) to the extent of a smaller one (for instance a SRTM tile).
  • Zonal statistics
    • My new favorite function. Takes as input a raster layer containing a landscape and a raster layer with zones. Then it calculates general statistics for all values in the landscape raster layer which are within each zone. For an explanation of the methodology see the help pages of ArcGis SpatialAnalyst tool.
    • Example: We want to calculate the average elevation (from a DEM) of each coniferous forest patch in a specific area. Here is what you do:
      1. First crop your DEM with the Landcover dataset using the “Intersect Landscapes” tool. If they have different projections first match their projection with the “Match two landscapes” tool.
      2. Now generate a zone layer by labeling all forest patches with a unique number using the “Label Landscape patches” tool
      3. Now run the Zonal Statistics tool with the cropped DEM as as landscape layer, the labeled forest patches as zonal grid and choose mean as method. The output can be generated as table and/or raster layer. Here is both:
    • Screenshot:
Zonal Statistics raster output. Classified each individual coniferous forest patch with its average elevation value

Zonal Statistics raster output. Classified each individual coniferous forest patch with its average elevation value


Displays the table results from the Zonal Statistics tools. You can for instance see that forest patch 100 has an average elevation of 109.875m

Thats it for now. If you find a bug. If you need an additional function or option contact me and we could talk about this (maybe have a small donation at hand).


Tags: , , , , , , ,

About Martin Jung

PhD researcher at the University of Sussex. Interested in nature conservation, ecology and biodiversity as well as statistics, GIS and 'big data'

3 responses to “ZonalStatistics, Neighbourhood analysis and more for LecoS”

  1. pvanb says :

    Nice work! Looking forward trying this out.

  2. pcav says :

    Got an error, sent to QGIS-dev: where should I send it?

Sussex Research Hive

Supporting the research community at the University of Sussex

Small Pond Science

Research, teaching, and mentorship in the sciences

Landscape Ecology 2.0

intersecting landscape ecology, open science, and R


The Research Blog of IIASA

Jörg Steinkamps Blog

Mainly things about R, Linux and vegetation modeling

Amy Whitehead's Research

the ecological musings of a conservation biologist

Michael McCarthy's Research

School of BioSciences, The University of Melbourne

The Rostrum

science, statistics, policy and more


Environmental Change - Understand, Predict, Adapt

Dynamic Ecology

Multa novit vulpes


METeorological Visualisation Utilities using R for Science and Teaching

A Birder´s Blog

"Everybody loves what they know"

Trust Me, I'm a Geographer

Using Technology to Explore Our World

Duncan Golicher's weblog

Research, scripts and life in Chiapas

%d bloggers like this: