2

I wonder how it may be possible to write a GTiff based on a matrix of specific colours? This may be when the colours do not represent a quantitative variable, but rather qualitative spatial phenomena such as demographic clusters. If I have e.g. a 1000x1000 matrix of colour values (character strings like '#FF0000'), I can easily plot these with grid.raster(mtrx, interpolate=FALSE) but this is difficult to overlay with other shapefiles. I can also convert them to a SpatialPixelsDataFrame, with fieldnames 'x','y','col'. But I'm stumped how to write either of these objects to a GTiff. For instance:

writeGDAL(SPDF, 'test.tif', drivername = "GTiff", colorTables=x$col)

yields

Error in function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘gridded’ for signature ‘"data.frame"’

Very grateful for any pointers.


Example:

require(sp)
require(rgdal)
mtrx = matrix(rgb(runif(10000), runif(10000), runif(10000)), ncol=100)
spdf = data.frame(x=rep(1:100,100), y=rep(1:100,each=100), col=as.vector(mtrx))
plot(spdf[,1:2], col=spdf$col, pch=15)
spdf = SpatialPixelsDataFrame(points=spdf[,1:2], data=spdf)
writeGDAL(spdf, 'test.tif', drivername = "GTiff", colorTables=spdf$col)
geotheory
  • 22,624
  • 29
  • 119
  • 196
  • Please help us help you by providing us with a reproducible example (i.e. code and example data), see http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example for details. – Paul Hiemstra May 21 '13 at 08:05
  • Sure @Paul Hiemstra, have added to question. – geotheory May 21 '13 at 11:12

1 Answers1

0

In your original call, I'm guessing that SPDF was the original data.frame, rather than a SpatialPixelsDataFrame that writeGDAL was expecting. Regardless, you have a second issue in your minimum example.

The issue is that colorTables is not expecting a data.frame: from help(writeGDAL):

default NULL, if not NULL, a list of length equal to the number of bands, with NULL components for bands with no color table, or either an integer matrix of red, green, blue and alpha values (0-255), or a character vector of colours. The number of colours permitted may vary with driver.

In your example, spdf$col is a factor (try class(spdf$col)). You can convert to a list of character vectors as requested list(levels(spdf$col)).

This brings up the question: what is the GeoTiff you are trying to write? Currently, it is a three band image with x, y and a character colour code... You need an actual attribute that will be coded according to a colour code.

Let's add a new attribute that is a random integer between 0 and 255, create a colour table that has a colour assigned for each value (256 values), based on your first 256 values:

spdf@data$new = sample(0:255, length(spdf$x), replace=TRUE)
coltable = list(levels(spdf$col)[0:255])

Now we can successfully write the file; note that we have to specify the type and NoData value handling to avoid errors:

writeGDAL(spdf[,'new'], '/Users/Benjamin/Desktop/test.tif', drivername = "GTiff", type="Byte", mvFlag=0, colorTables=coltable)

Resulting GeoTiff image with color table applied

Benjamin
  • 11,560
  • 13
  • 70
  • 119