Skip to content

Commit 2dd725f

Browse files
committed
Merge pull request #13 from itiboi/feature/queries
Simple queries
2 parents 8ffa755 + 4184783 commit 2dd725f

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ Setting this to `True` will get you debug output.
5555

5656
This takes a list in the form `[minlon, minlat, maxlon, maxlat]`, the default is the world: `[-180.0, -90.0, 180.0, 90.0]`
5757

58+
### Simple queries
59+
60+
In addition to just send you query and parse it, the wrapper provides shortcuts for often used map queries. To use them, just pass them like to normal query to the API.
61+
62+
#### MapQuery
63+
64+
Up to now, only a query for [complete ways and relations](http://wiki.openstreetmap.org/wiki/Overpass_API/Language_Guide#Completed_ways_and_relations) in a bounding box is supported.
65+
You just pass the bounding box to the constructor:
66+
67+
```python
68+
>>> map_query = overpass.MapQuery((50.746,7.154,50.748,7.157))
69+
>>> response = api.Get(map_query)
70+
```
71+
5872
## Need help? Want feature?
5973

6074
Create a [new issue](https://github.com/mvexel/overpass-api-python-wrapper/issues).

overpass/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
__version__ = '0.0.1'
77
__license__ = 'Apache 2.0'
88

9-
from .api import API
9+
from .api import API
10+
from .queries import MapQuery

overpass/api.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class API(object):
1313
_debug = False
1414
_bbox = [-180.0, -90.0, 180.0, 90.0]
1515

16+
_QUERY_TEMPLATE = "[out:{responseformat}];{query}out body;"
17+
1618
def __init__(self, *args, **kwargs):
1719
self.endpoint = kwargs.get("endpoint", self._endpoint)
1820
self.timeout = kwargs.get("timeout", self._timeout)
@@ -59,12 +61,14 @@ def _ConstructError(self, msg):
5961
}
6062

6163
def _ConstructQLQuery(self, userquery):
62-
if not userquery.endswith(";"):
63-
userquery += ";"
64-
fullquery = "[out:{responseformat}];".format(responseformat=self.responseformat) + userquery + "out body;"
64+
raw_query = str(userquery)
65+
if not raw_query.endswith(";"):
66+
raw_query += ";"
67+
68+
complete_query = self._QUERY_TEMPLATE.format(responseformat=self.responseformat, query=raw_query)
6569
if self.debug:
66-
print fullquery
67-
return "[out:{responseformat}];".format(responseformat=self.responseformat) + userquery + "out body;"
70+
print complete_query
71+
return complete_query
6872

6973
def _GetFromOverpass(self, query):
7074
"""This sends the API request to the Overpass instance and

overpass/queries.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- coding: utf-8 -*-
2+
3+
4+
class MapQuery(object):
5+
"""Query to retrieve complete ways and relations in an area."""
6+
7+
_QUERY_TEMPLATE = "(node({bbox[0]},{bbox[1]},{bbox[2]},{bbox[2]});<;);"
8+
9+
def __init__(self, bbox):
10+
"""
11+
Initialize query with given bounding box.
12+
:param bbox Bounding box with limit values in format (s, w, n, e) in a sequence.
13+
"""
14+
self.bbox = bbox
15+
16+
def __str__(self):
17+
return self._QUERY_TEMPLATE.format(bbox=self.bbox)

0 commit comments

Comments
 (0)