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

writeValues() inadvertently rounds values #315

Closed
pat-s opened this issue Sep 4, 2021 · 2 comments
Closed

writeValues() inadvertently rounds values #315

pat-s opened this issue Sep 4, 2021 · 2 comments

Comments

@pat-s
Copy link

pat-s commented Sep 4, 2021

I'd expect the values written to a SpatRaster not to be rounded.

library(terra)
#> terra version 1.3.22
r <- rast(nrow = 3, ncol = 4)
writeStart(r, filename = tempfile(fileext = ".tif"))
#> $row
#> [1] 1
#> 
#> $nrows
#> [1] 3
#> 
#> $n
#> [1] 1
v <- c(
  66.3484211266179, 67.8992571263675, 65.2368599385505, 60.0851186590024,
  65.1888453571109, 61.7919525966071, 63.1428918602016, 62.2687846669139,
  63.3999659927921, 65.7038877386742, 68.5510667370152, 58.9081320478074
)
writeValues(r, v, 1, 3)
writeStop(r)
#> class       : SpatRaster 
#> dimensions  : 3, 4, 1  (nrow, ncol, nlyr)
#> resolution  : 90, 60  (x, y)
#> extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#> coord. ref. : +proj=longlat +datum=WGS84 +no_defs 
#> source      : file12a0919f3184f.tif 
#> name        :    lyr.1 
#> min value   : 58.90813 
#> max value   : 68.55107

all.equal(as.numeric(values(r)), v)
#> [1] "Mean relative difference: 2.71163e-08"

