Repository navigation
Closed
Description
Maybe below problem will not happen for every CRS, but at least in below example the WKT2 string gets 'corrupted' in one way or another, when reading the GeoTIFF file, previously created with writeRaster(). It should remain unchanged in order to prevent conflicts with tools that rely on the WKT2 string.
The GeoTIFF is not the problem, as both rgdal and native GDAL tools (gdalsrsinfo etc) return the correct WKT.
options(rgdal_show_exportToProj4_warnings = "none")
library(raster)
#> Loading required package: sp
rgdal::rgdal_extSoftVersion()
#> GDAL GDAL_with_GEOS PROJ sp
#> "3.1.3" "TRUE" "7.2.1" "1.4-5"
# creating RasterLayer with CRS EPSG:31370 and write as GeoTIFF
filepath <- file.path(tempdir(), "test.tif")
r1 <- raster(extent(188500, 190350, 227550, 229550), res = 50)
values(r1) <- 1:ncell(r1)
crs(r1) <- 31370
crs1 <- crs(r1)
writeRaster(r1, filepath)
# read the GeoTIFF back
r2 <- raster(filepath)
crs2 <- crs(r2)
# the obsolete proj4strings still match (some parts were discarded automatically):
crs1@projargs == crs2@projargs
#> [1] TRUE
# but the formal CRS definition (WKT2) for use by PROJ/GDAL, does not match:
wkt(crs1) == wkt(crs2)
#> [1] FALSE
# there's no problem when using rgdal:
r3 <- rgdal::readGDAL(filepath, silent = TRUE)
wkt(r3) == wkt(crs1)
#> [1] TRUE
# lines only present in crs1:
setdiff(strsplit(wkt(crs1), "\n")[[1]], strsplit(wkt(crs2), "\n")[[1]])
#> [1] "PROJCRS[\"Belge 1972 / Belgian Lambert 72\","
#> [2] " BASEGEOGCRS[\"Belge 1972\","
#> [3] " DATUM[\"Reseau National Belge 1972\","
#> [4] " ELLIPSOID[\"International 1924\",6378388,297,"
#> [5] " LENGTHUNIT[\"metre\",1]]],"
#> [6] " ANGLEUNIT[\"degree\",0.0174532925199433]],"
#> [7] " ID[\"EPSG\",4313]],"
#> [8] " CONVERSION[\"Belgian Lambert 72\","
#> [9] " AXIS[\"easting (X)\",east,"
#> [10] " LENGTHUNIT[\"metre\",1]],"
#> [11] " AXIS[\"northing (Y)\",north,"
#> [12] " USAGE["
#> [13] " SCOPE[\"Engineering survey, topographic mapping.\"],"
#> [14] " AREA[\"Belgium - onshore.\"],"
#> [15] " BBOX[49.5,2.5,51.51,6.4]],"
#> [16] " ID[\"EPSG\",31370]]"
# lines only present in crs2:
setdiff(strsplit(wkt(crs2), "\n")[[1]], strsplit(wkt(crs1), "\n")[[1]])
#> [1] "PROJCRS[\"unknown\","
#> [2] " BASEGEOGCRS[\"unknown\","
#> [3] " DATUM[\"Unknown based on International 1909 (Hayford) ellipsoid\","
#> [4] " ELLIPSOID[\"International 1909 (Hayford)\",6378388,297,"
#> [5] " LENGTHUNIT[\"metre\",1,"
#> [6] " ID[\"EPSG\",9001]]]],"
#> [7] " ID[\"EPSG\",8901]]],"
#> [8] " CONVERSION[\"unknown\","
#> [9] " AXIS[\"(E)\",east,"
#> [10] " LENGTHUNIT[\"metre\",1,"
#> [11] " ID[\"EPSG\",9001]]],"
#> [12] " AXIS[\"(N)\",north,"
#> [13] " ID[\"EPSG\",9001]]]]"Created on 2021-04-28 by the reprex package (v2.0.0)
Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#> setting value
#> version R version 4.0.5 (2021-03-31)
#> os Linux Mint 20
#> system x86_64, linux-gnu
#> ui X11
#> language nl_BE:nl
#> collate nl_BE.UTF-8
#> ctype nl_BE.UTF-8
#> tz Europe/Brussels
#> date 2021-04-28
#>
#> ─ Packages ───────────────────────────────────────────────────────────────────
#> ! package * version date lib source
#> P cli 2.4.0 2021-04-05 [?] CRAN (R 4.0.5)
#> P codetools 0.2-18 2020-11-04 [3] CRAN (R 4.0.3)
#> P digest 0.6.27 2020-10-24 [?] CRAN (R 4.0.3)
#> P evaluate 0.14 2019-05-28 [?] CRAN (R 4.0.2)
#> P fs 1.5.0 2020-07-31 [?] CRAN (R 4.0.2)
#> P glue 1.4.2 2020-08-27 [?] CRAN (R 4.0.2)
#> P highr 0.9 2021-04-16 [?] CRAN (R 4.0.5)
#> P htmltools 0.5.1.1 2021-01-22 [?] CRAN (R 4.0.3)
#> P knitr 1.32 2021-04-14 [?] CRAN (R 4.0.5)
#> P lattice 0.20-41 2020-04-02 [3] CRAN (R 4.0.0)
#> P magrittr 2.0.1 2020-11-17 [?] CRAN (R 4.0.3)
#> P raster * 3.4-5 2020-11-14 [?] CRAN (R 4.0.3)
#> P Rcpp 1.0.6 2021-01-15 [?] CRAN (R 4.0.3)
#> P reprex 2.0.0 2021-04-02 [?] CRAN (R 4.0.5)
#> P rgdal 1.5-23 2021-02-03 [?] CRAN (R 4.0.3)
#> P rlang 0.4.10 2020-12-30 [?] CRAN (R 4.0.3)
#> P rmarkdown 2.7 2021-02-19 [?] CRAN (R 4.0.4)
#> P rstudioapi 0.13 2020-11-12 [?] CRAN (R 4.0.3)
#> P sessioninfo 1.1.1 2018-11-05 [?] CRAN (R 4.0.2)
#> P sp * 1.4-5 2021-01-10 [?] CRAN (R 4.0.3)
#> P stringi 1.5.3 2020-09-09 [?] CRAN (R 4.0.2)
#> P stringr 1.4.0 2019-02-10 [?] CRAN (R 4.0.2)
#> P withr 2.4.2 2021-04-18 [?] CRAN (R 4.0.5)
#> P xfun 0.22 2021-03-11 [?] CRAN (R 4.0.4)
#> P yaml 2.2.1 2020-02-01 [?] CRAN (R 4.0.2)
#>
#> [1] /media/floris/DATA/PROJECTS/09685_NatuurlijkMilieu/160 Bewerkingen en resultaat/Repos_en_data/n2khab-preprocessing/src/generate_raster_runif/renv/library/R-4.0/x86_64-pc-linux-gnu
#> [2] /tmp/RtmpBiBXbA/renv-system-library
#> [3] /usr/lib/R/library
#>
#> P ── Loaded and on-disk path mismatch.Source code only
options(rgdal_show_exportToProj4_warnings = "none")
library(raster)
rgdal::rgdal_extSoftVersion()
# creating RasterLayer with CRS EPSG:31370 and write as GeoTIFF
filepath <- file.path(tempdir(), "test.tif")
r1 <- raster(extent(188500, 190350, 227550, 229550), res = 50)
values(r1) <- 1:ncell(r1)
crs(r1) <- 31370
crs1 <- crs(r1)
writeRaster(r1, filepath)
# read the GeoTIFF back
r2 <- raster(filepath)
crs2 <- crs(r2)
# the obsolete proj4strings still match (some parts were discarded automatically):
crs1@projargs == crs2@projargs
# but the formal CRS definition (WKT2) for use by PROJ/GDAL, does not match:
wkt(crs1) == wkt(crs2)
# there's no problem when using rgdal:
r3 <- rgdal::readGDAL(filepath, silent = TRUE)
wkt(r3) == wkt(crs1)
# lines only present in crs1:
setdiff(strsplit(wkt(crs1), "\n")[[1]], strsplit(wkt(crs2), "\n")[[1]])
# lines only present in crs2:
setdiff(strsplit(wkt(crs2), "\n")[[1]], strsplit(wkt(crs1), "\n")[[1]])I thought it might be related to the PROJ_LIB environment variable, as referenced in https://gdal.org/drivers/raster/gtiff.html#georeferencing, but saw no effect when doing:
> options(rgdal_show_exportToProj4_warnings = "none")
> library(raster)
Loading required package: sp
>
> # creating RasterLayer with CRS EPSG:31370 and write as GeoTIFF
> filepath <- file.path(tempdir(), "test.tif")
> r1 <- raster(extent(188500, 190350, 227550, 229550), res = 50)
> values(r1) <- 1:ncell(r1)
> crs(r1) <- 31370
> writeRaster(r1, filepath)
>
> # read the GeoTIFF back
> Sys.getenv("PROJ_LIB")
[1] ""
> Sys.setenv(PROJ_LIB="/usr/share/proj")
>
> r2 <- raster(filepath)
> wkt(r1) == wkt(r2)
[1] FALSE
>
> Sys.setenv(PROJ_LIB="")Metadata
Metadata
Assignees
Labels
No labels
Activity
florisvdh commentedon Apr 29, 2021
Sidenote: in
terrathis problem does not occur 🙂 👍Created on 2021-04-29 by the reprex package (v2.0.0)
Session info
rhijmans commentedon May 2, 2021
Thank you very much for reporting this. That had escaped me when dealing with the PROJ changes mess.
florisvdh commentedon May 3, 2021
It works 🎉 - thanks a lot @rhijmans.
Created on 2021-05-03 by the reprex package (v2.0.0)
Session info