[R-sig-Geo] plotKML: image width and height of legend in KML file
Tomislav Hengl
hengl at spatial-analyst.net
Thu Apr 16 10:33:45 CEST 2015
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:
> Hello,
>
> With my first plotKML project I managed to create a KML file containing a layer with irregular points (air temperature data) and add a legend overlay to it.
> I tried changing the size of the legend to no avail, whatever I fill in for "width" and "height" I get a png file that is 120x240 pixels.
> What am I doing wrong?
>
> Cheers,
> JanE
>
> My code is:
> library(sp)
> library(plotKML)
> library(spacetime)
> #
> # working directory
> setwd('D:/A-user/Hi-Aware/plotKML')
> #
> # input file
> tripfile <- 'wbgt_input_data_new.csv'
> tripdata <- read.csv(tripfile)
> #
> # convert time to POSIX
> tripdata$ctime <- as.POSIXct(strptime(tripdata$timestamp,"%Y-%m-%d %H:%M:%S"),format="%Y-%m-%d %H:%M:%S")
> #
> # lat lon with decimal minutes
> tripdata$lat <- tripdata$lat + tripdata$latmin/60.
> tripdata$lon <- tripdata$lon + tripdata$lonmin/60.
> #
> # create STIDF object
> sp <- SpatialPoints(tripdata[,c("lon","lat")])
> proj4string(sp) <- CRS("+proj=longlat +datum=WGS84")
> tripdata.st <- STIDF(sp, time = tripdata$ctime, data = tripdata[,c("t2m","rh")])
> #
> # save to kml with legend bar overlay
> kml_open(file.name = paste(tripfile, ".kml", sep=""))
> kml_layer(tripdata.st, file.name=paste(tripfile, ".kml", sep=""), colour=t2m, labels="", points_names="",
> shape="http://maps.google.com/mapfiles/kml/pal2/icon18.png", kmz=FALSE, open.kml=TRUE)
> kml_legend.bar(x=tripdata$t2m, width=100, height=500, pointsize = 10, legend.file="legend.png",
> legend.pal=SAGA_pal$SG_COLORS_DEFAULT, z.lim = range(tripdata$t2m, na.rm=TRUE, finite=TRUE))
> kml_screen(image.file = "legend.png", position = "ML", sname = "legend")
> kml_close(file.name = paste(tripfile, ".kml", sep=""))
>
> end of code
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
More information about the R-sig-Geo
mailing list