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

1#!/usr/bin/env python3 

2 

3# Define function ... 

4def extract_points( 

5 shape, 

6 /, 

7 *, 

8 onlyValid = False, 

9): 

10 """Extract the Points from the shape 

11 

12 This function accepts any Shapely geometry and returns a flat list of all of 

13 the Points contained within. 

14 

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) 

22 

23 Returns 

24 ------- 

25 points : list of shapely.geometry.point.Point 

26 a flat list of all of the Points 

27 

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)``. 

32 

33 Copyright 2017 Thomas Guymer [1]_ 

34 

35 References 

36 ---------- 

37 .. [1] PyGuymer3, https://github.com/Guymer/PyGuymer3 

38 """ 

39 

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 

46 

47 # ************************************************************************** 

48 

49 # Check type ... 

50 if shape is None: 

51 return [] 

52 

53 # ************************************************************************** 

54 

55 # Check type ... 

56 if isinstance(shape, list): 

57 # Initialize list ... 

58 points = [] 

59 

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 ) 

69 

70 # Return answer ... 

71 return points 

72 

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 [] 

80 

81 # Return answer ... 

82 return [shape] 

83 

84 # Check if it is valid ... 

85 if shape.is_valid: 

86 # Skip bad Points ... 

87 if shape.is_empty: 

88 return [] 

89 

90 # Return answer ... 

91 return [shape] 

92 

93 # Return answer ... 

94 return [] 

95 

96 # Check type ... 

97 if isinstance(shape, shapely.geometry.multipoint.MultiPoint): 

98 # Initialize list ... 

99 points = [] 

100 

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 ) 

110 

111 # Return answer ... 

112 return points 

113 

114 # Check type ... 

115 if isinstance(shape, shapely.geometry.polygon.LinearRing): 

116 return [] 

117 

118 # Check type ... 

119 if isinstance(shape, shapely.geometry.linestring.LineString): 

120 return [] 

121 

122 # Check type ... 

123 if isinstance(shape, shapely.geometry.multilinestring.MultiLineString): 

124 return [] 

125 

126 # Check type ... 

127 if isinstance(shape, shapely.geometry.polygon.Polygon): 

128 return [] 

129 

130 # Check type ... 

131 if isinstance(shape, shapely.geometry.multipolygon.MultiPolygon): 

132 return [] 

133 

134 # Check type ... 

135 if isinstance(shape, shapely.geometry.collection.GeometryCollection): 

136 # Initialize list ... 

137 points = [] 

138 

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 ) 

148 

149 # Return answer ... 

150 return points 

151 

152 # ************************************************************************** 

153 

154 raise TypeError(f"\"shape\" is an unexpected type ({repr(type(shape))})") from None