howdy halojoy - good evening,
halojoy;11064283 wrote:I dont see much php-code in your posted script.
But this is probably what you want from us here ... š
gardez : this runs like a charme
<?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);
}
//node[tag[@k = "amenity" and @v = "school"]]
//tag[@k = "name"]/@v'
$query = 'node
["addr:postcode"~"RM12"]
(51.5557914,0.2118915,51.5673083,0.2369398);
node
(around:1000)
["amenity"~"fast_food"];
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));
and gives back the results
me/martin/dev/php/o1.php on line 68
linux-3645:/home/martin/dev/php # php o1.php
Query returned 2799 node(s) and took 17.02055 seconds.
33 School(s) found:
#00: ID:332534486 [39.5018840,16.2722854] Scuola Elementare
#01: ID:1428094278 [39.3320912,16.1862820] (unnamed)
#02: ID:1822746784 [38.9075566,16.5776597] (unnamed)
#03: ID:1822755951 [38.9120272,16.5713431] (unnamed)
#04: ID:2002566438 [39.1349460,16.0736446] (unnamed)
#05: ID:2056891127 [39.4106679,16.8254844] (unnamed)
#06: ID:2056892999 [39.4124687,16.8286119] (unnamed)
#07: ID:2272010226 [39.4481717,16.2894353] Scuola dell'infanzia San Francesco
#08: ID:2272017152 [39.4502366,16.2807664] Scuola Media
#09: ID:2358307794 [39.5015031,16.3905965] I.I.S.S. Liceo Statale V. Iulia
#10: ID:2358307796 [39.4926280,16.3853662] Liceo Classico
#11: ID:2358307797 [39.4973761,16.3858275] Scuola Media
#12: ID:2358307800 [39.5015527,16.3941156] I.T.C. e per Geometri
#13: ID:2358307801 [39.4983862,16.3807796] Istituto Professionale
#14: ID:2448031004 [38.6438417,16.3873106] (unnamed)
[....]
#31: ID:5297629775 [38.5768845,16.3263536] Scuola Media Statale "Ignazio La Russa"
#32: ID:5316865306 [39.0807997,17.1264225] Enrico Fermi
Query returned 3 node(s) and took 17.44780 seconds.
so far so good: - the only thing i wonder is why i get errors when doing minor changes
# 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);
}
if i do some additoinal lines here
list($name) = $school->xpath('tag[@k = "contact:website"]/@v');
list($name) = $school->xpath('tag[@k = "contact:email"]/@v');
into the Foreach-Loop - so that it looks like so:
{
# Get the name of the school (if any), again with xpath
list($name) = $school->xpath('tag[@k = "name"]/@v') + ['(unnamed)'];
list($name) = $school->xpath('tag[@k = "contact:website"]/@v');
list($name) = $school->xpath('tag[@k = "contact:email"]/@v');
printf("#%02d: ID:%' -10s [%s,%s] %s\n", $index, $school['id'], $school['lat'], $school['lon'], $name);
}
all this is entered into the second part of the code - the one that works with the XML-results
//
// 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)'];
list($name) = $school->xpath('tag[@k = "contact:website"]/@v');
list($name) = $school->xpath('tag[@k = "contact:email"]/@v');
printf("#%02d: ID:%' -10s [%s,%s] %s\n", $index, $school['id'], $school['lat'], $school['lon'], $name);
}
what is wanted here: i wanted to add /(or let me say to gain) some more infos eg Key:contact
contact:phone
contact:fax
contact:website
contact:email
it is aimed to extend the xpath requests in the Foreach-Loop relatively to the Schools-Nodes
tag[@k = "name"]/@v'
tag[@k = "contact:website"]/@v'
tag[@k = "contact:email"]/@v'
but if i do so - and have this foreach loop
//
// 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)'];
list($name) = $school->xpath('tag[@k = "contact:website"]/@v');
list($name) = $school->xpath('tag[@k = "contact:email"]/@v');
printf("#%02d: ID:%' -10s [%s,%s] %s\n", $index, $school['id'], $school['lat'], $school['lon'], $name);
}
i get the errors
linux-3645:/home/martin/dev/php # php o2.php
Query returned 2799 node(s) and took 6.22443 seconds.
33 School(s) found:
PHP Notice: Undefined offset: 0 in /home/martin/dev/php/o2.php on line 42
PHP Notice: Undefined offset: 0 in /home/martin/dev/php/o2.php on line 43
#00: ID:332534486 [39.5018840,16.2722854]
PHP Notice: Undefined offset: 0 in /home/martin/dev/php/o2.php on line 42
PHP Notice: Undefined offset: 0 in /home/martin/dev/php/o2.php on line 43
#01: ID:1428094278 [39.3320912,16.1862820]
PHP Notice: Undefined offset: 0 in /home/martin/dev/php/o2.php on line 42
PHP Notice: Undefined offset: 0 in /home/martin/dev/php/o2.php on line 43
#02: ID:1822746784 [38.9075566,16.5776597]
Well - serious errors : probably caused by some issues with the decoding and unwrapping
list($a, $b, $cā¦) = $array
we decode the content of the $array into the variables - wich is a short form for
$a = $array[0]; $b = $array[1]; $c = $array[2]; ā¦
in some cases i unfortunatly do not have any Tags with the contact:website or contact:email.
so - we can work aroound this
1 choose other variables that $name - otherwise we overwrite the gained infos
2.use the trick to attach at the Array a Default string
Because not all school nodes have a name, a default string is provided for display purposes by adding it to the (then empty) result array:
list($name) = $school->xpath('tag[@k = "name"]/@v') + ['(unnamed)'];
^^^^^^^^^^^^^^^
Provide Default Value
so here we can do like so:
# Get the name of the school (if any), again with xpath
list($name) = $school->xpath('tag[@k = "name"]/@v') + ['(unnamed)'];
list($website) = $school->xpath('tag[@k = "contact:website"]/@v') + ['(no website)'];
list($email) = $school->xpath('tag[@k = "contact:email"]/@v') + ['(no email)'];
printf("#%02d: ID:%' -10s [%s,%s] %s %s %s\n", $index, $school['id'], $school['lat'], $school