Debian Codegraph @

tests/gx/track_test.py

raw symbols SCIP
1 # Copyright (C) 2021 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 gx classes."""
18
19 import datetime
20
21 import pygeoif.geometry as geo
22 import pytest
23 from dateutil.tz import tzoffset
24 from dateutil.tz import tzutc
25
26 from fastkml.data import ExtendedData
27 from fastkml.data import SchemaData
28 from fastkml.gx import Angle
29 from fastkml.gx import MultiTrack
30 from fastkml.gx import Track
31 from fastkml.gx import TrackItem
32 from fastkml.gx.data import SimpleArrayData
33 from fastkml.times import KmlDateTime
34 from tests.base import Lxml
35 from tests.base import StdLibrary
36
37
38 class TestGetGxGeometry(StdLibrary):
39 def test_track(self) -> None:
40 doc = """<gx:Track xmlns:kml="http://www.opengis.net/kml/2.2"
41 xmlns:gx="http://www.google.com/kml/ext/2.2">
42 <when>2020-01-01T00:00:00Z</when>
43 <when>2020-01-01T00:10:00Z</when>
44 <gx:coord>0.000000 0.000000</gx:coord>
45 <gx:coord>1.000000 1.000000</gx:coord>
46 </gx:Track>"""
47 g = Track.from_string(doc, ns="")
48
49 assert g.geometry
50 assert g.geometry.__geo_interface__ == {
51 "type": "LineString",
52 "bbox": (0.0, 0.0, 1.0, 1.0),
53 "coordinates": ((0.0, 0.0), (1.0, 1.0)),
54 }
55
56 def test_track_etree_element(self) -> None:
57 g = Track()
58
59 g.etree_element()
60
61 assert g.track_items == []
62
63 def test_multitrack(self) -> None:
64 doc = """
65 <gx:MultiTrack xmlns:kml="http://www.opengis.net/kml/2.2"
66 xmlns:gx="http://www.google.com/kml/ext/2.2">
67 <gx:Track>
68 <kml:when>2020-01-01T00:00:00+00:00</kml:when>
69 <kml:when>2020-01-01T00:10:00+00:00</kml:when>
70 <gx:coord>0.000000 0.000000</gx:coord>
71 <gx:coord>1.000000 0.000000</gx:coord>
72 </gx:Track>
73 <gx:Track>
74 <kml:when>2020-01-01T00:10:00+00:00</kml:when>
75 <kml:when>2020-01-01T00:20:00+00:00</kml:when>
76 <gx:coord>0.000000 1.000000</gx:coord>
77 <gx:coord>1.000000 1.000000</gx:coord>
78 </gx:Track>
79 </gx:MultiTrack>
80 """
81
82 mt = MultiTrack.from_string(doc)
83
84 assert mt.geometry == geo.MultiLineString(
85 (((0.0, 0.0), (1.0, 0.0)), ((0.0, 1.0), (1.0, 1.0))),
86 )
87 assert "when>" in mt.to_string()
88 assert mt == MultiTrack(
89 tracks=[
90 Track(
91 track_items=[
92 TrackItem(
93 when=KmlDateTime(
94 dt=datetime.datetime(
95 2020,
96 1,
97 1,
98 0,
99 0,
100 tzinfo=tzoffset(None, 0),
101 ),
102 ),
103 coord=geo.Point(0.0, 0.0),
104 angle=Angle(heading=0.0, tilt=0.0, roll=0.0),
105 ),
106 TrackItem(
107 when=KmlDateTime(
108 dt=datetime.datetime(
109 2020,
110 1,
111 1,
112 0,
113 10,
114 tzinfo=tzoffset(None, 0),
115 ),
116 ),
117 coord=geo.Point(1.0, 0.0),
118 angle=Angle(heading=0.0, tilt=0.0, roll=0.0),
119 ),
120 ],
121 ),
122 Track(
123 track_items=[
124 TrackItem(
125 when=KmlDateTime(
126 dt=datetime.datetime(
127 2020,
128 1,
129 1,
130 0,
131 10,
132 tzinfo=tzoffset(None, 0),
133 ),
134 ),
135 coord=geo.Point(0.0, 1.0),
136 angle=Angle(heading=0.0, tilt=0.0, roll=0.0),
137 ),
138 TrackItem(
139 when=KmlDateTime(
140 dt=datetime.datetime(
141 2020,
142 1,
143 1,
144 0,
145 20,
146 tzinfo=tzoffset(None, 0),
147 ),
148 ),
149 coord=geo.Point(1.0, 1.0),
150 angle=Angle(heading=0.0, tilt=0.0, roll=0.0),
151 ),
152 ],
153 ),
154 ],
155 )
156
157
158 class TestTrack(StdLibrary):
159 """Test gx.Track."""
160
161 def test_track_from_track_items(self) -> None:
162 time1 = KmlDateTime(
163 datetime.datetime(2023, 1, 1, 0, 0, 0, tzinfo=datetime.timezone.utc),
164 )
165 angle = Angle()
166 track_items = [TrackItem(when=time1, coord=geo.Point(1, 2), angle=angle)]
167
168 track = Track(
169 ns="",
170 track_items=track_items,
171 )
172
173 assert "when>" in track.to_string()
174 assert ">2023-01-01T00:00:00+00:00</" in track.to_string()
175 assert "coord>" in track.to_string()
176 assert ">1 2</" in track.to_string()
177 assert "angles>" in track.to_string()
178 assert ">0.0 0.0 0.0</" in track.to_string()
179
180 def test_track_from_whens_and_coords(self) -> None:
181 whens = [
182 KmlDateTime(
183 datetime.datetime(2023, 1, 1, 0, 0, 0, tzinfo=datetime.timezone.utc),
184 ),
185 ]
186 coords = [(1, 2)]
187
188 track = Track(
189 whens=whens,
190 coords=coords,
191 )
192
193 assert "when>" in track.to_string()
194 assert ">2023-01-01T00:00:00+00:00</" in track.to_string()
195 assert "coord>" in track.to_string()
196 assert ">1 2</" in track.to_string()
197 assert track.coords == ((1, 2),)
198
199 def test_track_from_whens_and_coords_and_track_items(self) -> None:
200 whens = [
201 KmlDateTime(
202 datetime.datetime(2023, 1, 1, 0, 0, 0, tzinfo=datetime.timezone.utc),
203 ),
204 ]
205 coords = [(1, 2)]
206 time1 = KmlDateTime(
207 datetime.datetime(2023, 1, 1, 0, 0, 0, tzinfo=datetime.timezone.utc),
208 )
209 angle = Angle()
210 track_items = [TrackItem(when=time1, coord=geo.Point(1, 2), angle=angle)]
211
212 with pytest.raises(
213 ValueError,
214 match=r"^Cannot specify both geometry and track_items$",
215 ):
216 Track(
217 whens=whens,
218 coords=coords,
219 track_items=track_items,
220 )
221
222 def test_track_precision(self) -> None:
223 track = Track(
224 id="x",
225 target_id="y",
226 altitude_mode=None,
227 track_items=[
228 TrackItem(
229 when=KmlDateTime(
230 dt=datetime.datetime(2010, 5, 28, 2, 2, 9, tzinfo=tzutc()),
231 ),
232 coord=geo.Point(-122.207881, 37.371915, 156.0),
233 angle=Angle(heading=45.54676, tilt=66.2342, roll=77.0),
234 ),
235 TrackItem(
236 when=KmlDateTime(
237 dt=datetime.datetime(2010, 5, 28, 2, 2, 35, tzinfo=tzutc()),
238 ),
239 coord=geo.Point(-122.205712, 37.373288, 152.0),
240 angle=Angle(heading=1.0, tilt=2.0, roll=3.0),
241 ),
242 ],
243 )
244
245 xml = track.to_string(precision=2)
246 assert "angles>45.55 66.23 77.00</" in xml
247 assert "angles>1.00 2.00 3.00</" in xml
248 assert "coord>-122.21 37.37 156.00</" in xml
249 assert "coord>-122.21 37.37 152.00</" in xml
250
251 def test_track_from_str(self) -> None:
252 doc = """
253 <gx:Track xmlns:gx="http://www.google.com/kml/ext/2.2"
254 xmlns:kml="http://www.opengis.net/kml/2.2">
255 <kml:when>2010-05-28T02:02:09Z</kml:when>
256 <kml:when>2010-05-28T02:02:35Z</kml:when>
257 <kml:when>2010-05-28T02:02:44Z</kml:when>
258 <kml:when>2010-05-28T02:02:53Z</kml:when>
259 <kml:when>2010-05-28T02:02:54Z</kml:when>
260 <kml:when>2010-05-28T02:02:55Z</kml:when>
261 <kml:when>2010-05-28T02:02:56Z</kml:when>
262 <kml:when>2010-05-28T02:02:57Z</kml:when>
263 <gx:angles>45.54676 66.2342 77.0</gx:angles>
264 <gx:angles />
265 <gx:angles>1 2 3</gx:angles>
266 <gx:angles>1 2 3</gx:angles>
267 <gx:angles>1 2 3</gx:angles>
268 <gx:angles>1 2 3</gx:angles>
269 <gx:angles>1 2 3</gx:angles>
270 <gx:angles>1 2 3</gx:angles>
271 <gx:coord>-122.207881 37.371915 156.000000</gx:coord>
272 <gx:coord>-122.205712 37.373288 152.000000</gx:coord>
273 <gx:coord>-122.204678 37.373939 147.000000</gx:coord>
274 <gx:coord>-122.203572 37.374630 142.199997</gx:coord>
275 <gx:coord>-112.203451 37.374690 141.800000</gx:coord>
276 <gx:coord>-122.203451 37.374706 141.800003</gx:coord>
277 <gx:coord>-122.203329 37.374780 141.199997</gx:coord>
278 <gx:coord>-122.203207 37.374857 140.199997</gx:coord>
279 </gx:Track>
280 """
281 expected_track = Track(
282 ns="{http://www.google.com/kml/ext/2.2}",
283 name_spaces={
284 "kml": "{http://www.opengis.net/kml/2.2}",
285 "atom": "{http://www.w3.org/2005/Atom}",
286 "gx": "{http://www.google.com/kml/ext/2.2}",
287 },
288 id="",
289 target_id="",
290 altitude_mode=None,
291 track_items=[
292 TrackItem(
293 when=KmlDateTime(
294 dt=datetime.datetime(2010, 5, 28, 2, 2, 9, tzinfo=tzutc()),
295 ),
296 coord=geo.Point(-122.207881, 37.371915, 156.0),
297 angle=Angle(heading=45.54676, tilt=66.2342, roll=77.0),
298 ),
299 TrackItem(
300 when=KmlDateTime(
301 dt=datetime.datetime(2010, 5, 28, 2, 2, 35, tzinfo=tzutc()),
302 ),
303 coord=geo.Point(-122.205712, 37.373288, 152.0),
304 angle=Angle(heading=1.0, tilt=2.0, roll=3.0),
305 ),
306 TrackItem(
307 when=KmlDateTime(
308 dt=datetime.datetime(2010, 5, 28, 2, 2, 44, tzinfo=tzutc()),
309 ),
310 coord=geo.Point(-122.204678, 37.373939, 147.0),
311 angle=Angle(heading=1.0, tilt=2.0, roll=3.0),
312 ),
313 TrackItem(
314 when=KmlDateTime(
315 dt=datetime.datetime(2010, 5, 28, 2, 2, 53, tzinfo=tzutc()),
316 ),
317 coord=geo.Point(-122.203572, 37.37463, 142.199997),
318 angle=Angle(heading=1.0, tilt=2.0, roll=3.0),
319 ),
320 TrackItem(
321 when=KmlDateTime(
322 dt=datetime.datetime(2010, 5, 28, 2, 2, 54, tzinfo=tzutc()),
323 ),
324 coord=geo.Point(-112.203451, 37.37469, 141.8),
325 angle=Angle(heading=1.0, tilt=2.0, roll=3.0),
326 ),
327 TrackItem(
328 when=KmlDateTime(
329 dt=datetime.datetime(2010, 5, 28, 2, 2, 55, tzinfo=tzutc()),
330 ),
331 coord=geo.Point(-122.203451, 37.374706, 141.800003),
332 angle=Angle(heading=1.0, tilt=2.0, roll=3.0),
333 ),
334 TrackItem(
335 when=KmlDateTime(
336 dt=datetime.datetime(2010, 5, 28, 2, 2, 56, tzinfo=tzutc()),
337 ),
338 coord=geo.Point(-122.203329, 37.37478, 141.199997),
339 angle=Angle(heading=1.0, tilt=2.0, roll=3.0),
340 ),
341 TrackItem(
342 when=KmlDateTime(
343 dt=datetime.datetime(2010, 5, 28, 2, 2, 57, tzinfo=tzutc()),
344 ),
345 coord=geo.Point(-122.203207, 37.374857, 140.199997),
346 angle=Angle(heading=0.0, tilt=0.0, roll=0.0),
347 ),
348 ],
349 )
350
351 track = Track.from_string(doc)
352
353 assert track.geometry == geo.LineString(
354 (
355 (-122.207881, 37.371915, 156.0),
356 (-122.205712, 37.373288, 152.0),
357 (-122.204678, 37.373939, 147.0),
358 (-122.203572, 37.37463, 142.199997),
359 (-112.203451, 37.37469, 141.8),
360 (-122.203451, 37.374706, 141.800003),
361 (-122.203329, 37.37478, 141.199997),
362 (-122.203207, 37.374857, 140.199997),
363 ),
364 )
365
366 assert track.to_string() == expected_track.to_string()
367
368 def test_track_from_str_invalid_when(self) -> None:
369 doc = """
370 <gx:Track xmlns:gx="http://www.google.com/kml/ext/2.2"
371 xmlns:kml="http://www.opengis.net/kml/2.2">
372 <kml:when>2010-02-32T02:02:09Z</kml:when>
373 <gx:angles>45.54676 66.2342 77.0</gx:angles>
374 <gx:coord>-122.207881 37.371915 156.000000</gx:coord>
375 </gx:Track>
376 """
377
378 track = Track.from_string(doc, strict=False)
379
380 assert track.track_items == []
381
382 def test_track_from_str_invalid_coord(self) -> None:
383 doc = """
384 <gx:Track xmlns:gx="http://www.google.com/kml/ext/2.2"
385 xmlns:kml="http://www.opengis.net/kml/2.2">
386 <kml:when>2010-02-14T02:02:09Z</kml:when>
387 <gx:angles>45.54676 66.2342 77.0</gx:angles>
388 <gx:coord>XYZ 37.371915 156.000000</gx:coord>
389 </gx:Track>
390 """
391
392 track = Track.from_string(doc, strict=False)
393
394 assert track.track_items == []
395
396 def test_track_from_str_extended_data(self) -> None:
397 doc = (
398 '<gx:Track xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2">'
399 "<when>2010-05-28T02:02:09Z</when>"
400 "<when>2010-05-28T02:02:35Z</when>"
401 "<when>2010-05-28T02:02:44Z</when>"
402 "<when>2010-05-28T02:02:53Z</when>"
403 "<when>2010-05-28T02:02:54Z</when>"
404 "<when>2010-05-28T02:02:55Z</when>"
405 "<when>2010-05-28T02:02:56Z</when>"
406 "<gx:coord>-122.207881 37.371915 156.000000</gx:coord>"
407 "<gx:coord>-122.205712 37.373288 152.000000</gx:coord>"
408 "<gx:coord>-122.204678 37.373939 147.000000</gx:coord>"
409 "<gx:coord>-122.203572 37.374630 142.199997</gx:coord>"
410 "<gx:coord>-122.203451 37.374706 141.800003</gx:coord>"
411 "<gx:coord>-122.203329 37.374780 141.199997</gx:coord>"
412 "<gx:coord>-122.203207 37.374857 140.199997</gx:coord>"
413 "<kml:ExtendedData>"
414 '<kml:SchemaData schemaUrl="#schema">'
415 '<gx:SimpleArrayData name="cadence">'
416 "<gx:value>86</gx:value>"
417 "<gx:value>103</gx:value>"
418 "<gx:value>108</gx:value>"
419 "<gx:value>113</gx:value>"
420 "<gx:value>113</gx:value>"
421 "<gx:value>113</gx:value>"
422 "<gx:value>113</gx:value>"
423 "</gx:SimpleArrayData>"
424 '<gx:SimpleArrayData name="heartrate">'
425 "<gx:value>181</gx:value>"
426 "<gx:value>177</gx:value>"
427 "<gx:value>175</gx:value>"
428 "<gx:value>173</gx:value>"
429 "<gx:value>173</gx:value>"
430 "<gx:value>173</gx:value>"
431 "<gx:value>173</gx:value>"
432 "</gx:SimpleArrayData>"
433 '<gx:SimpleArrayData name="power">'
434 "<gx:value>327.0</gx:value>"
435 "<gx:value>177.0</gx:value>"
436 "<gx:value>179.0</gx:value>"
437 "<gx:value>162.0</gx:value>"
438 "<gx:value>166.0</gx:value>"
439 "<gx:value>177.0</gx:value>"
440 "<gx:value>183.0</gx:value>"
441 "</gx:SimpleArrayData>"
442 "</kml:SchemaData>"
443 "</kml:ExtendedData>"
444 "</gx:Track>"
445 )
446 expected_data = ExtendedData(
447 elements=[
448 SchemaData(
449 schema_url="#schema",
450 array_data=[
451 SimpleArrayData(
452 name="cadence",
453 data=["86", "103", "108", "113", "113", "113", "113"],
454 ),
455 SimpleArrayData(
456 name="heartrate",
457 data=["181", "177", "175", "173", "173", "173", "173"],
458 ),
459 SimpleArrayData(
460 name="power",
461 data=[
462 "327.0",
463 "177.0",
464 "179.0",
465 "162.0",
466 "166.0",
467 "177.0",
468 "183.0",
469 ],
470 ),
471 ],
472 ),
473 ],
474 )
475 track = Track.from_string(doc)
476 assert track.extended_data == expected_data
477
478
479 class TestMultiTrack(StdLibrary):
480 """Test gx.MultiTrack."""
481
482 def test_multitrack(self) -> None:
483 track = MultiTrack(
484 ns="",
485 interpolate=True,
486 tracks=[
487 Track(
488 ns="",
489 track_items=[
490 TrackItem(
491 when=KmlDateTime(
492 datetime.datetime(
493 2010,
494 5,
495 28,
496 2,
497 2,
498 55,
499 tzinfo=tzutc(),
500 ),
501 ),
502 coord=geo.Point(0, 0),
503 angle=None,
504 ),
505 TrackItem(
506 when=KmlDateTime(
507 datetime.datetime(
508 2010,
509 5,
510 28,
511 2,
512 2,
513 56,
514 tzinfo=tzutc(),
515 ),
516 ),
517 coord=geo.Point(1, 1),
518 angle=None,
519 ),
520 TrackItem(
521 when=KmlDateTime(
522 datetime.datetime(
523 2010,
524 5,
525 28,
526 2,
527 2,
528 57,
529 tzinfo=tzutc(),
530 ),
531 ),
532 coord=geo.Point(1, 2),
533 angle=None,
534 ),
535 TrackItem(
536 when=KmlDateTime(
537 datetime.datetime(
538 2010,
539 5,
540 28,
541 2,
542 2,
543 58,
544 tzinfo=tzutc(),
545 ),
546 ),
547 coord=geo.Point(2, 2),
548 angle=None,
549 ),
550 ],
551 ),
552 Track(
553 ns="",
554 track_items=[
555 TrackItem(
556 when=KmlDateTime(
557 datetime.datetime(
558 2010,
559 5,
560 28,
561 2,
562 2,
563 55,
564 tzinfo=tzutc(),
565 ),
566 ),
567 coord=geo.Point(-122.203451, 37.374706, 141.800003),
568 angle=Angle(heading=1.0, tilt=2.0, roll=3.0),
569 ),
570 TrackItem(
571 when=KmlDateTime(
572 datetime.datetime(
573 2010,
574 5,
575 28,
576 2,
577 2,
578 56,
579 tzinfo=tzutc(),
580 ),
581 ),
582 coord=geo.Point(-122.203329, 37.37478, 141.199997),
583 angle=Angle(heading=1.0, tilt=2.0, roll=3.0),
584 ),
585 ],
586 ),
587 ],
588 )
589
590 assert track.geometry == geo.MultiLineString(
591 (
592 ((0, 0), (1, 1), (1, 2), (2, 2)),
593 (
594 (-122.203451, 37.374706, 141.800003),
595 (-122.203329, 37.37478, 141.199997),
596 ),
597 ),
598 )
599 assert "MultiTrack>" in track.to_string()
600 assert "interpolate>1</" in track.to_string()
601 assert "Track>" in track.to_string()
602 assert "coord>" in track.to_string()
603 assert "angles>" in track.to_string()
604 assert "when>" in track.to_string()
605
606
607 class TestLxmlGetGxGeometry(Lxml, TestGetGxGeometry):
608 """Test with lxml."""
609
610
611 class TestLxmlTrack(Lxml, TestTrack):
612 """Test with lxml."""
613
614
615 class TestLxmlMultiTrack(Lxml, TestMultiTrack):
616 """Test with lxml."""