Created on 2021-09-04 by the reprex package (v2.0.1)

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                                      
#>  version  R version 4.1.1 Patched (2021-08-27 r80829)
#>  os       macOS Big Sur 10.16                        
#>  system   x86_64, darwin17.0                         
#>  ui       X11                                        
#>  language (EN)                                       
#>  collate  en_US.UTF-8                                
#>  ctype    en_US.UTF-8                                
#>  tz       Europe/Zurich                              
#>  date     2021-09-04                                 
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date       lib source                            
#>  backports     1.2.1      2020-12-09 [1] CRAN (R 4.1.0)                    
#>  cli           3.0.1      2021-07-17 [1] CRAN (R 4.1.0)                    
#>  codetools     0.2-18     2020-11-04 [2] CRAN (R 4.1.1)                    
#>  crayon        1.4.1      2021-02-08 [1] CRAN (R 4.1.0)                    
#>  digest        0.6.27     2020-10-24 [1] CRAN (R 4.1.0)                    
#>  ellipsis      0.3.2      2021-04-29 [1] CRAN (R 4.1.0)                    
#>  evaluate      0.14       2019-05-28 [1] CRAN (R 4.1.0)                    
#>  fansi         0.5.0      2021-05-25 [1] CRAN (R 4.1.0)                    
#>  fastmap       1.1.0      2021-01-25 [1] CRAN (R 4.1.0)                    
#>  fs            1.5.0      2020-07-31 [1] CRAN (R 4.1.0)                    
#>  glue          1.4.2      2020-08-27 [1] CRAN (R 4.1.0)                    
#>  highr         0.9        2021-04-16 [1] CRAN (R 4.1.0)                    
#>  htmltools     0.5.2      2021-08-25 [1] CRAN (R 4.1.1)                    
#>  knitr         1.33       2021-04-24 [1] CRAN (R 4.1.0)                    
#>  lattice       0.20-44    2021-05-02 [2] CRAN (R 4.1.1)                    
#>  lifecycle     1.0.0      2021-02-15 [1] standard (@1.0.0)                 
#>  magrittr      2.0.1      2020-11-17 [1] CRAN (R 4.1.0)                    
#>  pillar        1.6.2      2021-07-29 [1] CRAN (R 4.1.0)                    
#>  pkgconfig     2.0.3      2019-09-22 [1] CRAN (R 4.1.0)                    
#>  purrr         0.3.4      2020-04-17 [1] standard (@0.3.4)                 
#>  R.cache       0.15.0     2021-04-30 [1] CRAN (R 4.1.0)                    
#>  R.methodsS3   1.8.1      2020-08-26 [1] CRAN (R 4.1.0)                    
#>  R.oo          1.24.0     2020-08-26 [1] CRAN (R 4.1.0)                    
#>  R.utils       2.10.1     2020-08-26 [1] CRAN (R 4.1.0)                    
#>  raster        3.4-13     2021-06-18 [1] CRAN (R 4.1.0)                    
#>  Rcpp          1.0.7      2021-07-07 [1] CRAN (R 4.1.0)                    
#>  reprex        2.0.1      2021-08-05 [1] CRAN (R 4.1.0)                    
#>  rlang         0.4.11     2021-04-30 [1] CRAN (R 4.1.0)                    
#>  rmarkdown     2.10.6     2021-08-30 [1] Github (rstudio/rmarkdown@c8c502b)
#>  rstudioapi    0.13       2020-11-12 [1] CRAN (R 4.1.0)                    
#>  sessioninfo   1.1.1      2018-11-05 [1] CRAN (R 4.1.0)                    
#>  sp            1.4-5      2021-01-10 [1] CRAN (R 4.1.0)                    
#>  stringi       1.7.4      2021-08-25 [1] CRAN (R 4.1.1)                    
#>  stringr       1.4.0      2019-02-10 [1] standard (@1.4.0)                 
#>  styler        1.5.1.9001 2021-08-23 [1] Github (r-lib/styler@2b1eeac)     
#>  terra       * 1.3-22     2021-08-20 [1] CRAN (R 4.1.1)                    
#>  tibble        3.1.4      2021-08-25 [1] CRAN (R 4.1.1)                    
#>  utf8          1.2.2      2021-07-24 [1] CRAN (R 4.1.0)                    
#>  vctrs         0.3.8      2021-04-29 [1] CRAN (R 4.1.0)                    
#>  withr         2.4.2      2021-04-18 [1] CRAN (R 4.1.0)                    
#>  xfun          0.25       2021-08-06 [1] CRAN (R 4.1.0)                    
#>  yaml          2.2.1      2020-02-01 [1] standard (@2.2.1)                 
#> 
#> [1] /Users/pjs/Library/R/x86_64/4.1/library
#> [2] /Library/Frameworks/R.framework/Versions/4.1/Resources/library
@pat-s
Copy link
Author

pat-s commented Sep 6, 2021

Seems like I just need to set the proper datatype:

library(terra)
#> terra version 1.3.22
r <- rast(nrow = 3, ncol = 4)
writeStart(r, filename = tempfile(fileext = ".tif"), datatype = "FLT8S")
#> $row
#> [1] 1
#> 
#> $nrows
#> [1] 3
#> 
#> $n
#> [1] 1
v <- c(
  66.3484211266179, 67.8992571263675, 65.2368599385505, 60.0851186590024,
  65.1888453571109, 61.7919525966071, 63.1428918602016, 62.2687846669139,
  63.3999659927921, 65.7038877386742, 68.5510667370152, 58.9081320478074
)
writeValues(r, v, 1, 3)
writeStop(r)
#> class       : SpatRaster 
#> dimensions  : 3, 4, 1  (nrow, ncol, nlyr)
#> resolution  : 90, 60  (x, y)
#> extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#> coord. ref. : +proj=longlat +datum=WGS84 +no_defs 
#> source      : file1d1f2ffcbadf.tif 
#> name        :    lyr.1 
#> min value   : 58.90813 
#> max value   : 68.55107

all.equal(as.numeric(values(r)), v)
#> [1] TRUE

Created on 2021-09-06 by the reprex package (v2.0.1)

@pat-s pat-s closed this as completed Sep 6, 2021
@rhijmans
Copy link
Member

rhijmans commented Sep 9, 2021

That is correct. Use FLT8S for higher precision (and a larger file size).

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