Skip to content

Instantly share code, notes, and snippets.

@seumasmorrison
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seumasmorrison/927daeb723a44593bb43 to your computer and use it in GitHub Desktop.
Save seumasmorrison/927daeb723a44593bb43 to your computer and use it in GitHub Desktop.
Script for taking polyline shapefile to a Polygon shapefile using shapely 1.4.4 - https://pypi.python.org/pypi/Shapely ( Windows binary - http://www.lfd.uci.edu/~gohlke/pythonlibs/#shapely ) and fiona 1.4.8 - https://pypi.python.org/pypi/Fiona. Useful for Ordnance Survey coastline data, the polylines to polygons function in QGIS 2.6 used an olde…
from shapely.geometry import mapping
from shapely.ops import polygonize
import fiona
def polyline_to_polygon(input_file, output_file):
polylines = fiona.open(input_file)
geom = [x['geometry'] for x in polylines]
coords = [x['coordinates']for x in geom[:-1]]
schema = {'geometry': 'Polygon','properties': {'id': 'int'}}
with fiona.open(output_file, 'w', 'ESRI Shapefile', schema) as c:
for poly_id,polygon in enumerate(polygonize(coords)):
c.write({
'geometry': mapping(polygon),
'properties': {'id': poly_id},
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment