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

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 += extract_points(item, onlyValid = onlyValid) 

64 

65 # Return answer ... 

66 return points 

67 

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

75 

76 # Return answer ... 

77 return [shape] 

78 

79 # Check if it is valid ... 

80 if shape.is_valid: 

81 # Skip bad Points ... 

82 if shape.is_empty: 

83 return [] 

84 

85 # Return answer ... 

86 return [shape] 

87 

88 # Return answer ... 

89 return [] 

90 

91 # Check type ... 

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

93 # Initialize list ... 

94 points = [] 

95 

96 # Loop over Points ... 

97 for point in shape.geoms: 

98 # Add lists together ... 

99 points += extract_points(point, onlyValid = onlyValid) 

100 

101 # Return answer ... 

102 return points 

103 

104 # Check type ... 

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

106 return [] 

107 

108 # Check type ... 

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

110 return [] 

111 

112 # Check type ... 

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

114 return [] 

115 

116 # Check type ... 

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

118 return [] 

119 

120 # Check type ... 

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

122 return [] 

123 

124 # Check type ... 

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

126 # Initialize list ... 

127 points = [] 

128 

129 # Loop over geometries ... 

130 for geom in shape.geoms: 

131 # Add lists together ... 

132 points += extract_points(geom, onlyValid = onlyValid) 

133 

134 # Return answer ... 

135 return points 

136 

137 # ************************************************************************** 

138 

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