def get_polygons_coordinates(geometry):
"""
Extract exterior coordinates from polygon(s) to pass to OSM in a query by
polygon.
Parameters
----------
geometry : shapely Polygon or MultiPolygon
the geometry to extract exterior coordinates from
Returns
-------
polygon_coord_strs : list
"""
# extract the exterior coordinates of the geometry to pass to the API later
polygons_coords = []
if isinstance(geometry, Polygon):
x, y = geometry.exterior.xy
polygons_coords.append(list(zip(x, y)))
elif isinstance(geometry, MultiPolygon):
for polygon in geometry:
x, y = polygon.exterior.xy
polygons_coords.append(list(zip(x, y)))
else:
raise ValueError('Geometry must be a shapely Polygon or MultiPolygon')
# convert the exterior coordinates of the polygon(s) to the string format
# the API expects
polygon_coord_strs = []
for coords in polygons_coords:
s = ''
separator = ' '
for coord in list(coords):
# round floating point lats and longs to 14 places, so we can hash
# and cache strings consistently
s = '{}{}{:.14f}{}{:.14f}'.format(s, separator, coord[1], separator, coord[0])
polygon_coord_strs.append(s.strip(separator))
return polygon_coord_strs
评论列表
文章目录