KML到CSV(Google Earth到Agisoft Photoscan Pro)

时间:2013-12-05 06:16:46

标签: php xml kml

我正在尝试编写一个脚本,将Google Earth KML文件转换为CSV文件,以便在Agisoft的Photoscan Pro中使用。此脚本将使用户能够快速创建GCP(地面控制点)的CSV文件,以优化模型,以便更准确地覆盖在Google地球中。

如何将其写入CSV?

<?php

    $inputfile = simplexml_load_file('Points.kml');
    $placemarks = $inputfile->Document->Folder->Placemark;

    print "<pre>";
    print_r($inputfile->Document->Folder->Placemark[0]);

    foreach ($placemarks as $placemark){
        $name = $placemark->name;
        $longitude = $placemark->LookAt->longitude;
        $latitude = $placemark->LookAt->latitude;
        $altitude = $placemark->LookAt->altitude;
        //print $name.",".$longitude.",".$latitude.",".$altitude;
        //print "</br>";
        $array = array("$name", "$longitude", "$latitude", "$altitude");
        //var_dump($array);
    }
?>

示例KML:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
    <name>Points.kml</name>
    <Style id="sn_open-diamond">
        <IconStyle>
            <color>ff0e12b7</color>
            <scale>0.7</scale>
            <Icon>
                <href>http://maps.google.com/mapfiles/kml/shapes/open-diamond.png</href>
            </Icon>
        </IconStyle>
        <LabelStyle>
            <color>b30e12b7</color>
        </LabelStyle>
    </Style>
    <StyleMap id="msn_open-diamond">
        <Pair>
            <key>normal</key>
            <styleUrl>#sn_open-diamond</styleUrl>
        </Pair>
        <Pair>
            <key>highlight</key>
            <styleUrl>#sh_open-diamond</styleUrl>
        </Pair>
    </StyleMap>
    <Style id="sh_open-diamond">
        <IconStyle>
            <color>ff0e12b7</color>
            <scale>0.827273</scale>
            <Icon>
                <href>http://maps.google.com/mapfiles/kml/shapes/open-diamond.png</href>
            </Icon>
        </IconStyle>
        <LabelStyle>
            <color>b30e12b7</color>
        </LabelStyle>
    </Style>
    <Folder>
        <name>Points</name>
        <open>1</open>
        <Placemark>
            <name>1</name>
            <LookAt>
                <longitude>-81.11867631881405</longitude>
                <latitude>28.58871312454101</latitude>
                <altitude>0</altitude>
                <heading>1.04958304488296e-13</heading>
                <tilt>0</tilt>
                <range>16</range>
                <gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
            </LookAt>
            <styleUrl>#msn_open-diamond</styleUrl>
            <Point>
                <gx:drawOrder>1</gx:drawOrder>
                <coordinates>-81.11867631881405,28.58871312454101,0</coordinates>
            </Point>
        </Placemark>
        <Placemark>
            <name>2</name>
            <LookAt>
                <longitude>-81.11878160604603</longitude>
                <latitude>28.58868018607449</latitude>
                <altitude>0</altitude>
                <heading>-5.038190918931147e-05</heading>
                <tilt>0</tilt>
                <range>16.00000207857145</range>
                <gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
            </LookAt>
            <styleUrl>#msn_open-diamond</styleUrl>
            <Point>
                <gx:drawOrder>1</gx:drawOrder>
                <coordinates>-81.11878160604603,28.58868018607449,0</coordinates>
            </Point>
        </Placemark>
        <Placemark>
            <name>3</name>
            <LookAt>
                <longitude>-81.11889392441689</longitude>
                <latitude>28.58897376333365</latitude>
                <altitude>0</altitude>
                <heading>-5.676793149912727e-05</heading>
                <tilt>0</tilt>
                <range>16</range>
                <gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
            </LookAt>
            <styleUrl>#msn_open-diamond</styleUrl>
            <Point>
                <gx:drawOrder>1</gx:drawOrder>
                <coordinates>-81.11889392441689,28.58897376333365,0</coordinates>
            </Point>
        </Placemark>
        <Placemark>
            <name>4</name>
            <LookAt>
                <longitude>-81.11879074625233</longitude>
                <latitude>28.58900436190265</latitude>
                <altitude>0</altitude>
                <heading>-7.3948102396262e-06</heading>
                <tilt>0</tilt>
                <range>16.00000167331963</range>
                <gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
            </LookAt>
            <styleUrl>#msn_open-diamond</styleUrl>
            <Point>
                <gx:drawOrder>1</gx:drawOrder>
                <coordinates>-81.11879074625233,28.58900436190265,0</coordinates>
            </Point>
        </Placemark>
    </Folder>
</Document>
</kml>

示例CSV:

# Label,X,Y,Z
1,-81.11867632,28.58871312,0
2,-81.11878161,28.58868019,0
3,-81.11889392,28.58897376,0

2 个答案:

答案 0 :(得分:0)

simplexml_load_file将返回SimpleXMLElement类,因此您应该使用此类中的方法来访问xml元素。

例如,儿童方法 - http://www.php.net/manual/en/simplexmlelement.children.php

答案 1 :(得分:0)

如果您确实只需要点的坐标并且它们的标签无关紧要,我建议您可以为PhotoScan编写一个简单的Python脚本,该脚本将获取kml文件的路径,然后逐行搜索&lt; 坐标&GT;如果文本在给定的行中,那么您只需从字符串中提取坐标,甚至可以创建带有应用坐标的新标记。

下面是一个示例代码(应保存为.py文件并通过PhotoScan Pro中的“运行脚本”对话框启动),该代码要求提供kml文件并写入控制台窗格中提取的点的x,y,z坐标:

import PhotoScan

path = PhotoScan.app.getOpenFileName("Specify KML file:")
file = open(path, "rt")
feof = False
counter = 0

while not feof:

    str = file.readline()
    if not len(str):
        feof = True
        break

    if "<coordinates>" in str:
        counter += 1
        str = str.split("<coordinates>", 1)[1]
        str = str.rsplit("</coordinates>", 1)[0]
        (x, y, z) = str.split(",")
        print(x, y, z)

file.close()

如果您希望将坐标保存到.txt文件中(例如,使用计数器变量作为GCP标签),则可以修改此代码。但我认为将这些值直接导入地面控制窗格应该不难。