plotKML : Scientiﬁc Visualization of Spatio-Temporal Data

plotKML is an R package that provides methods for writing the most common R spatial classes into KML ﬁles. It builds up on the existing XML parsing functionality ( XML package), and provides similar plotting functionality as the lattice package. Its main objective is to provide a simple interface to generate KML ﬁles with a small number of arguments, and allows users to visually explore spatio-temporal data available in R : points, polygons, gridded maps, trajectory-type data, vertical proﬁles, ground photographs, time series vector objects or raster images, along with the results of spatial analysis such as geostatistical mapping, spatial simulations of vector and gridded objects, optimized sampling designs, species distribution models and similar. A generic plotKML() function automatically determines the parsing order and visualizes data directly from R ; lower level functions can be combined to allow for new user-created visualization templates. In comparison to other KML writing packages, plotKML seems to be more object oriented, it links more closely to the existing R classes for spatio-temporal data ( sp , spacetime and raster packages) than the alternatives, and provides users with the possibility to create their own templates.

plotKML: visualization of spatio-temporal data including annotation of maps and images (Wilson 2008;Wernecke 2009).It is the standard adopted by Google and now used by Google Earth➋ as its primary exchange format.There is a growing interest in using Google Earth to visualize spatio-temporal data produced in the R environment for statistical computing.We believe that the main reasons responsible for this growth are: 1. Google Earth is one of the most wide-spread geographical data browsers available with >1 billion downloads as of October 20111 .It is a largely intuitive software, easily accessible to people without any professional GIS training, and most importantly, a framework which can be used to increase a project reach to the general public (Butler 2006;Patterson 2007;Goodchild 2008).
2. According to McGavra et al. (2009), KML is one of the leading Open Geospatial Consortium (OGC) XML encoding standards.
3. Google Earth provides access to high-resolution remote sensing data (Ikonos, GeoEye and Cnes/Spot images, administrative vector data, SRTM DEM and similar), which allows users to qualitatively interpret the analysis results by matching the produced outputs with background imagery (Craglia et al. 2008;Fritz et al. 2012).

4.
As KML provides a diversity of visualization options, it is one of the more attractive platforms for scientific visualization of geographical phenomena -it can enable scientists to detect patterns in their data not visible in other software, but it can also help engaging a wider public into scientific research and decision making (Butler 2006).Goodchild (2008) refers to this as the 'citizen science'.
Although R is primarily known as a statistical computing environment (R Development Core Team 2013), it is of increasing interest to the field of geoinformatics and applied spatial data analysis due to its extensibility and the growing diversity of spatial and spatiotemporal data structures (Pebesma and Bivand 2005;Pebesma 2012b;Bivand et al. 2013).Geo-visualization functions in R, on the other hand, are limited because R was not originally designed for interactive exploration of spatial data or as a GIS platform.Although there are many packages in R already that allow interactive visualization, overlay and animated display of geographical phenomena (via packages RGGOBI and iPlots; for a review refer to (Cook and Swayne 2007) and/or (Theus and Urbanek 2009)), R has its limitations for interactively exploring geographical data.
There is considerable potential for building connectivity between the sophisticated spatial analysis possible in R and the geo-visualization capacities afforded by Google Earth.However, the export of spatial data from R to KML is not trivial.KML files can be produced using the using GDAL (Geospatial Data Abstraction Library) KML driver, but so far only limited functionality is supported.This article describes the main functionality and the design of the plotKML package and provides a number of examples of how to produce some common KML visualization templates.Some limitations of the package in comparison with other alternative KML writing software and possibilities to improve the package are given in the discussion section.More detailed tutorials including YouTube videos can be found via the package homepage4 .

