node as $node) { $q = $d->createInsertQuery(); $q->insertInto('poi') ->set( 'id', $q->bindValue( (string) $node['id'] ) ) ->set( 'type', $q->bindValue( 1 ) ) ->set( 'lat', $q->bindValue( (string) $node['lat'] ) ) ->set( 'lon', $q->bindValue( (string) $node['lon'] ) ); if (parseNode($q, $node)) { $s = $q->prepare(); $s->execute(); } // also keep a cache of all nodes and locations $locations[(string) $node['id']] = array( (string) $node['lat'], (string) $node['lon'] ); } foreach ($sxml->way as $way) { $q = $d->createInsertQuery(); $q->insertInto('poi') ->set( 'id', $q->bindValue( (string) $node['id'] ) ) ->set( 'type', $q->bindValue( 2 ) ); parseNode($q, $way); fetchLocations($q, $locations, $way); $s = $q->prepare(); $s->execute(); } function fetchLocations($q, $locations, $node) { $count = 0; $lat = $lon = 0; foreach ($node->nd as $nd) { $count++; $lat += $locations[(string) $nd['ref']][0]; $lon += $locations[(string) $nd['ref']][1]; } $q->set('lat', $q->bindValue( $lat / $count ) ); $q->set('lon', $q->bindValue( $lon / $count ) ); } function parseNode($q, $sxml) { $standAloneNode = false; $street = $postcode = $nr = $cuisine = false; // loop over tags foreach( $sxml->tag as $tag ) { if ($tag['k'] == 'name') { $q->set( 'name', $q->bindValue( trim( (string) $tag['v'] ) ) ); $standAloneNode = true; } if ($tag['k'] == 'cuisine') { $cuisine = (string) $tag['v']; } if ($tag['k'] == 'amenity' && $tag['v'] == 'pub') { $cuisine = 'pub'; } if ($tag['k'] == 'amenity' && $tag['v'] == 'bar') { $cuisine = 'bar'; } if ($tag['k'] == 'contact:phone') { $q->set( 'phone', $q->bindValue( trim( (string) $tag['v'] ) ) ); } if ($tag['k'] == 'addr:street') { $street = $tag['v']; } if ($tag['k'] == 'addr:postcode') { $postcode = ', ' . strtoupper( (string) $tag['v'] ); } if ($tag['k'] == 'addr:housenumber') { $nr = $tag['v']; } } if ( $street || $nr || $postcode ) { $q->set('address', $q->bindValue( trim( "{$street} {$nr} {$postcode}") ) ); } if ( $cuisine ) { $q->set( 'cuisine', $q->bindValue( $cuisine ) ); } return $standAloneNode; }