Try to search your question here, if you can't find : Ask Any Question Now ?

Convert a xml with coordinates to a dataframe in R

HomeCategory: stackoverflowConvert a xml with coordinates to a dataframe in R
kundan asked 2 weeks ago

I have a very large xml data which contains the lat long values. My goal is to convert the xml file to polygons. To do that I am first trying to convert the xml to a dataframe. I tried the xml2 and the dplyr packages but in vain. I also used the lapply function in the xml package but I am not able to convert the xml to a dataframe.
My data looks like this.

<?xml version="1.0" encoding="UTF-8"?>
-<rss version="2.0
xmlns:atom="http://www.w3.org/2005/Atom"xmlns:georss="http://www.georss.org/georss">-<channel>
<title>raisen:raisengp</title>
<description>null</description>
-<link>
-<![CDATA[http://geoportal.mp.gov.in:8080/geoserver/raisen/wms
service=wms&request=GetMap&version=1.1.1&format=application%2Frss%2Bxml&layers=raisen%3Araisengp&styles=poly_gp&height=545&width=768&transparent=false&bbox=8611854.0%2C2606654.0%2C8775218.0%2C2722591.0&srs=EPSG%3A3857]]>
</link>
<atom:link rel="self"
href="http://geoportal.mp.gov.in:8080/geoserver/raisen/wms?service=wms&request=GetMap&version=1.1.1&format=application%2Frss%2Bxml&layers=raisen%3Araisengp&styles=poly_gp&height=545&width=768&transparent=false&bbox=8611854.0%2C2606654.0%2C8775218.0%2C2722591.0&srs=EPSG%3A3857"/>
-<item>
<title>raisengp.1</title>
-<link>
-<![CDATA[http://geoportal.mp.gov.in:8080/geoserver/raisen/wms/reflect?format=application%2Fatom%2Bxml&layers=raisen%3Araisengp&featureid=raisengp
</link>
-<guid>
-<![CDATA[http://geoportal.mp.gov.in:8080/geoserver/raisen/wms/reflect?format=application%2Fatom%2Bxml&layers=raisen%3Araisengp&featureid=raisengp.1]]>
</guid>
-<description>
-<![CDATA[<h4>raisengp</h4>
<ul class="textattributes">
<li><strong><span class="atr-name">districtc</span>:</strong> <span class="atr-value">446</span></li>
  <li><strong><span class="atr-name">tehsilcode</span>:</strong> <span class="atr-value">3593</span></li>
<li><strong><span class="atr-name">blockcode</span>:</strong> <span class="atr-value">1172</span></li>
</description>
<georss:polygon>23.18939277468125 77.57356245525989 23.18919469824253
77.5738162614824 23.18898075873695 77.57405868357523 23.188783235268893
77.57426764701316 23.188552245459086 77.57444602814941 23.188233192400194
77.57456604367773 23.187997858030833 77.57465301739674 23.18782708762609
77.57470568856189 23.18773716304003 77.57475944413376 23.187614345311996
77.57479779220925 23.187560141307777 77.57482903021003 23.18743125870021
77.57487370595457 23.18733905673292 77.57489457266112 23.187148669743586
77.57495472144764 23.18697715655688 77.57499546065583 23.18681329439572
77.57506290929337 23.18666518442996 77.57510904621871 </georss:polygon>

Here is my code. Kindly help me in doing this.

> library(xml2)
> library(dplyr)
> dat <- "D:/Prakshep_project/raisen-raisengp.xml"
> doc <- read_xml(dat)
> docdf <- bind_rows(lapply(xml_find_all(doc, "//georss:polygon), function(x) {
parent <- data.frame(as.list(xml_attrs(x)), stringsAsFactors=FALSE)
kids <- bind_rows(lapply(xml_children(x), function(x) as.list(xml_attrs(x))))
cbind.data.frame(parent, kids, stringsAsFactors=FALSE)
}))

Using the xml package here is the code.

library(XML)
data <- xmlTreeParse("D:/Prakshep_project/raisen-raisengp.xml",useInternalNodes = TRUE)
guid <- xpathSApply(data, "//guid" ,xmlValue)
ply <- xpathSApply(data, "//georss:polygon" ,xmlValue)
df <- data.frame(guid= unlist(guid), 
             ply = unlist(georss:polygon)
1 Answers
Best Answer
Arben answered 2 weeks ago
Your Answer

12 + 2 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com