I am new to PHP's SimpleXML.
The original version of this question was derived from here: OSM Data parsing to get the nodes with child
I am thankful that hakre offered a great example in the comments that makes a overwhelming starting point for my project. Below I have added my own answer to the question, how to refine the code to ad more tags.
I want to filter the data to get the nodes with special category. Here is sample of the OSM data I want to get the whole schools within an area. The first script runs well - but now I want to refine the search and add more tags. I want to store all into MySQL.
So we need to make some XML parsing with PHP:
The following is a little OSM Overpass API example with PHP SimpleXM
how to get more out of it.. at least the adress and the website
where to add those tags in the request`?
in this line:
in advance i thank you for any and all help
The original version of this question was derived from here: OSM Data parsing to get the nodes with child
I am thankful that hakre offered a great example in the comments that makes a overwhelming starting point for my project. Below I have added my own answer to the question, how to refine the code to ad more tags.
I want to filter the data to get the nodes with special category. Here is sample of the OSM data I want to get the whole schools within an area. The first script runs well - but now I want to refine the search and add more tags. I want to store all into MySQL.
So we need to make some XML parsing with PHP:
The following is a little OSM Overpass API example with PHP SimpleXM
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
["addr:postcode"~"RM12"]
(51.5557914,0.2118915,51.5673083,0.2369398)->.point;
(
node
(around.point:100000)
["amenity"~"school"];
way
(around.point:100000)
["amenity"~"school"];
);
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);
}
?>
how to get more out of it.. at least the adress and the website
where to add those tags in the request`?
in this line:
PHP Code:
list($name) = $school->xpath('tag[@k = "name"]/@v') + ['(unnamed)'];