11import requests
22import json
3+ from shapely .geometry import mapping , Point
34
45
56class API (object ):
@@ -31,7 +32,7 @@ def __init__(self, *args, **kwargs):
3132 requests_log .setLevel (logging .DEBUG )
3233 requests_log .propagate = True
3334
34- def Get (self , query ):
35+ def Get (self , query , asGeoJSON = False ):
3536 """Pass in an Overpass query in Overpass QL"""
3637
3738 response = json .loads (
@@ -41,7 +42,11 @@ def Get(self, query):
4142 if "elements" not in response or len (response ["elements" ]) == 0 :
4243 return self ._ConstructError ('No OSM features satisfied your query' )
4344
44- return response
45+ if not asGeoJSON :
46+ return response
47+
48+ # construct geojson
49+ return self ._asGeoJSON (response ["elements" ])
4550
4651 def Search (self , feature_type , regex = False ):
4752 """Search for something."""
@@ -85,3 +90,17 @@ def _GetFromOverpass(self, query):
8590 return self ._ConstructError ('Something unexpected happened' )
8691
8792 return r .text
93+
94+ def _asGeoJSON (self , elements ):
95+ nodes = [{
96+ "id" : elem .get ("id" ),
97+ "tags" : elem .get ("tags" ),
98+ "geom" : Point (elem ["lon" ], elem ["lat" ])}
99+ for elem in elements if elem ["type" ] == "node" ]
100+ ways = [{
101+ "id" : elem .get ("id" ),
102+ "tags" : elem .get ("tags" ),
103+ "nodes" : elem .get ("nodes" )}
104+ for elem in elements if elem ["type" ] == "way" ]
105+ print nodes
106+ print ways
0 commit comments