1

我有一个数据框,其中包含我想与 Google 地球一起使用的各个位置的坐标。这是一个显示结构的简单示例:

data <- data.frame(country = "USA", city = "Saint Paul",
                   lat = 44.9629, lon = -93.00146)

我按照这个 SO 帖子本指南使用包中的函数成功创建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文档提到了一个layer_options属性,但没有什么直观地突出给我......

  • layer_options = c("<name>????</name>")?
  • layer_options = c("<styleUrl>#custom</styleUrl")?
  • 还有什么?

上面直接传递标签的尝试似乎不会影响输出。

除了从 中创建默认输出之外,我在谷歌搜索中发现的示例并不多writeOGR(),如上所示。感谢您的任何建议。

4

2 回答 2

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>"  

结果:

在此处输入图像描述

于 2014-01-31T20:55:58.480 回答
1

好吧,如果您要做的只是填充<name>each 中的元素<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 文件。

于 2014-01-31T20:00:13.973 回答