Skip to content

Commit

Permalink
issue #1, little optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
ppKrauss authored Aug 7, 2018
1 parent 4e51a3a commit 9e5abe7
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/osmWd2csv.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
<?php
/**
* OSM XML file with Wikidata tags, convert to CSV file.
* OSM XML file with Wikidata tags, convert to CSV file after pre-parse filter.
* The pre-parse pass only node/way/relation with optional wikidata and id-refs as content.
* @depends osmWd2csv_pre.sh
*/
print "osm_type,osm_id,otherIDs";
$r = new XMLReader; // any fast XML Pull parser (not DOM)
define("ND",'node'); // 'node' or 'n'

$r = new XMLReader; // any fast XML-Pull or SAX parser (not DOM!)
if (!$r->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";
}

0 comments on commit 9e5abe7

Please sign in to comment.