diff --git a/overpass/api.py b/overpass/api.py index c368c68882e..72e422f29cd 100644 --- a/overpass/api.py +++ b/overpass/api.py @@ -1,7 +1,8 @@ import sys import requests import json -from shapely.geometry import Point +import geojson + class API(object): """A simple Python wrapper for the OpenStreetMap Overpass API""" @@ -69,10 +70,15 @@ def _ConstructQLQuery(self, userquery, asGeoJSON=False): if not raw_query.endswith(";"): raw_query += ";" - complete_query = self._QUERY_TEMPLATE.format( + if asGeoJSON: + template = self._GEOJSON_QUERY_TEMPLATE + else: + template = self._QUERY_TEMPLATE + + complete_query = template.format( responseformat=self.responseformat, - query=raw_query - ) + query=raw_query) + if self.debug: print(complete_query) return complete_query @@ -116,9 +122,9 @@ def _asGeoJSON(self, elements): features = [] for elem in elements: elem_type = elem["type"] - if elem["type"] == "node": - geometry=geojson.Point((elem["lon"], elem["lat"])) - elif elem["type"] == "way": + if elem_type == "node": + geometry = geojson.Point((elem["lon"], elem["lat"])) + elif elem_type == "way": points = [] for coords in elem["geometry"]: points.append((coords["lon"], coords["lat"])) @@ -127,13 +133,14 @@ def _asGeoJSON(self, elements): continue feature = geojson.Feature( - id=elem["id"], - geometry=geometry, - properties=elem.get("tags")) + id=elem["id"], + geometry=geometry, + properties=elem.get("tags")) features.append(feature) return geojson.FeatureCollection(features) + class OverpassException(Exception): def __init__(self, status_code, message): self.status_code = status_code diff --git a/test_api.py b/test_api.py index 2ed954a1832..9e277341157 100644 --- a/test_api.py +++ b/test_api.py @@ -1,4 +1,3 @@ -import geojson import overpass @@ -7,16 +6,16 @@ class TestAPI(object): def test_initialize_api(self): api = overpass.API() assert isinstance(api, overpass.API) - assert api.debug == False + assert api.debug is False def test_geojson(self): api = overpass.API() #osm_elements = api.Get(overpass.MapQuery(37.86517,-122.31851,37.86687,-122.31635)) #print 'DEB osm_elements:', geojson.dumps(osm_elements,sort_keys=True,indent=2) - osm_geo = api.Get(overpass.MapQuery(37.86517,-122.31851,37.86687,-122.31635), asGeoJSON=True) + osm_geo = api.Get(overpass.MapQuery(37.86517, -122.31851, 37.86687, -122.31635), asGeoJSON=True) #with open('test.geojson','w') as f: # geojson.dump(osm_geo,f,indent=2,sort_keys=True) - assert len(osm_geo['features'])>1 + assert len(osm_geo['features']) > 1 def run_tests(self): self.test_initialize_api() @@ -25,4 +24,4 @@ def run_tests(self): if __name__ == '__main__': tapi = TestAPI() tapi.run_tests() - print "overpass PASS" + print("overpass PASS")