good evening dear commuinty,
I am new to PHP's SimpleXML. i want to work with SimpleXML on OSM-files.
The original version of this question was derived from here: OSM Data parsing to get the nodes with child https://stackoverflow.com/questions/16129184/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 can work on the methods using SimpleXML and Xpath; The job is most easily done with xpath, the used PHP XML library is based on libxml which supports XPath 1.0 which covers the various querying needs very well.
goal: how to get more out of it: 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. Finally 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 SimpleXML
should this be added in this part!?
# 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);
}
since i am learning - i break down the code into pieces...For my question, the second part is more interesting here.
That is querying the XML data we have already. Again - as mentioned above: This is most easily done with xpath, the used PHP XML library is based on libxml which supports XPath 1.0 which covers the various querying needs very well. The following example lists all schools and tries to obtain their names as well.
# 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);
}
The key point here are the xpath queries: Two are used, the first one to get the nodes that have certain tags.
//node[tag[@k = "amenity" and @v = "school"]]
This line says: Give me all node elements that have a tag element inside which has the k attribute value "amenity" and the v attribute value "school". Explanation: This is the condition we have to filter out those nodes that are tagged with amenity school.
Further on xpath is used again - a second time: now relative to those school nodes to see if there is a name and if so to fetch it: Therefore we use the foreach-syntax:
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);
}
and
tag[@k = "name"]/@v'
= $school->xpath('tag[@k = "name"]/@v') + ['(unnamed)'];
and this is pretty important
tag[@k = "name"]/@v'
This line says: Relative to the current node, give me the v attribute from a tag element that as the k attribute value "name". As you can see, some parts are again similar to the line before. I think you can both adopt them to your needs.
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 some of the results for that code-example:
Query returned 907 node(s) and took 1.10735 seconds.
more than 2000 School(s) found:
#00: ID:332534486 [39.5017565,16.2721899] Scuola Primaria
#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:1903859699 [38.6830409,16.5522243] Liceo Scientifico Statale A. Guarasci
#05: ID:2002566438 [39.1347698,16.0736924] (unnamed)
#06: ID:2056891127 [39.4106679,16.8254844] (unnamed)
#07: ID:2056892999 [39.4124687,16.8286119] (unnamed)
#08: ID:2272010226 [39.4481717,16.2894353] SCUOLA DELL'INFANZIA SAN FRANCESCO
#09: ID:2272017152 [39.4502366,16.2807664] SCUOLA MEDIA
and now i try to figure out how i can enter more xpath queries at the above mentioned code
goal: to get out even more important data - see here Key:contact - OpenStreetMap Wiki
contact:phone
contact:fax
contact:website
contact:email
Well - we are already extracting the name: If we want to have more data then we just have to run a few more xpath queries inside our loop for all the address keys and the website. So - additionally: we do not have to forget to look for the website key additional to contact:website. cf: https://wiki.openstreetmap.org/wiki/Key:website
conclusio: well - i think that i need to extend the xpath requests within the loop where xpath is used again, now relative to those school nodes to see if there is a name and if so to fetch it:
tag[@k = "name"]/@v'
tag[@k = "contact:website"]/@v'
tag[@k = "contact:email"]/@v'
What do you say...?
many many thanks to you for all your help.
your dilbert 2010