Repository navigation
Closed
Description
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)
Metadata
Metadata
Assignees
Labels
No labels
Activity
rhijmans commentedon Nov 4, 2021
This all seems to work fine for me. I changed
crs=st_crs(3005)$wkt)tocrs="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)?tylerdrudolph commentedon Nov 4, 2021
I'm still encountering the issue. Something related to warning # 2 of writeRaster()?
I can use the sf argument instead, but once again the CRS doesn't reload:
Session info:
rhijmans commentedon 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 commentedon Nov 5, 2021
The error still occurs using the latest version of RStudio and a brand new project. From the console:
However, from the terminal side of RStudio it works as expected:
Based on these results, it would appear the problem originates with RStudio. Closing this and opening a new issue at rstudio/rstudio#10052.
rhijmans commentedon 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 commentedon Nov 5, 2021
I'm working with current package versions at present. This was all freshly installed this morning.
And I now find this fails even in a fresh launch of R.exe:
Comments by @kevinushey at rstudio/rstudio#10052 indeed support the notion that RStudio is not to blame.
rhijmans commentedon Nov 6, 2021
That again points at your
rgdal(or perhapssp) having issues. Can you show a minimal full session, like this:tylerdrudolph commentedon Nov 6, 2021
From RStudio Console:
From Rstudio Terminal:
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 commentedon 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 dolibrary(raster), we are not using it. All you need to do issp::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 commentedon Nov 6, 2021
If this does not help, then you should probably ask for help over at
sp, as it appears that it isCRSthat is not working.tylerdrudolph commentedon Nov 8, 2021
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 commentedon Nov 8, 2021
Very mysterious. Sorry not to be able to provide more help
tylerdrudolph commentedon Nov 8, 2021
@rhijmans you have always been a great help to me sir!
Next step may be to uninstall and subsequently reinstall RStudio...