bei einer overpass-api-abfrage-am Endpunkt
hab ich mit folgendem code erste Ergebnisse
Gesucht sind Schulen…
<?php
$query = 'node
["addr:postcode"~"RM12"]
(51.5557914,0.2118915,51.5673083,0.2369398);
node
(around:100000)
["amenity"~"school"];
out;';
$context = stream_context_create(['http' => [
'method' => 'POST',
'header' => ['Content-Type: application/x-www-form-urlencoded'],
'content' => 'data=' . urlencode($query),
]]);
$endpoint = 'http://overpass-api.de/api/interpreter';
libxml_set_streams_context($context);
$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);
}
?>
die ergebnisse sind hier – ein Auszug:
Frage: wie muss ich die xpath-requests erweitern dass ich nicht nur die daten erhalte
id
lat
lon
name (und ggf. noch ein default für unnamed)
sehr sehr gerne hätte ich halt noch die ADRESSE - dazu.
Und wenn moeglich auch noch Kontaktdaten - wie die Webseite. -. nur für die Fälle in denen es eben eine gibt!?
Frage; also: Gibt es die Moeglichkeit hier noch mehr Tags in die Suche mit einzubeziehen
Sorry, du bist echt schnell, hatte meinen Beitrag zwischenzeitlich editiert, weil ich wie geschrieben nicht ganz zu Ende gelesen habe.
Du bist schon auf den richtigen Weg. Leider bin ich in PHP nicht ganz so fit. In Java würde ich das so machen: Wenn du den (XML) Node schon in der Hand hast, kannst du auf den ja auch nochmal ein oder mehrere XPath loslassen. Du fragst ja im Moment auch explizit nur OSM-Nodes ab, was ist aber mit den Schulen, die als WAY getaggt ist.
Ich würde vermutlich eine XML Element unabhängiges XPath aufsetzen, welches eben nur nach den Attributen sucht, mir davon dann das Parent-Node geben lassen und darin dann jeweils die gewünschten Subelemente.
PS: Jetzt wird mir erst wieder klar, wie einfach das mit JavaScript und JSON geht, da braucht man sich über solche Sachen überhaupt keine Gedanken mehr machen
D.h. nach allen XML Nodes “tag” suchen, die als Attribut k=“amenity” und v=“school” haben und dann /… eben den Parent zurückbekommen. Wenn du die dann mit foreach durchgehst, dann eben nochmal die Subabfragen machen.
<?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));
echo "<br/>";
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)'];
list($zip) = $school->xpath('tag[@k = "addr:postcode"]/@v');
printf("#%02d: ID:%' -10s [%s,%s] %s ZIP:%05d", $index, $school['id'], $school['lat'], $school['lon'], $name, $zip);
echo "<br/>";
}
?>