Jump to content

getting data via OSM Overpass API with XPath & PHP-SimpleXML


Recommended Posts

good evening dear php-freaks, 

 

hope you are all right. 


i am currently working on a parser that gets  data via OSM Overpass API with PHP-SimpleXML. I am workin with the pretty new to the work with PHPSimpleXML so do not bear with me for askin newby questions.  I am tryin to get data using Open Street Maps. I want to write a code that works with the endpoint of openstreetmap - and the given data is the starting point for further explorations.

My tactic is as follows:

 
I fetch data from the openstreetmap-endpoint 

1. i run requests to the openstreetmap-endpoint- (see below) and i try three different APIs per request isn't that easy on ressources and 

2. I don't know how to work with the results that i gather from the OpenStreetmap-Endpoint

 

as of the first point:  - workin on the endpoint of  OSM-Overpass-API.... see my approach: 


<?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
//

 

so far so good:  with this i get a bunch of data..... 


I have data in a file retrieved from a API pull request at the openstreetmap data source that looks something like this:

 

<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="Osmosis SNAPSHOT-r26564" xapi:planetDate="2014-10-07T15:42:02Z" xmlns:xapi="http://jxapi.openstreetmap.org/">
   <node id="251652819" version="6" timestamp="2013-10-19T14:03:10Z" uid="1198089" user="GeorgeKaplan" changeset="18434238" lat="48.8527413" lon="2.3333559">
     <tag k="dispensing" v="yes"/>
     <tag k="website" v="http://www.pharmacie-paris-citypharma.fr/"/>
     <tag k="name" v="Pharmacie Citypharma in downtown paris"/>
     <tag k="amenity" v="pharmacy"/>
  </node>
  <node id="251774849" version="9" timestamp="2014-07-28T21:28:26Z" uid="92075" user="Art Penteur" changeset="24411346" lat="48.8468043" lon="2.3696423">
     <tag k="phone" v="0143430996"/>
     <tag k="dispensing" v="yes"/>
     <tag k="name" v="Pharmacie de la Rapée in downtown paris"/>
     <tag k="amenity" v="pharmacy"/>
     <tag k="opening_hours" v="Mo-Fr 08:30-20:30; Sa 09:00-19:30"/>
 </node>
 <node id="263262912" version="3" timestamp="2014-07-28T21:37:24Z" uid="92075" user="Art Penteur" changeset="24411346" lat="48.8446917" lon="2.3101829">
     <tag k="dispensing" v="yes"/>
     <tag k="amenity" v="pharmacy"/>
 </node>


and so forth and so forth ....etc.

what is aimed: I am trying to filter the records based on the attributes that i find in the dataset,
 basically i am trying to retrieve the records that do not have a

    <tag k="name" v="blahblahblah"/> field.

 

I have been looking around and the classical way to do it its easy, however because of my poor skills in doing anything 
xml related i do have a couple of questions:

Is it possible to apply xml filtering for tags that look like this

<tag k="dispensing" v="yes"/> 

ones that do not have the

 <tag></tag>

.....format. Also how can i filter the records when each child of the node has different attributes,like v, 
i would knew how to do it if it would look like this

  <tag k="name">Pharmacie in Paris-downtown -  Rapée-metro-station</tag> 


but within this format received at this moment i cannot  not see a way out on how to get the children that i need

i have had a quick look at the PHP-SimpleXML and the XPath-approach: it may help me in achieving what is aimed.

 

but at the moment - i think i got stuck. 

 

i greatly appreciated hints and some ideas that may fit her. 

 

have a great day. -  yours dil_bert ;)
 

 

Edited by dil_bert
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.