Skip to content
This repository was archived by the owner on Jun 10, 2025. It is now read-only.

Commit e74316f

Browse files
committed
merging @mharnold s changes
2 parents 4f6e46a + 20c1582 commit e74316f

File tree

5 files changed

+60
-25
lines changed

5 files changed

+60
-25
lines changed

.gitignore

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
*~
2+
*.swp
3+
.DS_Store
14
*.pyc
2-
build
3-
dist
4-
overpass.egg-info
5+
6+
/build/
7+
/dist/
8+
/*.egg-info

overpass/api.py

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import sys
22
import requests
33
import json
4-
from shapely.geometry import Point
5-
4+
import geojson
65

76
class API(object):
87
"""A simple Python wrapper for the OpenStreetMap Overpass API"""
@@ -15,6 +14,7 @@ class API(object):
1514
_bbox = [-180.0, -90.0, 180.0, 90.0]
1615

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

1919
def __init__(self, *args, **kwargs):
2020
self.endpoint = kwargs.get("endpoint", self._endpoint)
@@ -42,7 +42,7 @@ def Get(self, query, asGeoJSON=False):
4242

4343
try:
4444
response = json.loads(self._GetFromOverpass(
45-
self._ConstructQLQuery(query)))
45+
self._ConstructQLQuery(query, asGeoJSON=asGeoJSON)))
4646
except OverpassException as oe:
4747
print(oe)
4848
sys.exit(1)
@@ -60,12 +60,18 @@ def Search(self, feature_type, regex=False):
6060
"""Search for something."""
6161
pass
6262

63-
def _ConstructQLQuery(self, userquery):
63+
def _ConstructQLQuery(self, userquery, asGeoJSON=False):
6464
raw_query = str(userquery)
6565
if not raw_query.endswith(";"):
6666
raw_query += ";"
6767

68-
complete_query = self._QUERY_TEMPLATE.format(responseformat=self.responseformat, query=raw_query)
68+
if asGeoJSON:
69+
template = self._GEOJSON_QUERY_TEMPLATE
70+
else:
71+
template = self._QUERY_TEMPLATE
72+
73+
complete_query = template.format(responseformat=self.responseformat, query=raw_query)
74+
6975
if self.debug:
7076
print(complete_query)
7177
return complete_query
@@ -96,20 +102,28 @@ def _GetFromOverpass(self, query):
96102
return r.text
97103

98104
def _asGeoJSON(self, elements):
99-
"""construct geoJSON from elements"""
100-
nodes = [{
101-
"id": elem.get("id"),
102-
"tags": elem.get("tags"),
103-
"geom": Point(elem["lon"], elem["lat"])}
104-
for elem in elements if elem["type"] == "node"]
105-
ways = [{
106-
"id": elem.get("id"),
107-
"tags": elem.get("tags"),
108-
"nodes": elem.get("nodes")}
109-
for elem in elements if elem["type"] == "way"]
110-
print(nodes)
111-
print(ways)
105+
#print 'DEB _asGeoJson elements:', elements
106+
107+
features = []
108+
for elem in elements:
109+
elem_type = elem["type"]
110+
if elem["type"] == "node":
111+
geometry=geojson.Point((elem["lon"], elem["lat"]))
112+
elif elem["type"] == "way":
113+
points = []
114+
for coords in elem["geometry"]:
115+
points.append((coords["lon"], coords["lat"]))
116+
geometry = geojson.LineString(points)
117+
else:
118+
continue
119+
120+
feature = geojson.Feature(
121+
id=elem["id"],
122+
geometry=geometry,
123+
properties=elem.get("tags"))
124+
features.append(feature)
112125

126+
return geojson.FeatureCollection(features)
113127

114128
class OverpassException(Exception):
115129
def __init__(self, status_code, message):

requirements.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
download_url='https://github.com/mvexel/overpass-api-python-wrapper/tarball/0.0.1',
1212
keywords=['openstreetmap', 'overpass', 'wrapper'],
1313
classifiers=[],
14-
install_requires=['requests>=2.3.0', 'shapely>=1.4.3'],
14+
install_requires=['requests>=2.3.0', 'geojson>=1.0.9'],
1515
)

test_api.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
1+
import geojson
12
import overpass
23

3-
44
class TestAPI:
55

66
def test_initialize_api(self):
77
api = overpass.API()
88
assert isinstance(api, overpass.API)
9-
assert api.debug == False
9+
assert api.debug == False
10+
11+
def test_geojson(self):
12+
api = overpass.API()
13+
#osm_elements = api.Get(overpass.MapQuery(37.86517,-122.31851,37.86687,-122.31635))
14+
#print 'DEB osm_elements:', geojson.dumps(osm_elements,sort_keys=True,indent=2)
15+
osm_geo = api.Get(overpass.MapQuery(37.86517,-122.31851,37.86687,-122.31635), asGeoJSON=True)
16+
#with open('test.geojson','w') as f:
17+
# geojson.dump(osm_geo,f,indent=2,sort_keys=True)
18+
assert len(osm_geo['features'])>1
19+
20+
def run_tests(self):
21+
self.test_initialize_api()
22+
self.test_geojson()
23+
24+
if __name__ == '__main__':
25+
tapi = TestAPI()
26+
tapi.run_tests()
27+
print "overpass PASS"

0 commit comments

Comments
 (0)