Coverage for pyguymer3/geo/fillinSrc/fillin_Polygon.py: 4%
24 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 fillin_Polygon(
5 poly,
6 fill,
7 /,
8 *,
9 debug = __debug__,
10 eps = 1.0e-12,
11 fillSpace = "EuclideanSpace",
12 nIter = 100,
13 prefix = ".",
14 ramLimit = 1073741824,
15 tol = 1.0e-10,
16):
17 """Fill in a Polygon
19 This function reads in a Polygon (with an exterior and any number of
20 interiors) that exists on the surface of the Earth and returns a
21 Polygon of the same Polygon filled in by a constant distance: either in
22 degrees in Euclidean space; or in metres in Geodesic space.
24 Parameters
25 ----------
26 poly : shapely.geometry.polygon.Polygon
27 the Polygon
28 fill : float
29 the Euclidean or Geodesic distance to fill in between each point within
30 the shape by (in degrees or metres)
31 debug : bool, optional
32 print debug messages
33 eps : float, optional
34 the tolerance of the Vincenty formula iterations
35 fillSpace : str, optional
36 the geometric space to perform the filling in (either "EuclideanSpace"
37 or "GeodesicSpace")
38 nIter : int, optional
39 the maximum number of iterations (particularly the Vincenty formula)
40 prefix : str, optional
41 change the name of the output debugging CSVs
42 ramLimit : int, optional
43 the maximum RAM usage of each "large" array (in bytes)
44 tol : float, optional
45 the Euclidean distance that defines two points as being the same (in
46 degrees)
48 Returns
49 -------
50 fills : shapely.geometry.polygon.Polygon
51 the filled in Polygon
53 Notes
54 -----
55 Copyright 2017 Thomas Guymer [1]_
57 References
58 ----------
59 .. [1] PyGuymer3, https://github.com/Guymer/PyGuymer3
60 """
62 # Import special modules ...
63 try:
64 import shapely
65 import shapely.geometry
66 import shapely.ops
67 except:
68 raise Exception("\"shapely\" is not installed; run \"pip install --user Shapely\"") from None
70 # Import sub-functions ...
71 from ..check import check
72 from .fillin_LinearRing import fillin_LinearRing
74 # **************************************************************************
76 # Check argument ...
77 assert isinstance(poly, shapely.geometry.polygon.Polygon), "\"poly\" is not a Polygon"
78 if debug:
79 check(poly, prefix = prefix)
81 # Fill in exterior LinearRing ...
82 exterior = fillin_LinearRing(
83 poly.exterior,
84 fill,
85 debug = debug,
86 eps = eps,
87 fillSpace = fillSpace,
88 nIter = nIter,
89 prefix = prefix,
90 ramLimit = ramLimit,
91 )
93 # Initialize list ...
94 interiors = []
96 # Loop over interior LinearRings ...
97 for interior in poly.interiors:
98 # Skip if it doesn't contain any length ...
99 if interior.length < tol:
100 if debug:
101 print(f"INFO: Removing a tiny-length interior ring at ({interior.centroid.x:+.6f}°,{interior.centroid.y:+.6f}°).")
102 continue
104 # Append filled in interior LinearRing to list ...
105 interiors.append(
106 fillin_LinearRing(
107 interior,
108 fill,
109 debug = debug,
110 eps = eps,
111 fillSpace = fillSpace,
112 nIter = nIter,
113 prefix = prefix,
114 ramLimit = ramLimit,
115 )
116 )
118 # Convert exterior LinearRing and list of interior LinearRings to a
119 # correctly oriented Polygon ...
120 fills = shapely.geometry.polygon.orient(shapely.geometry.polygon.Polygon(exterior, interiors))
121 if debug:
122 check(fills, prefix = prefix)
124 # Return answer ...
125 return fills