Skip to content
Prev 22600 / 29559 Next

plotKML: image width and height of legend in KML file

We did not consider legends for point data because users will most 
likely use multiple esthetics parameters (color, size, altitude), so yes 
you need to use the lower level functions "kml_open", "kml_layer" to 
achieve this :(
Also 'width' and 'height' arguments only really make sense for factor 
type variables (hence the easiest is that you then make the legend 
yourself and then insert it using "kml_screen").

Notice that you need to use "z.lim" 2 times to ensure that the legend 
matches the colors of points. Here is an example:

R> library(plotKML)
R> library(sp)
R> library(rgdal)
R> data(eberg)
R> coordinates(eberg) <- ~X+Y
R> proj4string(eberg) <- CRS("+init=epsg:31467")
R> eberg <- eberg[runif(nrow(eberg))<.1,]
R> shape = "http://maps.google.com/mapfiles/kml/pal2/icon18.png"
R> kml_open("eberg_CLYMHT_A.kml")
KML file opened for writing...
R> kml_layer(eberg["CLYMHT_A"], colour=CLYMHT_A, z.lim=c(20,60),
+    colour_scale=SAGA_pal[[1]], shape=shape, points_names="")
Reprojecting to +proj=longlat +datum=WGS84 ...
Writing to KML...
R> kml_legend.bar(x=eberg$CLYMHT_A, legend.file="kml_legend.png",
+    legend.pal=SAGA_pal[[1]], z.lim=c(20,60))
null device
           1
R> kml_screen(image.file="kml_legend.png")
R> kml_close("eberg_CLYMHT_A.kml")
Closing  eberg_CLYMHT_A.kml

If you need to do this N times, then the best is to make a function e.g.:

R> kml_points <- function(obj, file, z.lim, points_names="", ...){
   kml_open(file)
   kml_layer(obj, colour=obj at data[,1], 
shape="http://maps.google.com/mapfiles/kml/pal2/icon18.png", 
colour_scale=SAGA_pal[[1]], points_names="", ...)
   kml_legend.bar(x=obj at data[,1], legend.file=gsub(".kml", ".png", 
file), legend.pal=SAGA_pal[[1]], z.lim=z.lim)
   kml_screen(image.file=gsub(".kml", ".png", file))
   kml_close(file)
}
## test it:
R> kml_points(eberg["CLYMHT_A"], file="eberg_CLYMHT_A.kml", z.lim=c(20,60))

FYI, the kml_legend.bar function is at:

https://r-forge.r-project.org/scm/viewvc.php/pkg/R/legend.bar.R?view=markup&root=plotkml

HTH,

T. Hengl
On 15-4-2015 17:34, Elbers, Jan wrote: