Last active
August 29, 2015 14:09
-
-
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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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