使用R在KML文件中添加标签 [英] Adding a tag in KML file Using R

查看:143
本文介绍了使用R在KML文件中添加标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Google Earth Pro导出了kml文件,该文件由37个文件夹组成,每个文件夹包含次要"文件夹数量,次要"文件夹总数为168个,每个次要"文件夹均具有3个地标. >

我有HTML代码,我用R制作而成,并且想要将此kml文件导入R并将此HTML代码放入每个次要"文件夹的第一个地标"中,该HTML代码不是恒定的,它具有类似于该代码表中的值的变量,并且此变量将从我为此次要"文件夹创建的数据帧中附加,当我编辑此HTML代码时,我会将其放入该次要"文件夹,其他次要"文件夹依此类推.

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

这是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> 

这是我的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的答案写在

I've HTML code, i made it with R, and want to import this kml file into R and put this HTML code into the first "Placemark" for each "minor" Folder, this HTML code isn't constant, it has variables like the values in the table in this code, and this variables will attached from dataframe i made for this "minor" Folder,when i edit this HTML code, i'll put it into the first "placemark" for this "minor" Folder, and so on for the other "minor" Folders.

is there any fuction in R that can add this html code to the kml file ?

here's the code of "description" in 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 

here's the output in console

<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 my kml file

解决方案

i found away but not so efficient, i open the kml file on NotePad++, then get the root and put it in xml file, then read the xml using this code,

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

xmlTreeParse() allowed me to parse the xml file as a list, so i could add any thing to a specific place in the xml file, and this the code i used to add the node

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

be careful that there is difference between XMLInternalElementNode and XMLNode, so you can't use saveXML() directly like this ..

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

you should get the Root of data first

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

this answer for writing the xml was written here

then you can open the xml_data2.xml using NotePad++ and get what you want then put it again into the kml file.

这篇关于使用R在KML文件中添加标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