<html><head></head><body><div style="font-family:Helvetica Neue, Helvetica, Arial, sans-serif;font-size:16px;"><div>Dear GRASS list,</div><div><br></div><div>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).</div><div>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.</div><div><br></div><div>Is there already anything like that (some addon/module) that I am missing, just to avoid re-doing something already created?</div><div><br></div><div>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.</div><div>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:</div><div><a href="https://github.com/LEEClab/GeneralizedZonalStats" rel="nofollow" target="_blank">https://github.com/LEEClab/GeneralizedZonalStats</a><br></div><div><br></div><div>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.</div><div><br></div><div>Then I have two questions:</div><div>- 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.</div><div>- 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.</div><div><br></div><div>Any help or comment would be very welcome!</div><div><br></div><div>Best,</div><div>Bernardo Niebuhr</div></div></body></html>