Coverage for pyguymer3/geo/clean.py: 48%

25 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 clean( 

5 shape, 

6 /, 

7 *, 

8 debug = __debug__, 

9 prefix = ".", 

10 tol = 1.0e-10, 

11): 

12 """Clean a shape 

13 

14 This function cleans a shape by removing bad points. 

15 

16 Parameters 

17 ---------- 

18 shape : shapely.coords.CoordinateSequence, shapely.geometry.polygon.LinearRing, shapely.geometry.linestring.LineString, shapely.geometry.multilinestring.MultiLineString, shapely.geometry.polygon.Polygon, shapely.geometry.multipolygon.MultiPolygon 

19 the shape 

20 debug : bool, optional 

21 print debug messages 

22 prefix : str, optional 

23 change the name of the output debugging CSVs 

24 tol : float, optional 

25 the Euclidean distance that defines two points as being the same (in 

26 degrees) 

27 

28 Returns 

29 ------- 

30 cleans : shapely.coords.CoordinateSequence, shapely.geometry.polygon.LinearRing, shapely.geometry.linestring.LineString, shapely.geometry.multilinestring.MultiLineString, shapely.geometry.polygon.Polygon, shapely.geometry.multipolygon.MultiPolygon 

31 the cleaned shape 

32 

33 Notes 

34 ----- 

35 According to the `Shapely documentation for the function 

36 shapely.geometry.polygon.orient() 

37 <https://shapely.readthedocs.io/en/stable/manual.html#shapely.geometry.polygon.orient>`_ : 

38 

39 "A sign of 1.0 means that the coordinates of the product's exterior ring 

40 will be oriented counter-clockwise." 

41 

42 Copyright 2017 Thomas Guymer [1]_ 

43 

44 References 

45 ---------- 

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

47 """ 

48 

49 # Import special modules ... 

50 try: 

51 import shapely 

52 import shapely.geometry 

53 except: 

54 raise Exception("\"shapely\" is not installed; run \"pip install --user Shapely\"") from None 

55 

56 # Import sub-functions ... 

57 from .cleanSrc import clean_CoordinateSequence 

58 from .cleanSrc import clean_LinearRing 

59 from .cleanSrc import clean_LineString 

60 from .cleanSrc import clean_MultiLineString 

61 from .cleanSrc import clean_MultiPolygon 

62 from .cleanSrc import clean_Polygon 

63 

64 # ************************************************************************** 

65 

66 # Check if it is a CoordinateSequence and return it cleaned ... 

67 if isinstance(shape, shapely.coords.CoordinateSequence): 

68 return clean_CoordinateSequence( 

69 shape, 

70 debug = debug, 

71 prefix = prefix, 

72 tol = tol, 

73 ) 

74 

75 # Check if it is a LinearRing and return it cleaned ... 

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

77 return clean_LinearRing( 

78 shape, 

79 debug = debug, 

80 prefix = prefix, 

81 tol = tol, 

82 ) 

83 

84 # Check if it is a LineString and return it cleaned ... 

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

86 return clean_LineString( 

87 shape, 

88 debug = debug, 

89 prefix = prefix, 

90 tol = tol, 

91 ) 

92 

93 # Check if it is a MultiLineString and return it cleaned ... 

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

95 return clean_MultiLineString( 

96 shape, 

97 debug = debug, 

98 prefix = prefix, 

99 tol = tol, 

100 ) 

101 

102 # Check if it is a Polygon and return it cleaned ... 

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

104 return clean_Polygon( 

105 shape, 

106 debug = debug, 

107 prefix = prefix, 

108 tol = tol, 

109 ) 

110 

111 # Check if it is a MultiPolygon and return it cleaned ... 

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

113 return clean_MultiPolygon( 

114 shape, 

115 debug = debug, 

116 prefix = prefix, 

117 tol = tol, 

118 ) 

119 

120 # Crash ... 

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