def get_way_centroid(self, way):
"""Calculate the centroid of a way
way (object): overpy.Way object
Returns dict of lat/lon
"""
# Polygon has to have at least 3 points
if len(way.nodes) >= 3:
geom = []
for node in way.nodes:
geom.append((node.lon, node.lat))
poly = Polygon(geom)
cent = poly.centroid
return {'lat': cent.y, 'lon': cent.x}
elif len(way.nodes) == 2:
# if way has 2 nodes, use average position
lat = (way.nodes[0].lat + way.nodes[1].lat) / 2
lon = (way.nodes[0].lon + way.nodes[1].lon) / 2
return {'lat': lat, 'lon': lon}
elif len(way.nodes) == 1:
# if way has 1 node, use that position
# (unusual and certainly a bug but possible)
return {'lat': way.nodes[0].lat, 'lon': way.nodes[0].lon}
else:
raise RuntimeError
评论列表
文章目录