diff --git a/README.md b/README.md index a70170195bd..f34f8bd9677 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,20 @@ Setting this to `True` will get you debug output. This takes a list in the form `[minlon, minlat, maxlon, maxlat]`, the default is the world: `[-180.0, -90.0, 180.0, 90.0]` +### Simple queries + +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. + +#### MapQuery + +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. +You just pass the bounding box to the constructor: + +```python +>>> map_query = overpass.MapQuery((50.746,7.154,50.748,7.157)) +>>> response = api.Get(map_query) +``` + ## Need help? Want feature? Create a [new issue](https://github.com/mvexel/overpass-api-python-wrapper/issues). diff --git a/overpass/__init__.py b/overpass/__init__.py index ff16d78e66c..744a7e0ba9e 100644 --- a/overpass/__init__.py +++ b/overpass/__init__.py @@ -6,4 +6,5 @@ __version__ = '0.0.1' __license__ = 'Apache 2.0' -from .api import API \ No newline at end of file +from .api import API +from .queries import MapQuery diff --git a/overpass/api.py b/overpass/api.py index 6eecd6649ea..b94d0bbc2f5 100644 --- a/overpass/api.py +++ b/overpass/api.py @@ -13,6 +13,8 @@ class API(object): _debug = False _bbox = [-180.0, -90.0, 180.0, 90.0] + _QUERY_TEMPLATE = "[out:{responseformat}];{query}out body;" + def __init__(self, *args, **kwargs): self.endpoint = kwargs.get("endpoint", self._endpoint) self.timeout = kwargs.get("timeout", self._timeout) @@ -59,12 +61,14 @@ def _ConstructError(self, msg): } def _ConstructQLQuery(self, userquery): - if not userquery.endswith(";"): - userquery += ";" - fullquery = "[out:{responseformat}];".format(responseformat=self.responseformat) + userquery + "out body;" + raw_query = str(userquery) + if not raw_query.endswith(";"): + raw_query += ";" + + complete_query = self._QUERY_TEMPLATE.format(responseformat=self.responseformat, query=raw_query) if self.debug: - print fullquery - return "[out:{responseformat}];".format(responseformat=self.responseformat) + userquery + "out body;" + print complete_query + return complete_query def _GetFromOverpass(self, query): """This sends the API request to the Overpass instance and diff --git a/overpass/queries.py b/overpass/queries.py new file mode 100755 index 00000000000..19cb0dc43d6 --- /dev/null +++ b/overpass/queries.py @@ -0,0 +1,17 @@ +# -*- coding: utf-8 -*- + + +class MapQuery(object): + """Query to retrieve complete ways and relations in an area.""" + + _QUERY_TEMPLATE = "(node({bbox[0]},{bbox[1]},{bbox[2]},{bbox[2]});<;);" + + def __init__(self, bbox): + """ + Initialize query with given bounding box. + :param bbox Bounding box with limit values in format (s, w, n, e) in a sequence. + """ + self.bbox = bbox + + def __str__(self): + return self._QUERY_TEMPLATE.format(bbox=self.bbox) \ No newline at end of file