Scientific visualization of geographic phenomena
The purpose of scientific visualization is to "graphically illustrate scientific data to enable scientists to understand, illustrate, and glean insight from their data." 5According to Friendly and Denis (2001), scientific visualization can be primarily connected with visualization of 3D data "where the emphasis is on realistic renderings of volumes, surfaces, illumination sources, and so forth, perhaps with a dynamic (time) component."In that context, the main purpose of plotKML package is to enable interactive scientific visualization of geographic phenomena i.e. scientific geovisualization (Dykes et al. 2005).
Interactive geovisualization, i.e. dynamic user-controlled visualization of geographical phenomena, can be today closely connected with 3D computer environments referred to as the 'Virtual globe', 'Virtual Earth' or 'Digital Earth' (Gore 1998;Bleisch 2011).At the beginning of 21st century, the most known virtual globes are (Bleisch 2011): Open source alternatives to Google Earth and Microsoft Bing Maps 3D are: (KDE) Marble6 , OSSIM Planet7 , and Cesium8 .
According to Elmqvist and Tudoreanu (2007), there are two main reasons for creating 3D virtual globes: (a) replicate or simulate the real world, (b) use 3D as canvas for abstract information.
The plotKML package offers both.As we will show later, it allows Google Earth to be used not only for visualization of maps, but also as a canvas for 3D or 3D+T visualization of any such data created and analyzed in R (see further Figures 3 and 9).
Although one can argue whether virtual globes should be used more broadly for decision making and land management, two significant developments can not be disputed: (1) ubiquitous access to free high resolution satellite imagery through Google Earth, Yahoo and Bing has revolutionized applications of GIS for both commercial and non-commercial projects, and ( 2) anyone is today invited to make, edit, mash-up and share geodata (Fritz et al. 2012).

Space-time continuum
Everything we measure on Earth can be linked to some space-time 'location' : 1. geographic location (longitude and latitude) 2. height above the ground surface (altitude) 3. time of measurement (year, month, day, hour, minute, second) 4. spatio-temporal support (size of the blocks of material associated with measurements; time interval or duration of measurement).
By attaching spatio-temporal reference to measurements we can dynamically visualize them, but also run spatio-temporal data analysis (Bivand et al. 2008;Pebesma 2012b).Many analysts already find it useful to be able to visualize all input and derived maps or results of spatial analysis in a Virtual Earth type of environment such as Google Earth (Patterson 2007).In addition, creating a realistic visualization of observed dynamic phenomena can improve the spatial analysis process, in part, because it can help us make more thoroughly considered interpretations of analysis results (Craglia et al. 2008).
Figure 1 for example shows how spatio-temporal data can be visualized in a 2D+T space-time cube.The same data-set is further shown in Figure 9 visualized in Google Earth.

