Hi Bernado,

Please find attached the «work-in-progress» version of v.rast.bufferstats for 
some more inspiration.

v.db.select -r does what you are looking for:

However, if the polygons you are working with don`t overlap, looping should not 
be required, and you should be able to use r.univar with zones or r.stats for 
all polygons at once…


From: Bernardo Santos <bernardo_brand...@yahoo.com.br>
Sent: onsdag 11. april 2018 20.59
To: Helmut Kudrnovsky <hel...@web.de>; grass-user@lists.osgeo.org; Stefan 
Blumentrath <stefan.blumentr...@nina.no>
Subject: Re: [GRASS-user] zonal statistics/metrics - beyond simple statistics

Hi Helmut and Stefan,

thanks for sharing that with me, it is good to know that there are similar 
ongoing approaches to solve related issues.

very nice addon. However, it seems to me that the part on calculating 
statistics is still based on v.rast.stats, am I right?

Stefan, the question you pose is indeed very related. I took a better look at 
my code and I noticed that what really takes time is not the process of making 
a mask, but what comes before: the rasterization of one of the polygons of the 
input vector.
What I do is:
1) g.region using the vector layer, aligning it to the first of the input 
2) v.to.rast selecting only one of the polygons at a time from the vector layer
3) r.mask using the rasterized polygon
4) g.region zoom=rasterized_polygon
5) calculate any function for all raster maps (in principle using a loop), 
using this small region and the mask, and attaching the results to the 
attribute table
6) repeat 1-5 for all polygons in the input vector

However, I noticed now that what takes time is v.rast.stats, since it depends 
on the whole region selected, which in my case is much bigger than a single 
polygon (the vector is a set of ~6,000 small polygons).
Do you guys know if there is a way of using a single polygon from a vector to 
define the region using something like g.region?
[this would be really great!!]
something like
g.region vector=vector_of_interest cat=1

Em terça-feira, 10 de abril de 2018 06:01:34 BRT, Stefan Blumentrath 
<stefan.blumentr...@nina.no<mailto:stefan.blumentr...@nina.no>> escreveu:

Hei Bernardo,

Just for the record: I am currently working on something similar / related.

First of all I would like to improve speed of v.rast.stats for multiple inputs:
<https://www.mail-archive.com/grass-dev@lists.osgeo.org/msg52562.html%20> and

Adding a functionality to "tabulate areas" or "count categories" is a next step 
I have in mind which I am working on in v.rast.bufferstats (will be a port of 
v.what.rast.buffer [1] to GRASS 7 / pygrass (with some enhancements)). Once I 
have a good solution there I would like to see if it can be moved to 
v.rast.stats. Support for dbf driver is however a challenge...

I will add my "work in progress" script to github, so you can have a look if 
you like.


1: https://grass.osgeo.org/grass64/manuals/addons/

-----Original Message-----
From: grass-user 
On Behalf Of Helmut Kudrnovsky
Sent: mandag 9. april 2018 23.19
To: grass-user@lists.osgeo.org<mailto:grass-user@lists.osgeo.org>
Subject: Re: [GRASS-user] zonal statistics/metrics - beyond simple statistics

Bernardo Santos wrote
> Dear GRASS list,
> I am developing a Python script to be able to calculate (virtually
> any) metrics or statistics for zones/polygons in a vector - in analogy
> to zonal statistics (such as v.rast.stats).The idea is that one can
> calculate raster-based metrics (such as proportion of habitat, number
> of patches, or any metric that can be formalized as a function that
> takes some information from the input raster and returns a value) for
> each polygon in the vector, and this is updated as a value in a newly
> created column in the attribute table of this vector.
> Is there already anything like that (some addon/module) that I am
> missing, just to avoid re-doing something already created?
> If not, what I am doing is to create a loop over all the features in a
> vector, and for each one I zoom and use the polygon to define a mask
> (using r.mask), so that the calculation of the selected metric is
> performed only over that polygon, and this process is repeated.The
> script allows one to calculate metrics/statistics for multiple raster
> maps at once, and to incorporate other function for statistics also.
> It may be found here:https://github.com/LEEClab/GeneralizedZonalStats
> For small vectors this works nicely and I believe it has a great
> potential. However, when I try to calculate metrics for a large
> dataset (e.g. the Brazilian map of cities, with almost 6,000 polygons)
> - and that is when the tool would be interesting -, the process of
> creating each mask takes too long (387 steps), and the tool becomes kind of 
> useless.
> Then I have two questions:- First, what drives the number of steps
> GRASS takes to create a mask? Why it is very small for some maps but
> very large for others? I quite don't understand that yet.- Do you
> think of a easier or faster way of doing the same thing (instead of using 
> masks)?
> v.rast.stats seems to use r.univar and the option 'zones' for doing
> so, but then one gets restricted to the statistics calculated by this module.
> Any help or comment would be very welcome!
> Best,Bernardo Niebuhr
> _______________________________________________
> grass-user mailing list

> grass-user@.osgeo<mailto:grass-user@.osgeo>

> https://lists.osgeo.org/mailman/listinfo/grass-user

have look at


where I've coded some DEM derived stats and characteristics for polygones.

best regards
Sent from: http://osgeo-org.1560.x6.nabble.com/Grass-Users-f3884509.html
grass-user mailing list

grass-user mailing list

Attachment: v.rast.bufferstats.py
Description: v.rast.bufferstats.py

grass-user mailing list

Reply via email to