isciences / exactextractr

R package for fast and accurate raster zonal statistics
https://isciences.gitlab.io/exactextractr/
272 stars 26 forks source link

Raster index for each polygon row (Feature request) #86

Open ghost opened 1 year ago

ghost commented 1 year ago

For temporal raster data, I often have a stack of rasters that represent the same variable across time. For instance, raster 1 is 2000, raster 2 is 2001, and so on. I have polygon data where each row corresponds to one of those years. I wish to calculate some area metric using extact_extract based on the corresponding year of each polygon. It may be possible to do it efficiently in the current setup, but I could only do it by looping (applying) through each polygon row or breaking it up by time. I am wondering if an index feature could be added such that each polygon is matched to the corresponding raster based on the index. Let me know if that makes sense. Thank you!

dbaston commented 1 year ago

If I'm understanding correctly, we have N unique polygons and we need stats calculated from N unique raters. If that's the case, I don't see any computational efficiency gain from bringing the looping inside exactextractr...the gain would be in having more concise syntax. Is that right?

ghost commented 1 year ago

More like N unique polygons corresponding to M different rasters, with N > M. It can be done through looping on the outside for sure. I'm not 100% familiar with how the reading in/out of the rasters is handled, but my thought was that it would minimize reading rasters for each polygon separately. But now that you mention it, it seems like just a loop by the unique rasters, M, would be the simplest.