Coverage for pyguymer3/geo/extract_points.py: 2%
44 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 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 += extract_points(item, onlyValid = onlyValid)
65 # Return answer ...
66 return points
68 # Check type ...
69 if isinstance(shape, shapely.geometry.point.Point):
70 # Just return the answer if the user doesn't want any checks ...
71 if not onlyValid:
72 # Skip bad Points ...
73 if shape.is_empty:
74 return []
76 # Return answer ...
77 return [shape]
79 # Check if it is valid ...
80 if shape.is_valid:
81 # Skip bad Points ...
82 if shape.is_empty:
83 return []
85 # Return answer ...
86 return [shape]
88 # Return answer ...
89 return []
91 # Check type ...
92 if isinstance(shape, shapely.geometry.multipoint.MultiPoint):
93 # Initialize list ...
94 points = []
96 # Loop over Points ...
97 for point in shape.geoms:
98 # Add lists together ...
99 points += extract_points(point, onlyValid = onlyValid)
101 # Return answer ...
102 return points
104 # Check type ...
105 if isinstance(shape, shapely.geometry.polygon.LinearRing):
106 return []
108 # Check type ...
109 if isinstance(shape, shapely.geometry.linestring.LineString):
110 return []
112 # Check type ...
113 if isinstance(shape, shapely.geometry.multilinestring.MultiLineString):
114 return []
116 # Check type ...
117 if isinstance(shape, shapely.geometry.polygon.Polygon):
118 return []
120 # Check type ...
121 if isinstance(shape, shapely.geometry.multipolygon.MultiPolygon):
122 return []
124 # Check type ...
125 if isinstance(shape, shapely.geometry.collection.GeometryCollection):
126 # Initialize list ...
127 points = []
129 # Loop over geometries ...
130 for geom in shape.geoms:
131 # Add lists together ...
132 points += extract_points(geom, onlyValid = onlyValid)
134 # Return answer ...
135 return points
137 # **************************************************************************
139 raise TypeError(f"\"shape\" is an unexpected type ({repr(type(shape))})") from None