forked from tkrajina/gpxpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
2838 lines (2084 loc) · 131 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# Copyright 2011 Tomo Krajina
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Run all tests with:
$ python -m unittest test
Run minidom parser tests with:
$ python -m unittest test.MinidomTests
Run lxml parser tests with:
$ python -m unittest test.LxmlTests
Run single test with:
$ python -m unittest test.LxmlTests.test_method
"""
from __future__ import print_function
import logging as mod_logging
import os as mod_os
import time as mod_time
import copy as mod_copy
import datetime as mod_datetime
import random as mod_random
import math as mod_math
import sys as mod_sys
import xml.dom.minidom as mod_minidom
try:
import unittest2 as mod_unittest
except ImportError:
import unittest as mod_unittest
import gpxpy as mod_gpxpy
import gpxpy.gpx as mod_gpx
import gpxpy.gpxfield as mod_gpxfield
import gpxpy.parser as mod_parser
import gpxpy.geo as mod_geo
from gpxpy.utils import make_str
PYTHON_VERSION = mod_sys.version.split(' ')[0]
mod_logging.basicConfig(level=mod_logging.DEBUG,
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
def equals(object1, object2, ignore=None):
""" Testing purposes only """
if not object1 and not object2:
return True
if not object1 or not object2:
print('Not obj2')
return False
if not object1.__class__ == object2.__class__:
print('Not obj1')
return False
attributes = []
for attr in dir(object1):
if not ignore or not attr in ignore:
if not hasattr(object1, '__call__') and not attr.startswith('_'):
if not attr in attributes:
attributes.append(attr)
for attr in attributes:
attr1 = getattr(object1, attr)
attr2 = getattr(object2, attr)
if attr1 == attr2:
return True
if not attr1 and not attr2:
return True
if not attr1 or not attr2:
print('Object differs in attribute %s (%s - %s)' % (attr, attr1, attr2))
return False
if not equals(attr1, attr2):
print('Object differs in attribute %s (%s - %s)' % (attr, attr1, attr2))
return None
return True
def cca(number1, number2):
return 1 - number1 / number2 < 0.999
def get_dom_node(dom, path):
path_parts = path.split('/')
result = dom
for path_part in path_parts:
if '[' in path_part:
tag_name = path_part.split('[')[0]
n = int(path_part.split('[')[1].replace(']', ''))
else:
tag_name = path_part
n = 0
candidates = []
for child in result.childNodes:
if child.nodeName == tag_name:
candidates.append(child)
try:
result = candidates[n]
except Exception:
raise Exception('Can\'t fint %sth child of %s' % (n, path_part))
return result
def pretty_print_xml(xml):
dom = mod_minidom.parseString(xml)
print(dom.toprettyxml())
class AbstractTests:
"""
Add tests here.
Tests will be run twice (once with Lxml and once with Minidom Parser).
If you run 'make test' then all tests will be run with python2 and python3
To be even more sure that everything works as expected -- try...
python -m unittest test.MinidomTests
...with python-lxml and without python-lxml installed.
"""
def get_parser_type(self):
raise Exception('Implement this in subclasses')
def parse(self, file, encoding=None, version = None):
if PYTHON_VERSION[0] == '3':
f = open('test_files/%s' % file, encoding=encoding)
else:
f = open('test_files/%s' % file)
parser = mod_parser.GPXParser(f, parser=self.get_parser_type())
gpx = parser.parse(version)
f.close()
if not gpx:
print('Parser error: %s' % parser.get_error())
return gpx
def reparse(self, gpx):
xml = gpx.to_xml()
parser = mod_parser.GPXParser(xml, parser=self.get_parser_type())
gpx = parser.parse()
if not gpx:
print('Parser error while reparsing: %s' % parser.get_error())
return gpx
def test_parse_with_all_parser_types(self):
self.assertTrue(mod_gpxpy.parse(open('test_files/cerknicko-jezero.gpx')))
self.assertTrue(mod_gpxpy.parse(open('test_files/cerknicko-jezero.gpx'), parser='minidom'))
#self.assertTrue(mod_gpxpy.parse(open('test_files/cerknicko-jezero.gpx'), parser='lxml'))
def test_simple_parse_function(self):
# Must not throw any exception:
mod_gpxpy.parse(open('test_files/korita-zbevnica.gpx'), parser=self.get_parser_type())
def test_simple_parse_function_invalid_xml(self):
try:
mod_gpxpy.parse('<gpx></gpx', parser=self.get_parser_type())
self.fail()
except mod_gpx.GPXException as e:
self.assertTrue(('unclosed token: line 1, column 5' in str(e)) or ('expected \'>\'' in str(e)))
self.assertTrue(isinstance(e, mod_gpx.GPXXMLSyntaxException))
self.assertTrue(e.__cause__)
try:
# more checks if lxml:
import lxml.etree as mod_etree
import xml.parsers.expat as mod_expat
self.assertTrue(isinstance(e.__cause__, mod_etree.XMLSyntaxError)
or isinstance(e.__cause__, mod_expat.ExpatError))
except:
pass
def test_creator_field(self):
gpx = self.parse('cerknicko-jezero.gpx')
self.assertEquals(gpx.creator, "GPSBabel - http://www.gpsbabel.org")
def test_no_creator_field(self):
gpx = self.parse('cerknicko-jezero-no-creator.gpx')
self.assertEquals(gpx.creator, None)
def test_to_xml_creator(self):
gpx = self.parse('cerknicko-jezero.gpx')
xml = gpx.to_xml()
self.assertTrue('creator="GPSBabel - http://www.gpsbabel.org"' in xml)
gpx2 = self.reparse(gpx)
self.assertEquals(gpx2.creator, "GPSBabel - http://www.gpsbabel.org")
def test_waypoints_equality_after_reparse(self):
gpx = self.parse('cerknicko-jezero.gpx')
gpx2 = self.reparse(gpx)
self.assertTrue(equals(gpx.waypoints, gpx2.waypoints))
self.assertTrue(equals(gpx.routes, gpx2.routes))
self.assertTrue(equals(gpx.tracks, gpx2.tracks))
self.assertTrue(equals(gpx, gpx2))
def test_waypoint_time(self):
gpx = self.parse('cerknicko-jezero.gpx')
self.assertTrue(gpx.waypoints[0].time)
self.assertTrue(isinstance(gpx.waypoints[0].time, mod_datetime.datetime))
def test_add_elevation(self):
gpx = mod_gpx.GPX()
gpx.tracks.append(mod_gpx.GPXTrack())
gpx.tracks[0].segments.append(mod_gpx.GPXTrackSegment())
gpx.tracks[0].segments[0].points.append(mod_gpx.GPXTrackPoint(latitude=12, longitude=13, elevation=100))
gpx.tracks[0].segments[0].points.append(mod_gpx.GPXTrackPoint(latitude=12, longitude=13))
gpx.add_elevation(10)
self.assertEqual(gpx.tracks[0].segments[0].points[0].elevation, 110)
self.assertEqual(gpx.tracks[0].segments[0].points[1].elevation, None)
gpx.add_elevation(-20)
self.assertEqual(gpx.tracks[0].segments[0].points[0].elevation, 90)
self.assertEqual(gpx.tracks[0].segments[0].points[1].elevation, None)
def test_get_duration(self):
gpx = mod_gpx.GPX()
gpx.tracks.append(mod_gpx.GPXTrack())
gpx.tracks[0].segments.append(mod_gpx.GPXTrackSegment())
gpx.tracks[0].segments[0].points.append(mod_gpx.GPXTrackPoint(latitude=12, longitude=13,
time=mod_datetime.datetime(2013, 1, 1, 12, 30)))
self.assertEqual(gpx.get_duration(), 0)
gpx.tracks[0].segments.append(mod_gpx.GPXTrackSegment())
gpx.tracks[0].segments[1].points.append(mod_gpx.GPXTrackPoint(latitude=12, longitude=13))
self.assertEqual(gpx.get_duration(), 0)
gpx.tracks[0].segments.append(mod_gpx.GPXTrackSegment())
gpx.tracks[0].segments[2].points.append(mod_gpx.GPXTrackPoint(latitude=12, longitude=13,
time=mod_datetime.datetime(2013, 1, 1, 12, 30)))
gpx.tracks[0].segments[2].points.append(mod_gpx.GPXTrackPoint(latitude=12, longitude=13,
time=mod_datetime.datetime(2013, 1, 1, 12, 31)))
self.assertEqual(gpx.get_duration(), 60)
def test_remove_elevation(self):
gpx = self.parse('cerknicko-jezero.gpx')
for point, track_no, segment_no, point_no in gpx.walk():
self.assertTrue(point.elevation is not None)
gpx.remove_elevation(tracks=True, waypoints=True, routes=True)
for point, track_no, segment_no, point_no in gpx.walk():
self.assertTrue(point.elevation is None)
xml = gpx.to_xml()
self.assertFalse('<ele>' in xml)
def test_remove_time(self):
gpx = self.parse('cerknicko-jezero.gpx')
for point, track_no, segment_no, point_no in gpx.walk():
self.assertTrue(point.time is not None)
gpx.remove_time()
for point, track_no, segment_no, point_no in gpx.walk():
self.assertTrue(point.time is None)
def test_has_times_false(self):
gpx = self.parse('cerknicko-without-times.gpx')
self.assertFalse(gpx.has_times())
def test_has_times(self):
gpx = self.parse('korita-zbevnica.gpx')
self.assertTrue(len(gpx.tracks) == 4)
# Empty -- True
self.assertTrue(gpx.tracks[0].has_times())
# Not times ...
self.assertTrue(not gpx.tracks[1].has_times())
# Times OK
self.assertTrue(gpx.tracks[2].has_times())
self.assertTrue(gpx.tracks[3].has_times())
def test_unicode_name(self):
gpx = self.parse('unicode.gpx', encoding='utf-8')
name = gpx.waypoints[0].name
self.assertTrue(make_str(name) == 'šđčćž')
def test_unicode_2(self):
parser = mod_parser.GPXParser(open('test_files/unicode2.gpx'), parser=self.get_parser_type())
gpx = parser.parse()
gpx.to_xml()
def test_unicode_bom(self):
gpx = self.parse('unicode_with_bom.gpx')
name = gpx.waypoints[0].name
self.assertTrue(make_str(name) == 'test')
def test_force_version(self):
gpx = self.parse('unicode_with_bom.gpx', version = '1.1')
security = gpx.waypoints[0].extensions['security']
self.assertTrue(make_str(security) == 'Open')
def test_nearest_location_1(self):
gpx = self.parse('korita-zbevnica.gpx')
location = mod_geo.Location(45.451058791, 14.027903696)
nearest_location, track_no, track_segment_no, track_point_no = gpx.get_nearest_location(location)
point = gpx.tracks[track_no].segments[track_segment_no].points[track_point_no]
self.assertTrue(point.distance_2d(location) < 0.001)
self.assertTrue(point.distance_2d(nearest_location) < 0.001)
location = mod_geo.Location(1, 1)
nearest_location, track_no, track_segment_no, track_point_no = gpx.get_nearest_location(location)
point = gpx.tracks[track_no].segments[track_segment_no].points[track_point_no]
self.assertTrue(point.distance_2d(nearest_location) < 0.001)
location = mod_geo.Location(50, 50)
nearest_location, track_no, track_segment_no, track_point_no = gpx.get_nearest_location(location)
point = gpx.tracks[track_no].segments[track_segment_no].points[track_point_no]
self.assertTrue(point.distance_2d(nearest_location) < 0.001)
def test_long_timestamps(self):
# Check if timestamps in format: 1901-12-13T20:45:52.2073437Z work
gpx = self.parse('Mojstrovka.gpx')
# %Y-%m-%dT%H:%M:%SZ'
self.assertEqual(gpx.tracks[0].segments[0].points[0].elevation, 1614.678000)
self.assertEqual(gpx.tracks[0].segments[0].points[0].time, mod_datetime.datetime(1901, 12, 13, 20, 45, 52))
def test_reduce_gpx_file(self):
f = open('test_files/Mojstrovka.gpx')
parser = mod_parser.GPXParser(f, parser=self.get_parser_type())
gpx = parser.parse()
f.close()
max_reduced_points_no = 50
started = mod_time.time()
points_original = gpx.get_track_points_no()
time_original = mod_time.time() - started
gpx.reduce_points(max_reduced_points_no)
points_reduced = gpx.get_track_points_no()
result = gpx.to_xml()
if mod_sys.version_info[0] != 3:
result = result.encode('utf-8')
started = mod_time.time()
parser = mod_parser.GPXParser(result, parser=self.get_parser_type())
parser.parse()
time_reduced = mod_time.time() - started
print(time_original)
print(points_original)
print(time_reduced)
print(points_reduced)
self.assertTrue(points_reduced < points_original)
self.assertTrue(points_reduced < max_reduced_points_no)
def test_smooth_without_removing_extreemes_preserves_point_count(self):
gpx = self.parse('first_and_last_elevation.gpx')
l = len(list(gpx.walk()))
gpx.smooth(vertical=True, horizontal=False)
self.assertEquals(l, len(list(gpx.walk())))
def test_smooth_without_removing_extreemes_preserves_point_count_2(self):
gpx = self.parse('first_and_last_elevation.gpx')
l = len(list(gpx.walk()))
gpx.smooth(vertical=False, horizontal=True)
self.assertEquals(l, len(list(gpx.walk())))
def test_smooth_without_removing_extreemes_preserves_point_count_3(self):
gpx = self.parse('first_and_last_elevation.gpx')
l = len(list(gpx.walk()))
gpx.smooth(vertical=True, horizontal=True)
self.assertEquals(l, len(list(gpx.walk())))
def test_clone_and_hash(self):
f = open('test_files/cerknicko-jezero.gpx')
parser = mod_parser.GPXParser(f, parser=self.get_parser_type())
gpx = parser.parse()
f.close()
cloned_gpx = gpx.clone()
self.assertTrue(hash(gpx) == hash(cloned_gpx))
def test_clone_and_smooth(self):
f = open('test_files/cerknicko-jezero.gpx')
parser = mod_parser.GPXParser(f, parser=self.get_parser_type())
gpx = parser.parse()
f.close()
original_2d = gpx.length_2d()
original_3d = gpx.length_3d()
cloned_gpx = gpx.clone()
cloned_gpx.reduce_points(2000, min_distance=10)
cloned_gpx.smooth(vertical=True, horizontal=True)
cloned_gpx.smooth(vertical=True, horizontal=False)
print('2d:', gpx.length_2d())
print('2d cloned and smoothed:', cloned_gpx.length_2d())
print('3d:', gpx.length_3d())
print('3d cloned and smoothed:', cloned_gpx.length_3d())
self.assertTrue(gpx.length_3d() == original_3d)
self.assertTrue(gpx.length_2d() == original_2d)
self.assertTrue(gpx.length_3d() > cloned_gpx.length_3d())
self.assertTrue(gpx.length_2d() > cloned_gpx.length_2d())
def test_reduce_by_min_distance(self):
gpx = mod_gpxpy.parse(open('test_files/cerknicko-jezero.gpx'), parser=self.get_parser_type())
min_distance_before_reduce = 1000000
for point, track_no, segment_no, point_no in gpx.walk():
if point_no > 0:
previous_point = gpx.tracks[track_no].segments[segment_no].points[point_no - 1]
print(point.distance_3d(previous_point))
if point.distance_3d(previous_point) < min_distance_before_reduce:
min_distance_before_reduce = point.distance_3d(previous_point)
gpx.reduce_points(min_distance=10)
min_distance_after_reduce = 1000000
for point, track_no, segment_no, point_no in gpx.walk():
if point_no > 0:
previous_point = gpx.tracks[track_no].segments[segment_no].points[point_no - 1]
if point.distance_3d(previous_point) < min_distance_after_reduce:
min_distance_after_reduce = point.distance_3d(previous_point)
self.assertTrue(min_distance_before_reduce < min_distance_after_reduce)
self.assertTrue(min_distance_before_reduce < 10)
self.assertTrue(10 < min_distance_after_reduce)
def test_moving_stopped_times(self):
f = open('test_files/cerknicko-jezero.gpx')
parser = mod_parser.GPXParser(f, parser=self.get_parser_type())
gpx = parser.parse()
f.close()
print(gpx.get_track_points_no())
#gpx.reduce_points(1000, min_distance=5)
print(gpx.get_track_points_no())
length = gpx.length_3d()
print('Distance: %s' % length)
gpx.reduce_points(2000, min_distance=10)
gpx.smooth(vertical=True, horizontal=True)
gpx.smooth(vertical=True, horizontal=False)
moving_time, stopped_time, moving_distance, stopped_distance, max_speed = gpx.get_moving_data(stopped_speed_threshold=0.1)
print('-----')
print('Length: %s' % length)
print('Moving time: %s (%smin)' % (moving_time, moving_time / 60.))
print('Stopped time: %s (%smin)' % (stopped_time, stopped_time / 60.))
print('Moving distance: %s' % moving_distance)
print('Stopped distance: %s' % stopped_distance)
print('Max speed: %sm/s' % max_speed)
print('-----')
# TODO: More tests and checks
self.assertTrue(moving_distance < length)
print('Dakle:', moving_distance, length)
self.assertTrue(moving_distance > 0.75 * length)
self.assertTrue(stopped_distance < 0.1 * length)
def test_split_on_impossible_index(self):
f = open('test_files/cerknicko-jezero.gpx')
parser = mod_parser.GPXParser(f, parser=self.get_parser_type())
gpx = parser.parse()
f.close()
track = gpx.tracks[0]
before = len(track.segments)
track.split(1000, 10)
after = len(track.segments)
self.assertTrue(before == after)
def test_split(self):
f = open('test_files/cerknicko-jezero.gpx')
parser = mod_parser.GPXParser(f, parser=self.get_parser_type())
gpx = parser.parse()
f.close()
track = gpx.tracks[1]
track_points_no = track.get_points_no()
before = len(track.segments)
track.split(0, 10)
after = len(track.segments)
self.assertTrue(before + 1 == after)
print('Points in first (splitted) part:', len(track.segments[0].points))
# From 0 to 10th point == 11 points:
self.assertTrue(len(track.segments[0].points) == 11)
self.assertTrue(len(track.segments[0].points) + len(track.segments[1].points) == track_points_no)
# Now split the second track
track.split(1, 20)
self.assertTrue(len(track.segments[1].points) == 21)
self.assertTrue(len(track.segments[0].points) + len(track.segments[1].points) + len(track.segments[2].points) == track_points_no)
def test_split_and_join(self):
f = open('test_files/cerknicko-jezero.gpx')
parser = mod_parser.GPXParser(f, parser=self.get_parser_type())
gpx = parser.parse()
f.close()
track = gpx.tracks[1]
original_track = track.clone()
track.split(0, 10)
track.split(1, 20)
self.assertTrue(len(track.segments) == 3)
track.join(1)
self.assertTrue(len(track.segments) == 2)
track.join(0)
self.assertTrue(len(track.segments) == 1)
# Check that this splitted and joined track is the same as the original one:
self.assertTrue(equals(track, original_track))
def test_remove_point_from_segment(self):
f = open('test_files/cerknicko-jezero.gpx')
parser = mod_parser.GPXParser(f, parser=self.get_parser_type())
gpx = parser.parse()
f.close()
track = gpx.tracks[1]
segment = track.segments[0]
original_segment = segment.clone()
segment.remove_point(3)
print(segment.points[0])
print(original_segment.points[0])
self.assertTrue(equals(segment.points[0], original_segment.points[0]))
self.assertTrue(equals(segment.points[1], original_segment.points[1]))
self.assertTrue(equals(segment.points[2], original_segment.points[2]))
# ...but:
self.assertTrue(equals(segment.points[3], original_segment.points[4]))
self.assertTrue(len(segment.points) + 1 == len(original_segment.points))
def test_distance(self):
distance = mod_geo.distance(48.56806, 21.43467, None, 48.599214, 21.430878, None)
print(distance)
self.assertTrue(distance > 3450 and distance < 3500)
def test_haversine_distance(self):
loc1 = mod_geo.Location(1, 2)
loc2 = mod_geo.Location(2, 3)
self.assertEqual(loc1.distance_2d(loc2),
mod_geo.distance(loc1.latitude, loc1.longitude, None, loc2.latitude, loc2.longitude, None))
loc1 = mod_geo.Location(1, 2)
loc2 = mod_geo.Location(3, 4)
self.assertEqual(loc1.distance_2d(loc2),
mod_geo.distance(loc1.latitude, loc1.longitude, None, loc2.latitude, loc2.longitude, None))
loc1 = mod_geo.Location(1, 2)
loc2 = mod_geo.Location(3.1, 4)
self.assertEqual(loc1.distance_2d(loc2),
mod_geo.haversine_distance(loc1.latitude, loc1.longitude, loc2.latitude, loc2.longitude))
loc1 = mod_geo.Location(1, 2)
loc2 = mod_geo.Location(2, 4.1)
self.assertEqual(loc1.distance_2d(loc2),
mod_geo.haversine_distance(loc1.latitude, loc1.longitude, loc2.latitude, loc2.longitude))
def test_horizontal_smooth_remove_extremes(self):
f = open('test_files/track-with-extremes.gpx', 'r')
parser = mod_parser.GPXParser(f, parser=self.get_parser_type())
gpx = parser.parse()
points_before = gpx.get_track_points_no()
gpx.smooth(vertical=False, horizontal=True, remove_extremes=True)
points_after = gpx.get_track_points_no()
print(points_before)
print(points_after)
self.assertTrue(points_before - 2 == points_after)
def test_vertical_smooth_remove_extremes(self):
f = open('test_files/track-with-extremes.gpx', 'r')
parser = mod_parser.GPXParser(f, parser=self.get_parser_type())
gpx = parser.parse()
points_before = gpx.get_track_points_no()
gpx.smooth(vertical=True, horizontal=False, remove_extremes=True)
points_after = gpx.get_track_points_no()
print(points_before)
print(points_after)
self.assertTrue(points_before - 1 == points_after)
def test_horizontal_and_vertical_smooth_remove_extremes(self):
f = open('test_files/track-with-extremes.gpx', 'r')
parser = mod_parser.GPXParser(f, parser=self.get_parser_type())
gpx = parser.parse()
points_before = gpx.get_track_points_no()
gpx.smooth(vertical=True, horizontal=True, remove_extremes=True)
points_after = gpx.get_track_points_no()
print(points_before)
print(points_after)
self.assertTrue(points_before - 3 == points_after)
def test_positions_on_track(self):
gpx = mod_gpx.GPX()
track = mod_gpx.GPXTrack()
gpx.tracks.append(track)
segment = mod_gpx.GPXTrackSegment()
track.segments.append(segment)
location_to_find_on_track = None
for i in range(1000):
latitude = 45 + i * 0.001
longitude = 45 + i * 0.001
elevation = 100 + i * 2
point = mod_gpx.GPXTrackPoint(latitude=latitude, longitude=longitude, elevation=elevation)
segment.points.append(point)
if i == 500:
location_to_find_on_track = mod_gpx.GPXWaypoint(latitude=latitude, longitude=longitude)
result = gpx.get_nearest_locations(location_to_find_on_track)
self.assertTrue(len(result) == 1)
def test_positions_on_track_2(self):
gpx = mod_gpx.GPX()
track = mod_gpx.GPXTrack()
gpx.tracks.append(track)
location_to_find_on_track = None
# first segment:
segment = mod_gpx.GPXTrackSegment()
track.segments.append(segment)
for i in range(1000):
latitude = 45 + i * 0.001
longitude = 45 + i * 0.001
elevation = 100 + i * 2
point = mod_gpx.GPXTrackPoint(latitude=latitude, longitude=longitude, elevation=elevation)
segment.points.append(point)
if i == 500:
location_to_find_on_track = mod_gpx.GPXWaypoint(latitude=latitude, longitude=longitude)
# second segment
segment = mod_gpx.GPXTrackSegment()
track.segments.append(segment)
for i in range(1000):
latitude = 45.0000001 + i * 0.001
longitude = 45.0000001 + i * 0.001
elevation = 100 + i * 2
point = mod_gpx.GPXTrackPoint(latitude=latitude, longitude=longitude, elevation=elevation)
segment.points.append(point)
result = gpx.get_nearest_locations(location_to_find_on_track)
print('Found', result)
self.assertTrue(len(result) == 2)
def test_hash_location(self):
location_1 = mod_geo.Location(latitude=12, longitude=13, elevation=19)
location_2 = mod_geo.Location(latitude=12, longitude=13, elevation=19)
self.assertTrue(hash(location_1) == hash(location_2))
location_2.elevation *= 2.0
location_2.latitude *= 2.0
location_2.longitude *= 2.0
self.assertTrue(hash(location_1) != hash(location_2))
location_2.elevation /= 2.0
location_2.latitude /= 2.0
location_2.longitude /= 2.0
self.assertTrue(hash(location_1) == hash(location_2))
def test_hash_gpx_track_point(self):
point_1 = mod_gpx.GPXTrackPoint(latitude=12, longitude=13, elevation=19)
point_2 = mod_gpx.GPXTrackPoint(latitude=12, longitude=13, elevation=19)
self.assertTrue(hash(point_1) == hash(point_2))
point_2.elevation *= 2.0
point_2.latitude *= 2.0
point_2.longitude *= 2.0
self.assertTrue(hash(point_1) != hash(point_2))
point_2.elevation /= 2.0
point_2.latitude /= 2.0
point_2.longitude /= 2.0
self.assertTrue(hash(point_1) == hash(point_2))
def test_hash_track(self):
gpx = mod_gpx.GPX()
track = mod_gpx.GPXTrack()
gpx.tracks.append(track)
segment = mod_gpx.GPXTrackSegment()
track.segments.append(segment)
for i in range(1000):
latitude = 45 + i * 0.001
longitude = 45 + i * 0.001
elevation = 100 + i * 2.
point = mod_gpx.GPXTrackPoint(latitude=latitude, longitude=longitude, elevation=elevation)
segment.points.append(point)
self.assertTrue(hash(gpx))
self.assertTrue(len(gpx.tracks) == 1)
self.assertTrue(len(gpx.tracks[0].segments) == 1)
self.assertTrue(len(gpx.tracks[0].segments[0].points) == 1000)
cloned_gpx = mod_copy.deepcopy(gpx)
self.assertTrue(hash(gpx) == hash(cloned_gpx))
gpx.tracks[0].segments[0].points[17].elevation *= 2.
self.assertTrue(hash(gpx) != hash(cloned_gpx))
gpx.tracks[0].segments[0].points[17].elevation /= 2.
self.assertTrue(hash(gpx) == hash(cloned_gpx))
gpx.tracks[0].segments[0].points[17].latitude /= 2.
self.assertTrue(hash(gpx) != hash(cloned_gpx))
gpx.tracks[0].segments[0].points[17].latitude *= 2.
self.assertTrue(hash(gpx) == hash(cloned_gpx))
del gpx.tracks[0].segments[0].points[17]
self.assertTrue(hash(gpx) != hash(cloned_gpx))
def test_bounds(self):
gpx = mod_gpx.GPX()
track = mod_gpx.GPXTrack()
segment_1 = mod_gpx.GPXTrackSegment()
segment_1.points.append(mod_gpx.GPXTrackPoint(latitude=-12, longitude=13))
segment_1.points.append(mod_gpx.GPXTrackPoint(latitude=-100, longitude=-5))
segment_1.points.append(mod_gpx.GPXTrackPoint(latitude=100, longitude=-13))
track.segments.append(segment_1)
segment_2 = mod_gpx.GPXTrackSegment()
segment_2.points.append(mod_gpx.GPXTrackPoint(latitude=-12, longitude=100))
segment_2.points.append(mod_gpx.GPXTrackPoint(latitude=-10, longitude=-5))
segment_2.points.append(mod_gpx.GPXTrackPoint(latitude=10, longitude=-100))
track.segments.append(segment_2)
gpx.tracks.append(track)
bounds = gpx.get_bounds()
self.assertEqual(bounds.min_latitude, -100)
self.assertEqual(bounds.max_latitude, 100)
self.assertEqual(bounds.min_longitude, -100)
self.assertEqual(bounds.max_longitude, 100)
# Test refresh bounds:
gpx.refresh_bounds()
self.assertEqual(gpx.bounds.min_latitude, -100)
self.assertEqual(gpx.bounds.max_latitude, 100)
self.assertEqual(gpx.bounds.min_longitude, -100)
self.assertEqual(gpx.bounds.max_longitude, 100)
def test_time_bounds(self):
gpx = mod_gpx.GPX()
track = mod_gpx.GPXTrack()
segment_1 = mod_gpx.GPXTrackSegment()
segment_1.points.append(mod_gpx.GPXTrackPoint(latitude=-12, longitude=13))
segment_1.points.append(mod_gpx.GPXTrackPoint(latitude=-100, longitude=-5, time=mod_datetime.datetime(2001, 1, 12)))
segment_1.points.append(mod_gpx.GPXTrackPoint(latitude=100, longitude=-13, time=mod_datetime.datetime(2003, 1, 12)))
track.segments.append(segment_1)
segment_2 = mod_gpx.GPXTrackSegment()
segment_2.points.append(mod_gpx.GPXTrackPoint(latitude=-12, longitude=100, time=mod_datetime.datetime(2010, 1, 12)))
segment_2.points.append(mod_gpx.GPXTrackPoint(latitude=-10, longitude=-5, time=mod_datetime.datetime(2011, 1, 12)))
segment_2.points.append(mod_gpx.GPXTrackPoint(latitude=10, longitude=-100))
track.segments.append(segment_2)
gpx.tracks.append(track)
bounds = gpx.get_time_bounds()
self.assertEqual(bounds.start_time, mod_datetime.datetime(2001, 1, 12))
self.assertEqual(bounds.end_time, mod_datetime.datetime(2011, 1, 12))
def test_speed(self):
gpx = self.parse('track_with_speed.gpx')
gpx2 = self.reparse(gpx)
self.assertTrue(equals(gpx.waypoints, gpx2.waypoints))
self.assertTrue(equals(gpx.routes, gpx2.routes))
self.assertTrue(equals(gpx.tracks, gpx2.tracks))
self.assertTrue(equals(gpx, gpx2))
self.assertEqual(gpx.tracks[0].segments[0].points[0].speed, 1.2)
self.assertEqual(gpx.tracks[0].segments[0].points[1].speed, 2.2)
self.assertEqual(gpx.tracks[0].segments[0].points[2].speed, 3.2)
def test_dilutions(self):
gpx = self.parse('track_with_dilution_errors.gpx')
gpx2 = self.reparse(gpx)
self.assertTrue(equals(gpx.waypoints, gpx2.waypoints))
self.assertTrue(equals(gpx.routes, gpx2.routes))
self.assertTrue(equals(gpx.tracks, gpx2.tracks))
self.assertTrue(equals(gpx, gpx2))
self.assertTrue(hash(gpx) == hash(gpx2))
for test_gpx in (gpx, gpx2):
self.assertTrue(test_gpx.waypoints[0].horizontal_dilution == 100.1)
self.assertTrue(test_gpx.waypoints[0].vertical_dilution == 101.1)
self.assertTrue(test_gpx.waypoints[0].position_dilution == 102.1)
self.assertTrue(test_gpx.routes[0].points[0].horizontal_dilution == 200.1)
self.assertTrue(test_gpx.routes[0].points[0].vertical_dilution == 201.1)
self.assertTrue(test_gpx.routes[0].points[0].position_dilution == 202.1)
self.assertTrue(test_gpx.tracks[0].segments[0].points[0].horizontal_dilution == 300.1)
self.assertTrue(test_gpx.tracks[0].segments[0].points[0].vertical_dilution == 301.1)
self.assertTrue(test_gpx.tracks[0].segments[0].points[0].position_dilution == 302.1)
def test_name_comment_and_symbol(self):
gpx = mod_gpx.GPX()
track = mod_gpx.GPXTrack()
gpx.tracks.append(track)
segment = mod_gpx.GPXTrackSegment()
track.segments.append(segment)
point = mod_gpx.GPXTrackPoint(12, 13, name='aaa', comment='ccc', symbol='sss')
segment.points.append(point)
xml = gpx.to_xml()
self.assertTrue('<name>aaa' in xml)
gpx2 = self.reparse(gpx)
self.assertEquals(gpx2.tracks[0].segments[0].points[0].name, 'aaa')
self.assertEquals(gpx2.tracks[0].segments[0].points[0].comment, 'ccc')
self.assertEquals(gpx2.tracks[0].segments[0].points[0].symbol, 'sss')
def test_get_bounds_and_refresh_bounds(self):
gpx = mod_gpx.GPX()
latitudes = []
longitudes = []
for i in range(2):
track = mod_gpx.GPXTrack()
for i in range(2):
segment = mod_gpx.GPXTrackSegment()
for i in range(10):
latitude = 50. * (mod_random.random() - 0.5)
longitude = 50. * (mod_random.random() - 0.5)
point = mod_gpx.GPXTrackPoint(latitude=latitude, longitude=longitude)
segment.points.append(point)
latitudes.append(latitude)
longitudes.append(longitude)
track.segments.append(segment)
gpx.tracks.append(track)
bounds = gpx.get_bounds()
print(latitudes)
print(longitudes)
self.assertEqual(bounds.min_latitude, min(latitudes))
self.assertEqual(bounds.max_latitude, max(latitudes))
self.assertEqual(bounds.min_longitude, min(longitudes))
self.assertEqual(bounds.max_longitude, max(longitudes))
gpx.refresh_bounds()
self.assertEqual(gpx.bounds.min_latitude, min(latitudes))
self.assertEqual(gpx.bounds.max_latitude, max(latitudes))
self.assertEqual(gpx.bounds.min_longitude, min(longitudes))
self.assertEqual(gpx.bounds.max_longitude, max(longitudes))
def test_named_tuples_values_time_bounds(self):
gpx = self.parse('korita-zbevnica.gpx')
time_bounds = gpx.get_time_bounds()
start_time, end_time = gpx.get_time_bounds()
self.assertEqual(start_time, time_bounds.start_time)
self.assertEqual(end_time, time_bounds.end_time)
def test_named_tuples_values_moving_data(self):
gpx = self.parse('korita-zbevnica.gpx')
moving_data = gpx.get_moving_data()
moving_time, stopped_time, moving_distance, stopped_distance, max_speed = gpx.get_moving_data()
self.assertEqual(moving_time, moving_data.moving_time)
self.assertEqual(stopped_time, moving_data.stopped_time)
self.assertEqual(moving_distance, moving_data.moving_distance)
self.assertEqual(stopped_distance, moving_data.stopped_distance)
self.assertEqual(max_speed, moving_data.max_speed)
def test_named_tuples_values_uphill_downhill(self):
gpx = self.parse('korita-zbevnica.gpx')
uphill_downhill = gpx.get_uphill_downhill()
uphill, downhill = gpx.get_uphill_downhill()
self.assertEqual(uphill, uphill_downhill.uphill)
self.assertEqual(downhill, uphill_downhill.downhill)
def test_named_tuples_values_elevation_extremes(self):
gpx = self.parse('korita-zbevnica.gpx')
elevation_extremes = gpx.get_elevation_extremes()
minimum, maximum = gpx.get_elevation_extremes()
self.assertEqual(minimum, elevation_extremes.minimum)
self.assertEqual(maximum, elevation_extremes.maximum)
def test_named_tuples_values_nearest_location_data(self):
gpx = self.parse('korita-zbevnica.gpx')
location = gpx.tracks[1].segments[0].points[2]
location.latitude *= 1.00001
location.longitude *= 0.99999
nearest_location_data = gpx.get_nearest_location(location)