Question

I get this result from overpass api - this is streets.

<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="Overpass API">
<note>The data included in this document is from www.openstreetmap.org. The data is made available under ODbL.</note>
<meta osm_base="2013-12-03T12:52:02Z"/>

  <node id="1549294055" lat="49.4310141" lon="7.5117213"/>
  <node id="1549294085" lat="49.4313484" lon="7.5126816"/>
  <node id="1549294087" lat="49.4315384" lon="7.5132431"/>
  <node id="1549294093" lat="49.4318250" lon="7.5140125"/>
  <node id="1549294094" lat="49.4318541" lon="7.5140969"/>
  <node id="1549294104" lat="49.4322262" lon="7.5151568"/>
  <node id="1549294106" lat="49.4324901" lon="7.5159332"/>
  <node id="1552775307" lat="49.4328287" lon="7.5169585"/>
  <node id="1552775309" lat="49.4328551" lon="7.5170364"/>
  <node id="1552775318" lat="49.4330332" lon="7.5176039"/>
  <node id="1552775347" lat="49.4333308" lon="7.5186515"/>
  <node id="1552775375" lat="49.4341515" lon="7.5215118"/>
  <node id="1552775408" lat="49.4345873" lon="7.5229784"/>
  <node id="1552775447" lat="49.4358841" lon="7.5273364"/>
  <node id="1552775464" lat="49.4367267" lon="7.5302234"/>
  <node id="1552809430" lat="49.4368016" lon="7.5304614"/>
  <way id="28367045">
    <nd ref="1549294106"/>
    <nd ref="1552775307"/>
    <nd ref="1552775309"/>
    <nd ref="1552775318"/>
    <nd ref="1552775347"/>
    <nd ref="1552775375"/>
    <nd ref="1552775408"/>
    <nd ref="1552775447"/>
    <nd ref="1552775464"/>
    <nd ref="1552809430"/>
    <tag k="highway" v="secondary"/>
    <tag k="ref" v="L 356"/>
  </way>
  <way id="141545567">
    <nd ref="1549294104"/>
    <nd ref="1549294106"/>
    <tag k="bridge" v="yes"/>
    <tag k="highway" v="secondary"/>
    <tag k="layer" v="1"/>
    <tag k="ref" v="L 356"/>
  </way>
  <way id="141545568">
    <nd ref="1549294055"/>
    <nd ref="1549294085"/>
    <nd ref="1549294087"/>
    <nd ref="1549294093"/>
    <nd ref="1549294094"/>
    <nd ref="1549294104"/>
    <tag k="highway" v="secondary"/>
    <tag k="ref" v="L 356"/>
  </way>

</osm>

So i need to parse it and return every street, but when i trying to parse it with http://www.php.net/manual/en/book.dom.php i cant get anything by id. My code is below:

$doc = new DOMDocument;
$doc->loadXML($result);

$ways = $doc->getElementsByTagName('way');

foreach ($ways as $way) {
    $nodes = $way->getElementsByTagName('nd');

    foreach ($nodes as $node) {
        $id = intval($node->getAttribute('ref'));

        var_dump($id);
        var_dump($doc->getElementById($id));
    }
}

No correct solution

OTHER TIPS

The code below should loop through the nodes which is what it sounds like you're trying to do. The code you have is looping through the tags and the tags under each of those which do not contain any IDs.

$doc = new DOMDocument;
$doc->loadXML($result);

$nodes = $doc->getElementsByTagName('node');

foreach ($nodes as $node) {
    $id = intval($node->getAttribute('id'));
    $lat = intval($node->getAttribute('lat'));
    $lon = intval($node->getAttribute('lon'));

    var_dump($node);
}

Hope this helps at least get you on the right track.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top