Last active
April 27, 2016 12:34
-
-
Save fxi/58b3ed0ab4360f506402779f8844140b to your computer and use it in GitHub Desktop.
raster color table to sld
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(raster) | |
| library(XML) | |
| # get raster file | |
| # e.g. read from file: | |
| r <- raster("dat.tif") | |
| # create sample values | |
| r <- raster(ncol=10, nrow=10) | |
| r[] <- as.integer(runif(length(r))*255) | |
| colortable(r) <- terrain.colors(10) | |
| rToSld <- function(r,nodataval=NULL){ | |
| require(raster) | |
| require(XML) | |
| colTab <- colortable(r) | |
| if(length(colTab)==0) stop("no colortable") | |
| top <- newXMLNode("colorMap") | |
| names(colTab) <- 0:(length(colTab) - 1) | |
| valTab <- sort(unique(r)) | |
| sapply(valTab, function(x){ | |
| col <- colTab[x] | |
| if(length(col)>0 && !is.na(col)){ | |
| opa <- 1 | |
| if(!is.null(nodataval) && x == nodataval) opa <- 0 | |
| newXMLNode("colorMapEntry",attrs = list( | |
| color = col, | |
| quantity = x, | |
| opacity = opa | |
| ), | |
| parent=top | |
| ) | |
| } | |
| } | |
| ) | |
| return(top) | |
| } | |
| rToSld(r,8) | |
| # <colorMap> | |
| # <colorMapEntry color="#2DB600FF" quantity="2" opacity="1"/> | |
| # <colorMapEntry color="#63C600FF" quantity="3" opacity="1"/> | |
| # <colorMapEntry color="#E6E600FF" quantity="5" opacity="1"/> | |
| # <colorMapEntry color="#EDB48EFF" quantity="8" opacity="0"/> | |
| # </colorMap> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's a good starting point to develop something useful. Thanks!
Where is the
colortable()function from? -- Need to update myrasterpackage!It doesn't work straightforward for me. Here the way I'd comment the function to make it easier for me to read and understand.