-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmpol2shp.py
More file actions
33 lines (28 loc) · 1.01 KB
/
mpol2shp.py
File metadata and controls
33 lines (28 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from osgeo import ogr
from shapely.geometry import Polygon, MultiPolygon
from shapely.geometry import mapping, Polygon
import fiona
from fiona.crs import from_epsg
def mpol2shp(mpol, filename, epsg = 4326):
"""
Function to save a (MultiPolygon) as a Esri .shp
:param mpol: (Multi)Polygon to shapefile
:param filename: filename directory to store the shp
:param epsg: crs
"""
# make sure the function can handle both multpolygon and Polygons
if type(mpol) == Polygon:
mpol = MultiPolygon([mpol])
# Define a polygon feature geometry with one attribute
schema = {
'geometry': 'Polygon',
'properties': {'id': 'int'},
}
# Write a new Shapefile
with fiona.open(filename, 'w', 'ESRI Shapefile', schema = schema, crs = from_epsg(epsg)) as c:
## If there are multiple geometries, put the "for" loop here
for pol in mpol:
c.write({
'geometry': mapping(pol),
'properties': {'id': 123},
})