[R-sig-Geo] writeRaster to TIF changes raster maximum value

Guillaume.Drolet at mrn.gouv.qc.ca Guillaume.Drolet at mrn.gouv.qc.ca
Mon Sep 16 17:10:53 CEST 2013

Dear list members,

I need your help trying to figure out why my raster's maximum value gets
changed in the resulting TIF file from a call to the writeRaster function.

I read here

that this problem could be due to my version of GDAL (1.11dev released
2013/04/13). This is the GDAL version that is in my system's path. Don't
know if this can help but when I load rgdal, I can read this:

Loaded GDAL runtime: GDAL 1.9.2, released 2012/10/08

Here are the details of my raster (myRaster) before calling writeRaster:

  > raster(myRaster)

class       : RasterLayer
dimensions  : 7261, 7961, 57804821  (nrow, ncol, ncell)
resolution  : 30, 30  (x, y)
extent      : 457785, 696615, 5147085, 5364915  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=18 +datum=WGS84 +units=m +no_defs
+ellps=WGS84 +towgs84=0,0,0
data source : E:\temp\Fmask\Image\L71015027_02720000730_B10.tif
names       : L71015027_02720000730_B10
values      : 0, 255  (min, max)

I crop "myRaster" using the crop function with a bounding box and then I 
save the cropped raster as a TIF with:

  > writeRaster(rc, "myCroppedRaster.tif", datatype = "INT2S")
rgdal: version: 0.8-11, (SVN revision 479M)
Geospatial Data Abstraction Library extensions to R successfully loaded
Loaded GDAL runtime: GDAL 1.9.2, released 2012/10/08
Path to GDAL shared files:
GDAL does not use iconv for recoding strings.
Loaded PROJ.4 runtime: Rel. 4.7.1, 23 September 2009, [PJ_VERSION: 470]
Path to PROJ.4 shared files:

class       : RasterLayer
dimensions  : 3628, 3994, 14490232  (nrow, ncol, ncell)
resolution  : 30, 30  (x, y)
extent      : 457785, 577605, 5147085, 5255925  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=18 +datum=WGS84 +units=m +no_defs
+ellps=WGS84 +towgs84=0,0,0
data source : E:temp\myRaster.tif
names       : myRaster
values      : 0, 254  (min, max)

As shown in the output from writeRaster, the maximum value in the output 
TIF file is now 254. I double-checked and
there are pixels with value 255 in the cropped area of the original
raster so the maximum value shouldn't be 254 but 255.

Here's the output of my sessionInfo():

R version 3.0.1 (2013-05-16)
Platform: x86_64-w64-mingw32/x64 (64-bit)

[1] LC_COLLATE=French_Canada.1252  LC_CTYPE=French_Canada.1252
[4] LC_NUMERIC=C                   LC_TIME=French_Canada.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] rgdal_0.8-11  raster_2.1-49 sp_1.0-13

loaded via a namespace (and not attached):
[1] grid_3.0.1      lattice_0.20-15 tools_3.0.1

I thank you in advance for any hint that could help me solve this

Guillaume Drolet ing.f M.Sc.
Chercheur en télédétection et dynamique forestière
Direction de la recherche forestière
Ministère des ressources naturelles du Québec
2700, rue Einstein, C.RC.335
Québec (Québec), Canada G1P 3W8
T : +1 418 643 7994 # 6727
F : +1 418 643 2165
guillaume.drolet at mrn.gouv.qc.ca

More information about the R-sig-Geo mailing list