Skip to content

Commit

Permalink
merging pep8 changes from #19
Browse files Browse the repository at this point in the history
  • Loading branch information
mvexel committed Nov 25, 2014
1 parent e74316f commit f99f3fc
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 28 deletions.
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ $ python setup.py install

The module is not on PyPi yet.

If you get an error similar to

```
OSError: Could not find library geos_c or load any of its variants ['libgeos_c.so.1', 'libgeos_c.so']
```

you can install the required libraries on linux with
```
$ sudo apt-get install libgeos-c1 libgeos-3.4.2
```

## Use it

```python
Expand Down Expand Up @@ -53,13 +64,9 @@ api = overpass.API(timeout=600)

Setting this to `True` will get you debug output.

#### `bbox`

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.
In addition to just send your query and parse the result, the wrapper provides shortcuts for often used map queries. To use them, just pass them like to normal query to the API.

#### MapQuery

Expand Down
48 changes: 30 additions & 18 deletions overpass/api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys
import requests
import json
import geojson
from shapely.geometry import Point

class API(object):
"""A simple Python wrapper for the OpenStreetMap Overpass API"""
Expand All @@ -11,17 +11,18 @@ class API(object):
_endpoint = "http://overpass-api.de/api/interpreter"
_responseformat = "json"
_debug = False
_bbox = [-180.0, -90.0, 180.0, 90.0]

_QUERY_TEMPLATE = "[out:{responseformat}];{query}out body;"
_GEOJSON_QUERY_TEMPLATE = "[out:json];{query}out body geom;"

def __init__(self, *args, **kwargs):
self.endpoint = kwargs.get("endpoint", self._endpoint)
self.timeout = kwargs.get("timeout", self._timeout)
self.responseformat = kwargs.get("responseformat", self._responseformat)
self.responseformat = kwargs.get(
"responseformat",
self._responseformat
)
self.debug = kwargs.get("debug", self._debug)
self.bbox = kwargs.get("bbox", self._bbox)
self._status = None

if self.debug:
Expand All @@ -48,7 +49,10 @@ def Get(self, query, asGeoJSON=False):
sys.exit(1)

if "elements" not in response or len(response["elements"]) == 0:
raise OverpassException(204, 'No OSM features satisfied your query')
raise OverpassException(
204,
'No OSM features satisfied your query'
)

if not asGeoJSON:
return response
Expand All @@ -65,13 +69,10 @@ def _ConstructQLQuery(self, userquery, asGeoJSON=False):
if not raw_query.endswith(";"):
raw_query += ";"

if asGeoJSON:
template = self._GEOJSON_QUERY_TEMPLATE
else:
template = self._QUERY_TEMPLATE

complete_query = template.format(responseformat=self.responseformat, query=raw_query)

complete_query = self._QUERY_TEMPLATE.format(
responseformat=self.responseformat,
query=raw_query
)
if self.debug:
print(complete_query)
return complete_query
Expand All @@ -83,12 +84,20 @@ def _GetFromOverpass(self, query):
payload = {"data": query}

try:
r = requests.get(self.endpoint, params=payload, timeout=self.timeout)
r = requests.get(
self.endpoint,
params=payload,
timeout=self.timeout
)
except requests.exceptions.Timeout:
raise OverpassException(408,
'Query timed out. API instance is set to time out in {timeout} seconds. '
'Try passing in a higher value when instantiating this API:'
'api = Overpass.API(timeout=60)'.format(timeout=self.timeout))
raise OverpassException(
408,
'Query timed out. API instance is set to time out in {timeout}'
' seconds. Try passing in a higher value when instantiating'
' this API: api = Overpass.API(timeout=60)'.format(
timeout=self.timeout
)
)

self._status = r.status_code

Expand Down Expand Up @@ -131,4 +140,7 @@ def __init__(self, status_code, message):
self.message = message

def __str__(self):
return json.dumps({'status': self.status_code, 'message': self.message})
return json.dumps({
'status': self.status_code,
'message': self.message
})
17 changes: 13 additions & 4 deletions overpass/queries.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,27 @@ class MapQuery(object):
def __init__(self, south, west, north, east):
"""
Initialize query with given bounding box.
:param bbox Bounding box with limit values in format west, south, east, north.
:param bbox Bounding box with limit values in format west, south,
east, north.
"""
self.west = west
self.south = south
self.east = east
self.north = north

def __str__(self):
return self._QUERY_TEMPLATE.format(west=self.west, south=self.south, east=self.east, north=self.north)
return self._QUERY_TEMPLATE.format(
west=self.west,
south=self.south,
east=self.east,
north=self.north
)


class WayQuery(object):

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

_QUERY_TEMPLATE = "(way{query_parameters});(._;>;);"

Expand All @@ -35,4 +42,6 @@ def __init__(self, query_parameters):
self.query_parameters = query_parameters

def __str__(self):
return self._QUERY_TEMPLATE.format(query_parameters=self.query_parameters)
return self._QUERY_TEMPLATE.format(
query_parameters=self.query_parameters
)
3 changes: 2 additions & 1 deletion test_api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import geojson
import overpass

class TestAPI:

class TestAPI(object):

def test_initialize_api(self):
api = overpass.API()
Expand Down

0 comments on commit f99f3fc

Please sign in to comment.