Skip to content

Instantly share code, notes, and snippets.

@nishadhka
Last active November 21, 2017 09:26
Show Gist options
  • Save nishadhka/01d00eefe7e2b31582c6047ef52bca10 to your computer and use it in GitHub Desktop.
Save nishadhka/01d00eefe7e2b31582c6047ef52bca10 to your computer and use it in GitHub Desktop.
Create fiona geometry dict from the list
from shapely import geometry
ll=(84.945856,23.475927)
ue=(86.505248, 23.871591)
p1 = geometry.Point(ll[0],ll[1])
p2 = geometry.Point(ue[0],ll[1])
p3 = geometry.Point(ue[0],ue[1])
p4 = geometry.Point(ll[0],ue[1])
pointList = [p1, p2, p3, p4, p1]
poly = geometry.Polygon([[p.x, p.y] for p in pointList])
def extract_poly_coords(geom):
if geom.type == 'Polygon':
exterior_coords = geom.exterior.coords[:]
interior_coords = []
for interior in geom.interiors:
interior_coords += interior.coords[:]
elif geom.type == 'MultiPolygon':
exterior_coords = [[]]
interior_coords = []
for part in geom:
epc = extract_poly_coords(part) # Recursive call
exterior_coords += epc['exterior_coords']
interior_coords += epc['interior_coords']
else:
raise ValueError('Unhandled geometry type: ' + repr(geom.type))
return [{'coordinates': [exterior_coords],
'type': geom.type}]
extract_poly_coords(poly)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment