ecohealthalliance / fasterize

High performance raster conversion for modern spatial data 🚀🌏▦
https://ecohealthalliance.github.io/fasterize/
Other
182 stars 15 forks source link

incorrect file metadata on output raster #41

Closed jeffreyhanson closed 1 year ago

jeffreyhanson commented 3 years ago

Hi,

Thanks so much for developing fasterize! I've been playing around with using fasterize with terra, and I noticed that converting RasterLayer objects produced by fasterize::fasterize() into terra::rast() objects can have some issues (see here). This is because the output object from fasterize::fasterize() can be assigned the same filename information as the input raster -- even though the output raster should (in many cases) have different data. For example, please see the reprex below (using GitHub version of fasterize). I've also included my session info too. Please let me know if there's any further details I can provide?

# load packages
library(sf)
library(terra)
library(raster)
library(fasterize)
library(dplyr)
library(rnaturalearth)

# load vector data for country boundaries
d <-
  rnaturalearth::ne_countries(type = "countries", scale = "small") %>%
  sf::st_as_sf() %>%
  filter(region_wb %in% c("Sub-Saharan Africa", "North America")) %>%
  sf::st_make_valid() %>%
  sf::st_wrap_dateline(options = c("WRAPDATELINE=YES")) %>%
  sf::st_make_valid() %>%
  sf::st_transform(sf::st_crs("ESRI:54017")) %>%
  sf::st_union() %>%
  sf::st_make_valid() %>%
  sf::st_as_sf(idx = 1)

# load raster (1 km global raster with ESRI:54017 CRS)
r <- raster::raster(terra::rast(
  vals = 1, nrow = 14685, ncol = 34736,
  xmin = -17367531, xmax = 17368469, ymin = -7342231, ymax = 7342769,
  resolution = c(1000, 1000),
  crs = sf::st_crs("ESRI:54017")$wkt
))

# create raster with data
o1 <- fasterize::fasterize(d, as(r, "Raster"))

# print filenames for r and o1
print(raster::filename(r))
#> [1] "/tmp/RtmpK9zRHX/spat_JlGvrvI8vJ3XzsD.tif"

print(raster::filename(o1))
#> [1] "/tmp/RtmpK9zRHX/spat_JlGvrvI8vJ3XzsD.tif"

Session information

R version 4.1.1 (2021-08-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 21.04

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.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] rnaturalearth_0.1.0 dplyr_1.0.7         fasterize_1.0.3    
[4] raster_3.5-2        sp_1.4-5            terra_1.4-14       
[7] sf_1.0-3           

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.7         pillar_1.6.3       compiler_4.1.1     class_7.3-19      
 [5] tools_4.1.1        lifecycle_1.0.1    tibble_3.1.5       lattice_0.20-45   
 [9] pkgconfig_2.0.3    rlang_0.4.11       DBI_1.1.1          rgdal_1.5-27      
[13] e1071_1.7-9        s2_1.0.7           generics_0.1.0     vctrs_0.3.8       
[17] rgeos_0.5-8        classInt_0.4-3     grid_4.1.1         tidyselect_1.1.1  
[21] glue_1.4.2         R6_2.5.1           fansi_0.5.0        purrr_0.3.4       
[25] magrittr_2.0.1     codetools_0.2-18   ellipsis_0.3.2     units_0.7-2       
[29] assertthat_0.2.1   utf8_1.2.2         KernSmooth_2.23-20 proxy_0.4-26      
[33] wk_0.5.0           crayon_1.4.1  
jeffreyhanson commented 1 year ago

Awesome - thanks so much @mdsumner!

mdsumner commented 1 year ago

I haven't checked this really so much appreciated if you can

jeffreyhanson commented 1 year ago

Ah - sure, I can do that.

jeffreyhanson commented 1 year ago

Yeah this seems to work for me now. I can confirm that (1) the memory location (i.e. file path or "memory") is correct, and (2) accessing values from each raster yields expected results.