isciences / exactextractr

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

SpatRaster (terra): CRS failure #58

Closed amarbler closed 2 years ago

amarbler commented 3 years ago

Hi,

I just saw the recent extension of exactextractr to accept SpatRaster (terra) objects and decided to give it a try. There seems to be a problem with reading the crs from terra objects:

library(exactextractr)
library(raster)
library(terra)
library(sf)

shp = sf::st_as_sf(raster::getData("GADM", country = "AUT", level = 0))
rast= terra::rast(res = 1)
terra::values(rast) = 1
terra::crs(rast, proj=T)
[1] "+proj=longlat +datum=WGS84 +no_defs"
extr = exactextractr::exact_extract(rast, shp, fun = "sum")
Warning message:
In .local(x, y, ...) :
  No CRS specified for raster; assuming it has the same CRS as the polygons.

sessionInfo()
R version 3.5.2 (2018-12-20)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Debian GNU/Linux 10 (buster)

Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.8.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.8.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] sf_1.0-2            terra_1.3-12        raster_3.4-12
[4] sp_1.4-5            exactextractr_0.7.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.6         compiler_3.5.2     pillar_1.6.1       R.methodsS3_1.8.1
 [5] R.utils_2.10.1     class_7.3-19       iterators_1.0.13   tools_3.5.2
 [9] gdalUtils_2.0.3.2  lifecycle_1.0.0    tibble_3.1.2       lattice_0.20-44
[13] pkgconfig_2.0.3    rlang_0.4.11       foreach_1.5.1      DBI_1.1.1
[17] rgdal_1.5-23       e1071_1.7-7        dplyr_1.0.7        generics_0.1.0
[21] vctrs_0.3.8        classInt_0.4-3     grid_3.5.2         tidyselect_1.1.1
[25] glue_1.4.2         R6_2.5.0           fansi_0.5.0        purrr_0.3.4
[29] magrittr_2.0.1     codetools_0.2-18   ellipsis_0.3.2     units_0.7-2
[33] assertthat_0.2.1   KernSmooth_2.23-20 utf8_1.2.1         proxy_0.4-26
[37] crayon_1.4.1       R.oo_1.24.0
dbaston commented 3 years ago

Thanks for the report! https://github.com/r-spatial/sf/pull/1716 should fix it.

dbaston commented 3 years ago

Closing this out, as the sf pull request has been merged.

amarbler commented 3 years ago

Thank you!

amarbler commented 3 years ago

Hi,

would it be possible to also allow the "exact_resample" function to accept terra objects?

dbaston commented 2 years ago

would it be possible to also allow the "exact_resample" function to accept terra objects?

Done with 7001fd4d15175417adbe822b2f185353b4372dd1

amarbler commented 2 years ago

Thanks, again! :)

kadyb commented 2 years ago

@dbaston, did you notice that exactextractr is faster using terra than raster? I did a simple comparison (data, code) and I see:

raster (file)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  14.76   14.84   14.92   15.05   15.07   16.09

terra (file)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  8.899   8.928   8.982   9.255   9.070  11.536
dbaston commented 2 years ago

Yes! I guess that's worth a mention in the README.

kadyb commented 2 years ago

One more interesting thing - if the data is loaded into memory, the times are very similar in this case.

raster (memory)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  2.687   2.718   2.748   2.796   2.849   3.048 

terra (memory)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  2.864   2.884   2.890   2.930   2.938   3.100