Coverage for gws-app/gws/lib/extent/_test.py: 100%

100 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-16 23:09 +0200

1"""Tests for the extent module.""" 

2import math 

3 

4import gws 

5import gws.test.util as u 

6import gws.lib.extent as extent 

7 

8import gws.lib.crs as crs 

9 

10 

11def test_from_string(): 

12 assert extent.from_string('100,200,200,400') == (100, 200, 200, 400) 

13 

14 

15def test_from_list(): 

16 assert extent.from_list([100, 200, 300, 400]) == (100, 200, 300, 400) 

17 

18 

19def test_from_list_error(): 

20 assert not extent.from_list([100, 200]) 

21 

22 

23def test_from_list_value_error(): 

24 assert not extent.from_list(['a', 'b', 100, 200]) 

25 

26 

27def test_from_list_same_point(): 

28 assert not extent.from_list([100, 200, 100, 200]) 

29 

30 

31def test_from_points(): 

32 a = (100.0, 200.0) 

33 b = (300.0, 400.0) 

34 assert extent.from_points(a, b) == (100, 200, 300, 400) 

35 

36 

37def test_from_center(): 

38 a = (100.0, 200.0) 

39 size = (50, 100) 

40 assert extent.from_center(a, size) == (75, 150, 125, 250) 

41 

42 

43def test_from_box(): 

44 assert extent.from_box('box(100 200,300 400)') == (100, 200, 300, 400) 

45 

46 

47def test_from_box_pattern(): 

48 assert not extent.from_box('foo(100 200, 300 400)') 

49 

50 

51def test_from_box_empty(): 

52 assert not extent.from_box('') 

53 

54 

55def test_intersection(): 

56 a = (100, 100, 300, 300) 

57 b = (200, 200, 400, 400) 

58 c = (200, 100, 400, 300) 

59 exts = [a, b, c] 

60 assert extent.intersection(exts) == (200, 200, 300, 300) 

61 

62 

63def test_intersection_empty(): 

64 a = (100, 100, 300, 300) 

65 b = (200, 200, 400, 400) 

66 c = (500, 600, 700, 700) 

67 exts = [a, b, c] 

68 assert not extent.intersection(exts) 

69 

70 

71def test_intersection_empty_list(): 

72 assert not extent.intersection([]) 

73 

74 

75def test_center(): 

76 assert extent.center((100, 100, 200, 200)) == (150, 150) 

77 

78 

79def test_size(): 

80 assert extent.size((100, 100, 200, 200)) == (100, 100) 

81 

82 

83def test_diagonal(): 

84 assert extent.diagonal((1, 1, 4, 5)) == 5 

85 

86 

87def test_circumsquare(): 

88 assert extent.circumsquare((1, 1, 4, 5)) == (0, 0.5, 5, 5.5) 

89 

90 

91def test_buffer(): 

92 assert extent.buffer((100, 100, 200, 200), 100) == (0, 0, 300, 300) 

93 

94 

95def test_union(): 

96 exts = [ 

97 (1, 100, 200, 200), 

98 (100, 2, 200, 200), 

99 (100, 100, 300, 200), 

100 (100, 100, 200, 400) 

101 ] 

102 assert extent.union(exts) == (1, 2, 300, 400) 

103 

104 

105def test_union_empty(): 

106 with u.raises(Exception): 

107 extent.union([]) 

108 

109 

110def test_intersect(): 

111 a = (300, 400, 700, 800) 

112 b = (100, 200, 500, 600) 

113 assert extent.intersect(a, b) 

114 

115 

116def test_intersect_inf(): 

117 a = (1, 2, 3, 4) 

118 b = (-math.inf, -math.inf, math.inf, math.inf) 

119 assert extent.intersect(a, b) 

120 

121 

122def test_intersect_false(): 

123 a = (300, 300, 400, 400) 

124 b = (100, 100, 200, 200) 

125 assert not extent.intersect(a, b) 

126 

127 

128def test_transform(): 

129 from_crs = crs.WEBMERCATOR 

130 to_crs = crs.WGS84 

131 ext = extent.transform((100, 100, 200, 200), from_crs, to_crs) 

132 ext = ( 

133 math.trunc(ext[0] * 10000000), 

134 math.trunc(ext[1] * 10000000), 

135 math.trunc(ext[2] * 10000000), 

136 math.trunc(ext[3] * 10000000) 

137 ) 

138 assert ext == (8983, 8983, 17966, 17966) 

139 

140 

141def test_transform_to_wgs(): 

142 from_crs = crs.WEBMERCATOR 

143 ext = extent.transform_to_wgs((100, 100, 200, 200), from_crs) 

144 ext = ( 

145 math.trunc(ext[0] * 10000000), 

146 math.trunc(ext[1] * 10000000), 

147 math.trunc(ext[2] * 10000000), 

148 math.trunc(ext[3] * 10000000) 

149 ) 

150 assert ext == (8983, 8983, 17966, 17966) 

151 

152 

153def test_transform_from_wgs(): 

154 to_crs = crs.WEBMERCATOR 

155 ext = extent.transform_from_wgs((0.0008983, 0.0008983, 0.0017967, 0.0017967), to_crs) 

156 ext = ( 

157 math.trunc(ext[0]), 

158 math.trunc(ext[1]), 

159 math.trunc(ext[2]), 

160 math.trunc(ext[3]) 

161 ) 

162 assert ext == (99, 99, 200, 200) 

163 

164 

165def test_swap_xy(): 

166 assert extent.swap_xy((2, 1, 4, 3)) == (1, 2, 3, 4) 

167 

168 

169def test_is_valid(): 

170 assert extent.is_valid([1, 1, 2, 2]) 

171 assert extent.is_valid([1.123, 1.123, 2.123, 2.123]) 

172 

173 assert not extent.is_valid([1, 1, 1, 1]) 

174 assert not extent.is_valid([2.2, 1, 1]) 

175 assert not extent.is_valid([1, 2, 3, 4, 5]) 

176 assert not extent.is_valid([]) 

177 assert not extent.is_valid([1, 2]) 

178 assert not extent.is_valid(None) 

179 assert not extent.is_valid([1, 2, 3, math.inf]) 

180 assert not extent.is_valid([float("nan")] * 4)