I have a data frame containing coordinates to various locations that I'd like to use with Google Earth. Here's a simple example showing the structure:
data <- data.frame(country = "USA", city = "Saint Paul",
lat = 44.9629, lon = -93.00146)
I followed this SO post and this guide to create KML
output successfully using the writeOGR()
function from the rgdal
package, however I'm having trouble tweaking the attributes. Here's the code:
# you may need to install gdal itself for the package to install successfully
# install.packages("rgdal")
library(rgdal)
data_sp <- data
coordinates(data_sp) <- c("lon", "lat")
proj4string(data_sp) <- CRS("+init=epsg:4238")
data_ll <- spTransform(data_sp, CRS("+proj=longlat +datum=WGS84"))
writeOGR(data_ll["city"], "/path/to/test.kml", driver = "KML", layer = "city")
The result works fine for just viewing locations, but I'd like to change the <styleUrl>
attribute as well as have the <name>
attribute populated. Without it, Google Earth shows locations with a [no name]
attribute:
Here's the resultant .kml
file:
<?xml version="1.0" encoding="utf-8" ?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document><Folder><name>city</name>
<Placemark>
<ExtendedData><SchemaData schemaUrl="#city">
<SimpleData name="city">Saint Paul</SimpleData>
</SchemaData></ExtendedData>
<Point><coordinates>-93.001753817020003,44.96282130428127</coordinates></Point>
</Placemark>
</Folder>
<Schema name="city" id="city">
<SimpleField name="city" type="string"></SimpleField>
</Schema>
</Document></kml>
I need to either get a <name>
element to populate with the SimpleField name="city"
contents, or have <name>City</name>
tags added to each <Placemark>
. What I'd like is something like this as the final result (note added <Style>
definition, <styleUrl>
attribute for the <Placemark>
, and <name>
attribute added):
<?xml version="1.0" encoding="utf-8" ?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Style id="custom">
<IconStyle>
<scale>1.5</scale>
<Icon>
<href>http://upload.wikimedia.org/wikipedia/commons/a/af/Tux.png</href>
</Icon>
</IconStyle>
</Style>
<Folder><name>city</name>
<Placemark>
<name>Saint Paul</name>
<styleUrl>#custom</styleUrl>
<ExtendedData><SchemaData schemaUrl="#city">
<SimpleData name="city">Saint Paul</SimpleData>
</SchemaData></ExtendedData>
<Point><coordinates>-93.001753817020003,44.96282130428127</coordinates></Point>
</Placemark>
</Folder>
<Schema name="city" id="city">
<SimpleField name="city" type="string"></SimpleField>
</Schema>
</Document></kml>
Here's what the result looks like (similar to what I'm aiming for):
The rgdal
documentation mentions a layer_options
attribute, but nothing intuitively stuck out to me...
layer_options = c("<name>????</name>")
?layer_options = c("<styleUrl>#custom</styleUrl")
?- Something else?
The attempts above to pass a tag directly don't appear to affect the output.
There's not many examples I found in googling other than creating the default output from writeOGR()
, as shown above. Thanks for any suggestions.