【问题标题】:Values from xml file来自 xml 文件的值
【发布时间】:2018-10-07 06:10:12
【问题描述】:

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

我感兴趣的 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  

问候!

【问题讨论】:

    标签: r xml file extract


    【解决方案1】:

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

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

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-19
      • 1970-01-01
      • 2011-07-27
      • 1970-01-01
      • 1970-01-01
      • 2020-12-13
      • 2023-03-06
      • 1970-01-01
      相关资源
      最近更新 更多