Skip to content

Commit 8602154

Browse files
committed
adding exception class
1 parent f7e8612 commit 8602154

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

overpass/api.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
import requests
23
import json
34
from 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})

overpass/queries.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,27 @@ class MapQuery(object):
55

66
"""Query to retrieve complete ways and relations in an area."""
77

8-
_QUERY_TEMPLATE = "(node({bbox[0]},{bbox[1]},{bbox[2]},{bbox[2]});<;);"
8+
_QUERY_TEMPLATE = "(node({south},{west},{north},{east});<;);"
99

10-
def __init__(self, bbox):
10+
def __init__(self, south, west, north, east):
1111
"""
1212
Initialize query with given bounding box.
13-
:param bbox Bounding box with limit values in format (s, w, n, e) in a sequence.
13+
:param bbox Bounding box with limit values in format west, south, east, north.
1414
"""
15-
self.bbox = bbox
15+
self.west = west
16+
self.south = south
17+
self.east = east
18+
self.north = north
1619

1720
def __str__(self):
18-
return self._QUERY_TEMPLATE.format(bbox=self.bbox)
21+
return self._QUERY_TEMPLATE.format(west=self.west, south=self.south, east=self.east, north=self.north)
1922

2023

2124
class WayQuery(object):
2225

2326
"""Query to retrieve a set of ways and their dependent nodes satisfying the input parameters"""
2427

25-
_QUERY_TEMPLATE = "(way{query_parameters};(_.;>;););"
28+
_QUERY_TEMPLATE = "(way{query_parameters});(._;>;);"
2629

2730
def __init__(self, query_parameters):
2831
"""

0 commit comments

Comments
 (0)