[R-sig-Geo] Write GeoTiff with color table

Paul Hiemstra p.hiemstra at geo.uu.nl
Fri Apr 3 11:10:32 CEST 2009


Ned Horning schreef:
> Hi - Is it possible to output an Indexed (single band byte image) 
> GeoTiff using a predefined look-up-table in R? For now I output a byte 
> GeoTiff file from R and then import it into GRASS, create a color 
> table, and then export that as a colored GeoTiff. That works fine but 
> it would be nice to be able to create the colored GeoTiff directly in R.
>
> All the best,
>
> Ned
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
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")
}

-- 
Drs. Paul Hiemstra
Department of Physical Geography
Faculty of Geosciences
University of Utrecht
Heidelberglaan 2
P.O. Box 80.115
3508 TC Utrecht
Phone:  +3130 274 3113 Mon-Tue
Phone:  +3130 253 5773 Wed-Fri
http://intamap.geo.uu.nl/~paul



More information about the R-sig-Geo mailing list