-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
22 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |