11import sys
22import requests
33import json
4- from shapely .geometry import mapping , Point
4+ import geojson
5+ #from shapely.geometry import mapping, Point
56
67
78class API (object ):
@@ -96,23 +97,62 @@ def _GetFromOverpass(self, query):
9697 return r .text
9798
9899 def _asGeoJSON (self , elements ):
99- """construct geoJSON from elements"""
100+ """
101+ Construct geoJSON from elements
102+ TODO: Add secondary overpass call to get coords for ways for nodes not included in elements.
103+ """
104+ node_elements_by_id = {}
105+
106+ # index node elements by id
107+ for elem in elements :
108+ if elem ["type" ] == "node" :
109+ node_elements_by_id [elem ["id" ]] = elem
110+
111+ features = []
112+ for elem in elements :
113+ elem_type = elem ["type" ]
114+ if elem ["type" ] == "node" :
115+ geometry = geojson .Point ((elem ["lon" ], elem ["lat" ]))
116+ elif elem ["type" ] == "way" :
117+ points = []
118+ for node_id in elem ["nodes" ]:
119+ node_elem = node_elements_by_id .get (node_id )
120+ if node_elem :
121+ point = (node_elem ["lon" ], node_elem ["lat" ])
122+ points .append (point )
123+ else
124+ print 'WARNING _asGeoJson skipping missing node ref in way
125+ geometry = geojson .LineString (points )
126+ else :
127+ continue
128+
129+ feature = geojson .Feature (
130+ id = elem ["id" ],
131+ geometry = geometry ,
132+ properties = elem .get ("tags" ))
133+ features .append (feature )
134+
135+ return geojson .FeatureCollection (features )
136+
137+ '''
100138 nodes = [{
101139 "id": elem.get("id"),
102- "tags" : elem .get ("tags" ),
140+ "tags": elem.get("tags")
103141 "geom": Point(elem["lon"], elem["lat"])}
104142 for elem in elements if elem["type"] == "node"]
105143 ways = [{
106144 "id": elem.get("id"),
107145 "tags": elem.get("tags"),
108146 "nodes": elem.get("nodes")}
109147 for elem in elements if elem["type"] == "way"]
110- print nodes
111- print ways
148+ '''
149+
150+
151+
112152
113153class OverpassException (Exception ):
114154 def __init__ (self , status_code , message ):
115155 self .status_code = status_code
116156 self .message = message
117157 def __str__ (self ):
118- return json .dumps ({'status' : self .status_code , 'message' : self .message })
158+ return json .dumps ({'status' : self .status_code , 'message' : self .message })
0 commit comments