4 Replies - 235 Views - Last Post: 31 January 2018 - 06:12 PM Rate Topic: -----

#1 sayhello   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 12-November 17

Query an OSM Overpass API Endpoint - with PHP runs into errors

Posted 31 January 2018 - 12:41 PM

hello dear experts - good day dear community,


a query an OSM Overpass API Endpoint runs into issues - i wonder why?


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




see what happens: with the Query an OSM Overpass API Endpoint_:




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

Warning: simplexml_load_file(): php_network_getaddresses: getaddrinfo failed: System error in /in/2HOpS on line 17 Warning: simplexml_load_file(http://overpass-api.de/api/interpreter): failed to open stream: php_network_getaddresses: getaddrinfo failed: System error in /in/2HOpS on line 17 Warning: simplexml_load_file(): I/O warning : failed to load external entity "http://overpass-api.de/api/interpreter&quot; in /in/2HOpS on line 17 Notice: Trying to get property 'node' of non-object in /in/2HOpS on line 18 Warning: count(): Parameter must be an array or an object that implements Countable in /in/2HOpS on line 18 Query returned 0 node(s) and took 0.00419 seconds.


this is awful.... isn t it?

Is This A Good Question/Topic? 0
  • +

Replies To: Query an OSM Overpass API Endpoint - with PHP runs into errors

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4211
  • View blog
  • Posts: 13,301
  • Joined: 08-June 10

Re: Query an OSM Overpass API Endpoint - with PHP runs into errors

Posted 31 January 2018 - 12:46 PM

Quote

this is awful.... isn t it?

nope. rather simple:

Quote

failed to open stream: php_network_getaddresses: getaddrinfo failed


PHP couldn't open the URL. judging from http://overpass-api....terpreter&quot; a bit later that reveals an invalid URL (& is only allowed in the query string, not in the URL path)
Was This Post Helpful? 1
  • +
  • -

#3 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2052
  • View blog
  • Posts: 6,297
  • Joined: 15-January 14

Re: Query an OSM Overpass API Endpoint - with PHP runs into errors

Posted 31 January 2018 - 12:55 PM

That sounds like a DNS error if getaddrinfo says it failed, that would be resolving the domain name.
Was This Post Helpful? 1
  • +
  • -

#4 sayhello   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 12-November 17

Re: Query an OSM Overpass API Endpoint - with PHP runs into errors

Posted 31 January 2018 - 04:57 PM

good evening dear ArtificialSoldier, Dormilich,



many many thanks for the quick reply. Great to hear from you.


i just entered the URL into the Browser: http://overpass-api.de/api/interpreter

The data included in this document is from www.openstreetmap.org. The data is made available under ODbL.
Error: encoding error: Your input contains only whitespace. 




Well i do some further investigations - and hope that i will find out more.


i come back to this thread and keep you informed.

greetings say
Was This Post Helpful? 0
  • +
  • -

#5 sayhello   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 12-November 17

Re: Query an OSM Overpass API Endpoint - with PHP runs into errors

Posted 31 January 2018 - 06:12 PM

hello dear both


did a further test - now it works for me.

linux-3645:/home/martin/dev/php # php o1.php
Query returned 2799 node(s) and took 4.50238 seconds.




now i want to do some parsing with xPath

see here:
<?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
//

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


//node[tag[@k = "amenity" and @v = "school"]]
//tag[@k = "name"]/@v'

$query = 'node
  ["addr:postcode"~"RM12"]
  (51.5557914,0.2118915,51.5673083,0.2369398);
   node
  (around:1000)
  ["amenity"~"fast_food"];
           out;';

$context = stream_context_create(['http' => [
    'method'  => 'POST',
    'header' => ['Content-Type: application/x-www-form-urlencoded'],
    'content' => 'data=' . urlencode($query),
]]);

$endpoint = 'http://overpass-api.de/api/interpreter';
libxml_set_streams_context($context);

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



see the results that i get back:


linux-3645:/home/martin/dev/php # php o1.php
Query returned 2799 node(s) and took 17.02055 seconds.

33 School(s) found:
#00: ID:332534486   [39.5018840,16.2722854]  Scuola Elementare
#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:2002566438  [39.1349460,16.0736446]  (unnamed)
#05: ID:2056891127  [39.4106679,16.8254844]  (unnamed)
#06: ID:2056892999  [39.4124687,16.8286119]  (unnamed)
#07: ID:2272010226  [39.4481717,16.2894353]  Scuola dell'infanzia San Francesco
#08: ID:2272017152  [39.4502366,16.2807664]  Scuola Media
#09: ID:2358307794  [39.5015031,16.3905965]  I.I.S.S. Liceo Statale V. Iulia
#10: ID:2358307796  [39.4926280,16.3853662]  Liceo Classico
#11: ID:2358307797  [39.4973761,16.3858275]  Scuola Media
#12: ID:2358307800  [39.5015527,16.3941156]  I.T.C. e per Geometri
#13: ID:2358307801  [39.4983862,16.3807796]  Istituto Professionale
#14: ID:2448031004  [38.6438417,16.3873106]  (unnamed)
#15: ID:2458139204  [39.0803263,17.1291649]  Sacro Cuore
#16: ID:2552412313  [39.0765212,17.1224610]  (unnamed)
#17: ID:2582443083  [39.0815417,17.1178983]  Liceo Socio Biologico Gravina
#18: ID:2585754364  [38.8878393,16.4076323]  Scuola Elementare
#19: ID:2585754366  [38.8877600,16.4076216]  Scuola Media
#20: ID:3071126720  [38.6022703,16.5554408]  Scuola Media
#21: ID:3071127683  [38.6027273,16.5563125]  Scuola Elementare
#22: ID:3081362915  [39.2865638,16.2601963]  Convitto Nazionale Bernardino Telesio
#23: ID:3081362921  [39.2856714,16.2613594]  Liceo Classico B. Telesio
#24: ID:3081362926  [39.2888949,16.2577446]  Scuola
#25: ID:3732551794  [39.5132435,16.2863285]  (unnamed)
#26: ID:3740289655  [39.5167318,16.2838146]  scuola media
#27: ID:3740289656  [39.5164344,16.2821103]  scuola elementare
#28: ID:4004532684  [38.7804787,16.5122952]  Liceo Artistico
#29: ID:4589289756  [38.6794209,16.1063084]  Scuola Comprensiva Trentacapilli
#30: ID:4843966477  [39.0709866,17.1288384]  Pegaso
#31: ID:5297629775  [38.5768845,16.3263536]  Scuola Media Statale "Ignazio La Russa"
#32: ID:5316865306  [39.0807997,17.1264225]  Enrico Fermi
Query returned 3 node(s) and took 17.44780 seconds.





so far so good - i need to do more tests with Query an OSM Overpass API Endpoint - with PHP

Many thanks for your help so far

greetings
Say hello ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1