来自xml文件的值

时间:2018-04-26 13:50:05

标签: r xml file extract

我有一个xml文件,其中包含图像路径名称,GPS位置和方向。它是较大的xml文件的子集,它将GPS位置链接到使用UAV系统拍摄的图像。

我感兴趣的xml部分对于2张图片看起来像这样(总共有180张左右):

<?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>

xml格式是否正确?我想出了这个:

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

但是它一直给我一个具有正确尺寸的空数据表......

原始文件位于:link

我正在使用的解压缩文件是geofile3:link

最终我想将xml文件中的坐标链接到Agisoft中的图像处理图像,因为我需要以这样的格式导出为CSV格式:

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  

问候!

1 个答案:

答案 0 :(得分:2)

我相信您在提取时遇到问题,因为您要查找的值是作为xml节点的属性存储而不是值。

这是一个直截了当的问题。找到图像节点并提取出感兴趣的属性。我更喜欢在XML包上使用xml2包。

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)