You are not logged in.
- Topics: Active | Unanswered
Announcement
#1 2014-06-16 19:54:00
- tagtheworld
- Banned

- Registered: 2012-04-25
- Posts: 236
xpath-Abfrage erweitern - um moeglichst mehr infomationen zu erhalten.
hallo u. guten Abend
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:
#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
#2168: ID:2723815070 [50.8465429,-0.3030261] Lancing College
#2169: ID:2727170814 [51.5780664,-0.0249051] Adult Education Centre
#2170: ID:2833253896 [50.9928140,-0.7774996] (unnamed)
#2171: ID:2837001831 [51.1783749,-0.7970866] More House School
#2172: ID:2865091022 [50.9090614,-0.5565425] Dorset House School
#2173: ID:2882477853 [51.6261198,-0.7349665] Bowerdean Primary School
#2174: ID:2901434856 [51.6542477,-0.3098923] The Haberdashers' Aske's Girls School
#2175: ID:2901434857 [51.6565707,-0.3129822] The Haberdashers' Aske's Boys School
martin@linux-70ce:~/php>
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
Freu mich auf Tipps.
vg ttworld
Last edited by tagtheworld (2014-06-16 20:10:57)
https://wiki.openstreetmap.org/wiki/User:Tagtheworld - interested in all things concerning osm especially those related to the languages Perl, PHP, Python and the databases MySQL and Postgresql.
Offline
#2 2014-06-16 20:08:27
- Harald Hartmann
- Member

- From: 98667 Schönbrunn
- Registered: 2014-04-02
- Posts: 3,123
- Website
Re: xpath-Abfrage erweitern - um moeglichst mehr infomationen zu erhalten.
EDIT: Link auf OutputControlFormat entfernt, sorry, hatte im Codesnippet nicht ganz nach unten gescrollt und zu früh geantwortet
Last edited by Harald Hartmann (2014-06-16 20:14:28)
Mein aktives Gebiet: Gemeinde Schleusegrund
Fingerprint meines Schlüssels: 71F7 3CD9 B647 9079 6B88 326E 8B8B 72AE 34F9 5AAD
Offline
#3 2014-06-16 20:15:18
- tagtheworld
- Banned

- Registered: 2012-04-25
- Posts: 236
Re: xpath-Abfrage erweitern - um moeglichst mehr infomationen zu erhalten.
Hallo und guten Abend Harald
vorweg vielen vielen Dank für deine Schnelle Antwort. Freu mich von dir zu hoeren.
hmm - hab mir das gleich mal angesehen. Denke dass ich damit die Anfrage verfeinern kann.
Du meinst dass ich mit dieser ABFRAGE auch den PHP -Code verändern kann - also die xpath-Abfrage.
Es gibt ja für xpath auch die meoglichkeit alle nodes zu einzubeziehen.
das ist dann ggf. so etwas wie ein "catch_all" - mit xpath
//node(): $xpath->query('//node()'); which returns all element nodes only
(I assume because of //) Is there a way to add other nodes
like attributes and textnodes in attribute values?
Aber ich versuch mal jetzt mit dem von dir angegebenen Link und den dort zu findenden Beschreibungen weiterzukommen.
Meld mich wieder - später.
Nochmals vielen herzlichen Dank für alle Hilfe
TagTheWorld ;-)
https://wiki.openstreetmap.org/wiki/User:Tagtheworld - interested in all things concerning osm especially those related to the languages Perl, PHP, Python and the databases MySQL and Postgresql.
Offline
#4 2014-06-16 20:24:47
- Harald Hartmann
- Member

- From: 98667 Schönbrunn
- Registered: 2014-04-02
- Posts: 3,123
- Website
Re: xpath-Abfrage erweitern - um moeglichst mehr infomationen zu erhalten.
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 ![]()
Mein aktives Gebiet: Gemeinde Schleusegrund
Fingerprint meines Schlüssels: 71F7 3CD9 B647 9079 6B88 326E 8B8B 72AE 34F9 5AAD
Offline
#5 2014-06-16 20:33:09
- Harald Hartmann
- Member

- From: 98667 Schönbrunn
- Registered: 2014-04-02
- Posts: 3,123
- Website
Re: xpath-Abfrage erweitern - um moeglichst mehr infomationen zu erhalten.
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
//tag[@k="amenity" and @v="school"]/..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.
Mein aktives Gebiet: Gemeinde Schleusegrund
Fingerprint meines Schlüssels: 71F7 3CD9 B647 9079 6B88 326E 8B8B 72AE 34F9 5AAD
Offline
#6 2014-06-27 17:48:59
- tagtheworld
- Banned

- Registered: 2012-04-25
- Posts: 236
Re: xpath-Abfrage erweitern - um moeglichst mehr infomationen zu erhalten.
hallo - also deine idee harald klingt gut.
hab mal versucht den code ewas zu erweitern....
<?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/>";
}
?>
Z.b auch mit dem hier
list($website) = $school->xpath('tag[@k = "addr:website"]/@v');
werde am we noch weitermachen damit... hier.
http://wiki.openstreetmap.org/wiki/Tag:amenity%3Dschool
operator=* - Name of operator, often the local education authority.
addr=* - Address details
capacity=*, for the number of pupils taught at the school
isced:level=*, for the educational level (proposed tags)
fee=yes if the the school makes a direct charge for core services.
religion=*, if the school is associated with a particular religion (also denomination=*)
wikipedia=*, for a link to a Wikipedia article about the school
website=*, for a link to the school's own website
https://wiki.openstreetmap.org/wiki/User:Tagtheworld - interested in all things concerning osm especially those related to the languages Perl, PHP, Python and the databases MySQL and Postgresql.
Offline