Skip to content

WKT2 string change after reading GeoTIFF file created with raster #199

Closed

Description

@florisvdh

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="")

Activity

  1. florisvdh commented on Apr 29, 2021

    @florisvdh
    ContributorAuthor

    Sidenote: in terra this problem does not occur 🙂 👍

    library(terra)
    #> terra version 1.1.4
    rgdal::rgdal_extSoftVersion()
    #>           GDAL GDAL_with_GEOS           PROJ             sp 
    #>        "3.1.3"         "TRUE"        "7.2.1"        "1.4-5"
    
    # creating SpatRaster with CRS EPSG:31370 and write as GeoTIFF
    filepath <- file.path(tempdir(), "test.tif")
    r1 <- rast(extent = ext(188500, 190350, 227550, 229550), res = 50)
    values(r1) <- 1:ncell(r1)
    crs(r1) <- "EPSG:31370"
    writeRaster(r1, filepath)
    
    # read the GeoTIFF back
    r2 <- rast(filepath)
    
    crs(r1) == crs(r2)
    #> [1] TRUE
    
    # crs() returns WKT string; first 9 lines:
    cat(strsplit(crs(r2), "\n")[[1]][1:9], sep = "\n")
    #> PROJCRS["Belge 1972 / Belgian Lambert 72",
    #>     BASEGEOGCRS["Belge 1972",
    #>         DATUM["Reseau National Belge 1972",
    #>             ELLIPSOID["International 1924",6378388,297,
    #>                 LENGTHUNIT["metre",1]]],
    #>         PRIMEM["Greenwich",0,
    #>             ANGLEUNIT["degree",0.0174532925199433]],
    #>         ID["EPSG",4313]],
    #>     CONVERSION["Belgian Lambert 72",

    Created on 2021-04-29 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-29                  
    #> 
    #> ─ Packages ───────────────────────────────────────────────────────────────────
    #>  package     * version date       lib source        
    #>  cli           2.4.0   2021-04-05 [1] CRAN (R 4.0.5)
    #>  codetools     0.2-18  2020-11-04 [4] CRAN (R 4.0.3)
    #>  digest        0.6.27  2020-10-24 [1] CRAN (R 4.0.3)
    #>  evaluate      0.14    2019-05-28 [1] CRAN (R 4.0.2)
    #>  fs            1.5.0   2020-07-31 [1] CRAN (R 4.0.2)
    #>  glue          1.4.2   2020-08-27 [1] CRAN (R 4.0.2)
    #>  highr         0.8     2019-03-20 [1] CRAN (R 4.0.2)
    #>  htmltools     0.5.1.1 2021-01-22 [1] CRAN (R 4.0.3)
    #>  knitr         1.31    2021-01-27 [1] CRAN (R 4.0.3)
    #>  lattice       0.20-41 2020-04-02 [4] CRAN (R 4.0.0)
    #>  magrittr      2.0.1   2020-11-17 [1] CRAN (R 4.0.3)
    #>  raster        3.4-5   2020-11-14 [1] CRAN (R 4.0.3)
    #>  Rcpp          1.0.6   2021-01-15 [1] CRAN (R 4.0.3)
    #>  reprex        2.0.0   2021-04-02 [1] CRAN (R 4.0.5)
    #>  rgdal         1.5-23  2021-02-03 [1] CRAN (R 4.0.3)
    #>  rlang         0.4.10  2020-12-30 [1] CRAN (R 4.0.3)
    #>  rmarkdown     2.7     2021-02-19 [1] CRAN (R 4.0.4)
    #>  rstudioapi    0.13    2020-11-12 [1] CRAN (R 4.0.3)
    #>  sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 4.0.2)
    #>  sp            1.4-5   2021-01-10 [1] CRAN (R 4.0.3)
    #>  stringi       1.5.3   2020-09-09 [1] CRAN (R 4.0.2)
    #>  stringr       1.4.0   2019-02-10 [1] CRAN (R 4.0.2)
    #>  terra       * 1.1-4   2021-03-09 [1] CRAN (R 4.0.5)
    #>  withr         2.4.1   2021-01-26 [1] CRAN (R 4.0.3)
    #>  xfun          0.22    2021-03-11 [1] CRAN (R 4.0.4)
    #>  yaml          2.2.1   2020-02-01 [1] CRAN (R 4.0.2)
    #> 
    #> [1] /home/floris/lib/R/library
    #> [2] /usr/local/lib/R/site-library
    #> [3] /usr/lib/R/site-library
    #> [4] /usr/lib/R/library
  2. rhijmans commented on May 2, 2021

    @rhijmans
    Member

    Thank you very much for reporting this. That had escaped me when dealing with the PROJ changes mess.

  3. florisvdh commented on May 3, 2021

    @florisvdh
    ContributorAuthor

    It works 🎉 - thanks a lot @rhijmans.

    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 formal CRS definition (WKT2) for use by PROJ/GDAL, now match:
    wkt(crs1) == wkt(crs2)
    #> [1] TRUE

    Created on 2021-05-03 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-05-03                  
    #> 
    #> ─ Packages ───────────────────────────────────────────────────────────────────
    #>  package     * version date       lib source                          
    #>  cli           2.4.0   2021-04-05 [1] CRAN (R 4.0.5)                  
    #>  codetools     0.2-18  2020-11-04 [4] CRAN (R 4.0.3)                  
    #>  digest        0.6.27  2020-10-24 [1] CRAN (R 4.0.3)                  
    #>  evaluate      0.14    2019-05-28 [1] CRAN (R 4.0.2)                  
    #>  fs            1.5.0   2020-07-31 [1] CRAN (R 4.0.2)                  
    #>  glue          1.4.2   2020-08-27 [1] CRAN (R 4.0.2)                  
    #>  highr         0.8     2019-03-20 [1] CRAN (R 4.0.2)                  
    #>  htmltools     0.5.1.1 2021-01-22 [1] CRAN (R 4.0.3)                  
    #>  knitr         1.31    2021-01-27 [1] CRAN (R 4.0.3)                  
    #>  lattice       0.20-41 2020-04-02 [4] CRAN (R 4.0.0)                  
    #>  magrittr      2.0.1   2020-11-17 [1] CRAN (R 4.0.3)                  
    #>  raster      * 3.4-9   2021-05-03 [1] Github (rspatial/raster@17a5ac3)
    #>  Rcpp          1.0.6   2021-01-15 [1] CRAN (R 4.0.3)                  
    #>  reprex        2.0.0   2021-04-02 [1] CRAN (R 4.0.5)                  
    #>  rgdal         1.5-23  2021-02-03 [1] CRAN (R 4.0.3)                  
    #>  rlang         0.4.10  2020-12-30 [1] CRAN (R 4.0.3)                  
    #>  rmarkdown     2.7     2021-02-19 [1] CRAN (R 4.0.4)                  
    #>  rstudioapi    0.13    2020-11-12 [1] CRAN (R 4.0.3)                  
    #>  sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 4.0.2)                  
    #>  sp          * 1.4-5   2021-01-10 [1] CRAN (R 4.0.3)                  
    #>  stringi       1.5.3   2020-09-09 [1] CRAN (R 4.0.2)                  
    #>  stringr       1.4.0   2019-02-10 [1] CRAN (R 4.0.2)                  
    #>  withr         2.4.1   2021-01-26 [1] CRAN (R 4.0.3)                  
    #>  xfun          0.22    2021-03-11 [1] CRAN (R 4.0.4)                  
    #>  yaml          2.2.1   2020-02-01 [1] CRAN (R 4.0.2)                  
    #> 
    #> [1] /home/floris/lib/R/library
    #> [2] /usr/local/lib/R/site-library
    #> [3] /usr/lib/R/site-library
    #> [4] /usr/lib/R/library
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions