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)