isciences / exactextractr

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

SpatVector input implemented but not yet available? #82

Open AMBarbosa opened 2 years ago

AMBarbosa commented 2 years ago

Hi, here you present a couple of examples using a "SpatVector" (brazil_spat) as input, but when I try that myself, I get _Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘exactextract’ for signature ‘"SpatRaster", "SpatVector" (both with the CRAN and with the GitHub version of 'exactextractr'). Will the "SpatVector" implementation become available soon? It will be really useful! Cheers

dbaston commented 2 years ago

Those examples are comparing the performance of exactextractr::exact_extract to terra::extract. I don't have any plans to add support for SpatVector but would be happy to accept a contribution that does so.

AMBarbosa commented 2 years ago

My bad, I didn't notice some examples were with extract and others with exact_extract ;) But minimal implementation could be something so simple as coercing to 'sf' at the beginning of the function:

if (inherits(y, "SpatVector"))  y <- st_as_sf(y)

... and then adding "SpatVector" to the help file @param y

dbaston commented 2 years ago

I've been on the fence about that, because I'm wary of silently copying the full input dataset. But I guess it's no different than what I'm doing to "support" sp types. Do you think a warning would be appropriate, or is it just not that big a deal for most users?

AMBarbosa commented 2 years ago

I don't think it is warning-worth, but it might indeed be a good idea to mention in the help file @param y that 'Spatial*' and 'SpatVector' inputs will be converted to 'sf'.

fabeit commented 1 year ago

I am not sure if it's now possible to do exact_extract(rast,vect,fun=sum) ?

dbaston commented 1 year ago

I am not sure if it's now possible to do exact_extract(rast,vect,fun=sum) ?

You would need to do exact_extract(rast, st_as_sf(vect), fun = 'sum').