diff --git a/.gitignore b/.gitignore index e7817f364fd..803a5409b8e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,8 @@ +*~ +*.swp +.DS_Store *.pyc -build -dist -overpass.egg-info \ No newline at end of file + +/build/ +/dist/ +/*.egg-info diff --git a/overpass/api.py b/overpass/api.py index a2f244c7c9a..909f0aa5803 100644 --- a/overpass/api.py +++ b/overpass/api.py @@ -1,8 +1,7 @@ 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""" @@ -15,6 +14,7 @@ class API(object): _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) @@ -42,7 +42,7 @@ def Get(self, query, asGeoJSON=False): try: response = json.loads(self._GetFromOverpass( - self._ConstructQLQuery(query))) + self._ConstructQLQuery(query, asGeoJSON=asGeoJSON))) except OverpassException as oe: print(oe) sys.exit(1) @@ -60,12 +60,18 @@ def Search(self, feature_type, regex=False): """Search for something.""" pass - def _ConstructQLQuery(self, userquery): + def _ConstructQLQuery(self, userquery, asGeoJSON=False): raw_query = str(userquery) if not raw_query.endswith(";"): raw_query += ";" - complete_query = self._QUERY_TEMPLATE.format(responseformat=self.responseformat, query=raw_query) + if asGeoJSON: + template = self._GEOJSON_QUERY_TEMPLATE + else: + template = self._QUERY_TEMPLATE + + complete_query = template.format(responseformat=self.responseformat, query=raw_query) + if self.debug: print(complete_query) return complete_query @@ -96,20 +102,28 @@ def _GetFromOverpass(self, query): return r.text def _asGeoJSON(self, elements): - """construct geoJSON from elements""" - nodes = [{ - "id": elem.get("id"), - "tags": elem.get("tags"), - "geom": Point(elem["lon"], elem["lat"])} - for elem in elements if elem["type"] == "node"] - ways = [{ - "id": elem.get("id"), - "tags": elem.get("tags"), - "nodes": elem.get("nodes")} - for elem in elements if elem["type"] == "way"] - print(nodes) - print(ways) + #print 'DEB _asGeoJson elements:', 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": + points = [] + for coords in elem["geometry"]: + points.append((coords["lon"], coords["lat"])) + geometry = geojson.LineString(points) + else: + continue + + feature = geojson.Feature( + 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): diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index 44e68c921ec..00000000000 --- a/requirements.txt +++ /dev/null @@ -1 +0,0 @@ -requests>=2.3.0 \ No newline at end of file diff --git a/setup.py b/setup.py index 941fd05a30e..00169660516 100644 --- a/setup.py +++ b/setup.py @@ -11,5 +11,5 @@ download_url='https://github.com/mvexel/overpass-api-python-wrapper/tarball/0.0.1', keywords=['openstreetmap', 'overpass', 'wrapper'], classifiers=[], - install_requires=['requests>=2.3.0', 'shapely>=1.4.3'], + install_requires=['requests>=2.3.0', 'geojson>=1.0.9'], ) diff --git a/test_api.py b/test_api.py index 2154f6c8f62..4b0ee7b6e0a 100644 --- a/test_api.py +++ b/test_api.py @@ -1,9 +1,27 @@ +import geojson import overpass - class TestAPI: def test_initialize_api(self): api = overpass.API() assert isinstance(api, overpass.API) - assert api.debug == False \ No newline at end of file + assert api.debug == 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) + #with open('test.geojson','w') as f: + # geojson.dump(osm_geo,f,indent=2,sort_keys=True) + assert len(osm_geo['features'])>1 + + def run_tests(self): + self.test_initialize_api() + self.test_geojson() + +if __name__ == '__main__': + tapi = TestAPI() + tapi.run_tests() + print "overpass PASS"