Coverage for pyguymer3/geo/extract_points.py: 2%
44 statements
« prev ^ index » next coverage.py v7.10.3, created at 2025-08-16 08:31 +0000
« prev ^ index » next coverage.py v7.10.3, created at 2025-08-16 08:31 +0000
1#!/usr/bin/env python3
3# Define function ...
4def extract_points(
5 shape,
6 /,
7 *,
8 onlyValid = False,
9):
10 """Extract the Points from the shape
12 This function accepts any Shapely geometry and returns a flat list of all of
13 the Points contained within.
15 Parameters
16 ----------
17 shape : list, shapely.geometry.point.Point, shapely.geometry.multipoint.MultiPoint, shapely.geometry.polygon.LinearRing, shapely.geometry.linestring.LineString, shapely.geometry.multilinestring.MultiLineString, shapely.geometry.polygon.Polygon, shapely.geometry.multipolygon.MultiPolygon, shapely.geometry.collection.GeometryCollection
18 the Shapely geometry
19 onlyValid : bool, optional
20 only return valid Points (checks for validity can take a while, if
21 being called often)
23 Returns
24 -------
25 points : list of shapely.geometry.point.Point
26 a flat list of all of the Points
28 Notes
29 -----
30 To pass GeoJSON objects you must first convert them to Shapely objects by
31 doing something like ``shape = shapely.geometry.shape(shape)``.
33 Copyright 2017 Thomas Guymer [1]_
35 References
36 ----------
37 .. [1] PyGuymer3, https://github.com/Guymer/PyGuymer3
38 """
40 # Import special modules ...
41 try:
42 import shapely
43 import shapely.geometry
44 except:
45 raise Exception("\"shapely\" is not installed; run \"pip install --user Shapely\"") from None
47 # **************************************************************************
49 # Check type ...
50 if shape is None:
51 return []
53 # **************************************************************************
55 # Check type ...
56 if isinstance(shape, list):
57 # Initialize list ...
58 points = []
60 # Loop over items ...
61 for item in shape:
62 # Add lists together ...
63 points.extend(
64 extract_points(
65 item,
66 onlyValid = onlyValid,
67 )
68 )
70 # Return answer ...
71 return points
73 # Check type ...
74 if isinstance(shape, shapely.geometry.point.Point):
75 # Just return the answer if the user doesn't want any checks ...
76 if not onlyValid:
77 # Skip bad Points ...
78 if shape.is_empty:
79 return []
81 # Return answer ...
82 return [shape]
84 # Check if it is valid ...
85 if shape.is_valid:
86 # Skip bad Points ...
87 if shape.is_empty:
88 return []
90 # Return answer ...
91 return [shape]
93 # Return answer ...
94 return []
96 # Check type ...
97 if isinstance(shape, shapely.geometry.multipoint.MultiPoint):
98 # Initialize list ...
99 points = []
101 # Loop over Points ...
102 for point in shape.geoms:
103 # Add lists together ...
104 points.extend(
105 extract_points(
106 point,
107 onlyValid = onlyValid,
108 )
109 )
111 # Return answer ...
112 return points
114 # Check type ...
115 if isinstance(shape, shapely.geometry.polygon.LinearRing):
116 return []
118 # Check type ...
119 if isinstance(shape, shapely.geometry.linestring.LineString):
120 return []
122 # Check type ...
123 if isinstance(shape, shapely.geometry.multilinestring.MultiLineString):
124 return []
126 # Check type ...
127 if isinstance(shape, shapely.geometry.polygon.Polygon):
128 return []
130 # Check type ...
131 if isinstance(shape, shapely.geometry.multipolygon.MultiPolygon):
132 return []
134 # Check type ...
135 if isinstance(shape, shapely.geometry.collection.GeometryCollection):
136 # Initialize list ...
137 points = []
139 # Loop over geometries ...
140 for geom in shape.geoms:
141 # Add lists together ...
142 points.extend(
143 extract_points(
144 geom,
145 onlyValid = onlyValid,
146 )
147 )
149 # Return answer ...
150 return points
152 # **************************************************************************
154 raise TypeError(f"\"shape\" is an unexpected type ({repr(type(shape))})") from None