-
Notifications
You must be signed in to change notification settings - Fork 2
/
step2-osmWd2csv.php
36 lines (32 loc) · 1.3 KB
/
step2-osmWd2csv.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
/**
* 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
*/
include 'GeoHash.php';
print "osm_type,osm_id,other_ids";
define("ND",'n'); // '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");
for( $lastLine=$tx=$lastCentroid='',$nm=ND; $r->read(); )
if ($r->nodeType == XMLReader::ELEMENT) {
printLine($lastLine.$lastCentroid, $tx, $nm);
$tx='';
$nm = substr($r->localName,0,1); // results in n=node, w=way, r=relation
$lat = $r->getAttribute('lat'); // on node element, coordinates as centroid
$lastCentroid = $lat? ('c'.GeoHash::encode($r->getAttribute('lon'),$lat).' '): '';
$lastLine = "$nm,". $r->getAttribute('id') .',';
} elseif ($r->nodeType == XMLReader::TEXT) {
$tx .= trim( preg_replace('/\s+/s',' ',$r->value) );
if (strpos($tx,' ')===false && substr($tx,0,1)=='l') $tx=''; // is empty node.
}
printLine($lastLine,$tx,$nm);
$r->close();
// // // LIB
function printLine($line,$tx,$nm) {
if ($nm!=ND || $tx) // exclude empty nodes
print "\n".trim("$line$tx");
}