Search code examples
htmlrxmlkml

Adding a tag in KML file Using R


I've kml file exported from Google Earth Pro, consist of 37 Folder, each Folder contain number of "minor" Folder, the number of total "minor" Folders is 168, each "minor" has 3 placemarks.

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


Solution

  • 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.