Skip to content
Prev 5421 / 29559 Next

Write GeoTiff with color table

Ned Horning schreef:
Hi,

You can create color RGB tiffs using the functions I wrote once. Hope it 
helps!

Paul

val2rgb = function(val, color, at)
# Function to map the value to an RGB value
{
    for(i in 1:length(at))
    {
        if(val <= at[i]) return(color[i])
    }
    return(color[length(color)])
}

writeTIFF = function(data, column_name, file, color, at, style = "quantile")
# Function to construct a color (RGB) GeoTIFF
#
# data :        The data for making the GeoTIFF
# file :        file for storing the tif file
# column_name : The name of the column in 'data' that is used for the 
GeoTIFF
# color :       A list with the colors that will be used. Most used is 
output
#               from functions such as 'rainbow' and 'RColorBrewer'.
# at :          The data values where the colors change.
{
        print(paste("writing",file))

    data2color = sapply(data[[column_name]], FUN = function(x) 
val2rgb(x, color,
    data2rgb = col2rgb(data2color)

    data$band1 = data2rgb["red",]
    data$band2 = data2rgb["green",]
    data$band3 = data2rgb["blue",]

    writeGDAL(data[c("band1","band2","band3")],file,type="Byte")
}