[R-sig-Geo] Byte-value tif files not properly read by readGDAL
Roger Bivand
Roger.Bivand at nhh.no
Tue Mar 16 09:50:20 CET 2010
On Mon, 15 Mar 2010, reeves at nceas.ucsb.edu wrote:
> Hello List:
>
> I am reading an 8 bit format raster .tif image, containing 0s and 1s and
> written using the Export Data utility of ArcMap GIS on a Windows 7
> platform.
>
> The image read into R does not consist of 0s and 1s, rather, 0s, 1s, and
> 128s.
Rick,
When you display the image, are the 128s in the locations of NODATA? (Read
decimated to save time displaying - raster display should get faster in R
2.11.* when Northern Hemisphere Spring comes). Much of what you provide
below simply makes things harder to read, please only provide the output
of sessionInfo(), and the message returned when rgdal is loaded. As
table() of the band shows 0, 1, and 128 (included below), then 128 is
likely to be the encoding used by the software creating the file for
NODATA. As is clearly explained, rgdal does no more than interface the
GDAL drivers for the GDAL version loaded - here the most recent.
> Information from the R session (ver 2.10.1, update packages run).
........
>
>
>> require(rgdal)
> Loading required package: rgdal
> Loading required package: sp
> Geospatial Data Abstraction Library extensions to R successfully loaded
> Loaded GDAL runtime: GDAL 1.7.1, released 2010/02/08
> Path to GDAL shared files: D:/PROGRA~1/R/R-210~1.1/library/rgdal/gdal
> Loaded PROJ.4 runtime: Rel. 4.7.1, 23 September 2009
> Path to PROJ.4 shared files: D:/PROGRA~1/R/R-210~1.1/library/rgdal/proj
>> setwd("F:/Projects/NCEAS/Balch")
>> CheatgrassMapInfo = GDALinfo("BrteNVModisWGS8Bit.tif")
>> CheatgrassMapInfo
> rows 1681
> columns 1748
> bands 1
> origin.x -120.1789
> origin.y 37.95729
> res.x 0.002427655
> res.y 0.002427655
> oblique.x 0
> oblique.y 0
> driver GTiff
> projection +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs
> file BrteNVModisWGS8Bit.tif
> apparent band summary:
> GDType Bmin Bmax
> 1 Byte -128 127
> Metadata:
> TIFFTAG_SOFTWARE=IMAGINE TIFF Support
> Copyright 1991 - 1999 by ERDAS, Inc. All Rights Reserved
> @(#)$RCSfile: etif.c $ $Revision: 1.10.1.9.1.9.2.11 $ $Date: 2004/09/15
> 18:42:01EDT $
> TIFFTAG_XRESOLUTION=1
> TIFFTAG_YRESOLUTION=1
> TIFFTAG_RESOLUTIONUNIT=1 (unitless)
> AREA_OR_POINT=Area
>> CheatgrassMapIn = readGDAL("BrteNVModisWGS8Bit.tif")
> BrteNVModisWGS8Bit.tif has GDAL driver GTiff
> and has 1681 rows and 1748 columns
>> summary(CheatgrassMapIn at data$band1)
> Min. 1st Qu. Median Mean 3rd Qu. Max.
> 0.000 0.000 0.000 5.248 1.000 128.000
......
>> summary(toUnSigned(CheatgrassMapIn at data$band1))
> Error in toUnSigned(CheatgrassMapIn at data$band1) : already unsigned
.....
Well, if the values are 0, 1, and 128, they are unsigned, there are no
negative values and Byte can use the 0:255 range, stored in R as integer,
as you see here:
>> str(CheatgrassMapIn)
> Formal class 'SpatialGridDataFrame' [package "sp"] with 6 slots
> ..@ data :'data.frame': 2938388 obs. of 1 variable:
> .. ..$ band1: int [1:2938388] 128 128 128 128 128 128 128 128 128 128 ...
> ..@ grid :Formal class 'GridTopology' [package "sp"] with 3 slots
> .. .. ..@ cellcentre.offset: Named num [1:2] -120.2 38
> .. .. .. ..- attr(*, "names")= chr [1:2] "x" "y"
> .. .. ..@ cellsize : num [1:2] 0.00243 0.00243
> .. .. ..@ cells.dim : int [1:2] 1748 1681
> ..@ grid.index : int(0)
> ..@ coords : num [1:2, 1:2] -120.2 -115.9 38 42
> .. ..- attr(*, "dimnames")=List of 2
> .. .. ..$ : NULL
> .. .. ..$ : chr [1:2] "x" "y"
> ..@ bbox : num [1:2, 1:2] -120.2 38 -115.9 42
> .. ..- attr(*, "dimnames")=List of 2
> .. .. ..$ : chr [1:2] "x" "y"
> .. .. ..$ : chr [1:2] "min" "max"
> ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots
> .. .. ..@ projargs: chr " +proj=longlat +ellps=WGS84 +datum=WGS84
> +no_defs +towgs84=0,0,0"
...........
>> unique(CheatgrassMapIn$band1)
> [1] 128 0 1
>> table(CheatgrassMapIn$band1)
>
> 0 1 128
> 2142594 680644 115150
>> summary(CheatgrassMapIn$band1)
> Min. 1st Qu. Median Mean 3rd Qu. Max.
> 0.000 0.000 0.000 5.248 1.000 128.000
>
> Again, the values in CheatgrassMapIn$band1 should be 0 or 1, somehow the
> value 128 has appeared. There is a conflict between output of GDALinfo and
> the summary(CheatgrassMapIn$band1) statement.
>
No - in the absence of any input from the file metadata, the GDAL driver
takes a guess at whether the Byte is signed or unsigned (GDALinfo() says
"apparent"). The first guess is signed, but it reads unsigned, which is
probably the intention in the creating software. Double-check with
gdalinfo externally, but also search the Arc documentation to see how it
is encoding NODATA, and whether you can encourage the creating software to
declare the NODATA encoding so that the GDAL driver has a reasonable
chance of guessing right.
Hope this helps,
Roger
> I do not see a parameter for readGDAL for specifying word size or sign
> treatment. Can anyone offer advice/insight?
>
> Rick Reeves
> NCEAS
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no
More information about the R-sig-Geo
mailing list