File tree Expand file tree Collapse file tree 1 file changed +58
-0
lines changed Expand file tree Collapse file tree 1 file changed +58
-0
lines changed Original file line number Diff line number Diff line change 1+ import requests
2+ import json
3+
4+
5+ class API (object ):
6+ """A simple Python wrapper for the OpenStreetMap Overpass API"""
7+
8+ # defaults for the API class
9+ TIMEOUT = 25 # seconds
10+ ENDPOINT = "http://overpass-api.de/api/interpreter"
11+
12+ def __init__ (self ,
13+ endpoint = None ,
14+ timeout = None ,
15+ ):
16+ if endpoint is None :
17+ self .endpoint = self .ENDPOINT
18+ else :
19+ self .endpoint = endpoint
20+ if timeout is None :
21+ self .timeout = self .TIMEOUT
22+ else :
23+ self .timeout = timeout
24+ self .status = None
25+
26+ def Get (self , query ):
27+ """Pass in an Overpass query in Overpass XML or Overpass QL"""
28+
29+ payload = {"data" : query }
30+
31+ try :
32+ r = requests .get (self .endpoint , params = payload , timeout = self .timeout )
33+ except requests .exceptions .Timeout :
34+ return self ._ConstructError (
35+ 'Query timed out. API instance is set to time out in {timeout} seconds. '
36+ 'Try passing in a higher value when instantiating this API:'
37+ 'api = Overpass.API(timeout=60)' .format (timeout = self .timeout ))
38+
39+ self .status = r .status_code
40+
41+ if self .status != 200 :
42+ if self .status == 400 :
43+ return self ._ConstructError ('Query syntax error' )
44+ elif self .status == 500 :
45+ return self ._ConstructError ('Overpass internal server error' )
46+
47+ response = json .loads (r .text )
48+
49+ if "elements" not in response or len (response ["elements" ]) == 0 :
50+ return self ._ConstructError ('No OSM features satisfied your query' )
51+
52+ return response
53+
54+ def _ConstructError (self , msg ):
55+ return {
56+ "status" : self .status ,
57+ "message" : msg
58+ }
You can’t perform that action at this time.
0 commit comments