[GRASS-user] zonal statistics for multiple areas

Micha Silver tsvibar at gmail.com
Wed Oct 31 13:55:43 PDT 2018


On 10/31/18 11:59 AM, Alessandro Sebastiani wrote:
> Dear all, i was able to compute zonal statistics for multiple areas 
> using this script. I used
> > import grass.script as gscript
> > v_list = gscript.read_command("g.list" , type = 
> "vect").strip().split("\n")
> # i have 5 vectors in my list
> > method = ("sum" , "average")
> > for v in v_list:
>  >    gscript.run_command("v.rast.stats", map_=v, raster = "LC1", 
> column_prefix = "LC1", method = methods)
>
> Now i have like 4 rasters that i would like to use as input to compute 
> my statistics. Is there a way to loop this process for rasters and 
> vector at the same time? i tried creating a list for rasters and using 
> the name list as raster input but it seems not to work! cheers, Alessandro


What exactly did you try? There should not be any problem to do nested 
loops. i.e.:


import grass.script as gscript

v_list = gscript.read_command("g.list" , type = "vect").strip().split("\n")
# i have 5 vectors in my list
r_list = gscript.read_command("g.list" , type = "rast", 
pattern="LC*").strip().split("\n")
method = ("sum" , "average")
for v in v_list:
     for r in r_list:
         gscript.run_command("v.rast.stats",
             map_=v, raster = r, column_prefix = r, method = methods)


>
> Il giorno mar 30 ott 2018 alle ore 11:52 Alessandro Sebastiani 
> <alessandro.sebastiani at uniroma1.it 
> <mailto:alessandro.sebastiani at uniroma1.it>> ha scritto:
>
>     Thanks a lot for your help!
>
>     Il giorno mar 30 ott 2018 alle ore 11:49 Nikos Alexandris
>     <nik at nikosalexandris.net <mailto:nik at nikosalexandris.net>> ha scritto:
>
>         * Alessandro Sebastiani <alessandro.sebastiani at uniroma1.it
>         <mailto:alessandro.sebastiani at uniroma1.it>> [2018-10-29
>         21:32:51 +0100]:
>
>         >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 Alessandro,
>
>         I don't have time to write back as I would like. Instead, I
>         will send
>         you off-list some unpublished script.  With a bit of reading,
>         there, you
>         might identify some ideas that will eventually help in what
>         you are
>         doing.
>
>         Kind regards, Nikos
>
>
> _______________________________________________
> grass-user mailing list
> grass-user at lists.osgeo.org
> https://lists.osgeo.org/mailman/listinfo/grass-user

-- 
Micha Silver
Ben Gurion Univ.
Sde Boker, Remote Sensing Lab
cell: +972-523-665918



More information about the grass-user mailing list