Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

kmlPolygons

Create and write a KML file on the basis of a given Polygons object or list of Polygons or SpatialPolygonsDataFrame


Description

The function is used to create and write a KML file on the basis of a given Polygons object (a list of Polygon objects of SpatialPolygonsDataFrame class) for the usage in Google Earth and Google Maps.

Usage

kmlPolygons(obj=NULL, kmlfile=NULL, 
    name="KML Polygons", description="", col=NULL, visibility=1, lwd=1,
    border="white", kmlname="", kmldescription="")

Arguments

obj

a Polygons or SpatialPolygonsDataFrame object or list of objects

kmlfile

if not NULL the name as character string of the kml file to be written to working directory as "NAME.kml"

name

the name of the KML polygon in Google Earth

description

the description of the KML polygon displayed in Google Earth or Maps (HTML tags allowed)

col

the fill color (see also Color Specification) of the KML polygon. If passing a list of Polyons or SpatialPolygonsDataFrame and length(col) is less than length(object) the first color in col will be applied to all objects in the list

visibility

if set to 1 or TRUE specifies that the KML polygon should be visible after loading

lwd

the stroke (polygon's border line) width for the KML polygon

border

the stroke color (see also Color Specification) for the KML polygon

kmlname

the name of the KML layer

kmldescription

the description of the KML layer (HTML tags allowed)

Details

The function is used to convert a given Polygons object (a list of Polygon objects) or the Polygons object listed in a passed SpatialPolygonsDataFrame object into KML polygon. If kmlfile is not NULL the result will be written into that file. If kmlfile is NULL the generated KML lines will be returned (see also value).

The conversion can also handle polygons which are marked as holes inside of the Polygons object if these holes are listed right after that polygon in which these holes appear. That implies that a given plot order set in the Polygons object will not be considered.

For a passed Polygons object the function generates a <Style> tag whereby its id attribute is set to the passed object's ID.

Note that the geometries should be in geographical coordinates with datum WGS84.

The resulting KML polygon will be embedded in <Placemark><MultiGeometry><Polygon>.

Value

x is a list with the elements style and content containing the generated lines of the KML file as character vectors if kmlfile is NULL.

y is a list with the elements header and footer representing the KML file' header resp. footer if obj is NULL (see second example).

Color Specification

The following color specifications are allowed: 'red', 2, or as hex code '#RRGGBB' resp. '#RRGGBBAA' for passing the alpha value.

Author(s)

Hans-J. Bibiko, Jon Callihan, Steven Brey

See Also

Examples

data(wrld_simpl)
td <- tempdir()
kmlfile <- paste(td, "worldPolitical.kml", sep="/")
## creates a KML file containing the polygons of a political world map
kmlPolygons(wrld_simpl, kmlfile = kmlfile, name = "KML Polygons", 
         description = "the world", col = "red",
         visibility = 1, lwd = 1, border = "white", kmlname = "R Test", 
         kmldescription = "This is <b>only</b> a <a href='http://www.r-project.org'>R</a> test.")

data(wrld_simpl)
## create a KML file containing the polygons of Brazil, Uganda, and Canada
regions <- c("Brazil","Canada","Uganda")
wrld_simpl_subset <- wrld_simpl[wrld_simpl$NAME %in% regions,]
kmlfile <- paste(td, "worldPoliticalSubset.kml", sep="/")
kmlPolygons(wrld_simpl_subset, kmlfile = kmlfile,
 name = "KML Polygons subset", description = "three countries", col = "blue", 
 visibility = 1, lwd = 1, border = "white", kmlname = "R Test 2", 
 kmldescription = "This is <b>only</b> a <a href='http://www.r-project.org'>R</a> test.")
## combine to make a list of polygon objects to plot
polList <- c(regions,wrld_simpl)
kmlfile <- paste(td, "worldPoliticalandSubset.kml", sep="/")
kmlPolygons(wrld_simpl_subset, kmlfile = kmlfile,
 name = "KML Polygons subset", description = "three countries highlighted in world", 
 col = sample(colours(), length(polList)), visibility = 1, lwd = 1, border = "white", 
 kmlname = "R Test 2", 
 kmldescription = "This is <b>only</b> a <a href='http://www.r-project.org'>R</a> test.")

maptools

Tools for Handling Spatial Objects

v1.1-1
GPL (>= 2)
Authors
Roger Bivand [cre, aut] (<https://orcid.org/0000-0003-2392-6140>), Nicholas Lewin-Koh [aut], Edzer Pebesma [ctb], Eric Archer [ctb], Adrian Baddeley [ctb], Nick Bearman [ctb], Hans-Jörg Bibiko [ctb], Steven Brey [ctb], Jonathan Callahan [ctb], German Carrillo [ctb], Stéphane Dray [ctb], David Forrest [ctb], Michael Friendly [ctb], Patrick Giraudoux [ctb], Duncan Golicher [ctb], Virgilio Gómez Rubio [ctb], Patrick Hausmann [ctb], Karl Ove Hufthammer [ctb], Thomas Jagger [ctb], Kent Johnson [ctb], Matthew Lewis [ctb] (<https://orcid.org/0000-0003-2244-4078>), Sebastian Luque [ctb], Don MacQueen [ctb], Andrew Niccolai [ctb], Edzer Pebesma [ctb], Oscar Perpiñán Lamigueiro [ctb], Ethan Plunkett [ctb], Ege Rubak [ctb] (<https://orcid.org/0000-0002-6675-533X>), Tom Short [ctb], Greg Snow [ctb], Ben Stabler [ctb], Murray Stokely [ctb], Rolf Turner [ctb]
Initial release
2021-03-14

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.