我有一个数据框,其中包含我想要与Google地球一起使用的各种位置的坐标。这是一个显示结构的简单示例:
data <- data.frame(country = "USA", city = "Saint Paul",
lat = 44.9629, lon = -93.00146)
我跟着this SO post和this guide使用KML
包中的writeOGR()
函数成功创建rgdal
输出,但是我无法调整属性。这是代码:
# 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")
结果适用于仅查看位置,但我想更改<styleUrl>
属性以及填充<name>
属性。没有它,Google地球会显示[no name]
属性的位置:
以下是生成的.kml
文件:
<?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>
我需要获取<name>
元素以填充SimpleField name="city"
内容,或者为每个<name>City</name>
添加<Placemark>
个标记。我想要的是最终结果(注释添加了<Style>
定义,<styleUrl>
属性<Placemark>
和添加了<name>
属性:
<?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>
这是结果的样子(类似于我的目标):
rgdal
documentation提到了layer_options
属性,但没有任何直觉可以解释我......
layer_options = c("<name>????</name>")
?layer_options = c("<styleUrl>#custom</styleUrl")
?以上尝试直接传递标记似乎不会影响输出。
除了创建writeOGR()
的默认输出之外,我在谷歌搜索中找到的示例并不多,如上所示。感谢您的任何建议。
答案 0 :(得分:2)
为了扩展@ jlhoward上面的答案,我能够使用kmlPoints()
来完成我想要的事情:
data <- data.frame(country = "USA", city = "Saint Paul",
lat = 44.9629, lon = -93.00146)
# you may need to install gdal itself for the package to install successfully
# 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")
输出包含所需的<name>
属性以及自定义图标的<Style>
定义,该定义已成功应用于<Placemark>
条目:
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>"
结果:
答案 1 :(得分:1)
好吧,如果你要做的就是填充每个<name>
中的<Placemark>
元素,这样就可以了:
library(maptools)
kmlPoints(data_ll,"test.kml",name=data$city)
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>"
# ...
# [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>"
如果您还需要更改<Style>
,那么我担心您可能不得不使用XML
包破解kml文件。