I download Open Street Map data for a small region, I want to filter the data to get the nodes with special category.
Here is sample of the OSM data
<node id="505126369" lat="31.2933856" lon="34.2687443" user="JumpStart International" uid="125156" visible="true" version="1" changeset="2568758" timestamp="2009-09-22T13:05:10Z"/>
<node id="505126372" lat="31.2682934" lon="34.2745680" user="JumpStart International" uid="125156" visible="true" version="1" changeset="2568758" timestamp="2009-09-22T13:05:10Z"/>
<node id="505126375" lat="31.2953082" lon="34.3471630" user="JumpStart International" uid="125156" visible="true" version="1" changeset="2568758" timestamp="2009-09-22T13:05:10Z"/>
<node id="505126378" lat="31.2807872" lon="34.2757999" user="JumpStart International" uid="125156" visible="true" version="1" changeset="2568758" timestamp="2009-09-22T13:05:11Z">
<tag k="amenity" v="school"/>
<tag k="name" v="Al Aqqad Basic & Secondary Female School"/>
<tag k="name:ar" v="مدرسة العقاد الأساسية والثانوية للبنات"/>
</node>
I want to get the whole schools, hospitals in the data.
If anybody had made the XML parsing with PHP or Java, I would highly appreciate sharing it with me and all interests.
Edit Here is a simple start I have just
$dataFile = base_url() . 'media/files/osmdata/map_3.xml';
//echo ($dataFile);
$xml = simplexml_load_file($dataFile);
// $countTotal = count($xml->node);
// echo 'here'.$countTotal;
foreach ($xml as $key => $val) {
var_dump($val);
// can't manage things overs here
}
The following is a little OSM Overpass API example with PHP SimpleXML I've compiled because we do not have it here for PHP and I love OSM, so let's show some useful examples.
The first part shows how you can query an Overpass Endpoint with standard PHP. You do not need that part because you have already saved the data on your harddisk:
For you the second part is more interesting. That is querying the XML data you have already. 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. I have not covered translations yet because my sample data didn't have those, but you can also look for all kind of names including translations and just prefer a specific one):
The key point here are the xpath queries. Two are used, the first one to get the nodes that have certain tags. I think this is the most interesting one for you:
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". This is the condition you have to filter out those nodes that are tagged with amenity school.
Further on xpath is used again, now relative to those school nodes to see if there is a name and if so to fetch it:
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:
So here my results for that code-example:
I hope this is useful already, let me know if you have more clarification questions.