Debian Codegraph @

tests/overlays_test.py

raw symbols SCIP
1 # Copyright (C) 2021 - 2023 Christian Ledermann
2 #
3 # This library is free software; you can redistribute it and/or modify it under
4 # the terms of the GNU Lesser General Public License as published by the Free
5 # Software Foundation; either version 2.1 of the License, or (at your option)
6 # any later version.
7 #
8 # This library is distributed in the hope that it will be useful, but WITHOUT
9 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11 # details.
12 #
13 # You should have received a copy of the GNU Lesser General Public License
14 # along with this library; if not, write to the Free Software Foundation, Inc.,
15 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17 """Test the kml overlay classes."""
18
19 import contextlib
20
21 from pygeoif import geometry as geo
22
23 from fastkml import enums
24 from fastkml import geometry
25 from fastkml import links
26 from fastkml import overlays
27 from fastkml import views
28 from fastkml.enums import AltitudeMode
29 from fastkml.enums import Units
30 from tests.base import Lxml
31 from tests.base import StdLibrary
32
33
34 class TestScreenOverlay(StdLibrary):
35 def test_screen_overlay_from_string(self) -> None:
36 """Create a ScreenOverlay object with all optional parameters."""
37 doc = (
38 '<ScreenOverlay xmlns="http://www.opengis.net/kml/2.2">'
39 "<name>Simple crosshairs</name>"
40 "<visibility>0</visibility>"
41 "<description>This screen overlay uses fractional positioning to put the "
42 "image in the exact center of the screen</description>"
43 "<Icon>"
44 "<href>http://developers.google.com/kml/images/crosshairs.png</href>"
45 "</Icon>"
46 '<overlayXY x="0.5" y="0.5" xunits="fraction" yunits="fraction"/>'
47 '<screenXY x="0.5" y="0.5" xunits="fraction" yunits="fraction"/>'
48 '<rotationXY x="0.5" y="0.5" xunits="fraction" yunits="fraction"/>'
49 '<size x="0" y="0" xunits="pixels" yunits="pixels"/>'
50 "<rotation>-45</rotation>"
51 "</ScreenOverlay>"
52 )
53
54 screen_overlay = overlays.ScreenOverlay.from_string(doc)
55
56 assert screen_overlay == overlays.ScreenOverlay(
57 name="Simple crosshairs",
58 visibility=False,
59 description=(
60 "This screen overlay uses fractional positioning to put the image "
61 "in the exact center of the screen"
62 ),
63 icon=links.Icon(
64 href="http://developers.google.com/kml/images/crosshairs.png",
65 ),
66 overlay_xy=overlays.OverlayXY(
67 x=0.5,
68 y=0.5,
69 x_units=Units.fraction,
70 y_units=Units.fraction,
71 ),
72 screen_xy=overlays.ScreenXY(
73 x=0.5,
74 y=0.5,
75 x_units=Units.fraction,
76 y_units=Units.fraction,
77 ),
78 rotation_xy=overlays.RotationXY(
79 x=0.5,
80 y=0.5,
81 x_units=Units.fraction,
82 y_units=Units.fraction,
83 ),
84 size=overlays.Size(
85 x=0.0,
86 y=0.0,
87 x_units=Units.pixels,
88 y_units=Units.pixels,
89 ),
90 rotation=-45,
91 )
92 with contextlib.suppress(TypeError):
93 screen_overlay.validate()
94
95
96 class TestGroundOverlay(StdLibrary):
97 def test_default_to_string(self) -> None:
98 g = overlays.GroundOverlay()
99
100 expected = overlays.GroundOverlay.from_string(
101 '<kml:GroundOverlay xmlns:kml="http://www.opengis.net/kml/2.2">'
102 "</kml:GroundOverlay>",
103 )
104 assert g.to_string() == expected.to_string()
105
106 def test_to_string(self) -> None:
107 g = overlays.GroundOverlay()
108 icon = links.Icon(href="http://example.com")
109 g.icon = icon
110 g.draw_order = 1
111 g.color = "00010203"
112
113 expected = overlays.GroundOverlay.from_string(
114 '<kml:GroundOverlay xmlns:kml="http://www.opengis.net/kml/2.2">'
115 "<kml:color>00010203</kml:color>"
116 "<kml:drawOrder>1</kml:drawOrder>"
117 "<kml:Icon>"
118 "<kml:href>http://example.com</kml:href>"
119 "</kml:Icon>"
120 "</kml:GroundOverlay>",
121 )
122
123 assert g.to_string() == expected.to_string()
124
125 def test_altitude_from_int(self) -> None:
126 g = overlays.GroundOverlay()
127 g.altitude = 123.0
128
129 expected = overlays.GroundOverlay.from_string(
130 '<kml:GroundOverlay xmlns:kml="http://www.opengis.net/kml/2.2">'
131 "<kml:altitude>123</kml:altitude>"
132 "</kml:GroundOverlay>",
133 )
134
135 assert g.to_string() == expected.to_string()
136
137 def test_altitude_from_float(self) -> None:
138 g = overlays.GroundOverlay()
139 g.altitude = 123.4
140
141 expected = overlays.GroundOverlay.from_string(
142 '<kml:GroundOverlay xmlns:kml="http://www.opengis.net/kml/2.2">'
143 "<kml:altitude>123.4</kml:altitude>"
144 "</kml:GroundOverlay>",
145 )
146
147 assert g.to_string() == expected.to_string()
148
149 def test_altitude_invalid(self) -> None:
150 g = overlays.GroundOverlay.from_string(
151 '<kml:GroundOverlay xmlns:kml="http://www.opengis.net/kml/2.2">'
152 "<kml:altitude> one two</kml:altitude>"
153 "</kml:GroundOverlay>",
154 strict=False,
155 )
156
157 assert g.altitude is None
158
159 def test_draw_order_from_invalid(self) -> None:
160 g = overlays.GroundOverlay.from_string(
161 '<kml:GroundOverlay xmlns:kml="http://www.opengis.net/kml/2.2">'
162 "<kml:drawOrder>nan</kml:drawOrder>"
163 "</kml:GroundOverlay>",
164 strict=False,
165 )
166
167 assert g.draw_order is None
168
169 def test_altitude_from_string(self) -> None:
170 g = overlays.GroundOverlay(
171 altitude=123.4,
172 altitude_mode=AltitudeMode.clamp_to_ground,
173 )
174
175 expected = overlays.GroundOverlay.from_string(
176 '<kml:GroundOverlay xmlns:kml="http://www.opengis.net/kml/2.2">'
177 "<kml:altitude>123.4</kml:altitude>"
178 "<kml:altitudeMode>clampToGround</kml:altitudeMode>"
179 "</kml:GroundOverlay>",
180 )
181
182 assert g.to_string() == expected.to_string()
183
184 def test_altitude_mode_absolute(self) -> None:
185 g = overlays.GroundOverlay(altitude=123.4, altitude_mode=AltitudeMode.absolute)
186 expected = overlays.GroundOverlay.from_string(
187 '<kml:GroundOverlay xmlns:kml="http://www.opengis.net/kml/2.2">'
188 "<kml:altitude>123.4</kml:altitude>"
189 "<kml:altitudeMode>absolute</kml:altitudeMode>"
190 "</kml:GroundOverlay>",
191 )
192
193 assert g.to_string() == expected.to_string()
194
195 def test_latlonbox_no_rotation(self) -> None:
196 llb = overlays.LatLonBox(
197 ns="{http://www.opengis.net/kml/2.2}",
198 north=10.0,
199 south=20.0,
200 east=30.0,
201 west=40.0,
202 )
203 g = overlays.GroundOverlay(lat_lon_box=llb)
204
205 expected = overlays.GroundOverlay.from_string(
206 '<kml:GroundOverlay xmlns:kml="http://www.opengis.net/kml/2.2">'
207 "<kml:LatLonBox>"
208 "<kml:north>10</kml:north>"
209 "<kml:south>20</kml:south>"
210 "<kml:east>30</kml:east>"
211 "<kml:west>40</kml:west>"
212 "</kml:LatLonBox>"
213 "</kml:GroundOverlay>",
214 )
215
216 assert g.to_string() == expected.to_string()
217
218 def test_latlonbox_rotation(self) -> None:
219 llb = overlays.LatLonBox(
220 ns="{http://www.opengis.net/kml/2.2}",
221 north=10.0,
222 south=20.0,
223 east=30.0,
224 west=40.0,
225 rotation=50.0,
226 )
227 g = overlays.GroundOverlay(lat_lon_box=llb)
228
229 expected = overlays.GroundOverlay.from_string(
230 '<kml:GroundOverlay xmlns:kml="http://www.opengis.net/kml/2.2">'
231 "<kml:LatLonBox>"
232 "<kml:north>10</kml:north>"
233 "<kml:south>20</kml:south>"
234 "<kml:east>30</kml:east>"
235 "<kml:west>40</kml:west>"
236 "<kml:rotation>50</kml:rotation>"
237 "</kml:LatLonBox>"
238 "</kml:GroundOverlay>",
239 )
240
241 assert g.to_string() == expected.to_string()
242
243 def test_latlonbox_nswer(self) -> None:
244 llb = overlays.LatLonBox(
245 ns="{http://www.opengis.net/kml/2.2}",
246 north=10.0,
247 south=20.0,
248 east=30.0,
249 west=40.0,
250 rotation=50.0,
251 )
252 g = overlays.GroundOverlay()
253 g.lat_lon_box = llb
254 expected = overlays.GroundOverlay.from_string(
255 '<kml:GroundOverlay xmlns:kml="http://www.opengis.net/kml/2.2">'
256 "<kml:LatLonBox>"
257 "<kml:north>10</kml:north>"
258 "<kml:south>20</kml:south>"
259 "<kml:east>30</kml:east>"
260 "<kml:west>40</kml:west>"
261 "<kml:rotation>50</kml:rotation>"
262 "</kml:LatLonBox>"
263 "</kml:GroundOverlay>",
264 )
265
266 assert g.to_string() == expected.to_string()
267
268
269 class TestPhotoOverlay(StdLibrary):
270 def test_create_photo_overlay_with_all_optional_parameters(self) -> None:
271 """Create a PhotoOverlay object with all optional parameters."""
272 photo_overlay = overlays.PhotoOverlay(
273 id="photo_overlay_1",
274 name="Photo Overlay",
275 visibility=True,
276 description="This is a photo overlay",
277 icon=links.Icon(href="https://example.com/photo.jpg"),
278 view=views.LookAt(
279 longitude=-122.0822035425683,
280 latitude=37.42228990140251,
281 altitude=0,
282 heading=0,
283 tilt=0,
284 range=1000,
285 altitude_mode=AltitudeMode.clamp_to_ground,
286 ),
287 point=geometry.Point(
288 id="point_1",
289 geometry=geo.Point(-122.0822035425683, 37.42228990140251, 0),
290 ),
291 shape=enums.Shape.rectangle,
292 rotation=0,
293 view_volume=overlays.ViewVolume(
294 left_fov=-60,
295 right_fov=60,
296 bottom_fov=-45,
297 top_fov=45,
298 near=1,
299 ),
300 image_pyramid=overlays.ImagePyramid(
301 tile_size=256,
302 max_width=2048,
303 max_height=2048,
304 grid_origin=enums.GridOrigin.lower_left,
305 ),
306 )
307 assert photo_overlay.id == "photo_overlay_1"
308 assert photo_overlay.name == "Photo Overlay"
309 assert photo_overlay.visibility
310 assert photo_overlay.description == "This is a photo overlay"
311 assert photo_overlay.shape == enums.Shape.rectangle
312 assert photo_overlay.rotation == 0
313 assert photo_overlay.view_volume.__bool__() is True
314 assert bool(photo_overlay.view_volume)
315 assert bool(photo_overlay.image_pyramid)
316
317 def test_read_photo_overlay(self) -> None:
318 """Read a PhotoOverlay object from a KML file."""
319 doc = (
320 '<kml:PhotoOverlay xmlns:kml="http://www.opengis.net/kml/2.2" '
321 'id="photo_overlay_1"><kml:name>Photo Overlay</kml:name>'
322 "<kml:visibility>1</kml:visibility>"
323 "<kml:description>This is a photo overlay</kml:description>"
324 "<kml:LookAt><kml:longitude>-122.0822035425683</kml:longitude>"
325 "<kml:latitude>37.42228990140251</kml:latitude>"
326 "<kml:altitude>0</kml:altitude><kml:heading>0</kml:heading>"
327 "<kml:tilt>0</kml:tilt><kml:altitudeMode>clampToGround</kml:altitudeMode>"
328 "<kml:range>1000</kml:range></kml:LookAt>"
329 "<kml:Icon><kml:href>https://example.com/photo.jpg</kml:href></kml:Icon>"
330 "<kml:rotation>0</kml:rotation>"
331 "<kml:ViewVolume><kml:leftFov>-60</kml:leftFov>"
332 "<kml:rightFov>60</kml:rightFov><kml:bottomFov>-45</kml:bottomFov>"
333 "<kml:topFov>45</kml:topFov><kml:near>1</kml:near>"
334 "</kml:ViewVolume><kml:ImagePyramid><kml:tileSize>256</kml:tileSize>"
335 "<kml:maxWidth>2048</kml:maxWidth><kml:maxHeight>2048</kml:maxHeight>"
336 "<kml:gridOrigin>lowerLeft</kml:gridOrigin></kml:ImagePyramid>"
337 '<kml:Point id="point_1">'
338 "<kml:coordinates>-122.082204,37.422290,0.000000</kml:coordinates>"
339 "</kml:Point><kml:shape>rectangle</kml:shape></kml:PhotoOverlay>"
340 )
341
342 p_overlay = overlays.PhotoOverlay.from_string(doc)
343
344 assert p_overlay.id == "photo_overlay_1"
345 assert p_overlay.name == "Photo Overlay"
346 assert p_overlay.visibility
347 assert p_overlay.description == "This is a photo overlay"
348 assert p_overlay.shape == enums.Shape.rectangle
349 assert p_overlay.rotation == 0
350 assert p_overlay.view.longitude == -122.0822035425683
351 assert p_overlay.view.latitude == 37.42228990140251
352 assert p_overlay.view.altitude == 0
353 assert p_overlay.view.heading == 0
354 assert p_overlay.view.tilt == 0
355 assert p_overlay.view.range == 1000
356 assert p_overlay.view.altitude_mode == AltitudeMode.clamp_to_ground
357 assert p_overlay.icon.href == "https://example.com/photo.jpg"
358 assert p_overlay.view_volume.left_fov == -60
359 assert p_overlay.view_volume.right_fov == 60
360 assert p_overlay.view_volume.bottom_fov == -45
361 assert p_overlay.view_volume.top_fov == 45
362 assert p_overlay.view_volume.near == 1
363 assert p_overlay.image_pyramid.tile_size == 256
364 assert p_overlay.image_pyramid.max_width == 2048
365 assert p_overlay.image_pyramid.max_height == 2048
366 assert p_overlay.image_pyramid.grid_origin == enums.GridOrigin.lower_left
367 assert p_overlay.point.id == "point_1"
368 assert p_overlay.point.geometry.x == -122.082204
369 assert p_overlay.point.geometry.y == 37.422290
370 assert p_overlay.point.geometry.z == 0
371
372 def test_camera_altitude_int(self) -> None:
373 po = overlays.PhotoOverlay(view=views.Camera())
374 po.view.altitude = 123
375 assert po.view.altitude == 123
376
377 def test_camera_altitude_float(self) -> None:
378 po = overlays.PhotoOverlay(view=views.Camera())
379 po.view.altitude = 123.4
380 assert po.view.altitude == 123.4
381
382 def test_camera_altitude_string(self) -> None:
383 po = overlays.PhotoOverlay(view=views.Camera())
384 po.view.altitude = 123
385 assert po.view.altitude == 123
386
387 def test_camera_altitude_none(self) -> None:
388 po = overlays.PhotoOverlay(view=views.Camera())
389 po.view.altitude = 123
390 assert po.view.altitude == 123
391 po.view.altitude = None
392 assert po.view.altitude is None
393
394 def test_camera_altitude_mode_default(self) -> None:
395 po = overlays.PhotoOverlay(view=views.Camera())
396 assert po.view.altitude_mode is None
397
398 def test_camera_altitude_mode_clamp(self) -> None:
399 po = overlays.PhotoOverlay(view=views.Camera())
400 po.view.altitude_mode = AltitudeMode("clampToGround")
401 assert po.view.altitude_mode == AltitudeMode("clampToGround")
402
403 def test_camera_altitude_mode_absolute(self) -> None:
404 po = overlays.PhotoOverlay(view=views.Camera())
405 po.view.altitude_mode = "absolute"
406 assert po.view.altitude_mode == "absolute"
407
408 def test_camera_initialization(self) -> None:
409 po = overlays.PhotoOverlay(view=views.Camera())
410 po.view = views.Camera(
411 longitude=10,
412 latitude=20,
413 altitude=30,
414 heading=40,
415 tilt=50,
416 roll=60,
417 )
418 assert po.view.longitude == 10
419 assert po.view.latitude == 20
420 assert po.view.altitude == 30
421 assert po.view.heading == 40
422 assert po.view.tilt == 50
423 assert po.view.roll == 60
424
425
426 class TestScreenOverlayLxml(Lxml, TestScreenOverlay):
427 """Test with lxml."""
428
429
430 class TestGroundOverlayLxml(Lxml, TestGroundOverlay):
431 """Test with lxml."""
432
433
434 class TestPhotoOverlayLxml(Lxml, TestPhotoOverlay):
435 """Test with lxml."""