# [GRASS-user] zonal statistics for multiple areas

Alessandro Sebastiani alessandro.sebastiani at uniroma1.it
Mon Oct 29 13:32:51 PDT 2018

```thank you all for your suggestions.
Dear Nikos, I have some 0-1 rasters that represents presence-absence of
different land covers. Each raster's resolution is 10x10m, thus i want to
compute the sum in order to obtain the surface covered by each land cover
within my buffers. As regards to the buffer, i want to use the 0-100,
0-200, 0-300, 0-400 and 0-500 for computing stats. I can do it separately
with the module v.rast.stats. I just don't know how to loop it in a python
script
Dear Stefan, thank you, i guess that using multiple rasters as input would
be a good solution. however i would really like to loop it using a python
script since that would be useful in other situation that i'm going to face
in the next days
Dear Micha thank you, i'll try ASAP!

Il giorno lun 29 ott 2018 alle ore 21:07 Micha Silver <tsvibar at gmail.com>
ha scritto:

> Some further python specific tips that might help.
>
>
> To get a list of rasters that match some pattern (say you have many
> rasters like "band1", "band2",...), then run v.rast.stats on each of them
> in a loop to get zonal stats in the vector "buffers":
>
>
> # This must be run from within a grass session
>
> import grass.script as gscript
>
> rast_list = gscript.read_command("g.list", type = "rast", pattern =
> "band*").strip().split("\n")
>
> # Loop thru the rasters, run v.rast.stats, and specify the column prefix
> using the raster name.
>
> methods = ("minimum", "maximum", "average", "sum")  # add whatever stats
> you need
>
> for r in rast_list:
>
>     gscript.run_command("v.rast.stats",
>
>         map_ = "buffers", raster=r, column_prefix=r,  method=methods)
>
>
>
> Be aware that if you have many rasters, you'll end up with many,many
> columns in the "buffers" vector
>
>
>
> On 10/29/18 9:54 PM, Stefan Blumentrath wrote:
>
> Hi Alessandro,
>
>
>
> You could try v.rast.bufferstats [1], if you do not have hundrets of
> thousands of points….
>
>
>
> If the buffers don`t overlap, you could use v.rast.stats [2] from the
> upcoming GRASS 7.6 release, that will allow multiple raster input…
>
>
>
> Otherwise have a look at the general introduction to GRASS and Python [3].
>
>
>
> Cheers
>
> Stefan
>
>
>
> 1: https://grass.osgeo.org/grass74/manuals/addons/v.rast.bufferstats.html
>
> 2: https://grass.osgeo.org/grass76/manuals/v.rast.stats.html
>
> 3: https://grasswiki.osgeo.org/wiki/GRASS_and_Python
>
>
>
>
>
> *From:* grass-user <grass-user-bounces at lists.osgeo.org>
> <grass-user-bounces at lists.osgeo.org> *On Behalf Of *Alessandro Sebastiani
> *Sent:* mandag 29. oktober 2018 16:06
> *To:* grass-user at lists.osgeo.org
> *Subject:* [GRASS-user] zonal statistics for multiple areas
>
>
>
> Hello to everybody,
>
> I hope my question is appropriate for this mail list. I have created 5
> different buffer layers (d=100,200,300,400,500 m) from a point vector. Now
> i want to compute some zonal statistics using different rasters as input. I
> know how to do that separately, but i was wondering how could i automate
> this procedure using a python script. I know python's basics, but i was not
> able to do so following guidelines that i found on the internet, e.g GRASS
> tutorial. Thank you in advance, A
>
> _______________________________________________
> grass-user mailing listgrass-user at lists.osgeo.orghttps://lists.osgeo.org/mailman/listinfo/grass-user
>
> --
> Micha Silver
> Ben Gurion Univ.
> Sde Boker, Remote Sensing Lab
> cell: +972-523-665918
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/grass-user/attachments/20181029/466114d9/attachment.html>
```

More information about the grass-user mailing list