<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:Helvetica;
        panose-1:2 11 6 4 2 2 2 2 2 4;}
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman",serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
p.msonormal0, li.msonormal0, div.msonormal0
        {mso-style-name:msonormal;
        mso-margin-top-alt:auto;
        margin-right:0cm;
        mso-margin-bottom-alt:auto;
        margin-left:0cm;
        font-size:12.0pt;
        font-family:"Times New Roman",serif;}
span.EmailStyle18
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:70.85pt 70.85pt 70.85pt 70.85pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="NO-BOK" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US">Hi Bernado,<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US">Please find attached the «work-in-progress» version of v.rast.bufferstats for some more inspiration.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US">v.db.select -r does what you are looking for:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US"><a href="https://grass.osgeo.org/grass74/manuals/v.db.select.html">https://grass.osgeo.org/grass74/manuals/v.db.select.html</a><o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US">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…<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US">Cheers<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US">Stefan
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-GB" style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif">From:</span></b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif"> Bernardo Santos <bernardo_brandaum@yahoo.com.br>
<br>
<b>Sent:</b> onsdag 11. april 2018 20.59<br>
<b>To:</b> Helmut Kudrnovsky <hellik@web.de>; grass-user@lists.osgeo.org; Stefan Blumentrath <Stefan.Blumentrath@nina.no><br>
<b>Subject:</b> Re: [GRASS-user] zonal statistics/metrics - beyond simple statistics<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal"><span style="font-family:"Helvetica",sans-serif">Hi Helmut and Stefan,<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Helvetica",sans-serif"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Helvetica",sans-serif">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.<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Helvetica",sans-serif"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Helvetica",sans-serif">Helmut,<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Helvetica",sans-serif">very nice addon. However, it seems to me that the part on calculating statistics is still based on v.rast.stats, am I right?<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Helvetica",sans-serif"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Helvetica",sans-serif">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.<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Helvetica",sans-serif">What I do is:<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Helvetica",sans-serif">1) g.region using the vector layer, aligning it to the first of the input rasters<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Helvetica",sans-serif">2) v.to.rast selecting only one of the polygons at a time from the vector layer<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Helvetica",sans-serif">3) r.mask using the rasterized polygon<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Helvetica",sans-serif">4) g.region zoom=rasterized_polygon<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Helvetica",sans-serif">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<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Helvetica",sans-serif">6) repeat 1-5 for all polygons in the input vector<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Helvetica",sans-serif"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Helvetica",sans-serif">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).<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Helvetica",sans-serif">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?<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Helvetica",sans-serif">[this would be really great!!]<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Helvetica",sans-serif">something like<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Helvetica",sans-serif">g.region vector=vector_of_interest cat=1<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Helvetica",sans-serif"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Helvetica",sans-serif">Best,<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Helvetica",sans-serif">Bernardo<o:p></o:p></span></p>
</div>
<div id="yahoo_quoted_3745491540">
<div>
<div>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Helvetica",sans-serif;color:#26282A">Em terça-feira, 10 de abril de 2018 06:01:34 BRT, Stefan Blumentrath <<a href="mailto:Stefan.Blumentrath@nina.no">Stefan.Blumentrath@nina.no</a>> escreveu:
<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Helvetica",sans-serif;color:#26282A"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Helvetica",sans-serif;color:#26282A"><o:p> </o:p></span></p>
</div>
<div>
<div>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Helvetica",sans-serif;color:#26282A">Hei Bernardo,<br>
<br>
Just for the record: I am currently working on something similar / related.<br>
<br>
First of all I would like to improve speed of v.rast.stats for multiple inputs:<br>
<a href="https://www.mail-archive.com/grass-dev@lists.osgeo.org/msg52562.html%20" target="_blank">https://www.mail-archive.com/grass-dev@lists.osgeo.org/msg52562.html
</a>and <br>
<a href="https://trac.osgeo.org/grass/ticket/3523" target="_blank">https://trac.osgeo.org/grass/ticket/3523</a><br>
<br>
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>
<br>
I will add my "work in progress" script to github, so you can have a look if you like.<br>
<br>
Cheers<br>
Stefan<br>
<br>
1: <a href="https://grass.osgeo.org/grass64/manuals/addons/" target="_blank">https://grass.osgeo.org/grass64/manuals/addons/</a><br>
<br>
-----Original Message-----<br>
From: grass-user <<a href="mailto:grass-user-bounces@lists.osgeo.org">grass-user-bounces@lists.osgeo.org</a>> On Behalf Of Helmut Kudrnovsky<br>
Sent: mandag 9. april 2018 23.19<br>
To: <a href="mailto:grass-user@lists.osgeo.org">grass-user@lists.osgeo.org</a><br>
Subject: Re: [GRASS-user] zonal statistics/metrics - beyond simple statistics<br>
<br>
Bernardo Santos wrote<br>
> Dear GRASS list,<br>
> I am developing a Python script to be able to calculate (virtually <br>
> any) metrics or statistics for zones/polygons in a vector - in analogy <br>
> to zonal statistics (such as v.rast.stats).The idea is that one can <br>
> calculate raster-based metrics (such as proportion of habitat, number <br>
> of patches, or any metric that can be formalized as a function that <br>
> takes some information from the input raster and returns a value) for <br>
> each polygon in the vector, and this is updated as a value in a newly <br>
> created column in the attribute table of this vector.<br>
> Is there already anything like that (some addon/module) that I am <br>
> missing, just to avoid re-doing something already created?<br>
> If not, what I am doing is to create a loop over all the features in a <br>
> vector, and for each one I zoom and use the polygon to define a mask <br>
> (using r.mask), so that the calculation of the selected metric is <br>
> performed only over that polygon, and this process is repeated.The <br>
> script allows one to calculate metrics/statistics for multiple raster <br>
> maps at once, and to incorporate other function for statistics also. <br>
> It may be found here:<a href="https://github.com/LEEClab/GeneralizedZonalStats" target="_blank">https://github.com/LEEClab/GeneralizedZonalStats</a><br>
> <br>
> For small vectors this works nicely and I believe it has a great <br>
> potential. However, when I try to calculate metrics for a large <br>
> dataset (e.g. the Brazilian map of cities, with almost 6,000 polygons) <br>
> - and that is when the tool would be interesting -, the process of <br>
> creating each mask takes too long (387 steps), and the tool becomes kind of useless.<br>
> Then I have two questions:- First, what drives the number of steps <br>
> GRASS takes to create a mask? Why it is very small for some maps but <br>
> very large for others? I quite don't understand that yet.- Do you <br>
> think of a easier or faster way of doing the same thing (instead of using masks)?<br>
> v.rast.stats seems to use r.univar and the option 'zones' for doing <br>
> so, but then one gets restricted to the statistics calculated by this module.<br>
> Any help or comment would be very welcome!<br>
> Best,Bernardo Niebuhr<br>
> _______________________________________________<br>
> grass-user mailing list<br>
<br>
> <a href="mailto:grass-user@.osgeo">grass-user@.osgeo</a><br>
<br>
> <a href="https://lists.osgeo.org/mailman/listinfo/grass-user" target="_blank">https://lists.osgeo.org/mailman/listinfo/grass-user</a><br>
<br>
have look at<br>
<br>
<a href="https://grass.osgeo.org/grass74/manuals/addons/v.habitat.dem.html" target="_blank">https://grass.osgeo.org/grass74/manuals/addons/v.habitat.dem.html</a><br>
<br>
where I've coded some DEM derived stats and characteristics for polygones.<br>
<br>
<br>
<br>
<br>
-----<br>
best regards<br>
Helmut<br>
--<br>
Sent from: <a href="http://osgeo-org.1560.x6.nabble.com/Grass-Users-f3884509.html" target="_blank">
http://osgeo-org.1560.x6.nabble.com/Grass-Users-f3884509.html</a><br>
_______________________________________________<br>
grass-user mailing list<br>
<a href="mailto:grass-user@lists.osgeo.org">grass-user@lists.osgeo.org</a><o:p></o:p></span></p>
<div id="yqtfd18594">
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Helvetica",sans-serif;color:#26282A"><br>
<a href="https://lists.osgeo.org/mailman/listinfo/grass-user" target="_blank">https://lists.osgeo.org/mailman/listinfo/grass-user</a><br>
_______________________________________________<br>
grass-user mailing list<br>
<a href="mailto:grass-user@lists.osgeo.org">grass-user@lists.osgeo.org</a><br>
<a href="https://lists.osgeo.org/mailman/listinfo/grass-user" target="_blank">https://lists.osgeo.org/mailman/listinfo/grass-user</a><o:p></o:p></span></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>