使用 R 在 KML 文件中添加标签

Adding a tag in KML file Using R

我从Google Earth Pro导出了kml文件,包含37个文件夹,每个文件夹包含"minor"个文件夹,总共"minor"个文件夹是168个,每个 "minor" 有 3 个地标。

我有 HTML 代码,我是用 R 编写的,我想将这个 kml 文件导入 R 并将这个 HTML 代码放入每个 "Placemark" 的第一个 "minor" 文件夹,此 HTML 代码不是常量,它具有类似于此代码中 table 中的值的变量,并且此变量将从我为此 [=25= 制作的数据框中附加] 文件夹,当我编辑此 HTML 代码时,我会将其放入此 "minor" 文件夹的第一个 "placemark" 中,以此类推其他 "minor" 文件夹。

R 中是否有任何功能可以将此 html 代码添加到 kml 文件中?

这是 "description" 在 R 中的代码。

URL <- paste("file:///C:/Users/pc/Downloads/Googletraffic/Tazbet/Autostrad;Helwan To Da2ery/",FileName,sep = "")
library(XML)

top = newXMLNode("description")

table = newXMLNode("table ", attrs = c(width = 300, border = 1), parent = top)
tbody <- newXMLNode("tbody",parent = tr)
tr <- newXMLNode("tr",parent = table)
th <- newXMLNode("th",attrs = c(scope = "col"),scope1 = max(Bey2ollak$V3),parent = tr)
th <- newXMLNode("th",attrs = c(scope = "col"),scope1 = "MD",parent = tr)
th <- newXMLNode("th",attrs = c(scope = "col"),scope1 = "PM",parent = tr)
tr <- newXMLNode("tr",parent = table)
th <- newXMLNode("th",attrs = c(scope = "col"),scope1 = max(Bey2ollak$V3),parent = tr)
th <- newXMLNode("th",attrs = c(scope = "col"),scope1 = "MD",parent = tr)
th <- newXMLNode("th",attrs = c(scope = "col"),scope1 = "PM",parent = tr)
tr <- newXMLNode("tr",parent = table)
th <- newXMLNode("th",attrs = c(scope = "col"),scope1 = max(Bey2ollak$V3),parent = tr)
th <- newXMLNode("th",attrs = c(scope = "col"),scope1 = "MD",parent = tr)
th <- newXMLNode("th",attrs = c(scope = "col"),scope1 = "PM",parent = tr)

th <- newXMLNode("img",attrs = c(src = URL,width = "700",height= "777",alt=""),parent =top )



top 

这是控制台中的输出

<description>
  <table  width="300" border="1">
    <tr>
      <th scope="col">5</th>
      <th scope="col">MD</th>
      <th scope="col">PM</th>
    </tr>
    <tr>
      <th scope="col">5</th>
      <th scope="col">MD</th>
      <th scope="col">PM</th>
    </tr>
    <tr>
      <th scope="col">5</th>
      <th scope="col">MD</th>
      <th scope="col">PM</th>
    </tr>
  </table >
  <img src="file:///C:/Users/pc/Downloads/Googletraffic/Tazbet/Autostrad;Helwan To Da2ery/Spiral.jpg " width="700" height="777" alt=""/>
</description> 

here's 我的 kml 文件

我发现了但效率不高,我在 NotePad++ 上打开 kml 文件,然后获取根并将其放入 xml 文件,然后使用此代码读取 xml,

Url <- "xml_data1.xml"
data <- xmlTreeParse(Url)

xmlTreeParse() 允许我将 xml 文件解析为列表,因此我可以将任何内容添加到 xml 文件中的特定位置,这是我使用的代码添加节点

data$doc$children$Folder[[3]][[3]][[3]][["description"]] <- top 

注意XMLInternalElementNodeXMLNode是有区别的,不能这样直接用saveXML()..

saveXML(data, file ="xml_data2.kml")

你应该先得到数据的根

xmlroot <- xmlRoot(data)
saveXML(xmlroot, file ="xml_data2.xml")

这个写 xml 的答案被写成 here

然后您可以使用 NotePad++ 打开 xml_data2.xml 并获取您想要的内容,然后将其重新放入 kml 文件中。