Closed tylerdrudolph closed 2 years ago
This all seems to work fine for me. I changed crs=st_crs(3005)$wkt)
to crs="EPSG:3005"
, but either works. I changed that to have a minimal number of packages attached. Perhaps you can try the below in a clean workspace (no other packages or things loaded from an old session)?
library(raster)
x <- raster(xmn=273366.5, xmx=1870587, ymn=359771.8, ymx=1735721, crs="EPSG:3005")
writeRaster(x, driver='GTiff', filename='x.tif', overwrite=T)
r <- raster('x.tif')
crs(r)
#CRS arguments:
# +proj=aea +lat_0=45 +lon_0=-126 +lat_1=50 +lat_2=58.5 +x_0=1000000 +y_0=0 +datum=NAD83 +units=m +no_defs
terra::rast(r)
#class : SpatRaster
#dimensions : 180, 360, 1 (nrow, ncol, nlyr)
#resolution : 4436.724, 7644.162 (x, y)
#extent : 273366.5, 1870587, 359771.8, 1735721 (xmin, xmax, ymin, ymax)
#coord. ref. : NAD83 / BC Albers (EPSG:3005)
filepath <- file.path(tempdir(), "test.tif")
r1 <- raster(extent(188500, 190350, 227550, 229550), res = 50)
values(r1) <- 1:ncell(r1)
crs(r1) <- 31370
crs(r1) <- "EPSG:31370"
#Warning message:
#In showSRID(SRS_string, format = "PROJ", multiline = "NO", prefer_proj = prefer_proj) :
# Discarded datum Reseau National Belge 1972 in Proj4 definition
terra::rast(r1)
#class : SpatRaster
#dimensions : 40, 37, 1 (nrow, ncol, nlyr)
#resolution : 50, 50 (x, y)
#extent : 188500, 190350, 227550, 229550 (xmin, xmax, ymin, ymax)
#coord. ref. : Belge 1972 / Belgian Lambert 72 (EPSG:31370)
sessionInfo()
#R version 4.1.1 (2021-08-10)
#Platform: x86_64-w64-mingw32/x64 (64-bit)
#Running under: Windows 10 x64 (build 19043)
#
#attached base packages:
#[1] stats graphics grDevices utils datasets methods base
#
#other attached packages:
#[1] raster_3.5-2 sp_1.4-5
#
#loaded via a namespace (and not attached):
#[1] compiler_4.1.1 rgdal_1.5-27 Rcpp_1.0.7 codetools_0.2-18
#[5] grid_4.1.1 lattice_0.20-44 terra_1.4-16
I'm still encountering the issue. Something related to warning # 2 of writeRaster()?
rm(list=ls())
#Restarting R session...
> library(raster)
#Loading required package: sp
> x <- raster(xmn=273366.5, xmx=1870587, ymn=359771.8, ymx=1735721, crs="EPSG:3005")
#Error in sp::CRS(SRS_string = x) : NA
> writeRaster(x, driver='GTiff', filename='x.tif', overwrite=T)
#Warning messages:
#1: In .local(x, filename, ...) : all cell values are NA
#2: In .gd_SetProject(object, ...) : NOT UPDATED FOR PROJ >= 6
> r <- raster('x.tif')
> crs(r)
CRS arguments: NA
I can use the sf argument instead, but once again the CRS doesn't reload:
> x <- raster(xmn=273366.5, xmx=1870587, ymn=359771.8, ymx=1735721, crs=sf::st_crs(3005)$wkt)
> writeRaster(x, driver='GTiff', filename='x.tif', overwrite=T)
#Warning message:
#In .local(x, filename, ...) : all cell values are NA
> raster('x.tif')
#class : RasterLayer
#dimensions : 180, 360, 64800 (nrow, ncol, ncell)
#resolution : 4436.724, 7644.162 (x, y)
#extent : 273366.5, 1870587, 359771.8, 1735721 (xmin, xmax, ymin, ymax)
#crs : NA
#source : x.tif
#names : x
Session info:
> (.packages())
#[1] "raster" "sp" "stats" "graphics" "grDevices" "utils" "datasets" "methods" "base"
> sessioninfo::session_info()
- Session info ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
hash: flag: Ukraine, red triangle pointed up, man mechanic
setting value
version R version 4.1.2 (2021-11-01)
os Windows 10 x64 (build 19042)
system x86_64, mingw32
ui RStudio
language (EN)
collate English_Canada.1252
ctype English_Canada.1252
tz America/New_York
date 2021-11-04
rstudio 1.4.1717 Juliet Rose (desktop)
pandoc NA
- Packages -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
package * version date (UTC) lib source
assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.1.1)
class 7.3-19 2021-05-03 [1] CRAN (R 4.1.2)
classInt 0.4-3 2020-04-07 [1] CRAN (R 4.1.1)
cli 3.1.0 2021-10-27 [1] CRAN (R 4.1.1)
codetools 0.2-18 2020-11-04 [1] CRAN (R 4.1.2)
crayon 1.4.2 2021-10-29 [1] CRAN (R 4.1.1)
DBI 1.1.1 2021-01-15 [1] CRAN (R 4.1.1)
dplyr 1.0.7 2021-06-18 [1] CRAN (R 4.1.1)
e1071 1.7-9 2021-09-16 [1] CRAN (R 4.1.1)
ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.1.1)
fansi 0.5.0 2021-05-25 [1] CRAN (R 4.1.1)
generics 0.1.1 2021-10-25 [1] CRAN (R 4.1.1)
glue 1.4.2 2020-08-27 [1] CRAN (R 4.1.1)
KernSmooth 2.23-20 2021-05-03 [1] CRAN (R 4.1.2)
lattice 0.20-45 2021-09-22 [1] CRAN (R 4.1.2)
lifecycle 1.0.1 2021-09-24 [1] CRAN (R 4.1.1)
magrittr 2.0.1 2020-11-17 [1] CRAN (R 4.1.1)
pillar 1.6.4 2021-10-18 [1] CRAN (R 4.1.1)
pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.1.1)
proxy 0.4-26 2021-06-07 [1] CRAN (R 4.1.1)
purrr 0.3.4 2020-04-17 [1] CRAN (R 4.1.1)
R6 2.5.1 2021-08-19 [1] CRAN (R 4.1.1)
raster * 3.5-2 2021-10-11 [1] CRAN (R 4.1.1)
Rcpp 1.0.7 2021-07-07 [1] CRAN (R 4.1.1)
rgdal 1.5-27 2021-09-16 [1] CRAN (R 4.1.1)
rlang 0.4.12 2021-10-18 [1] CRAN (R 4.1.1)
sessioninfo 1.2.1 2021-11-02 [1] CRAN (R 4.1.2)
sf 1.0-3 2021-10-07 [1] CRAN (R 4.1.1)
sp * 1.4-5 2021-01-10 [1] CRAN (R 4.1.1)
terra 1.4-11 2021-10-11 [1] CRAN (R 4.1.1)
tibble 3.1.5 2021-09-30 [1] CRAN (R 4.1.1)
tidyselect 1.1.1 2021-04-30 [1] CRAN (R 4.1.1)
units 0.7-2 2021-06-08 [1] CRAN (R 4.1.1)
utf8 1.2.2 2021-07-24 [1] CRAN (R 4.1.1)
vctrs 0.3.8 2021-04-29 [1] CRAN (R 4.1.1)
[1] C:/Users/trudolph/Documents/R/R-4.1.2/library
you still are in a "dirty session" with lots of packages loaded. It looks like you are using Rstudio. I do not know if that is the issue, but it might be. You could use "Terminate R" (restart does not do much), but would still need to make sure that no old workspace is loaded, you can use unlist(".RData")
to delete old workspaces.
The error still occurs using the latest version of RStudio and a brand new project. From the console:
> sessioninfo::session_info()
#- Session info ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# hash: flag: Tokelau, pizza, supervillain: medium-dark skin tone
#
# setting value
# version R version 4.1.2 (2021-11-01)
# os Windows 10 x64 (build 19042)
# system x86_64, mingw32
# ui RStudio
# language (EN)
# collate English_Canada.1252
# ctype English_Canada.1252
# tz America/New_York
# date 2021-11-05
# rstudio 2021.09.0+351 Ghost Orchid (desktop)
# pandoc NA
#
#- Packages -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# package * version date (UTC) lib source
# cli 3.1.0 2021-10-27 [1] CRAN (R 4.1.1)
# sessioninfo 1.2.1 2021-11-02 [1] CRAN (R 4.1.2)
#
# [1] C:/Program Files/R/R-4.1.2/library
#
#----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> library(raster)
#Loading required package: sp
> x <- raster(xmn=273366.5, xmx=1870587, ymn=359771.8, ymx=1735721, crs="EPSG:3005")
#Error in sp::CRS(SRS_string = x) : NA
However, from the terminal side of RStudio it works as expected:
> sessioninfo::session_info()
#- Session info -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# hash: man: medium-dark skin tone, curly hair, face with medical mask, flag: Brunei
#
# setting value
# version R version 4.1.2 (2021-11-01)
# os Windows 10 x64 (build 19042)
# system x86_64, mingw32
# ui RTerm
# language (EN)
# collate English_Canada.1252
# ctype English_Canada.1252
# tz America/New_York
# date 2021-11-05
# pandoc NA
#
#- Packages ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# package * version date (UTC) lib source
# cli 3.1.0 2021-10-27 [1] CRAN (R 4.1.1)
# sessioninfo 1.2.1 2021-11-02 [1] CRAN (R 4.1.2)
#
# [1] C:/Program Files/R/R-4.1.2/library
#
#---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> library(raster)
# Loading required package: sp
> x <- raster(xmn=273366.5, xmx=1870587, ymn=359771.8, ymx=1735721, crs="EPSG:$
> writeRaster(x, driver='GTiff', filename='x.tif', overwrite=T)
# Warning message:
# In .local(x, filename, ...) : all cell values are NA
> raster('x.tif')
# class : RasterLayer
# dimensions : 180, 360, 64800 (nrow, ncol, ncell)
# resolution : 4436.724, 7644.162 (x, y)
# extent : 273366.5, 1870587, 359771.8, 1735721 (xmin, xmax, ymin, ymax)
# crs : +proj=aea +lat_0=45 +lon_0=-126 +lat_1=50 +lat_2=58.5 +x_0=1000000 +y_0=0 +datum=NAD83 +units=m +no_defs
# source : x.tif
# names : x
Based on these results, it would appear the problem originates with RStudio. Closing this and opening a new issue at https://github.com/rstudio/rstudio/issues/10052.
I doubt that it directly related to R studio. It is almost certainly because of an old version of sp or rgdal being used.
I'm working with current package versions at present. This was all freshly installed this morning.
> packageVersion("sp")
# [1] ‘1.4.5’
> packageVersion("rgdal")
# [1] ‘1.5.27’
And I now find this fails even in a fresh launch of R.exe:
> sp::CRS("EPSG:3005")
#proj_create: C:\Program Files\PostgreSQL\14\share\contrib\postgis-3.1\proj\proj.db lacks DATABASE.LAYOUT.VERSION.MAJOR / DATABASE.LAYOUT.VERSION.MINOR metadata. It comes from another PROJ installation.
#proj_create: no database context specified
#proj_create: C:\Program Files\PostgreSQL\14\share\contrib\postgis-3.1\proj\proj.db lacks DATABASE.LAYOUT.VERSION.MAJOR / DATABASE.LAYOUT.VERSION.MINOR metadata. It comes from another PROJ installation.
#proj_create: no database context specified
#Error in sp::CRS("EPSG:3005") : NA
Comments by @kevinushey at https://github.com/rstudio/rstudio/issues/10052 indeed support the notion that RStudio is not to blame.
That again points at your rgdal
(or perhaps sp
) having issues. Can you show a minimal full session, like this:
R version 4.1.2 (2021-11-01) -- "Bird Hippie"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
>
> sp::CRS("EPSG:3005")
CRS arguments:
+proj=aea +lat_0=45 +lon_0=-126 +lat_1=50 +lat_2=58.5 +x_0=1000000
+y_0=0 +datum=NAD83 +units=m +no_defs
> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19043)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_4.1.2 rgdal_1.5-27 sp_1.4-5 grid_4.1.2
[5] lattice_0.20-45
>
From RStudio Console:
R version 4.1.2 (2021-11-01) -- "Bird Hippie"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(raster)
Loading required package: sp
> sp::CRS("EPSG:3005")
Error in sp::CRS("EPSG:3005") : NA
> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)
Matrix products: default
locale:
[1] LC_COLLATE=English_Canada.1252 LC_CTYPE=English_Canada.1252 LC_MONETARY=English_Canada.1252 LC_NUMERIC=C LC_TIME=English_Canada.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] raster_3.5-2 sp_1.4-5
loaded via a namespace (and not attached):
[1] compiler_4.1.2 rgdal_1.5-27 tools_4.1.2 Rcpp_1.0.7 codetools_0.2-18 grid_4.1.2 lattice_0.20-45 terra_1.4-11
From Rstudio Terminal:
C:\Users\trudolph\Downloads\test2>R
R version 4.1.2 (2021-11-01) -- "Bird Hippie"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(raster)
Loading required package: sp
> sp::CRS("EPSG:3005")
CRS arguments:
+proj=aea +lat_0=45 +lon_0=-126 +lat_1=50 +lat_2=58.5 +x_0=1000000
+y_0=0 +datum=NAD83 +units=m +no_defs
> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)
Matrix products: default
locale:
[1] LC_COLLATE=English_Canada.1252 LC_CTYPE=English_Canada.1252
[3] LC_MONETARY=English_Canada.1252 LC_NUMERIC=C
[5] LC_TIME=English_Canada.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] raster_3.5-2 sp_1.4-5
loaded via a namespace (and not attached):
[1] compiler_4.1.2 rgdal_1.5-27 Rcpp_1.0.7 codetools_0.2-18
[5] grid_4.1.2 lattice_0.20-45 terra_1.4-11
Behaviour is somewhat erratic. Before this attempt, both console and terminal produced errors. Sometimes the terminal behaves as expected, but it never seems to work in the console.
Can you make sure to start R studio with no open tabs. That is, close them all, then run unlink(".RData")
and close R Studio (do not save the workspace), and then try again. Do not do library(raster)
, we are not using it. All you need to do is
sp::CRS("EPSG:3005")
The reason for all this is that you appear to have the correct versions of the packages, but I suspect that an old version is stull being used because R studio tries to be smart.
If this does not help, then you should probably ask for help over at sp
, as it appears that it is CRS
that is not working.
Indeed, thank you Robert.
I have done as you instructed, with only the current version of R and all up-to-date packages installed, no open tabs or lingering session data. Result is consistent lately: sp::CRS("EPSG:3005") works in R, but does not work in RStudio.
I have posted anew at https://github.com/edzer/sp/issues/111.
Very mysterious. Sorry not to be able to provide more help
@rhijmans you have always been a great help to me sir!
Next step may be to uninstall and subsequently reinstall RStudio...
Issue # 199 (WKT2 string change after reading GeoTIFF file created with raster) appears to have resurfaced:
When I specify the crs using the PROJ.4 argument, I get the following warning, and the saved raster is returned with an incorrect EPSG code:
Using the EPSG # to define crs(x) also no longer works (using original example by @florisvdh):
My computing environment: