1+ import sys
12import requests
23import json
34from shapely .geometry import mapping , Point
@@ -37,12 +38,17 @@ def __init__(self, *args, **kwargs):
3738 def Get (self , query , asGeoJSON = False ):
3839 """Pass in an Overpass query in Overpass QL"""
3940
40- response = json .loads (
41- self ._GetFromOverpass (
41+ response = ""
42+
43+ try :
44+ response = json .loads (self ._GetFromOverpass (
4245 self ._ConstructQLQuery (query )))
46+ except OverpassException as oe :
47+ print oe
48+ sys .exit (1 )
4349
4450 if "elements" not in response or len (response ["elements" ]) == 0 :
45- return self . _ConstructError ( 'No OSM features satisfied your query' )
51+ raise OverpassException ( 204 , 'No OSM features satisfied your query' )
4652
4753 if not asGeoJSON :
4854 return response
@@ -54,12 +60,6 @@ def Search(self, feature_type, regex=False):
5460 """Search for something."""
5561 pass
5662
57- def _ConstructError (self , msg ):
58- return {
59- "status" : self ._status ,
60- "message" : msg
61- }
62-
6363 def _ConstructQLQuery (self , userquery ):
6464 raw_query = str (userquery )
6565 if not raw_query .endswith (";" ):
@@ -79,7 +79,7 @@ def _GetFromOverpass(self, query):
7979 try :
8080 r = requests .get (self .endpoint , params = payload , timeout = self .timeout )
8181 except requests .exceptions .Timeout :
82- return self . _ConstructError (
82+ raise OverpassException ( 408 ,
8383 'Query timed out. API instance is set to time out in {timeout} seconds. '
8484 'Try passing in a higher value when instantiating this API:'
8585 'api = Overpass.API(timeout=60)' .format (timeout = self .timeout ))
@@ -88,13 +88,12 @@ def _GetFromOverpass(self, query):
8888
8989 if self ._status != 200 :
9090 if self ._status == 400 :
91- return self ._ConstructError ('Query syntax error' )
92- elif self ._status == 500 :
93- return self ._ConstructError ('Overpass internal server error' )
91+ message = 'Query syntax error'
9492 else :
95- return self ._ConstructError ('Something unexpected happened' )
96-
97- return r .text
93+ message = 'Error from Overpass API'
94+ raise OverpassException (self ._status , message )
95+ else :
96+ return r .text
9897
9998 def _asGeoJSON (self , elements ):
10099 """construct geoJSON from elements"""
@@ -110,3 +109,10 @@ def _asGeoJSON(self, elements):
110109 for elem in elements if elem ["type" ] == "way" ]
111110 print nodes
112111 print ways
112+
113+ class OverpassException (Exception ):
114+ def __init__ (self , status_code , message ):
115+ self .status_code = status_code
116+ self .message = message
117+ def __str__ (self ):
118+ return json .dumps ({'status' : self .status_code , 'message' : self .message })
0 commit comments