diff --git a/src/osmWd2csv.php b/src/osmWd2csv.php index d9f3848..d6039f4 100644 --- a/src/osmWd2csv.php +++ b/src/osmWd2csv.php @@ -1,15 +1,30 @@ open("php://stdin")) die("\nFailed to open file\n"); $r->read(); if ($r->name!='osm') die("\n XML is not OSM\n"); -while($r->read()) - if ($r->nodeType == XMLReader::ELEMENT) - print "\n{$r->name},". $r->getAttribute('id') .','; - elseif ($r->nodeType == XMLReader::TEXT) - print trim( preg_replace('/\s+/s',' ',$r->value) ); + +for( $lastLine=$tx='',$nm=ND; $r->read(); ) + if ($r->nodeType == XMLReader::ELEMENT) { + printLine($lastLine,$tx,$nm); + $tx = ''; + $nm = $r->localName; //substr($r->localName,1); // results in n=node, w=way, r=relation + $lastLine = "$nm,". $r->getAttribute('id') .','; + } elseif ($r->nodeType == XMLReader::TEXT) + $tx = trim( preg_replace('/\s+/s',' ',$r->value) ); + +printLine($lastLine,$tx,$nm); $r->close(); + +function printLine($line,$tx,$nm) { + if ($nm!=ND || $tx) // exclude empty nodes + print "\n$line$tx"; +}