kmlDevice: Create graphics device to generate KML content

Description Usage Arguments Value Author(s) References See Also Examples

Description

This function allows us to create a regular graphics device for creating R plots and graphical displays. The content is generated as KML and can be displayed in Google Earth. The idea is that the caller specifies a geographical rectangular region for the device and the R plots are drawn within that region.

Usage

1
2
3
4
5
kmlDevice(origin, w, h, filename = character(), rdims = c(1000, 1000),
           col = "white", bg = "transparent", ps = 10, ...,
            docName = "R KML", description = "R plots in KML",
            doc = createKMLDoc(docName, description, origin + c(w/2, h/2)),
             center = TRUE, folder = "Plot")

Arguments

origin

the longitude, latitude pair giving the bottom left corner of the rectangular region, or the mid-point of the rectangle if center is TRUE. Longitude ranges from -180 to 180. Longitude from 90 to -90

w,h

the width and height of the rectangular region in degrees

filename

the name of the file to which to write the generated KML content when the device is closed. This can be an empty character vector and the content is not written to a file. Instead, the caller can access the KML document via the return value from this function.

rdims

an integer vector of dimensions giving the width and height to use within R for the graphics device. This is how big R sees the device, regardless of the size of the rectangle on Googe Earth.

col

the default color to use for drawing. This can be a named color, e.g. "red", or an RGB or RGB alpha specification, e.g. "#FF0000" or "#0044CC99", or a string with class AsIs giving a color in KML color format, i.e. alpha blue green red, e.g. "99cc4400".

bg

the background color for the graphics device

ps

the default size for characters

...

unused at present

docName

a string giving the a short description or name for the KML document being created. This is displayed in Google Earth's Place's list when the KML is loaded

description

a string giving a longer description of the document. This is available in Google Earth to the viewer.

doc

the KML document object, or this can be an XMLInternalNode which allows a caller to add to an existing KML document and to control where the content is added.

center

a logical value controlling whether we center the rectangle or use the origin as the South-West point

folder

the name of the KML folder in which to collect the elements of the plot. If this is an empty vector, no folder is created and the elements are added to the Document element of the KML document. If more than one plot is created within this device, this is used as a prefix for each folder created to house the elements of each plot.

Value

A list with two elements

dev

an object representing the C-level graphics device structure

doc

a reference to the XML document that is updated by the device as graphics commands are processed

Author(s)

Duncan Temple Lang

References

http://code.google.com/apis/kml/documentation/kmlreference.html R Internals Manual

See Also

RKML package http://www.omegahat.org/RKML

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
data(SFTemperatures)
dev = kmlDevice(c(-122.68, 37.75), 3, 3, I("sf.kml"), c(500, 800))
with(subset(SFTemperatures, dates >= as.Date("1-1-2004", "%d-%m-%Y")),
             { plot(dates, temp, type = "l", main = "Temperature")
               lines(supsmu(dates, temp), col = "red", lwd = 3)
             })
dev.off()


library(lattice)
dev = kmlDevice(c(-122.68, 37.75), 3, 3, I("sfBoxplot.kml"), c(500, 800))
bwplot(temp ~ ordered(months(dates), unique(months(dates))), SFTemperatures)
dev.off()


# Now we will create three plots, one for each city in 3 different
# KML files.

data(USCities, package = "RKML") # From RKML
data(PortlandTemperatures)
data(SeattleTemperatures)
USCities$longitude = -USCities$longitude
locs = subset(USCities, name %in% c("SanFrancisco", "Portland", "Seattle") & state %in% c("CA", "OR", "WA"))[-1,]
locs = locs[, c(2:1, 3)]
cities = as.character(locs$name)
mapply(function(city, locs, data) {
         dev = kmlDevice(locs[1, 1:2], 3, 3,
                          I(sprintf("%s.kml",   city)),dims = c(500,800))
         print(bwplot(temp ~ ordered(months(dates), unique(months(dates))), data))
         dev.off()
       }, cities, split(locs[, 1:2], locs$name),
          list(SFTemperatures, PortlandTemperatures, SeattleTemperatures))


# Now we will create the same 3 plots, but put them in a single KML
#  file. We create a KMLDoc ourselves and 

library(RKML)  # for createKMLDoc
doc = createKMLDoc("3 Boxplots", "Boxplots for 3 cities", c(-122, 42))
docNode = xmlRoot(doc)[["Document"]]
mapply(function(city, loc, temps) {
        kmlDevice(loc[1, 1:2], 3, 3, doc = docNode, dims = c(500,800), folder = city)
        print(bwplot(temp ~ ordered(months(dates), unique(months(dates))), temps))
        dev.off()
       }, cities, split(locs[, 1:2], locs$name)[cities],
          list(SFTemperatures, PortlandTemperatures, SeattleTemperatures))

saveXML(doc, I("boxplots.kml"))

duncantl/RKMLDevice documentation built on May 15, 2019, 5:31 p.m.