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 pathsimplify_poly.py
41 lines (34 loc) · 1.68 KB
/
simplify_poly.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
# -*- 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/simplify_poly.py #
##############################################################################################
def simplify_poly(poly1, simp = 0.1):
"""
This function accepts either a Polygon or a MultiPolygon and creates a
Polygon or a MultiPolygon from the simplified (member) Polygon(s).
"""
# Import modules ...
import shapely
import shapely.geometry
import shapely.validation
# Create empty list ...
poly2 = []
# Check what the argument is ...
if isinstance(poly1, shapely.geometry.multipolygon.MultiPolygon):
# Loop over Polygons and add simplified copys to the list ...
for tmp1 in poly1.geoms:
poly2.append(tmp1.simplify(simp))
elif isinstance(poly1, shapely.geometry.polygon.Polygon):
# Add simplified copy to the list ...
poly2.append(poly1.simplify(simp))
else:
raise TypeError("\"poly1\" is an unexpected type")
# Convert list to MultiPolygon ...
poly2 = shapely.geometry.multipolygon.MultiPolygon(poly2)
if not poly2.is_valid:
raise Exception("\"poly2\" is not a valid [Multi]Polygon ({0:s})".format(shapely.validation.explain_validity(poly2)))
# Return answer ...
return poly2