Spatial and spatio-temporal objects
For Erwig et al. (1999) spatio-temporal data sets and corresponding databases represent two major groups of features: (1) moving or dynamic objects (discrete or vector geometries), and (2) regions or fields (continuous).Objects and fields can be further based on regular or irregular sampling systems and representation models.Many features can be modeled and represented both using discrete (vector) and continuous (raster) GIS models.For example, objects such as a population of animals can be modeled and represented as discrete objects (trajectories or points), but also as densities (regions) or polygons representing home ranges.Likewise, earthquakes are in their essence regions (of sudden and violent shaking of the ground), but are often represented as points.This is just to illustrate the complexity of choosing the right model for representing such dynamic features.For an introduction to the complexity of spatial and spatio-temporal objects and fields refer also to Galton (2004).Bivand et al. (2008, pp.28-55) and Pebesma (2012b) implemented classes for spatial and spatio-temporal data in R via the sp and spacetime packages.These classes are also highly extendable and are already widely used inside the R spatial data analysis community.The sp package (Pebesma and Bivand 2005) is currently one of the top 10 packages with highest number of dependencies on CRAN9 , and has been used as the main starting point for building visualization functionality in plotKML.
A schematic overview of 2D, 3D, 2D+T and 3D+T combinations of spatio-temporal object types is given in Table 2.3.Note that not all space-time combinations of 2D/3D+T objects are yet implemented in R, and some are implemented but are still rather experimental.For example, voxels can be constructed by adding the third dimension to object of class SpatialPixels, but methods for such type of data are still limited.
Although KML can probably accommodate all spatio-temporal objects listed in The same object in KML can be generated by using the XML package (Lang 2013): R> library("XML") R> pnt.kml <-newXMLNode("kml") R> h2 <-newXMLNode("Document", parent = pnt.kml)R> h3 <-newXMLNode("name", "Google headquarters", parent = h2) R> h4 <-newXMLNode ("Folder", parent=pnt.kml[["Document"]where newXMLNode creates nodes in an XML object, sprintf is the wrapper for the fast C function that returns a character vector, and saveXML writes the object to a file (this function is does most of the work in the plotKML package).An XML object is basically a hierarchically structured object with nodes of different type.The rules to build and extend such objects are defined via the specific XML scheme, in this case the OGC KML 2.2 scheme (Wilson 2008).
Note that, although both R objects and KML (XML) files are human readable and have similar hierarchical structure, they have some systematic differences.First, KML accepts only geographical coordinates in WGS84 system, which must include altitude i.e.only 3D georeference is acceptable, while in R any proj4 supported projection can be used and the georeference can be 2D or 3D.Secondly, KML is by default used to display objects ("placemarks") on Earth's surface, while the sp package does not restrict considering the bounding box and relative position based on land surface.To understand all rules and validity checks of the KML format refer to Wernecke (2009), and for an introduction to the sp class objects refer to Bivand et al. (2013).

Basic principles and main use
The purpose of plotKML is to write standard spatio-temporal objects -spatial and spacetime points, lines, polygons and grids, trajectories, georeferenced photographs and similar -from R to KML/KMZ files in such a way that they correspond, as much as possible, to standard cartographic plots or standard scientific visualizations.The main philosophy of plotKML is thus: 1. Create a spatio-temporal object of some class, 2. Transform coordinates to a coordinate reference system compatible with a virtual globe (geographical coordinates in WGS84; altitude in meters; time in the UTC system), 3. Visualize it using the plotKML function.
We refer to complete scientific visualization templates in plotKML as 'views'.Views are generated using a combination of low level KML writing functions, which basically coerce the spatio-temporal objects in R to the KML schema.
Visualization-specific generic settings such as icons, color and size of icons (icon styles), labels etc. are referred to as 'aesthetics' in plotKML.These can be set by changing function arguments within each "kml_layer" function.Views and their components have been designed to be cartographically 'complete', meaning that: ❼ All spatio-temporal objects are automatically converted to the WGS84 geographical coordinates.Hence the projection system needs to be specified for each input spatial layer.
❼ Legends for all aesthetics are provided using screen overlays or at least labels are attached to individual plotting objects.
❼ Missing values (NA), extrapolation areas and/or masked pixels are automatically removed or made transparent.
❼ Each spatial layer carries some minimum metadata that can be entered via the description tag.This way the distributed KML files can be used as official representations of registered data sets.
The following example shows how to produce a bubble type plot using the plotKML package (plot shown in Figure 2).We start by loading the Ebergötzen soil mapping data set (available via the plotKML package):

R> eberg.ll <-reproject(eberg)
so that it can be plotted in Google Earth by using:

Closing eberg__CLYMHT_A__.kml
This means that both plotKML(eberg) and plotKML(eberg.ll) would work with the same object.It is recommended, however, that the users pre-transform spatial objects into the WGS84 geographic coordinate system as this step can be time consuming when working with large data sets.
By combining multiple aesthetics, plotKML can be used also to visualize multivariate data (Figure 3).For example, to visualize two variables at the same time we would run: which would use colors to visualize the clay content, and labels and altitude to represent the sand content.Possibility of multivariate visualization makes plotKML comparable to the lattice package for R (Gabor 2008).

KML building utilities
Aside from the generic plotKML() method, the package also contains a number of dedicated methods and functions, which can be referred to as the KML building utilities.The basic KML building utilities are (Figure 4): ❼ kml_close() -closes the KML file.
❼ kml_screenoverlay() -puts a PNG file on the Google Earth screen; usually a legend attached to the map or a logo image.
❼ kml_legend() -generates a legend depending on the type of spatio-temporal object.
These utilities actually provide an advanced mode for KML creation, and allow the user to create multi-layers KML with specific legends.This means that a single KML file can be created that contains all layers of interest (e.g.grids and points), associated legends and explanations of how were the maps derived.
The following example demonstrates how several layers can be put together in the same KML file.We start by loading some gridded layers: To write a gridded layer with a point layer on the top we can run: R> kml_open("eberg.kml")R> kml_layer(eberg_grid, colour=TWISRT6)

plotKML specific classes
In addition to the existing classes already available in R, we have constructed several new plotKML classes to provide even richer visualization possibilities: ❼ "SpatialMetadata"a class to store and exchange spatial metadata.
❼ "SpatialPhoto"a class to store spatial location, image (photograph) and its geometric properties.
❼ "SpatialPredictions"a class to store results of geostatistical mapping.
❼ "SpatialVectorsSimulations"a class to store list of equiprobable simulations of point, line and polygon features.❼ "RasterBrickSimulations"a class to store a list of equiprobable realizations of the same feature.
❼ "RasterBrickTimeSeries"a class to store a time series of grids representing the same feature.
Most of the classes listed above extend the basic sp and raster based classes by attaching the inputs and outputs of the spatial analysis.For example, the "SpatialPredictions" class contains both the input sampled values, the results of model fitting (regression and the variogram model), predictions and the results of cross validation.By plotting such object via the plotKML-method, one can prepare complete scientific visualization for Google Earth (see Figure 6).
The process of getting from input data (R) to a map in Google Earth is now straightforward and requires only four steps: (1 ) load/format the data, (2 ) fit the geostatistical model, (3 ) make predictions, (4 ) visualize maps, Consider for example the Meuse data set commonly used for geostatistical exercises (Pebesma 2004):
To plot this data in Google Earth we can do (Figure 8):

Visualization of time series of rasters
plotKML can also be used to visualize raster stacks i.e. time series of rasters.Raster stacks or raster bricks (Hijmans and van Etten 2012) can be different type of remote sensing data or predicted or modelled values.The plot shown in Figure 9 is based on generic functions for plotting of "RasterBrickTimeSeries" class data.It shows a time series of MODIS Land Surface Temperature images for a small area in Istria.We start by building an spatio-temporal object from table data:

Figure 1 :
Figure 1: Space-time cube visualized in R: (a) cloud plot showing location of meteorological stations in Croatia (data set from Hengl et al. (2012)), (b) illustration of spatial and temporal support in the space-time cube.

Figure 2 :
Figure 2: Bubble-type plots in R and the same plot produced using the plotKML shown in Google Earth.

Figure 3 :
Figure 3: Multivariate visualization using three aesthetics parameters (in the case above: color, labels and elevation).The plot shows changes in sand content in the soil for the Ebergötzen case study.

Figure 4 :
Figure 4: Overview of the KML building utilities available in the plotKML package.

Figure 5 :
Figure 5: Example of a multi-layer KML file produced using plotKML: Topographic Wetness Index derived in SAGA GIS (raster image with legend) and contour lines overlaid.This visualization template has been largely inspired by the SAGA GIS software (Conrad 2007).

Figure 6 :
Figure 6: Scientific visualization of the results of geostatistical mapping (percent organic matter in top-soil) in Google Earth➋: a combination of visualization of gridded map with a legend and map showing the sampling locations and fitted regression and variogram models.

Figure 7 :
Figure 7: Spatio-temporal plot of point pattern in time: foot-and-mouth epidemic data from north Cumbria (UK) (Diggle et al. 2005).

Figure 9 :
Figure 9: Example of a time series of MODIS images (RasterBrickTimeSeries class) plotted in Google Earth.The values of MODIS Land Surface Temperature at ground stations can be interactively explored by clicking at the points of interest.This way both changes in 2D+T and T can be visually explored.
(Beaudette and Roudier 2012) 3 .Our objective with the plotKML package was to provide a simple interface to generate KML files with a small number of arguments, and allow users to directly plot spatio-temporal data classes, available in R via the sp(Pebesma and Bivand 2005), spacetime (Pebesma 2012a), aqp(Beaudette and Roudier 2012)and raster (Hijmans and van Etten 2012) packages, in a virtual globe type of browser.
(Keitt et al. 2013)iption page indicates: "At this time, only vector layers are handled by the KML driver...limited support is available for fills, line color and other styling attributes."2Within the R community, other packages produced specifically to allow creation of KML files include: rgdal(Keitt et al. 2013)and raster (Hijmans and van Etten 2012), while truly specialized KML writing packages include R2G2 (

Table 2 .
3, visualization of densely sampled 3D+T objects, e.g.millions of voxels, is probably still not recommended in KML.For example, by making COLLADA (COLLAborative Design Activity; an open standard XML schema) 3D objects one can potentially generate any type of 3D spatio-temporal object, but this is then highly complex and requires good knowledge of COLLADA and KML.

Table 1 :
(Pebesma 2012bo-temporal objects (points and regions) based on the number of dimensions (2D, 3D, 2D+T and 3D+T) and support type and corresponding R classes.STFDF stands for a class for spatio-temporal data with full space-time grid, STIDF stands for a class for unstructured spatio-temporal or irregular data, and STTDF stands for a class for spatiotemporal trajectory data(Pebesma 2012b).⋆-classesnot yet available in R.2.4.Spatial data structures in R and KMLConsider for example the Google headquarters in Mountain View, CA.The point location can be prepared in R as object of class SpatialPoints*, as implemented in the sp package, which takes few steps:

Table 2 :
A matrix comparison of some KML writing software.⋆ -full capability, ⋆possible but with limitations, − -not possible in this package.Functionality subjectively approximated by the authors (subject to discussion).