PHP从KML文件中获取坐标

时间:2013-11-10 07:07:55

标签: php xml arrays coordinates kml

我正在使用KML文件提取坐标并将其插入多维数组中。起初我只想输出页面上的坐标 - 而且它似乎没有工作

这是我到目前为止尝试过的代码:

<?php
$url = "myKML.kml";
$contents = file_get_contents($url);
$xml      = new SimpleXMLElement($contents);

$value    = (string)$xml->Document->Placemark->Point->coordinates;
$coords   = array();
foreach($value as $coord) {    
$args     = explode(",", $value);
$coords[] = array($args[0], $args[1], $args[2]);
}

print_r($coord);
?>

继承人KML结构:

<Document>
<name>...</name>
<open>1</open>
<Snippet maxLines='0'></Snippet>
<description>.../description>
<Style >
<BalloonStyle>
<text>
...
</text>
</BalloonStyle>
</Style>
<Placemark >
<name>...</name>
<styleUrl>...</styleUrl>
<Snippet maxLines='0'></Snippet>
<ExtendedData>
<Data name='__title'>
<value>...</value>
</Data>
<Data name='__imgUrl'>
<value>...</value>
</Data>
<Data name='__data'>
<value>...</value>
</Data>
</ExtendedData>
 <Point>
 <coordinates>14.8184806108,56.8630456924,196.0000000000</coordinates>
 </Point>
 </Placemark>

2 个答案:

答案 0 :(得分:3)

您尝试解析的XML无效,您需要在解析之前解决该问题。这些是我看到的问题:

  • 您未正确关闭<description>代码
  • 开头标记和结束标记不匹配

修复错误后,您的XML结构应如下所示:

<Document>
<name>...</name>
<open>1</open>
<Snippet maxLines='0'></Snippet>
<description>...</description>
<Style >
    <BalloonStyle>
        <text>
            ...
        </text>
    </BalloonStyle>
</Style>
<Placemark >
    <name>...</name>
    <styleUrl>...</styleUrl>
    <Snippet maxLines='0'></Snippet>
    <ExtendedData>
        <Data name='__title'>
            <value>...</value>
        </Data>
        <Data name='__imgUrl'>
            <value>...</value>
        </Data>
        <Data name='__data'>
            <value>...</value>
        </Data>
    </ExtendedData>
    <Point>
        <coordinates>14.8184806108,56.8630456924,196.0000000000</coordinates>
    </Point>
</Placemark>
</Document>

来到您的代码,您有以下内容:

$value    = (string)$xml->Document->Placemark->Point->coordinates;

您将$value转换为字符串,因此foreach循环不起作用。 foreach需要一个数组作为参数,但你要传递一个字符串。

更正的代码应如下所示:

$value = (array) $xml->Placemark->Point->coordinates;

$coords   = array();
foreach($value as $coord) {    
    $args     = explode(",", $coord);
    $coords[] = array($args[0], $args[1], $args[2]);
}

输出:

Array
(
    [0] => Array
        (
            [0] => 14.8184806108
            [1] => 56.8630456924
            [2] => 196.0000000000
        )

)

Demo!

上面的代码将尝试将所有坐标放入$coords数组中。如果您只想获得一个坐标,可以将其转换为字符串,然后执行以下操作:

$value = (string) $xml->Placemark->Point->coordinates;
list($coord1, $coord2, $coord3) = explode(',', $value);
$coords = array($coord1, $coord2, $coord3);
print_r($coords);

这将生成以下数组:

Array
(
    [0] => 14.8184806108
    [1] => 56.8630456924
    [2] => 196.0000000000
)

Demo!

<强>更新

您发布的KML文件的结构有点不同。以下代码应该有效:

foreach ($xml->Document->Placemark as $coord) {
    $coord = (string) $coord->Point->coordinates."<br/>";
    $args     = explode(",", $coord);
    $coords[] = array($args[0], $args[1], $args[2]);
}
print_r($coords);

答案 1 :(得分:1)

$kml = new SimpleXMLElement(file_get_contents('myKML.kml'));

foreach ($kml->xpath('//Placemark/Point/coordinates') as $kml_coordinates) {
    // for legibility...
    sscanf((string) $kml_coordinates, '%f,%f,%f', $latitude, $longitude, $altitude);
    $coords[] = array($latitude, $longitude, $altitude);
    // for simplicity...
    $coords[] = explode(',', (string) $kml_coordinates);
}

演示:https://eval.in/65272