Skip to content

Commit

Permalink
Small code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Müller committed Sep 10, 2019
1 parent 171fcf3 commit f0c4560
Showing 1 changed file with 20 additions and 32 deletions.
52 changes: 20 additions & 32 deletions overpass/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ def _as_geojson(self, elements):

features = []
geometry = None
no_match_count = 0
for elem in elements:
elem_type = elem.get("type")
elem_tags = elem.get("tags")
if elem_type and elem_type == "node":
geometry = geojson.Point((elem.get("lon"), elem.get("lat")))
elif elem_type and elem_type == "way":
Expand All @@ -218,17 +218,21 @@ def _as_geojson(self, elements):
# also sometimes the next member may not actually connect to the previous member, so if necessary,
# find a matching member
if points:
dist_start = (points[-1][0] - mem["geometry"][0]["lon"])**2 + (points[-1][1] - mem["geometry"][0]["lat"])**2
dist_end = (points[-1][0] - mem["geometry"][-1]["lon"])**2 + (points[-1][1] - mem["geometry"][-1]["lat"])**2
dist_start = (points[-1][0] - mem["geometry"][0]["lon"]) ** 2 + (
points[-1][1] - mem["geometry"][0]["lat"]) ** 2
dist_end = (points[-1][0] - mem["geometry"][-1]["lon"]) ** 2 + (
points[-1][1] - mem["geometry"][-1]["lat"]) ** 2
if dist_start == 0:
pass # don't need to do anything
elif dist_end == 0:
# flip the next member - it is entered in the wrong direction
mem["geometry"] = list(reversed(mem["geometry"]))
else:
# try flipping the previous member
dist_flipped_start = (points[0][0] - mem["geometry"][0]["lon"])**2 + (points[0][1] - mem["geometry"][0]["lat"])**2
dist_flipped_end = (points[0][0] - mem["geometry"][-1]["lon"])**2 + (points[0][1] - mem["geometry"][-1]["lat"])**2
dist_flipped_start = (points[0][0] - mem["geometry"][0]["lon"]) ** 2 + (
points[0][1] - mem["geometry"][0]["lat"]) ** 2
dist_flipped_end = (points[0][0] - mem["geometry"][-1]["lon"]) ** 2 + (
points[0][1] - mem["geometry"][-1]["lat"]) ** 2
if dist_flipped_start == 0:
# just flip the start
points = list(reversed(points))
Expand All @@ -242,24 +246,22 @@ def _as_geojson(self, elements):
for i in range(pos + 1, len(elem['members'])):
if not point_found:
new_pt = elem['members'][i]
dist_start = (new_pt['geometry'][0]['lon'] - points[-1][0])**2 + (new_pt['geometry'][0]['lat'] - points[-1][1])**2
dist_end = (new_pt['geometry'][-1]['lon'] - points[-1][0])**2 + (new_pt['geometry'][-1]['lat'] - points[-1][1])**2
dist_start = (new_pt['geometry'][0]['lon'] - points[-1][0]) ** 2 + (
new_pt['geometry'][0]['lat'] - points[-1][1]) ** 2
dist_end = (new_pt['geometry'][-1]['lon'] - points[-1][0]) ** 2 + (
new_pt['geometry'][-1]['lat'] - points[-1][1]) ** 2

if dist_start == 0 or dist_end == 0:
point_found = True
# swap the order of the members -- we have found the one we want
elem['members'][pos], elem['members'][i] = elem['members'][i], elem['members'][pos]
elem['members'][pos], elem['members'][i] = elem['members'][i], \
elem['members'][pos]
# save this new point as mem
mem = elem['members'][pos]

if dist_end == 0:
mem['geometry'] = list(reversed(mem['geometry']))

if not point_found:
no_match_count += 1
# don't work with this park
continue

# address outer values
if mem['role'] == 'outer':
if prev == "inner":
Expand Down Expand Up @@ -293,7 +295,7 @@ def _as_geojson(self, elements):
if points[-1] == points[0]:
poly.append(points)
points = []
# update conditoin
# update condition
prev = "inner"

not_first = True
Expand All @@ -302,30 +304,16 @@ def _as_geojson(self, elements):
polygons.append(poly)

if polygons != [[]]:
# create MultiPolygon feature - separate multipolygon for each outer
for outer_poly in polygons:
poly_props = elem.get("tags")
poly_props.update({'id': elem['id']})
multipoly = {
"type": "Feature",
"properties": poly_props,
"geometry": {
"type": "MultiPolygon",
"coordinates": [outer_poly]
}
}
# add to features
features.append(multipoly)
geometry = geojson.MultiPolygon(polygons)
else:
continue

if elem_type and (elem_type == "node" or elem_type == "way"):
if geometry:
feature = geojson.Feature(
id=elem["id"],
geometry=geometry,
properties=elem.get("tags")
properties=elem_tags
)
features.append(feature)

# print('number of unmatched parks: {}'.format(no_match_count))

return geojson.FeatureCollection(features)

0 comments on commit f0c4560

Please sign in to comment.