Coverage for pyguymer3/geo/cleanSrc/clean_MultiPolygon.py: 6%
17 statements
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-08 18:47 +0000
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-08 18:47 +0000
1#!/usr/bin/env python3
3# Define function ...
4def clean_MultiPolygon(
5 multipoly,
6 /,
7 *,
8 debug = __debug__,
9 prefix = ".",
10 tol = 1.0e-10,
11):
12 """Clean a MultiPolygon
14 This function cleans a MultiPolygon, made up of Polygons (with an exterior
15 and any number of interiors), by removing bad points.
17 Parameters
18 ----------
19 multipoly : shapely.geometry.multipolygon.MultiPolygon
20 the MultiPolygon
21 debug : bool, optional
22 print debug messages
23 prefix : str, optional
24 change the name of the output debugging CSVs
25 tol : float, optional
26 the Euclidean distance that defines two points as being the same (in
27 degrees)
29 Returns
30 -------
31 cleans : shapely.geometry.multipolygon.MultiPolygon
32 the cleaned MultiPolygon
34 Notes
35 -----
36 According to the `Shapely documentation for the function
37 shapely.geometry.polygon.orient()
38 <https://shapely.readthedocs.io/en/stable/manual.html#shapely.geometry.polygon.orient>`_ :
40 "A sign of 1.0 means that the coordinates of the product's exterior ring
41 will be oriented counter-clockwise."
43 Copyright 2017 Thomas Guymer [1]_
45 References
46 ----------
47 .. [1] PyGuymer3, https://github.com/Guymer/PyGuymer3
48 """
50 # Import special modules ...
51 try:
52 import shapely
53 import shapely.geometry
54 import shapely.ops
55 except:
56 raise Exception("\"shapely\" is not installed; run \"pip install --user Shapely\"") from None
58 # Import sub-functions ...
59 from ..check import check
60 from .clean_Polygon import clean_Polygon
62 # **************************************************************************
64 # Check argument ...
65 assert isinstance(multipoly, shapely.geometry.multipolygon.MultiPolygon), "\"multipoly\" is not a MultiPolygon"
67 # Initialize list ...
68 polys = []
70 # Loop over Polygons ...
71 for poly in multipoly.geoms:
72 # Append cleaned Polygon to list ...
73 polys.append(
74 clean_Polygon(
75 poly,
76 debug = debug,
77 prefix = prefix,
78 tol = tol,
79 )
80 )
82 # Convert list of Polygons to a (unified) MultiPolygon ...
83 cleans = shapely.ops.unary_union(polys)
84 if debug:
85 check(cleans, prefix = prefix)
87 # Return answer ...
88 return cleans