This repository has been archived by the owner on Jan 19, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer_multipolygon.py
68 lines (58 loc) · 2.9 KB
/
buffer_multipolygon.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# -*- coding: utf-8 -*-
##############################################################################################
# This file is deprecated because Python 2.x is deprecated #
# A Python 3.x version of this file can be found at: #
# #
# https://github.com/Guymer/PyGuymer3/blob/master/buffer_multipolygon.py #
##############################################################################################
def buffer_multipolygon(multipoly, dist, nang = 19, simp = 0.1, debug = False):
"""
This function reads in a MultiPolygon, made up of Polygons (with an exterior
and any number of interiors), that exists on the surface of the Earth and
returns the same [Multi]Polygon buffered by a constant distance (in metres).
"""
# Import modules ...
import shapely
import shapely.geometry
import shapely.ops
import shapely.validation
# Load sub-functions ...
from .buffer_polygon import buffer_polygon
# Check argument ...
if not isinstance(multipoly, shapely.geometry.multipolygon.MultiPolygon):
raise TypeError("\"multipoly\" is not a MultiPolygon")
if not multipoly.is_valid:
raise Exception("\"multipoly\" is not a valid MultiPolygon ({0:s})".format(shapely.validation.explain_validity(multipoly)))
# Create empty list ...
buffs = []
# Loop over Polygons ...
for poly in multipoly.geoms:
# Buffer Polygon ...
buff = buffer_polygon(poly, dist, nang, simp, debug)
# Check how many polygons describe the buffer and append them to the
# list ...
if isinstance(buff, shapely.geometry.multipolygon.MultiPolygon):
for tmp1 in buff.geoms:
if not tmp1.is_valid:
raise Exception("\"tmp1\" is not a valid Polygon ({0:s})".format(shapely.validation.explain_validity(tmp1)))
tmp2 = tmp1.simplify(simp)
if tmp2.is_valid:
buffs.append(tmp2)
else:
buffs.append(tmp1)
elif isinstance(buff, shapely.geometry.polygon.Polygon):
if not buff.is_valid:
raise Exception("\"buff\" is not a valid Polygon ({0:s})".format(shapely.validation.explain_validity(buff)))
tmp1 = buff.simplify(simp)
if tmp1.is_valid:
buffs.append(tmp1)
else:
buffs.append(buff)
else:
raise Exception("\"buff\" is an unexpected type")
# Convert list to (unified) Polygon and check it ...
buffs = shapely.ops.unary_union(buffs)
if not buffs.is_valid:
raise Exception("\"buffs\" is not a valid [Multi]Polygon ({0:s})".format(shapely.validation.explain_validity(buffs)))
# Return answer ...
return buffs