KML Export From R
For these examples, this code creates rectangles for the Universal Transverse Mercator (UTM) zones.
library(rgdal)
north = lapply(seq(-179.99, 174.01, 6), function(west) {
Polygon(matrix(data = c(west,west, west + 5.98, west + 5.98, west,
0, 84, 84, 0, 0), ncol=2)) })
south = lapply(seq(-179.99, 174.01, 6), function(west) {
Polygon(matrix(data = c(west,west, west + 5.98, west + 5.98, west,
0, -80, -80, 0, 0), ncol=2)) })
polygons = c(north, south)
polygons = sapply(1:length(polygons), function(x) {
Polygons(polygons[x], ID=x) })
wgs84 = CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
polygons = SpatialPolygons(polygons, proj4string = wgs84)
data = data.frame(ZONE = rep(1:60, 2), HEMISPHERE = c(rep("N", 60), rep("S", 60)))
utm_zones = SpatialPolygonsDataFrame(polygons, data = data)
The easiest and least-flexible way to export to KML is the KML driver in writeOGR(). There is no direct control over lines/fills/icons, and the only fields that can be specified are popup Name and Description.
# Create name field
utm_zones$NAME = paste0("UTM Zone ", utm_zones$ZONE, utm_zones$HEMISPHERE)
# Write
writeOGR(utm_zones, "utm_zones.kml", "Universal Transverse Mercator Zones", driver="KML",
layer_options=c(NameField = "NAME", DescriptionField = "NAME"))
# Replace colors
text = readLines("utm_zones.kml")
text = gsub(pattern = "ff0000ff", replace = "40800000", x=text)
writeLines(text, con="utm_zones.kml")
If you need more control over the coloring the kmlPolygon() function from the maptools library is what you need, although it only handles one polygon at a time (why?!) and requires quite a bit of code for what should be a one-function operation:
library(maptools)
# Create name field
utm_zones$NAME = paste0("UTM Zone ", utm_zones$ZONE, utm_zones$HEMISPHERE)
# Different colors for the northern and southern hemispheres
colors = c(rep("#00008040", 60), rep("#00800040", 60))
borders = c(rep("#00008080", 60), rep("#00800080", 60))
# Create a list of KML polygons
utm_polygons = sapply(1:length(utm_zones), function(x) {
kmlPolygon(utm_zones[x,], name=utm_zones$NAME[x], description=utm_zones$NAME[x],
lwd=1, col=colors[x], border=borders[x]) })
# Open the file and write the header
kmlfile = file("utm_zones.kml", "w")
cat(kmlPolygon(kmlname="Universal Transverse Mercator Zones")$header, file=kmlfile, sep="\n")
# Write the style information
cat(unlist(utm_polygons["style",]), file=kmlfile, sep="\n")
# Write the placemarks
cat(unlist(utm_polygons["content",]), file=kmlfile, sep="\n")
# Write the footer and close the file
cat(kmlPolygon(kmlname="State Plane Coordinate System")$footer, file=kmlfile, sep="\n")
close(kmlfile)
There is also a plotKML library that is useful if you are creating a choropleth or some other kind of thematic map:
library(plotKML)
# Create an array of colors
breaks = quantile(utm_zones$ZONE, na.rm=T)
categories = as.numeric(cut(utm_zones$ZONE, breaks))
palette = colorRampPalette(c("#800000", "#000080"))
ramp = palette(4)
colors = ramp[categories]
kml_open("temp.kml")
kml_layer(utm_zones, colour=colors)
utm_zones$ZONE)
kml_close("temp.kml")