[R-sig-Geo] plotKML transparency and legend

Alexander.Herr at csiro.au Alexander.Herr at csiro.au
Mon Aug 17 03:22:58 CEST 2015


Hi List,

I am unable to get the transparency (alpha) and the legend (colour_scale, or colour) to work using kml. Attached my working example, which attempts to build kml outputs for individual raster values.

library(plotKML)
library(sp)
library(raster)
require(grDevices)
demo(meuse, echo=FALSE)
raster(meuse.grid['soil'])->r


#set up colors
jet.colors <-
  colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan",
                    "#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000"))

col1=jet.colors(r at data@max)

#setup data extraction from raster and filenaming
fun1<-function(x){x[x != i]<-NA; return(x)}
spa4under<-function(x){gsub('([[:punct:]])|\\s+','_',x)}

# go for extraction
as.data.frame(levels(r))->rat
rat$col1<-col1

for (i in rat[,1])
{
 print(paste0("ID: ",i))
  calc(r, fun=fun1)->r1
   ratify(r1)->r2
    as.data.frame(levels(r2))->ratx
     ratx$level<-factor(paste0("x",rat[i,2]))
      print(paste0(ratx$level))
      levels(r2)<-ratx

      nam1<-paste0(spa4under(rat[i,2]))
       plotKML(r2,raster_name=paste0("p_",nam1,".png"), folder.name = paste0("p_",nam1), filename=paste0("p_",nam1,".png") ,colour_scale=col1[i], alpha=0.75)
       kml(r2,raster_name=paste0('k_',nam1,".png"), folder.name = paste0('k_',nam1,".png"), file=paste0('k_',nam1,'.kml') ,colour='level',colour_scale=col1[i], alpha=0.75)
        kml(r2,raster_name=paste0('kx_',nam1,".png"), folder.name = paste0('kx_',nam1,".png"), file=paste0('kx_',nam1,'.kml') ,colour_scale=col1[i], alpha=0.75)
        system(paste0("open ",file=paste0('k_',nam1,'.kml'))) 
       system(paste0("open ",file=paste0('kx_',nam1,'.kml')))
}

Any suggestions on where I am going wrong?

Thanks
Herry


---


More information about the R-sig-Geo mailing list