dtdvlazd56637 2015-05-19 03:41
浏览 101
已采纳

使用Php从.osm XML文件中提取数据

I tried to extract some data from an .Osm File.

So I have something like this:

<way id="28747493" visible="true" version="7" changeset="9347177" timestamp="2011-09-19T21:48:11Z" user="Camilo Alvarez" uid="492132">
  <nd ref="316077528"/>
  <nd ref="316077503"/>
  <tag k="highway" v="primary"/>
  <tag k="lanes" v="1"/>
  <tag k="name" v="Calle 51"/>
  <tag k="oneway" v="yes"/>
  <tag k="ref" v="Boyacá"/>
 </way>
<way id="28747492" visible="true" version="9" changeset="7227086" timestamp="2011-02-08T15:33:22Z" user="dmartinh" uid="314700">
  <nd ref="358031212"/>
  <nd ref="316077505"/>
  <tag k="foot" v="permissive"/>
  <tag k="highway" v="footway"/>
  <tag k="name" v="Calle 52"/>
 </way>

So, I want to extract every single "ref" and put into a table called "referencia and I want to assign tag name "calle 51" to that ref values.

Something like this:

Table referencia

idnode -------- via

316077528 |  Calle 51 |
316077503 |  Calle 51 |
358031212 |  Calle 52 |
316077505 |  Calle 52 |
----------------------

So, I can store "idnode" values very well but I can't store the "via" values. I tried to make some kind of foreach without sucess.

And definitely I don't know why is suposse we have one Iteration that get "idnode" values and "via" why is not possible to store together.

<?php < ? global $referencia;
global $via;
/*
/**
* OSM Overpass API with PHP SimpleXML / XPath
*
* PHP Version: 5.4 - Can be back-ported to 5.3 by using 5.3 Array-Syntax (not PHP 5.4's square brackets)
*/

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db('map');

if (!$conn)
    {
    die('Could not connect: ' . mysql_error());
    }

echo 'Connected successfully';

$xml = simplexml_load_file("map.osm");
$counter = - 1;

foreach($xml->children() AS $child)
    {
    $counter++;
    if ($child->getName() == 'way')
        {

        // if($child->getName() == 'node'){
        // echo $counter;

        $name = "";
        $id = "";
        $lat = "";
        $lon = "";
        $name = $child['version'];
        $id = $child['id'];
        $lat = $child['lat'];
        $lon = $child['lon'];

        }

    foreach($child->children() AS $grandchild)
        {
        if ($grandchild->getname() == 'nd')
            {
            $referencia = $grandchild['ref'];

            if ($grandchild->getName() == 'tag')
                {
                if ($grandchild['k'] == 'name')
                    {
                    $via = $grandchild['v'];
                    }
                }

            mysql_query("INSERT INTO referencia (idnode,via) VALUES ('$referencia','$via')");
            }
        }
    }




?>

Thanks in Advance.

  • 写回答

1条回答 默认 最新

  • dongwu9972 2015-05-19 05:03
    关注

    You can use SimpleXMLElement::xpath to get both "via" and the corresponding "idnode" values. For example :

    $raw = <<<EOF
    <root>
        <way id="28747493" visible="true" version="7" changeset="9347177" timestamp="2011-09-19T21:48:11Z" user="Camilo Alvarez" uid="492132">
          <nd ref="316077528"/>
          <nd ref="316077503"/>
          <tag k="highway" v="primary"/>
          <tag k="lanes" v="1"/>
          <tag k="name" v="Calle 51"/>
          <tag k="oneway" v="yes"/>
          <tag k="ref" v="Boyacá"/>
         </way>
        <way id="28747492" visible="true" version="9" changeset="7227086" timestamp="2011-02-08T15:33:22Z" user="dmartinh" uid="314700">
          <nd ref="358031212"/>
          <nd ref="316077505"/>
          <tag k="foot" v="permissive"/>
          <tag k="highway" v="footway"/>
          <tag k="name" v="Calle 52"/>
         </way>
    </root>
    EOF;
    $xml = simplexml_load_string($raw);
    
    foreach($xml->xpath("//way") AS $way){
        $via = $way->xpath("tag[@k='name']/@v")[0];
        foreach($way->nd AS $nd){
            $idnode = $nd["ref"];
            echo $idnode .", ". $via  ."<br>";
        }
    }
    

    Demo

    output :

    316077528, Calle 51
    316077503, Calle 51
    358031212, Calle 52
    316077505, Calle 52
    

    xpath explanation :

    • //way selects all <way> elements anywhere in the XML document.

    • tag[@k='name'] selects <tag> child of current context node having k attribute value equal name. Then from that <tag>, /@v returns v attribute.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于这个复杂问题的解释说明
  • ¥50 三种调度算法报错 采用的你的方案
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败