[gdal-dev] gdal_translate shifted values

Roger Bivand Roger.Bivand at nhh.no
Thu Feb 1 04:06:41 PST 2024


>From the R code snippet I see: "Surprisingly, the value 9.75 is shifted to 106, 86 = 105,85 in Python indexing", but R is 1-base and Python 0-base. In R:

> library(terra)
terra 1.7.71
> gdal()
[1] "3.8.3"
> a <- rast("attachment.obj")
> b <- rast("st4_pr.2017092016.01h.tif")
> a
class       : SpatRaster 
dimensions  : 205, 205, 1  (nrow, ncol, nlyr)
resolution  : 4762, 4762  (x, y)
extent      : 4940902, 5917112, -7210793, -6234583  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=stere +lat_0=90 +lat_ts=60 +lon_0=-105 +x_0=0 +y_0=0 +R=6367470 +units=m +no_defs 
source      : attachment.obj 
name        : SFC (Ground or water surface); Total precipitation [kg/m^2] 
unit        :                                                      kg/m^2 
time        : 2017-09-20 16:00:00 UTC 
> b
class       : SpatRaster 
dimensions  : 205, 205, 1  (nrow, ncol, nlyr)
resolution  : 4762, 4762  (x, y)
extent      : 4940902, 5917112, -7210793, -6234583  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=stere +lat_0=90 +lat_ts=60 +lon_0=-105 +x_0=0 +y_0=0 +R=6367470 +units=m +no_defs 
source      : st4_pr.2017092016.01h.tif 
name        : 0[-] SFC (Ground or water surface) 
> all.equal(values(a), values(b))
[1] "Attributes: < Component \"dimnames\": Component 2: 1 string mismatch >"
> dimnames(values(a))
[[1]]
NULL

[[2]]
[1] "SFC (Ground or water surface); Total precipitation [kg/m^2]"

> dimnames(values(b))
[[1]]
NULL

[[2]]
[1] "0[-] SFC (Ground or water surface)"
> all.equal(values(a), values(b), check.attributes=FALSE)
[1] TRUE

Reading both into R shows that the input GRIB data and the GTiff data converted by gdal_translate are identical.

By the way, better to use the terra package directly, raster uses it indirectly.

Hope this clarifies,

Roger

--
Roger Bivand
Emeritus Professor
Norwegian School of Economics
Postboks 3490 Ytre Sandviken, 5045 Bergen, Norway
Roger.Bivand at nhh.no


More information about the gdal-dev mailing list