hallo und guten Abend
bin neu in diesem Forum - und auch relativ neu in PHP. Also was ich grade vorhabe ist
eine openstreetmap-Abfrage mittels overpass-Api zu stellen.
Kenn mich mit xpath u. DOM noch nicht so gut aus - hab heute mittag mal die xpath-Dokus gelesen und auch jene zu simpleXML
hier was schon mal geht....
xpath Abfragen
zwei sind dort oben gebraucht eine xpath query sieht nacher den nodes die einen bestimmten tag haben
das hier
gib uns alle Elements die ein tag element drinne haben wie zb. das k attribute - value "amenity" und das v attribute mit dem Wert "school".
Damit werden aus den og. Nodes alle jene ausgefiltert die die amenity school haben
dann wird xpath weiter genutzt, jetzt um all die school nodes um zu sehen ob da ein name drinne ist - und um diesen zu holen :
Relative zu diesem node, gib mir das v attribut des tag elements mit dem k attribute value "name".
weil nicht alle Schulen einen namen haben , wird noch ein default string angehängt . um auch namenlose schulen ins array zu laden.:
hier die resultate
2179 School(s) found: the last ones are shown below.....
#2151: ID:2688357765 [51.4668941,-0.9731135] New Directrions, North Reading
#2152: ID:2702504696 [51.5884265,-0.7829013] Burford School
#2153: ID:2702549737 [51.5802201,-0.7653918] Great Marlow School
#2154: ID:2706219304 [51.3779317,-0.0895302] ARK Oval Primary Academy
#2155: ID:2706219314 [51.3871935,-0.0623001] Ashburton Primary School
#2156: ID:2706219320 [51.3210977,-0.1398859] CALAT Smitham Centre
#2157: ID:2706219326 [51.3638861,-0.0922032] Elmhurst School
#2158: ID:2706219339 [51.4007121,-0.0743710] Harris Academy South Norwood
#2159: ID:2706219343 [51.3831662,-0.0405476] Orchard Way Primary School
#2160: ID:2706219347 [51.3531047,-0.0959447] Purley Oaks Primary School
#2161: ID:2706219348 [51.3428384,-0.0069931] Rowdown Primary School
#2162: ID:2706219350 [51.3954917,-0.0732185] South Norwood Primary School
#2163: ID:2706219351 [51.3377151,-0.1230482] St David's Preparatory School
#2164: ID:2706219353 [51.3993760,-0.1144352] Winterbourne School
#2165: ID:2717394621 [51.8706538,0.1480886] Prep
#2166: ID:2717394636 [51.8685838,0.1463720] Pre-Prep
#2167: ID:2722704201 [51.1398429,-0.0457445] Felbridge Primary School
nun will ich noch mehr xpath -anfragen einbauen in die loop entsprechend Key:contact - OpenStreetMap Wiki
also - die von mir versuchten erweiterungen der xpath Abfrage in der Loop sind gescheitert... Kann jeamdn weiterhelfen.
freu mich auf eine Antwort...
bin neu in diesem Forum - und auch relativ neu in PHP. Also was ich grade vorhabe ist
eine openstreetmap-Abfrage mittels overpass-Api zu stellen.
Kenn mich mit xpath u. DOM noch nicht so gut aus - hab heute mittag mal die xpath-Dokus gelesen und auch jene zu simpleXML
hier was schon mal geht....
PHP-Code:
<?php
/**
* OSM Overpass API with PHP SimpleXML / XPath
*
* PHP Version: 5.4 - Can be back-ported to 5.3 by using 5.3 Array-Syntax (not PHP 5.4's square brackets)
*/
//
// 1.) Query an OSM Overpass API Endpoint
//
$query = 'node
["amenity"~".*"]
(38.415938460513274,16.06338500976562,39.52205163048525,17.51220703125);
out;';
$context = stream_context_create(['http' => [
'method' => 'POST',
'header' => ['Content-Type: application/x-www-form-urlencoded'],
'content' => 'data=' . urlencode($query),
]]);
# please do not stress this service, this example is for demonstration purposes only.
$endpoint = 'http://overpass-api.de/api/interpreter';
libxml_set_streams_context($context);
$start = microtime(true);
$result = simplexml_load_file($endpoint);
printf("Query returned %2\$d node(s) and took %1\$.5f seconds.\n\n", microtime(true) - $start, count($result->node));
//
// 2.) Work with the XML Result
//
# get all school nodes with xpath
$xpath = '//node[tag[@k = "amenity" and @v = "school"]]';
$schools = $result->xpath($xpath);
printf("%d School(s) found:\n", count($schools));
foreach ($schools as $index => $school)
{
# Get the name of the school (if any), again with xpath
list($name) = $school->xpath('tag[@k = "name"]/@v') + ['(unnamed)'];
printf("#%02d: ID:%' -10s [%s,%s] %s\n", $index, $school['id'], $school['lat'], $school['lon'], $name);
}
zwei sind dort oben gebraucht eine xpath query sieht nacher den nodes die einen bestimmten tag haben
das hier
Code:
//node[tag[@k = "amenity" and @v = "school"]]
Damit werden aus den og. Nodes alle jene ausgefiltert die die amenity school haben
dann wird xpath weiter genutzt, jetzt um all die school nodes um zu sehen ob da ein name drinne ist - und um diesen zu holen :
Code:
tag[@k = "name"]/@v'
weil nicht alle Schulen einen namen haben , wird noch ein default string angehängt . um auch namenlose schulen ins array zu laden.:
Code:
list($name) = $school->xpath('tag[@k = "name"]/@v') + ['(unnamed)']; ^^^^^^^^^^^^^^^ Provide Default Value
2179 School(s) found: the last ones are shown below.....
#2151: ID:2688357765 [51.4668941,-0.9731135] New Directrions, North Reading
#2152: ID:2702504696 [51.5884265,-0.7829013] Burford School
#2153: ID:2702549737 [51.5802201,-0.7653918] Great Marlow School
#2154: ID:2706219304 [51.3779317,-0.0895302] ARK Oval Primary Academy
#2155: ID:2706219314 [51.3871935,-0.0623001] Ashburton Primary School
#2156: ID:2706219320 [51.3210977,-0.1398859] CALAT Smitham Centre
#2157: ID:2706219326 [51.3638861,-0.0922032] Elmhurst School
#2158: ID:2706219339 [51.4007121,-0.0743710] Harris Academy South Norwood
#2159: ID:2706219343 [51.3831662,-0.0405476] Orchard Way Primary School
#2160: ID:2706219347 [51.3531047,-0.0959447] Purley Oaks Primary School
#2161: ID:2706219348 [51.3428384,-0.0069931] Rowdown Primary School
#2162: ID:2706219350 [51.3954917,-0.0732185] South Norwood Primary School
#2163: ID:2706219351 [51.3377151,-0.1230482] St David's Preparatory School
#2164: ID:2706219353 [51.3993760,-0.1144352] Winterbourne School
#2165: ID:2717394621 [51.8706538,0.1480886] Prep
#2166: ID:2717394636 [51.8685838,0.1463720] Pre-Prep
#2167: ID:2722704201 [51.1398429,-0.0457445] Felbridge Primary School
nun will ich noch mehr xpath -anfragen einbauen in die loop entsprechend Key:contact - OpenStreetMap Wiki
contact
hone
contact:fax
contact:website
contact:email

contact:fax
contact:website
contact:email
freu mich auf eine Antwort...
Kommentar