<html><head></head><body><div style="font-family:Helvetica Neue, Helvetica, Arial, sans-serif;font-size:16px;"><div></div>
<div>Hi Stephan,<br></div><div><br></div><div>Some minutes before you sent me this e-mails, I'd just found out about v.db.select -r. It worked and made the process much faster. Thanks for that anyway.</div><div><br></div><div>In my case, the polygons do not overlap. Indeed, many things could be done with r.univar, r.stats or a combination of them and their output info. However, my aim was to think about a way of generalizing it, so that we would be able to use many other functions from the raster maps using the same architecture for calculating the zonal statistics/metrics.</div><div><br></div><div>For example, I am still not sure how I would use a combination of r.univar/r.stats and some kind of input raster map to produce, say, zonal info of number of habitat patches, structural or functional connectivity, isolation, or other more complex metrics used in landscape ecology.</div><div>That's way I was building something like that, so that we can just write an alternative function using any raster input map that returns the value of one of such metrics, and then use it to calculate them over sets of zones.</div><div><br></div><div>Best,</div><div>B</div><div><br></div><div>PS: thanks for sharing your code! This v.rast.bufferstats seems very useful, and I can think of some very interesting applications of that. I helped a friend to write something similar but using ArcPy as a ArcMap toolbox, and we applied it to some interesting cases in the delimitation of Protected Areas' buffer zones (but still did not published it). But doing that with a free and open source tool would be much more interesting! </div>
<div id="yahoo_quoted_4488561620" class="yahoo_quoted">
<div style="font-family:'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:13px;color:#26282a;">
<div>
Em quarta-feira, 11 de abril de 2018 18:32:18 BRT, Stefan Blumentrath <Stefan.Blumentrath@nina.no> escreveu:
</div>
<div><br></div>
<div><br></div>
<div><div id="yiv6359193583"><style>#yiv6359193583 #yiv6359193583 --
_filtered #yiv6359193583 {font-family:Helvetica;panose-1:2 11 6 4 2 2 2 2 2 4;}
_filtered #yiv6359193583 {panose-1:2 4 5 3 5 4 6 3 2 4;}
_filtered #yiv6359193583 {font-family:Calibri;panose-1:2 15 5 2 2 2 4 3 2 4;}
#yiv6359193583
#yiv6359193583 p.yiv6359193583MsoNormal, #yiv6359193583 li.yiv6359193583MsoNormal, #yiv6359193583 div.yiv6359193583MsoNormal
{margin:0cm;margin-bottom:.0001pt;font-size:12.0pt;font-family:New serif;}
#yiv6359193583 a:link, #yiv6359193583 span.yiv6359193583MsoHyperlink
{color:blue;text-decoration:underline;}
#yiv6359193583 a:visited, #yiv6359193583 span.yiv6359193583MsoHyperlinkFollowed
{color:purple;text-decoration:underline;}
#yiv6359193583 p.yiv6359193583msonormal0, #yiv6359193583 li.yiv6359193583msonormal0, #yiv6359193583 div.yiv6359193583msonormal0
{margin-right:0cm;margin-left:0cm;font-size:12.0pt;font-family:New serif;}
#yiv6359193583 span.yiv6359193583EmailStyle18
{font-family:sans-serif;color:#1F497D;}
#yiv6359193583 .yiv6359193583MsoChpDefault
{font-size:10.0pt;}
_filtered #yiv6359193583 {margin:70.85pt 70.85pt 70.85pt 70.85pt;}
#yiv6359193583 div.yiv6359193583WordSection1
{}
#yiv6359193583 </style><div>
<div class="yiv6359193583WordSection1">
<p class="yiv6359193583MsoNormal"><span style="font-size:11.0pt;">Hi Bernado,</span></p>
<p class="yiv6359193583MsoNormal"><span style="font-size:11.0pt;"> </span></p>
<p class="yiv6359193583MsoNormal"><span lang="EN-GB" style="font-size:11.0pt;">Please find attached the «work-in-progress» version of v.rast.bufferstats for some more inspiration.</span></p>
<p class="yiv6359193583MsoNormal"><span lang="EN-GB" style="font-size:11.0pt;"> </span></p>
<p class="yiv6359193583MsoNormal"><span lang="EN-GB" style="font-size:11.0pt;">v.db.select -r does what you are looking for:</span></p>
<p class="yiv6359193583MsoNormal"><span lang="EN-GB" style="font-size:11.0pt;"><a rel="nofollow" shape="rect" target="_blank" href="https://grass.osgeo.org/grass74/manuals/v.db.select.html">https://grass.osgeo.org/grass74/manuals/v.db.select.html</a></span></p>
<p class="yiv6359193583MsoNormal"><span lang="EN-GB" style="font-size:11.0pt;"> </span></p>
<p class="yiv6359193583MsoNormal"><span lang="EN-GB" style="font-size:11.0pt;">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…</span></p>
<p class="yiv6359193583MsoNormal"><span lang="EN-GB" style="font-size:11.0pt;"> </span></p>
<p class="yiv6359193583MsoNormal"><span lang="EN-GB" style="font-size:11.0pt;">Cheers</span></p>
<p class="yiv6359193583MsoNormal"><span lang="EN-GB" style="font-size:11.0pt;">Stefan
</span></p>
<p class="yiv6359193583MsoNormal"><span lang="EN-GB" style="font-size:11.0pt;"> </span></p>
<p class="yiv6359193583MsoNormal"><span lang="EN-GB" style="font-size:11.0pt;"> </span></p>
<p class="yiv6359193583MsoNormal"><span lang="EN-GB" style=""> </span></p>
<div class="yiv6359193583yqt9694868894" id="yiv6359193583yqt84316"><div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0cm 0cm 0cm;">
<p class="yiv6359193583MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;">From:</span></b><span lang="EN-US" style="font-size:11.0pt;"> Bernardo Santos <bernardo_brandaum@yahoo.com.br>
<br clear="none">
<b>Sent:</b> onsdag 11. april 2018 20.59<br clear="none">
<b>To:</b> Helmut Kudrnovsky <hellik@web.de>; grass-user@lists.osgeo.org; Stefan Blumentrath <Stefan.Blumentrath@nina.no><br clear="none">
<b>Subject:</b> Re: [GRASS-user] zonal statistics/metrics - beyond simple statistics</span></p>
</div>
</div>
<p class="yiv6359193583MsoNormal"> </p>
<div>
<div>
<p class="yiv6359193583MsoNormal"><span style="">Hi Helmut and Stefan,</span></p>
</div>
<div>
<p class="yiv6359193583MsoNormal"><span style=""> </span></p>
</div>
<div>
<p class="yiv6359193583MsoNormal"><span style="">thanks for sharing that with me, it is good to know that there are similar
<span style="color:black;">ongoing </span>approaches to solve related issues.</span></p>
</div>
<div>
<p class="yiv6359193583MsoNormal"><span style=""> </span></p>
</div>
<div>
<p class="yiv6359193583MsoNormal"><span style="">Helmut,</span></p>
</div>
<div>
<p class="yiv6359193583MsoNormal"><span style="">very nice addon. However, it seems to me that the part on calculating statistics is still based on v.rast.stats, am I right?</span></p>
</div>
<div>
<p class="yiv6359193583MsoNormal"><span style=""> </span></p>
</div>
<div>
<p class="yiv6359193583MsoNormal"><span style="">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.</span></p>
</div>
<div>
<p class="yiv6359193583MsoNormal"><span style="">What I do is:</span></p>
</div>
<div>
<p class="yiv6359193583MsoNormal"><span style="">1) g.region using the vector layer, aligning it to the first of the input rasters</span></p>
</div>
<div>
<p class="yiv6359193583MsoNormal"><span style="">2) v.to.rast selecting only one of the polygons at a time from the vector layer</span></p>
</div>
<div>
<p class="yiv6359193583MsoNormal"><span style="">3) r.mask using the rasterized polygon</span></p>
</div>
<div>
<p class="yiv6359193583MsoNormal"><span style="">4) g.region zoom=rasterized_polygon</span></p>
</div>
<div>
<p class="yiv6359193583MsoNormal"><span style="">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</span></p>
</div>
<div>
<p class="yiv6359193583MsoNormal"><span style="">6) repeat 1-5 for all polygons in the input vector</span></p>
</div>
<div>
<p class="yiv6359193583MsoNormal"><span style=""> </span></p>
</div>
<div>
<p class="yiv6359193583MsoNormal"><span style="">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).</span></p>
</div>
<div>
<p class="yiv6359193583MsoNormal"><span style="">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?</span></p>
</div>
<div>
<p class="yiv6359193583MsoNormal"><span style="">[this would be really great!!]</span></p>
</div>
<div>
<p class="yiv6359193583MsoNormal"><span style="">something like</span></p>
</div>
<div>
<p class="yiv6359193583MsoNormal"><span style="">g.region vector=vector_of_interest cat=1</span></p>
</div>
<div>
<p class="yiv6359193583MsoNormal"><span style=""> </span></p>
</div>
<div>
<p class="yiv6359193583MsoNormal"><span style="">Best,</span></p>
</div>
<div>
<p class="yiv6359193583MsoNormal"><span style="">Bernardo</span></p>
</div>
<div id="yiv6359193583yahoo_quoted_3745491540">
<div>
<div>
<p class="yiv6359193583MsoNormal"><span style="font-size:10.0pt;">Em terça-feira, 10 de abril de 2018 06:01:34 BRT, Stefan Blumentrath <<a rel="nofollow" shape="rect" ymailto="mailto:Stefan.Blumentrath@nina.no" target="_blank" href="mailto:Stefan.Blumentrath@nina.no">Stefan.Blumentrath@nina.no</a>> escreveu:
</span></p>
</div>
<div>
<p class="yiv6359193583MsoNormal"><span style="font-size:10.0pt;"> </span></p>
</div>
<div>
<p class="yiv6359193583MsoNormal"><span style="font-size:10.0pt;"> </span></p>
</div>
<div>
<div>
<p class="yiv6359193583MsoNormal"><span style="font-size:10.0pt;">Hei Bernardo,<br clear="none">
<br clear="none">
Just for the record: I am currently working on something similar / related.<br clear="none">
<br clear="none">
First of all I would like to improve speed of v.rast.stats for multiple inputs:<br clear="none">
<a rel="nofollow" shape="rect" target="_blank" href="https://www.mail-archive.com/grass-dev@lists.osgeo.org/msg52562.html%20">https://www.mail-archive.com/grass-dev@lists.osgeo.org/msg52562.html
</a>and <br clear="none">
<a rel="nofollow" shape="rect" target="_blank" href="https://trac.osgeo.org/grass/ticket/3523">https://trac.osgeo.org/grass/ticket/3523</a><br clear="none">
<br clear="none">
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...<br clear="none">
<br clear="none">
I will add my "work in progress" script to github, so you can have a look if you like.<br clear="none">
<br clear="none">
Cheers<br clear="none">
Stefan<br clear="none">
<br clear="none">
1: <a rel="nofollow" shape="rect" target="_blank" href="https://grass.osgeo.org/grass64/manuals/addons/">https://grass.osgeo.org/grass64/manuals/addons/</a><br clear="none">
<br clear="none">
-----Original Message-----<br clear="none">
From: grass-user <<a rel="nofollow" shape="rect" ymailto="mailto:grass-user-bounces@lists.osgeo.org" target="_blank" href="mailto:grass-user-bounces@lists.osgeo.org">grass-user-bounces@lists.osgeo.org</a>> On Behalf Of Helmut Kudrnovsky<br clear="none">
Sent: mandag 9. april 2018 23.19<br clear="none">
To: <a rel="nofollow" shape="rect" ymailto="mailto:grass-user@lists.osgeo.org" target="_blank" href="mailto:grass-user@lists.osgeo.org">grass-user@lists.osgeo.org</a><br clear="none">
Subject: Re: [GRASS-user] zonal statistics/metrics - beyond simple statistics<br clear="none">
<br clear="none">
Bernardo Santos wrote<br clear="none">
> Dear GRASS list,<br clear="none">
> I am developing a Python script to be able to calculate (virtually <br clear="none">
> any) metrics or statistics for zones/polygons in a vector - in analogy <br clear="none">
> to zonal statistics (such as v.rast.stats).The idea is that one can <br clear="none">
> calculate raster-based metrics (such as proportion of habitat, number <br clear="none">
> of patches, or any metric that can be formalized as a function that <br clear="none">
> takes some information from the input raster and returns a value) for <br clear="none">
> each polygon in the vector, and this is updated as a value in a newly <br clear="none">
> created column in the attribute table of this vector.<br clear="none">
> Is there already anything like that (some addon/module) that I am <br clear="none">
> missing, just to avoid re-doing something already created?<br clear="none">
> If not, what I am doing is to create a loop over all the features in a <br clear="none">
> vector, and for each one I zoom and use the polygon to define a mask <br clear="none">
> (using r.mask), so that the calculation of the selected metric is <br clear="none">
> performed only over that polygon, and this process is repeated.The <br clear="none">
> script allows one to calculate metrics/statistics for multiple raster <br clear="none">
> maps at once, and to incorporate other function for statistics also. <br clear="none">
> It may be found here:<a rel="nofollow" shape="rect" target="_blank" href="https://github.com/LEEClab/GeneralizedZonalStats">https://github.com/LEEClab/GeneralizedZonalStats</a><br clear="none">
> <br clear="none">
> For small vectors this works nicely and I believe it has a great <br clear="none">
> potential. However, when I try to calculate metrics for a large <br clear="none">
> dataset (e.g. the Brazilian map of cities, with almost 6,000 polygons) <br clear="none">
> - and that is when the tool would be interesting -, the process of <br clear="none">
> creating each mask takes too long (387 steps), and the tool becomes kind of useless.<br clear="none">
> Then I have two questions:- First, what drives the number of steps <br clear="none">
> GRASS takes to create a mask? Why it is very small for some maps but <br clear="none">
> very large for others? I quite don't understand that yet.- Do you <br clear="none">
> think of a easier or faster way of doing the same thing (instead of using masks)?<br clear="none">
> v.rast.stats seems to use r.univar and the option 'zones' for doing <br clear="none">
> so, but then one gets restricted to the statistics calculated by this module.<br clear="none">
> Any help or comment would be very welcome!<br clear="none">
> Best,Bernardo Niebuhr<br clear="none">
> _______________________________________________<br clear="none">
> grass-user mailing list<br clear="none">
<br clear="none">
> <a rel="nofollow" shape="rect" ymailto="mailto:grass-user@.osgeo" target="_blank" href="mailto:grass-user@.osgeo">grass-user@.osgeo</a><br clear="none">
<br clear="none">
> <a rel="nofollow" shape="rect" target="_blank" href="https://lists.osgeo.org/mailman/listinfo/grass-user">https://lists.osgeo.org/mailman/listinfo/grass-user</a><br clear="none">
<br clear="none">
have look at<br clear="none">
<br clear="none">
<a rel="nofollow" shape="rect" target="_blank" href="https://grass.osgeo.org/grass74/manuals/addons/v.habitat.dem.html">https://grass.osgeo.org/grass74/manuals/addons/v.habitat.dem.html</a><br clear="none">
<br clear="none">
where I've coded some DEM derived stats and characteristics for polygones.<br clear="none">
<br clear="none">
<br clear="none">
<br clear="none">
<br clear="none">
-----<br clear="none">
best regards<br clear="none">
Helmut<br clear="none">
--<br clear="none">
Sent from: <a rel="nofollow" shape="rect" target="_blank" href="http://osgeo-org.1560.x6.nabble.com/Grass-Users-f3884509.html">
http://osgeo-org.1560.x6.nabble.com/Grass-Users-f3884509.html</a><br clear="none">
_______________________________________________<br clear="none">
grass-user mailing list<br clear="none">
<a rel="nofollow" shape="rect" ymailto="mailto:grass-user@lists.osgeo.org" target="_blank" href="mailto:grass-user@lists.osgeo.org">grass-user@lists.osgeo.org</a></span></p>
<div id="yiv6359193583yqtfd18594">
<p class="yiv6359193583MsoNormal"><span style="font-size:10.0pt;"><br clear="none">
<a rel="nofollow" shape="rect" target="_blank" href="https://lists.osgeo.org/mailman/listinfo/grass-user">https://lists.osgeo.org/mailman/listinfo/grass-user</a><br clear="none">
_______________________________________________<br clear="none">
grass-user mailing list<br clear="none">
<a rel="nofollow" shape="rect" ymailto="mailto:grass-user@lists.osgeo.org" target="_blank" href="mailto:grass-user@lists.osgeo.org">grass-user@lists.osgeo.org</a><br clear="none">
<a rel="nofollow" shape="rect" target="_blank" href="https://lists.osgeo.org/mailman/listinfo/grass-user">https://lists.osgeo.org/mailman/listinfo/grass-user</a></span></p>
</div>
</div>
</div>
</div>
</div>
</div></div>
</div>
</div></div></div>
</div>
</div></div></body></html>