Skip to content

Commit 17cfdf8

Browse files
committed
Adding debugging (fixes #2) and don't require full QL syntax (fixes #1)
1 parent fa4ae00 commit 17cfdf8

File tree

2 files changed

+54
-8
lines changed

2 files changed

+54
-8
lines changed

Overpass.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ class API(object):
88
# defaults for the API class
99
TIMEOUT = 25 # seconds
1010
ENDPOINT = "http://overpass-api.de/api/interpreter"
11+
DEBUG = False
1112

1213
def __init__(self,
1314
endpoint=None,
1415
timeout=None,
16+
debug=None
1517
):
1618
if endpoint is None:
1719
self.endpoint = self.ENDPOINT
@@ -21,12 +23,27 @@ def __init__(self,
2123
self.timeout = self.TIMEOUT
2224
else:
2325
self.timeout = timeout
24-
self.status = None
26+
if debug is None:
27+
self.debug = self.DEBUG
28+
else:
29+
self.debug = debug
30+
self._status = None
31+
32+
if self.debug:
33+
import httplib
34+
import logging
35+
httplib.HTTPConnection.debuglevel = 1
36+
37+
logging.basicConfig()
38+
logging.getLogger().setLevel(logging.DEBUG)
39+
requests_log = logging.getLogger("requests.packages.urllib3")
40+
requests_log.setLevel(logging.DEBUG)
41+
requests_log.propagate = True
2542

2643
def Get(self, query):
2744
"""Pass in an Overpass query in Overpass XML or Overpass QL"""
2845

29-
payload = {"data": query}
46+
payload = {"data": self._ConstructQLQuery(query)}
3047

3148
try:
3249
r = requests.get(self.endpoint, params=payload, timeout=self.timeout)
@@ -36,12 +53,12 @@ def Get(self, query):
3653
'Try passing in a higher value when instantiating this API:'
3754
'api = Overpass.API(timeout=60)'.format(timeout=self.timeout))
3855

39-
self.status = r.status_code
56+
self._status = r.status_code
4057

41-
if self.status != 200:
42-
if self.status == 400:
58+
if self._status != 200:
59+
if self._status == 400:
4360
return self._ConstructError('Query syntax error')
44-
elif self.status == 500:
61+
elif self._status == 500:
4562
return self._ConstructError('Overpass internal server error')
4663

4764
response = json.loads(r.text)
@@ -53,6 +70,13 @@ def Get(self, query):
5370

5471
def _ConstructError(self, msg):
5572
return {
56-
"status": self.status,
73+
"status": self._status,
5774
"message": msg
5875
}
76+
77+
def _ConstructQLQuery(self, userquery):
78+
if self.debug:
79+
print "[out:json];" + userquery + "out body;"
80+
if not userquery.endswith(";"):
81+
userquery += ";"
82+
return "[out:json];" + userquery + "out body;"

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,38 @@ This is a thin wrapper around the OpenStreetMap [Overpass API](http://wiki.opens
77
```
88
>>> import Overpass
99
>>> api = Overpass.API()
10-
>>> response = api.Get('[out:json];node["name":"Salt Lake City"];out body;')
10+
>>> response = api.Get('node["name":"Salt Lake City"]')
1111
```
1212

13+
Note that you don't have to include any of the output meta statements. The wrapper will, well, wrap those.
14+
1315
You will get your result as a dictionary, which (for now) represents the JSON output you would get [from the Overpass API directly](http://overpass-api.de/output_formats.html#json). So you could do this for example:
1416

1517
```
1618
>>> print [(feature['tags']['name'], feature['id']) for feature in response['elements']]
1719
[(u'Salt Lake City', 150935219), (u'Salt Lake City', 585370637), (u'Salt Lake City', 1615721573)]
1820
```
1921

22+
### Parameters
23+
24+
The API takes a few parameters:
25+
26+
#### `endpoint`
27+
28+
The default endpoint is `http://overpass-api.de/api/interpreter` but you can pass in the rambler instance (`http://overpass.osm.rambler.ru/cgi/interpreter`) or your own:
29+
30+
api = Overpass.API(endpoint=http://overpass.myserver/interpreter)
31+
32+
#### `timeout`
33+
34+
The default timeout is 25 seconds, but you can set it to whatever you want.
35+
36+
api = Overpass.API(timeout=600)
37+
38+
#### `debug`
39+
40+
Setting this to `True` will get you debug output.
41+
2042
## Fork it
2143

2244
[Yes please](https://github.com/mvexel/overpass-api-python-wrapper/fork).

0 commit comments

Comments
 (0)