Zonal Statistics QGIS [closed]
Asked Answered
H

4

5

Is there any open source alternative for Zonal Statistics tool (ArcGIS Spatial Analyst)? What is the best tool (which I can use in script) dor counting statistics of raster files?

Hawkbill answered 27/4, 2011 at 10:30 Comment(0)
P
3

You can do this with GRASS using various methods. Which one is most suitable will depend on your data and the required output. Note that you can use GRASS also from within QGIS using the GRASS toolbox or Sextante toolbox.

Let's assume you have:

  • a vector map, e.g., vector_zones with the zones defined in the column myzones in the attribute table.
  • a raster layer 'values' for which you want to calculate your zonal statistics

r.statistics

To use r.statistics, you first need to convert the vector map to a raster layer, which you can do with v.to.rast. Next, use r.statistics to calculate the zonal statistics.

v.to.rast input=vector_zones output=zones column=myzones
r.statistics base=zones cover=values out=outputmap method=average

This will give you a new layer with the selected zonal statistic, which could be average, mode, median, variance, etc. (see the man page link above).

r.univar

The r.univar function also works on raster layers.

v.to.rast input=vector_zones output=zones column=myzones    
r.univar map=values zones=zones output=output.file fs=;

The output is a table with the zonal statistics.

v.rast.stats

This does not require you to convert the vector layer to a raster layer (this is done internally). The function calculates basic univariate statistics per vector category (cat) from the raster map.

v.rast.stats vector=vector_zones layer=1 raster=values column_prefix=val

The results are uploaded to the vector map attribute table.

Popper answered 21/10, 2012 at 9:30 Comment(0)
D
2

you can use the raster package in R

library(raster)
v <- raster('raster filename')
z <- raster('zones raster filename')
zv <- zonal(v, z, fun=mean)
Doody answered 29/4, 2011 at 3:52 Comment(1)
see help(zonal) in R for a runnable example with sample dataAcanthoid
K
1

Correct me if I'm wrong, RobertH, but I believe zonal() requires that the zones are already 'rasterized' in some sense, whereas many times one will want the statistics of raster cells that fall within polygons. The various overlay methods in R within the sp package (see: ?"overlay-methods" ) are necessary for this, though if I am wrong I would be glad to hear it. I quite prefer the raster package over using SpatialGridsDataFrames, but I think one must rely on sp classes to mix polygons and gridded data. Which is ok, except becomes problematic because it lacks the great memory management of the raster package, which making point-in-polygons style operations really hard to do in R on large rasters.

I am also led to believe, but have not tried, that this can be done within GRASS, and/or through QGIS, with the next release of QGIS (1.7) to have some sort of built-in zonal stats feature.

Komsa answered 8/6, 2011 at 5:2 Comment(0)
B
1

The Rasterstats package is a nice open source tool that worked well for me: http://blog.perrygeo.net/2013/09/24/python-raster-stats/

I started using it as a work-around because arcpy's ZonalStatistics method was producing a problematic raster that lead to an odd error when trying to convert the raster to an array (https://gis.stackexchange.com/questions/110274/save-fails-on-raster-object-created-from-numpyarraytoraster). Rasterstats worked well and provided an efficient solution for my problem.

Bellwort answered 2/4, 2015 at 20:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.