Coverage for gws-app/gws/gis/extent/_test.py: 0%
103 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-17 01:37 +0200
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-17 01:37 +0200
1"""Tests for the extent module."""
2import math
4import gws
5import gws.test.util as u
6import gws.gis.extent as extent
8import gws.gis.crs as crs
11def test_from_string():
12 assert extent.from_string('100,200,200,400') == (100, 200, 200, 400)
15def test_from_list():
16 assert extent.from_list([100, 200, 300, 400]) == (100, 200, 300, 400)
19def test_from_list_error():
20 assert not extent.from_list([100, 200])
23def test_from_list_value_error():
24 assert not extent.from_list(['a', 'b', 100, 200])
27def test_from_list_same_point():
28 assert not extent.from_list([100, 200, 100, 200])
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)
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)
43def test_from_box():
44 assert extent.from_box('box(100 200,300 400)') == (100, 200, 300, 400)
47def test_from_box_commas():
48 with u.raises(Exception):
49 extent.from_box('box(100,200,300,400)')
52def test_from_box_pattern():
53 assert not extent.from_box('foo(100 200, 300 400)')
56def test_from_box_empty():
57 assert not extent.from_box('')
60def test_intersection():
61 a = (100, 100, 300, 300)
62 b = (200, 200, 400, 400)
63 c = (200, 100, 400, 300)
64 exts = [a, b, c]
65 assert extent.intersection(exts) == (200, 200, 300, 300)
68def test_intersection_empty():
69 a = (100, 100, 300, 300)
70 b = (200, 200, 400, 400)
71 c = (500, 600, 700, 700)
72 exts = [a, b, c]
73 assert not extent.intersection(exts)
76def test_intersection_empty_list():
77 assert not extent.intersection([])
80def test_center():
81 assert extent.center((100, 100, 200, 200)) == (150, 150)
84def test_size():
85 assert extent.size((100, 100, 200, 200)) == (100, 100)
88def test_diagonal():
89 assert extent.diagonal((1, 1, 4, 5)) == 5
92def test_circumsquare():
93 assert extent.circumsquare((1, 1, 4, 5)) == (0, 0.5, 5, 5.5)
96def test_buffer():
97 assert extent.buffer((100, 100, 200, 200), 100) == (0, 0, 300, 300)
100def test_union():
101 exts = [
102 (1, 100, 200, 200),
103 (100, 2, 200, 200),
104 (100, 100, 300, 200),
105 (100, 100, 200, 400)
106 ]
107 assert extent.union(exts) == (1, 2, 300, 400)
110def test_union_empty():
111 with u.raises(Exception):
112 extent.union([])
115def test_intersect():
116 a = (300, 400, 700, 800)
117 b = (100, 200, 500, 600)
118 assert extent.intersect(a, b)
121def test_intersect_inf():
122 a = (1, 2, 3, 4)
123 b = (-math.inf, -math.inf, math.inf, math.inf)
124 assert extent.intersect(a, b)
127def test_intersect_false():
128 a = (300, 300, 400, 400)
129 b = (100, 100, 200, 200)
130 assert not extent.intersect(a, b)
133def test_transform():
134 from_crs = crs.WEBMERCATOR
135 to_crs = crs.WGS84
136 ext = extent.transform((100, 100, 200, 200), from_crs, to_crs)
137 ext = (
138 math.trunc(ext[0] * 10000000),
139 math.trunc(ext[1] * 10000000),
140 math.trunc(ext[2] * 10000000),
141 math.trunc(ext[3] * 10000000)
142 )
143 assert ext == (8983, 8983, 17966, 17966)
146def test_transform_to_wgs():
147 from_crs = crs.WEBMERCATOR
148 ext = extent.transform_to_wgs((100, 100, 200, 200), from_crs)
149 ext = (
150 math.trunc(ext[0] * 10000000),
151 math.trunc(ext[1] * 10000000),
152 math.trunc(ext[2] * 10000000),
153 math.trunc(ext[3] * 10000000)
154 )
155 assert ext == (8983, 8983, 17966, 17966)
158def test_transform_from_wgs():
159 to_crs = crs.WEBMERCATOR
160 ext = extent.transform_from_wgs((0.0008983, 0.0008983, 0.0017967, 0.0017967), to_crs)
161 ext = (
162 math.trunc(ext[0]),
163 math.trunc(ext[1]),
164 math.trunc(ext[2]),
165 math.trunc(ext[3])
166 )
167 assert ext == (99, 99, 200, 200)
170def test_swap_xy():
171 assert extent.swap_xy((2, 1, 4, 3)) == (1, 2, 3, 4)
174def test_is_valid():
175 assert extent.is_valid([1, 1, 2, 2])
176 assert extent.is_valid([1.123, 1.123, 2.123, 2.123])
178 assert not extent.is_valid([1, 1, 1, 1])
179 assert not extent.is_valid([2.2, 1, 1])
180 assert not extent.is_valid([1, 2, 3, 4, 5])
181 assert not extent.is_valid([])
182 assert not extent.is_valid([1, 2])
183 assert not extent.is_valid(None)
184 assert not extent.is_valid([1, 2, 3, math.inf])
185 assert not extent.is_valid([float("nan")] * 4)