r - Assistance with <name> and <styleUrl> in .kml when using writeOGR() from rgdal -
i have data frame containing coordinates various locations i'd use google earth. here's simple example showing structure:
data <- data.frame(country = "usa", city = "saint paul", lat = 44.9629, lon = -93.00146)
i followed this post , this guide create kml
output using writeogr()
function rgdal
package, i'm having trouble tweaking attributes. here's code:
# may need install gdal package install # 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 viewing locations, i'd change <styleurl>
attribute have <name>
attribute populated. without it, google earth shows locations [no name]
attribute:
here's 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 either <name>
element populate simplefield name="city"
contents, or have <name>city</name>
tags added each <placemark>
. i'd final result (note added <style>
definition, <styleurl>
attribute <placemark>
, , <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 result looks (similar i'm aiming for):
the rgdal
documentation mentions layer_options
attribute, nothing intuitively stuck out me...
layer_options = c("<name>????</name>")
?layer_options = c("<styleurl>#custom</styleurl")
?- something else?
the attempts above pass tag directly don't appear affect output.
there's not many examples found in googling other creating default output writeogr()
, shown above. suggestions.
to expand on @jlhoward's answer above, able use kmlpoints()
accomplish looking for:
data <- data.frame(country = "usa", city = "saint paul", lat = 44.9629, lon = -93.00146) # may need install gdal package install # install.packages("rgdal") library(rgdal) library(maptools) 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")) kmlpoints(data_ll["city"], kmlfile = "~/desktop/test.kml", name = data_ll$city, icon = "http://upload.wikimedia.org/wikipedia/commons/a/af/tux.png")
the output contains both desired <name>
attribute <style>
definition custom icon, applied <placemark>
entries:
readlines("test.kml") readlines("test.kml") [1] "<?xml version=\"1.0\" encoding=\"utf-8\"?>" [2] "<kml xmlns=\"http://earth.google.com/kml/2.2\">" [3] "<document>" [4] "<name></name>" [5] "<description><![cdata[]]></description>" [6] "" [7] "<style id=\"style1\">" [8] " <iconstyle>" [9] " <icon>" [10] " <href>http://upload.wikimedia.org/wikipedia/commons/a/af/tux.png</href>" [11] " </icon>" [12] " </iconstyle>" [13] "</style>" [14] "" [15] "<placemark>" [16] " <name>saint paul</name>" [17] " <description><![cdata[]]></description>" [18] " <styleurl>#style1</styleurl>" [19] " <point>" [20] " <coordinates>" [21] "-93.00175381702,44.9628213042813" [22] " </coordinates>" [23] " </point>" [24] "</placemark>" [25] "</document>" [26] "</kml>"
the result:
Comments
Post a Comment