rspatial / terra

R package for spatial data handling https://rspatial.github.io/terra/reference/terra-package.html
GNU General Public License v3.0
541 stars 89 forks source link

project not working in terra. It works with sf #1470

Open alexyshr opened 7 months ago

alexyshr commented 7 months ago

Hi there. Thanks for your work!

Any suggestion to make this example run with terra?

Best

library("sf")
#> Linking to GEOS 3.11.2, GDAL 3.8.2, PROJ 9.3.1; sf_use_s2() is TRUE
library("terra")
#> terra 1.7.74

n <- 3000 * 3
r <- rast(nrows = n, ncols = n, nlyrs = 3, vals = runif(n^2 * 3))
input <- tempfile(fileext = ".tif")
writeRaster(r, input)
#> Warning: PROJ: proj_create_from_name: C:\Program Files\GDAL\projlib\proj.db
#> lacks DATABASE.LAYOUT.VERSION.MAJOR / DATABASE.LAYOUT.VERSION.MINOR metadata.
#> It comes from another PROJ installation. (GDAL error 1)
output_terra <- tempfile(fileext = ".tif")
output_gdal <- tempfile(fileext = ".tif")

# PROJECT FILES
# not working with terra
r <- rast(input)
project(r, "EPSG:3857",
  method = "bilinear", gdal = TRUE,
  filename = output_terra, overwrite = TRUE
)
#> Warning: PROJ: proj_create_from_database: C:\Program Files\GDAL\projlib\proj.db
#> lacks DATABASE.LAYOUT.VERSION.MAJOR / DATABASE.LAYOUT.VERSION.MINOR metadata.
#> It comes from another PROJ installation. (GDAL error 1)
#> Warning: PROJ: proj_create_from_database: C:\Program Files\GDAL\projlib\proj.db
#> lacks DATABASE.LAYOUT.VERSION.MAJOR / DATABASE.LAYOUT.VERSION.MINOR metadata.
#> It comes from another PROJ installation. (GDAL error 1)
#> Warning: [project] Cannot set raster SRS: empty srs
#> Error: [project] cannot get output boundaries

gdal_utils(
  util = "warp",
  source = input,
  destination = output_gdal,
  options = c(
  "-t_srs", "EPSG:3857",
  "-r", "bilinear",
  "-co", "COMPRESS=LZW",
  "-overwrite"
  )
)
#> Warning in CPL_gdalwarp(source, destination, options, oo, doo, config_options,
#> : GDAL Message 1: Clamping output bounds to (-20037508.342789,-20037508.342789)
#> -> (20037508.342789, 20037508.342789)

rp <- rast(output_gdal)

Created on 2024-04-07 with reprex v2.0.2

Session info ``` r sessioninfo::session_info() #> - Session info --------------------------------------------------------------- #> setting value #> version R version 4.3.3 (2024-02-29 ucrt) #> os Windows 10 x64 (build 19045) #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate English_United States.utf8 #> ctype English_United States.utf8 #> tz America/New_York #> date 2024-04-07 #> pandoc 2.17.1.1 @ C:/Users/500596~1/AppData/Local/Pandoc/ (via rmarkdown) #> #> - Packages ------------------------------------------------------------------- #> package * version date (UTC) lib source #> class 7.3-22 2023-05-03 [2] CRAN (R 4.3.3) #> classInt 0.4-10 2023-09-05 [1] CRAN (R 4.3.1) #> cli 3.6.2 2023-12-11 [2] CRAN (R 4.3.3) #> codetools 0.2-19 2023-02-01 [2] CRAN (R 4.3.3) #> DBI 1.2.2 2024-02-16 [1] CRAN (R 4.3.3) #> digest 0.6.33 2023-07-07 [1] CRAN (R 4.3.1) #> e1071 1.7-14 2023-12-06 [2] CRAN (R 4.3.3) #> evaluate 0.21 2023-05-05 [1] CRAN (R 4.3.1) #> fastmap 1.1.1 2023-02-24 [1] CRAN (R 4.3.1) #> fs 1.6.3 2023-07-20 [1] CRAN (R 4.3.1) #> glue 1.7.0 2024-01-09 [2] CRAN (R 4.3.3) #> htmltools 0.5.6 2023-08-10 [1] CRAN (R 4.3.1) #> KernSmooth 2.23-22 2023-07-10 [1] CRAN (R 4.3.1) #> knitr 1.44 2023-09-11 [1] CRAN (R 4.3.1) #> lifecycle 1.0.4 2023-11-07 [1] CRAN (R 4.3.3) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.3.1) #> proxy 0.4-27 2022-06-09 [1] CRAN (R 4.3.1) #> purrr 1.0.2 2023-08-10 [1] CRAN (R 4.3.1) #> R.cache 0.16.0 2022-07-21 [1] CRAN (R 4.3.2) #> R.methodsS3 1.8.2 2022-06-13 [1] CRAN (R 4.3.1) #> R.oo 1.26.0 2024-01-24 [1] CRAN (R 4.3.2) #> R.utils 2.12.3 2023-11-18 [1] CRAN (R 4.3.2) #> Rcpp 1.0.12 2024-01-09 [1] CRAN (R 4.3.3) #> reprex 2.0.2 2022-08-17 [1] CRAN (R 4.3.1) #> rlang 1.1.3 2024-01-10 [2] CRAN (R 4.3.3) #> rmarkdown 2.25 2023-09-18 [1] CRAN (R 4.3.1) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.3.1) #> sf * 1.0-17 2024-04-06 [1] Github (r-spatial/sf@2867dd2) #> styler 1.10.2 2023-08-29 [1] CRAN (R 4.3.2) #> terra * 1.7-74 2024-04-07 [1] Github (rspatial/terra@29fe968) #> units 0.8-5 2023-11-28 [2] CRAN (R 4.3.3) #> vctrs 0.6.5 2023-12-01 [2] CRAN (R 4.3.3) #> withr 3.0.0 2024-01-16 [1] CRAN (R 4.3.3) #> xfun 0.40 2023-08-09 [1] CRAN (R 4.3.1) #> yaml 2.3.7 2023-01-23 [1] CRAN (R 4.3.0) #> #> [1] C:/Users/500596972/AppData/Local/R/win-library/4.3 #> [2] C:/Program Files/R/R-4.3.3/library #> #> ------------------------------------------------------------------------------ ```
kadyb commented 7 months ago
project(r, "EPSG:3857", method = "bilinear", gdal = TRUE, filename = output_terra)
#> class       : SpatRaster 
#> dimensions  : 12685, 1048, 3  (nrow, ncol, nlyr)
#> resolution  : 38239.51, 38239.55  (x, y)
#> extent      : -20037508, 20037503, -242540027, 242528681  (xmin, xmax, ymin, ymax)
#> coord. ref. : WGS 84 / Pseudo-Mercator (EPSG:3857) 
#> source      : file2ab8194d5d8e.tif 
#> names       :        lyr.1,        lyr.2,        lyr.3 
#> min values  : 0.0002326849, 0.0005849558, 0.0002357024 
#> max values  : 0.9998279214, 0.9997069240, 0.9998372197 

I can't reproduce with terra 1.7.74 installed from R-universe on Windows 10. Maybe you have some problem with the installation as: It comes from another PROJ installation. (GDAL error 1).