Jump to content

how to retrieve datas from the endpoint of overpass-api


dil_bert

Recommended Posts

currently want to dive into php with some real world projects +

connecting to the endpoint of Overpass.

a list of comma seperated values - is this correct.... ?

i want to translate the following overopass-turbo requests into a request that i run against the endpoint of the overpass-api ... like so,

$endpoint = 'http://overpass-api.de/api/interpreter';

i want to retrieve the data that i get with the following term -- that i use with the overpass-turbo-access;:

[out:csv(::id,::type,"name","addr:postcode","addr:city","addr:street","addr:housenumber","website"," contact:email=*")][timeout:600];
{{geocodeArea:Schweiz}}->.a;
( node(area.a)[amenity=hospital];
  way(area.a)[amenity=hospital];
  rel(area.a)[amenity=hospital];
out;

so i have to port over the above mentioned code into the following approach:

# 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
 

 

any idea!? Look forward to hear from you

Link to comment
Share on other sites

hello dear ginerjm

 

 

many thanks for the answer - try to get some more insigths

 

The following is a little OSM Overpass API example with PHP SimpleXML - I love OSM, so let's show some useful examples.

The first part shows how you can query an Overpass Endpoint with standard PHP.

<?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));

well after the fetching the data the second part is also interesting.

That is querying the XML data.

Well - this is probably most easily done with xpath, the used PHP XML library are based on libxml which supports XPath 1.0 which covers the various querying needs very well.

The following example some - example - schools and tries to obtain their names as well.

//
// 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);
}

The key point here are the xpath queries. Two are used, the first one to get the nodes that have certain tags.


 

but - my question is: how to get the request on the endpoint in a better was ...

so that i can do this request in general - with all other options of a (general) requeast.

 

greetings

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.