Jobbo90
Jobbo90

Reputation: 175

Values from xml file

I have an xml file which contains image path names, GPS locations and orientation. It is a subset from a larger xml file which links the GPS locations to images taken with UAV system.

The xml part i'm interested in looks like this for 2 images (in total there is 180 or so):

<?xml version="1.0" encoding="UTF8"?>
<images>
    <image path="D:\DCIM\100MEDIA\AMBA1124.jpg">
        <time value="2018:04:20 14:47:55"/>
        <gps lat="+05.90003016" lng="-055.22443772" alt="-0069.752" />
        <ori yaw="+124.65" pitch="-090.00" roll="-003.80    " />
    </image>
        <time value="2018:04:20 14:47:57"/>
        <gps lat="+05.89998104" lng="-055.22442246" alt="-0069.802" />
        <ori yaw="+179.79" pitch="-090.00" roll="-005.43    " />
    <image path="D:\DCIM\100MEDIA\AMBA1125.jpg">
        <time value="2018:04:20 14:47:59"/>
        <gps lat="+05.89998104" lng="-055.22442246" alt="-0069.802" />
        <ori yaw="+179.79" pitch="-090.00" roll="-005.43    " />
    </image>
        <time value="2018:04:20 14:48:02"/>
        <gps lat="+05.90014408" lng="-055.22444534" alt="-0069.480" />
        <ori yaw="+179.51" pitch="-090.00" roll="-006.32    " />
</images>

Is the xml format correct? I came up with this:

geofile <- xmlParse(file = "../../../GEOFILE3.xml")
data <- xmlToDataFrame(nodes = getNodeSet(geofile, "/images/image"))

But it keeps giving me an empty data table with the correct dimensions...

The original file is here: link

The extracted file i'm using is geofile3: link

Ultimately i'd like to link the coordinates from the xml file to the images for image processing in Agisoft, for that I need to export as a CSV in format like this:

Label           X/East      Y/North     Z/Altitude

IMG_0002.JPG    261.638147  46.793178   391.780913  
IMG_0003.JPG    261.638176  46.793470   391.980780  
IMG_0004.JPG    261.638278  46.793908   393.313226  

Regards!

Upvotes: 0

Views: 62

Answers (1)

Dave2e
Dave2e

Reputation: 24079

I believe you are having trouble extracting because the values you are looking for a stored as attributes to the xml nodes and not as values.

This is a straight forward problem. Find the image nodes and extract out the attributes of interest. I prefer to use the xml2 package over the XML package.

library(xml2)

page<-read_xml('<images><image path="D:/DCIM/100MEDIA/AMBA1124.jpg">
        <time value="2018:04:20 14:47:55"/>
               <gps lat="+05.90003016" lng="-055.22443772" alt="-0069.752" />
               <ori yaw="+124.65" pitch="-090.00" roll="-003.80    " />
               </image>
               <time value="2018:04:20 14:47:57"/>
               <gps lat="+05.89998104" lng="-055.22442246" alt="-0069.802" />
               <ori yaw="+179.79" pitch="-090.00" roll="-005.43    " />
               <image path="D:/DCIM/100MEDIA/AMBA1125.jpg">
               <time value="2018:04:20 14:47:59"/>
               <gps lat="+05.89998104" lng="-055.22442246" alt="-0069.802" />
               <ori yaw="+179.79" pitch="-090.00" roll="-005.43    " />
               </image>
               <time value="2018:04:20 14:48:02"/>
               <gps lat="+05.90014408" lng="-055.22444534" alt="-0069.480" />
               <ori yaw="+179.51" pitch="-090.00" roll="-006.32    " />
               </images>')

#extract the image nodes
images<-xml_find_all(page, "image")
#extract out the desired attributes
names<-xml_attr(images, "path")
locations<-xml_find_first(images, "gps")
latitude<-xml_attr(locations, "lat")
longitude<-xml_attr(locations, "lng")
alt<-xml_attr(locations, "alt")

#pack into a dataframe
answer<-data.frame(names, latitude, longitude, alt)

Upvotes: 2

Related Questions