Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unrecognized or altered CRS following writeRaster() #232

Closed
tylerdrudolph opened this issue Nov 4, 2021 · 13 comments
Closed

Unrecognized or altered CRS following writeRaster() #232

tylerdrudolph opened this issue Nov 4, 2021 · 13 comments

Comments

@tylerdrudolph
Copy link

Issue # 199 (WKT2 string change after reading GeoTIFF file created with raster) appears to have resurfaced:

# 1) create raster with well-defined CRS
> x <- raster(xmn=273366.5, xmx=1870587, ymn=359771.8, ymx=1735721, crs=st_crs(3005)$wkt)

# 2) CRS present in memory
> crs(x)
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 

# 3) write to file
> writeRaster(x, driver='GTiff', filename='x.tif', overwrite=T)
Warning message:
In .local(x, filename, ...) : all cell values are NA

# 4) CRS not uptaken by raster
> crs(raster('x.tif'))
#> CRS arguments: NA 

# 5) ...yet CRS is correctly interpreted by GDAL
> sf::gdal_crs('x.tif')
Coordinate Reference System:
  User input: PROJCS["NAD83 / BC Albers",GEOGCS["NAD83",DATUM["North_American_Datum_1983",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],AUTHORITY["EPSG","6269"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4269"]],PROJECTION["Albers_Conic_Equal_Area"],PARAMETER["latitude_of_center",45],PARAMETER["longitude_of_center",-126],PARAMETER["standard_parallel_1",50],PARAMETER["standard_parallel_2",58.5],PARAMETER["false_easting",1000000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH],AUTHORITY["EPSG","3005"]] 
  wkt:
PROJCRS["NAD83 / BC Albers",
    BASEGEOGCRS["NAD83",
        DATUM["North American Datum 1983",
            ELLIPSOID["GRS 1980",6378137,298.257222101,
                LENGTHUNIT["metre",1]]],
        PRIMEM["Greenwich",0,
            ANGLEUNIT["degree",0.0174532925199433]],
        ID["EPSG",4269]],
    CONVERSION["unnamed",
        METHOD["Albers Equal Area",
            ID["EPSG",9822]],
        PARAMETER["Latitude of false origin",45,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8821]],
        PARAMETER["Longitude of false origin",-126,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8822]],
        PARAMETER["Latitude of 1st standard parallel",50,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8823]],
        PARAMETER["Latitude of 2nd standard parallel",58.5,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8824]],
        PARAMETER["Easting at false origin",1000000,
            LENGTHUNIT["metre",1],
            ID["EPSG",8826]],
        PARAMETER["Northing at false origin",0,
            LENGTHUNIT["metre",1],
            ID["EPSG",8827]]],
    CS[Cartesian,2],
        AXIS["easting",east,
            ORDER[1],
            LENGTHUNIT["metre",1]],
        AXIS["northing",north,
            ORDER[2],
            LENGTHUNIT["metre",1]],
    ID["EPSG",3005]]

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:

> x <- raster(xmn=273366.5, xmx=1870587, ymn=359771.8, ymx=1735721, crs=crs("+proj=aea +lat_1=50 +lat_2=58.5 +lat_0=45 +lon_0=-126 +x_0=1000000 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs "))  
Warning message:
In showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj = prefer_proj) :
  Discarded datum Unknown based on GRS80 ellipsoid in Proj4 definition

> writeRaster(x, driver='GTiff', filename='x.tif', overwrite=T)  
> sf::gdal_crs('x.tif')
Coordinate Reference System:
  User input: PROJCS["unknown",GEOGCS["unknown",DATUM["Unknown_based_on_GRS80_ellipsoid",SPHEROID["GRS 1980",6378137,298.257222101004,AUTHORITY["EPSG","7019"]]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]]],PROJECTION["Albers_Conic_Equal_Area"],PARAMETER["latitude_of_center",45],PARAMETER["longitude_of_center",-126],PARAMETER["standard_parallel_1",50],PARAMETER["standard_parallel_2",58.5],PARAMETER["false_easting",1000000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH]] 
  wkt:
PROJCRS["unknown",
    BASEGEOGCRS["unknown",
        DATUM["Unknown_based_on_GRS80_ellipsoid",
            ELLIPSOID["GRS 1980",6378137,298.257222101004,
                LENGTHUNIT["metre",1],
                ID["EPSG",7019]]],
        PRIMEM["Greenwich",0,
            ANGLEUNIT["degree",0.0174532925199433,
                ID["EPSG",9122]]]],
    CONVERSION["unnamed",
        METHOD["Albers Equal Area",
            ID["EPSG",9822]],
        PARAMETER["Latitude of false origin",45,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8821]],
        PARAMETER["Longitude of false origin",-126,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8822]],
        PARAMETER["Latitude of 1st standard parallel",50,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8823]],
        PARAMETER["Latitude of 2nd standard parallel",58.5,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8824]],
      PARAMETER["Easting at false origin",1000000,
            LENGTHUNIT["metre",1],
            ID["EPSG",8826]],
        PARAMETER["Northing at false origin",0,
            LENGTHUNIT["metre",1],
            ID["EPSG",8827]]],
    CS[Cartesian,2],
        AXIS["easting",east,
            ORDER[1],
            LENGTHUNIT["metre",1,
                ID["EPSG",9001]]],
        AXIS["northing",north,
            ORDER[2],
            LENGTHUNIT["metre",1,
                ID["EPSG",9001]]]]

Using the EPSG # to define crs(x) also no longer works (using original example by @florisvdh):

