Skip to content

Commit 4a61d9f

Browse files
committed
added support for relations
1 parent b35ac6e commit 4a61d9f

File tree

1 file changed

+71
-8
lines changed

1 file changed

+71
-8
lines changed

overpass/api.py

Lines changed: 71 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,81 @@ def _asGeoJSON(self, elements):
139139
elem_type = elem["type"]
140140
if elem_type == "node":
141141
geometry = geojson.Point((elem["lon"], elem["lat"]))
142+
feature = geojson.Feature(
143+
id=elem["id"],
144+
geometry=geometry,
145+
properties=elem.get("tags"))
146+
features.append(feature)
147+
142148
elif elem_type == "way":
143149
points = []
144150
for coords in elem["geometry"]:
145151
points.append((coords["lon"], coords["lat"]))
146152
geometry = geojson.LineString(points)
147-
else:
148-
continue
149-
150-
feature = geojson.Feature(
151-
id=elem["id"],
152-
geometry=geometry,
153-
properties=elem.get("tags"))
154-
features.append(feature)
153+
feature = geojson.Feature(
154+
id=elem["id"],
155+
geometry=geometry,
156+
properties=elem.get("tags"))
157+
features.append(feature)
158+
159+
elif elem_type == "relation":
160+
# initialize result lists
161+
polygons = []
162+
poly = []
163+
points = []
164+
# conditions
165+
prev = "inner"
166+
not_first = False
167+
for mem in elem["members"]:
168+
# address outer values
169+
if mem['role'] == 'outer':
170+
if prev == "inner":
171+
# start new outer polygon
172+
points = []
173+
174+
if points == [] and not_first:
175+
# append the previous poly to the polygon list
176+
polygons.append(poly)
177+
poly = []
178+
179+
for coords in mem["geometry"]:
180+
points.append([coords["lon"], coords["lat"]])
181+
182+
if points[-1] == points[0]:
183+
# finish the outer polygon if it has met the start
184+
is_complete = True
185+
poly.append(points)
186+
points = []
187+
# update condition
188+
prev = "outer"
189+
190+
# address inner points
191+
if mem['role'] == "inner":
192+
for coords in mem["geometry"]:
193+
points.append([coords["lon"], coords["lat"]])
194+
195+
# check if the inner is complete
196+
if points[-1] == points[0]:
197+
poly.append(points)
198+
points = []
199+
# update conditoin
200+
prev = "inner"
201+
202+
not_first = True
203+
#
204+
polygons.append(poly)
205+
206+
if polygons != [[]]:
207+
# create MultiPolygon feature
208+
poly_props = elem.get("tags")
209+
poly_props.update({'id':elem['id']})
210+
multipoly = {"type": "Feature",
211+
"properties" : poly_props,
212+
"geometry": {
213+
"type": "MultiPolygon",
214+
"coordinates": polygons}}
215+
216+
# add to features
217+
features.append(multipoly)
155218

156219
return geojson.FeatureCollection(features)

0 commit comments

Comments
 (0)