Skip to content

Reading a TIFF file

4 messages · Julio Rojas, Uwe Ligges

#
Dear Uwe, find attached an small portion of the file I'm working with. ArcGIS values for this file are in the CSV file. They are a vector of all rows put together.

Thanks and regards.


--- El vie, 4/22/11, Uwe Ligges <ligges at statistik.tu-dortmund.de> escribi?:
#
Fine, looking at the code shows that readTiff read the image with using 
libtiff which should be fine. Afterwards, a pixmap obnject is generated 
via  pixmapRGB() which includes the following lines:

     datamax <- max(data)
     datamin <- min(data)
     data <- as.numeric(data)
     if (datamax > 1 || datamin < 0)
         data <- (data - datamin)/(datamax - datamin)

That means the data is perfectly fitted into the [0,1] interval.

That means we have not only rescaled but also another 0 now.

What I did is:

pic <- readTiff("test1_layer1.tif")
pic <- pic at red
ARC <- read.csv2("test1_arcgis.csv", header=TRUE)
ARC <- matrix(ARC[,2], nrow=nrow(pic), byrow=TRUE)

plot(pic)
plot(ARC)# looks *very* similar

plot(as.vector(pic) ~ as.vector(ARC))
# all on one line

summary(lm(as.vector(pic) ~ as.vector(ARC)))
# resuiduals < 10^(-14)

So the formula use to get from the ARC to the pixmap data is
pixmapdata = -0.82278 * 0.01266 ARCdata

if you want to get the original data, you can adapt the readTiff 
function for your own use for greyscales (and save memory that way) as in:


myReadTiff <- function (fn, page = 0)
{
   w <- .C("TiffGetWidth", as.character(fn), w = as.integer(0),
     PACKAGE = "rtiff")$w
   h <- .C("TiffGetHeight", as.character(fn), h = as.integer(0),
     PACKAGE = "rtiff")$h
   nw <- ceiling((1 - reduce) * w)
   nh <- ceiling((1 - reduce) * h)
   if (w > 0 && h > 0) {
     tiff <- .C("TiffReadTIFFRGBA", as.character(fn),
       page = as.integer(page),
       r = integer(w * h), g = integer(w * h), b = integer(w * h),
       PACKAGE = "rtiff")
     tiff <- tiff$r / 255
     tiff <- pixmapGrey(data = tiff, nrow = nh, ncol = nw)
     return(tiff)
   }
   stop("Could not open", fn, ".  File corrupted or missing.\n")
}

pic2 <- myReadTiff("test1_layer1.tif")


Now pic2 at grey will be exactly the same as ARC/255 from above.

Uwe Ligges
On 22.04.2011 15:10, Julio Rojas wrote:
#
Thanks Uwe. I totally understood the problem. BTW, in your code you forgot to define "reduce". Can you define it in the function? If the file has different minimum as well as maximum levels from this one, will it work?

Regards.

Julio


--- El vie, 4/22/11, Uwe Ligges <ligges at statistik.tu-dortmund.de> escribi?:
#
On 22.04.2011 16:19, Julio Rojas wrote:
Just see how I modified the function, you can do so as well.
As long as it is in [0, 255], yes, see the code.

Best,
Uwe