> filepath <- file.path(tempdir(), "test.tif")
> r1 <- raster(extent(188500, 190350, 227550, 229550), res = 50)
> values(r1) <- 1:ncell(r1)
> crs(r1) <- 31370
Error in CRS(SRS_string = x) : NA

My computing environment:

> sessioninfo::session_info()
- Session info  ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 hash: transgender symbol, person golfing, radio

 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)
 backports     1.3.0   2021-10-27 [1] CRAN (R 4.1.1)
 broom         0.7.10  2021-10-31 [1] CRAN (R 4.1.1)
 cellranger    1.1.0   2016-07-27 [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)
 colorspace    2.0-2   2021-06-24 [1] CRAN (R 4.1.1)
 crayon        1.4.2   2021-10-29 [1] CRAN (R 4.1.1)
 data.table    1.14.2  2021-09-27 [1] CRAN (R 4.1.1)
 DBI           1.1.1   2021-01-15 [1] CRAN (R 4.1.1)
 dbplyr        2.1.1   2021-04-06 [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)
 fasterize     1.0.3   2020-07-27 [1] CRAN (R 4.1.1)
 forcats     * 0.5.1   2021-01-27 [1] CRAN (R 4.1.1)
 foreign       0.8-81  2020-12-22 [1] CRAN (R 4.1.2)
 fs            1.5.0   2020-07-31 [1] CRAN (R 4.1.1)
 generics      0.1.1   2021-10-25 [1] CRAN (R 4.1.1)
 ggplot2     * 3.3.5   2021-06-25 [1] CRAN (R 4.1.1)
 glue          1.4.2   2020-08-27 [1] CRAN (R 4.1.1)
 gtable        0.3.0   2019-03-25 [1] CRAN (R 4.1.1)
 haven         2.4.3   2021-08-04 [1] CRAN (R 4.1.1)
 hms           1.1.1   2021-09-26 [1] CRAN (R 4.1.1)
 httr          1.4.2   2020-07-20 [1] CRAN (R 4.1.1)
 janitor     * 2.1.0   2021-01-05 [1] CRAN (R 4.1.1)
 jsonlite      1.7.2   2020-12-09 [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)
 lubridate     1.8.0   2021-10-07 [1] CRAN (R 4.1.1)
 magrittr      2.0.1   2020-11-17 [1] CRAN (R 4.1.1)
 modelr        0.1.8   2020-05-19 [1] CRAN (R 4.1.1)
 munsell       0.5.0   2018-06-12 [1] CRAN (R 4.1.1)
 openxlsx    * 4.2.4   2021-06-16 [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)
 rasterDT      0.3.1   2020-03-04 [1] CRAN (R 4.1.1)
 Rcpp          1.0.7   2021-07-07 [1] CRAN (R 4.1.1)
 readr       * 2.0.2   2021-09-27 [1] CRAN (R 4.1.1)
 readxl        1.3.1   2019-03-13 [1] CRAN (R 4.1.1)
 reprex        2.0.1   2021-08-05 [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)
 rstudioapi    0.13    2020-11-12 [1] CRAN (R 4.1.1)
 rvest         1.0.2   2021-10-16 [1] CRAN (R 4.1.1)
 scales        1.1.1   2020-05-11 [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)
 snakecase     0.11.0  2019-05-25 [1] CRAN (R 4.1.1)
 sp          * 1.4-5   2021-01-10 [1] CRAN (R 4.1.1)
 stringi       1.7.5   2021-10-04 [1] CRAN (R 4.1.1)
 stringr     * 1.4.0   2019-02-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)
 tidyr       * 1.1.4   2021-09-27 [1] CRAN (R 4.1.1)
 tidyselect    1.1.1   2021-04-30 [1] CRAN (R 4.1.1)
 tidyverse   * 1.3.1   2021-04-15 [1] CRAN (R 4.1.1)
 tzdb          0.2.0   2021-10-27 [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)
 withr         2.4.2   2021-04-18 [1] CRAN (R 4.1.1)
 xml2          1.3.2   2020-04-23 [1] CRAN (R 4.1.1)
 zip           2.2.0   2021-05-31 [1] CRAN (R 4.1.1)
@rhijmans
Copy link
Member

rhijmans commented Nov 4, 2021

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    

@tylerdrudolph
Copy link
Author

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

@rhijmans
Copy link
Member

rhijmans commented Nov 4, 2021

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.

@tylerdrudolph
Copy link
Author

tylerdrudolph commented Nov 5, 2021

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 rstudio/rstudio#10052.

@rhijmans
Copy link
Member

rhijmans commented Nov 5, 2021

I doubt that it directly related to R studio. It is almost certainly because of an old version of sp or rgdal being used.

@tylerdrudolph
Copy link
Author

tylerdrudolph commented Nov 5, 2021

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 rstudio/rstudio#10052 indeed support the notion that RStudio is not to blame.

@tylerdrudolph tylerdrudolph reopened this Nov 5, 2021
@rhijmans
Copy link
Member

rhijmans commented Nov 6, 2021

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
> 

@tylerdrudolph
Copy link
Author

tylerdrudolph commented Nov 6, 2021

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.

@rhijmans
Copy link
Member

rhijmans commented Nov 6, 2021

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.

@rhijmans
Copy link
Member

rhijmans commented Nov 6, 2021

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.

@tylerdrudolph
Copy link
Author

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 edzer/sp#111.

@rhijmans
Copy link
Member

rhijmans commented Nov 8, 2021

Very mysterious. Sorry not to be able to provide more help

@tylerdrudolph
Copy link
Author

@rhijmans you have always been a great help to me sir!

Next step may be to uninstall and subsequently reinstall RStudio...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants