File: test_clienthelpers.py

package info (click to toggle)
gpsd 3.27-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 44,056 kB
  • sloc: ansic: 74,438; python: 16,521; sh: 890; cpp: 848; php: 225; makefile: 197; perl: 111; javascript: 26; xml: 11
file content (352 lines) | stat: -rwxr-xr-x 13,156 bytes parent folder | download
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
#!/usr/bin/env python
#
# Test gps/clienthelpers.py
#
# This code runs compatibly under Python 2 and 3.x for x >= 2.
# Preserve this property!
"""Partial test suite for gps.clienthelpers module."""

from __future__ import absolute_import, print_function, division

import math               # for math.fabs()
import os                 # for os.environ()
import sys                # for stderr, etc.

import gps.clienthelpers
import gps.misc

debug = 0

test1 = [(0, 0, "  0.00000000"),          # deg_dd
         (0, 89.999, " 89.99900000"),
         (0, 90.1, " 90.10000000"),
         (0, 180.21, "180.21000000"),
         (0, 359.321, "359.32100000"),
         (0, 360.0, "  0.00000000"),
         (1, 0, "  0 00.000000'"),        # deg_ddmm
         (1, 89.999, " 89 59.940000'"),
         (1, 90.1, " 90 06.000000'"),
         (1, 180.21, "180 12.600000'"),
         (1, 359.321, "359 19.260000'"),
         (1, 360.0, "  0 00.000000'"),
         (2, 0, "  0 00' 00.00000\""),    # deg_ddmmss
         (2, 89.999, " 89 59' 56.40000\""),
         (2, 90.1, " 90 06' 00.00000\""),
         (2, 180.21, "180 12' 36.00000\""),
         (2, 359.321, "359 19' 15.60000\""),
         (2, 360.0, "  0 00' 00.00000\""),
         ]

# maidenhead
# keep in sync with tests/test_gpsdclient.c
test2 = [(48.864710, 2.373051, "JN18eu47sm", "Paris"),
         (41.934982, 12.436523, "JN61fw24jj", "Rome"),
         (39.97714, -75.16855, "FM29jx94sm", "Philadelphia, PA USA"),
         (44.068786, -121.314247, "CN94ib26gm", "Bend, OR USA"),
         (-23.40288, -50.97669, "GG46mo23th", "Sao Paulo"),
         (-33.868810, 151.209291, "QF56od51cl", "Sydney, NSW AU"),
         (90, 180, "RR99xx99xx", "North Pole"),
         (-90, -180, "AA00aa00aa", "South Pole"),
         ]

test3 = [
    #  wgs84 separation, cm precision
    #  online calculator:
    #  https://geographiclib.sourceforge.io/cgi-bin/GeoidEval
    #
    # magnetic variation, hundredths of a degree precision.
    #
    # same tests as tests/test_geoid.c.  Keep them in sync.
    #

    # Easter Island: EGM2008 -3.8178, EGM96 -4.9979, EGM84 -5.1408
    # wmm2015 2.90
    (27.1127, -109.3497, -31.29, 8.45, "Easter Island"),
    # Kalahari: EGM2008, 20.6560, EGM96, 20.8419, EGM84 23.4496
    # wmm2015 6.73
    (25.5920, 21.0937, 21.80, 3.35, "Kalahari Desert"),
    # Greenland: EGM2008 40.3981, EGM96 40.5912, EGM84 41.7056
    # wmm2015 28.26  AWEFUL!
    (71.7069, -42.6043, 40.11, -28.25, "Greenland"),
    # Kuk Swamp: EGM2008 62.8837, EGM96, 62.8002, EGM84, 64.5655
    # wmm2015 9.11
    # seems to be over a gravitational anomaly
    (5.7837, 144.3317, 62.34, 2.42, "Kuk Swamp PG"),
    # KBDN: EGM2008 -20.3509, EGM96 -19.8008, EGM84 -18.5562
    # wmm2015 14.61
    (44.094556, -121.200222, -21.95, 14.40, "Bend Airport, OR (KBDN)"),
    # PANC: EGM2008 7.6508, EGM96 7.8563, EGM84 8.0838
    # wmm2015 15.78
    (61.171333, -149.991164, 13.54, 15.52, "Anchorage Airport, AK (PANC)"),
    # KDEN: EGM2008 -18.1941, EGM96 -18.4209, EGM84 -15.9555
    # wmm2015 7.95
    (39.861666, -104.673166, -18.15, 7.84, "Denver Airport, CO (KDEN)"),
    # LHR: EGM2008 46.4499, EGM96 46.3061, EGM84 47.7620
    # wmm2015 0.17
    (51.46970, -0.45943, 46.26, -0.28, "London Heathrow Airport, UK (LHR)"),
    # SCPE: EGM2008 37.9592, EGM96 39.3400, EGM84 46.6604
    # wmm2015 -6.17
    (-22.92170, -68.158401, 35.46, -6.11,
     "San Pedro de Atacama Airport, CL (SCPE)"),
    # SIN: EGM2008 8.6453, EGM96 8.3503, EGM84 8.2509
    # wmm2015 0.22
    (1.350190, 103.994003, 7.51, 0.17, "Singapore Changi Airport, SG (SIN)"),
    # UURB: EGM2008 13.6322, EGM96 13.6448, EGM84 13.1280
    # wmm2015 11.41
    (55.617199, 38.06000, 13.22, 11.42, "Moscow Bykovo Airport, RU (UUBB)"),
    # SYD: EGM2008 13.0311, EGM96 13.3736, EGM84 13.3147
    # wmm2015 -4.28
    (33.946098, 151.177002, 13.59, -4.26, "Sydney Airport, AU (SYD)"),
    # Doyle: EGM2008 -23.3366, EGM96 -23.3278, EGM84 -21.1672
    # wmm2015 13.35
    (40, -120, -23.34, 13.35, "Near Doyle, CA"),

    # test calc at delta lat == 0
    # North Poll: EGM2008 14.8980, EGM96 13.6050, EGM84 13.0980
    # wmm2015 1.75
    (90, 0, 14.90, 1.75, "North Poll 0"),
    # wmm2015 3.75
    (90, 2, 14.90, 3.75, "North Poll 2"),
    # wmm2015 4.25
    (90, 2.5, 14.90, 4.25, "North Poll 2.5"),
    # wmm2015 1.75
    (90, 3, 14.90, 4.75, "North Poll 3"),
    # wmm2015 1.75
    (90, 5, 14.90, 6.75, "North Poll 5"),
    # wmm2015 -178.25
    (90, 180, 14.90, -178.25, "North Poll"),

    # Equator 0, EGM2008 17.2260, EGM96 17.1630, EGM84 18.3296
    # wmm2015 -4.84
    (0, 0, 17.23, -4.84, "Equator 0W"),

    # South Poll: EGM2008 -30.1500, EGM96 -29.5350, EGM84 -29.7120
    # wmm2015 -30.80
    (-90, 0, -30.15, -30.80, "South Poll"),

    # test calc at delta lon == 0
    # 2 0: EGM2008 17.1724, EGM96 16.8962, EGM84 17.3676
    # wmm2015 -4.17
    (2, 0, 18.42, -4.23, "2N 0W"),
    # 2.5 0: EGM2008 16.5384, EGM96 16.5991, EGM84 17.0643
    # wmm2015 -4.02
    (2.5, 0, 18.71, -4.08, "2.5N 0W"),
    # 3 0: EGM2008 16.7998, EGM96 16.6161, EGM84 16.7857
    # wmm2015 -3.87
    (3, 0, 19.01, -3.92, "3N 0W"),
    # 3.5 0: EGM2008 17.0646, EGM96 17.0821, EGM84 16.7220
    # wmm2015 -3.72
    (3.5, 0, 19.31, -3.77, "3.5N 0W"),
    # 5 0: EGM2008 20.1991, EGM96 20.4536, EGM84 20.3181
    # wmm2015 -3.31
    (5, 0, 20.20, -3.31, "5N 0W"),

    # test calc on diagonal
    # Equator 0, EGM2008 17.2260, EGM96 17.1630, EGM84 18.3296
    # wmm2015 -4.84
    # 2 2: EGM2008 16.2839, EGM96 16.1579, EGM84 17.5354
    # wmm2015 -3.53
    (2, 2, 18.39, -3.60, "2N 2E"),
    # 2.5 2.5: EGM2008 15.7918, EGM96 15.5314, EGM84 16.3230
    # wmm2015 -3.24
    (2.5, 2.5, 18.78, -3.30, "2.5N 2.5E"),
    # 3 3: EGM2008 15.2097, EGM96 15.0751, EGM84 14.6542
    # wmm2015 -2.95
    (3, 3, 19.20, -3.01, "3N 3E"),
    # 3.5 3.5: EGM2008 14.8706, EGM96 14.6668, EGM84 13.9592
    # wmm2015 -3.72
    (3.5, 3.5, 19.66, -2.73, "3.5N 3.5E"),

    # some 5x5 points, s/b exact EGM2008, +/- rounding
    # 5, 5:  EGM2008 21.2609, EGM96 20.8917, EGM84 20.3509
    # wmm2015 -1.91
    (5, 5, 21.26, -1.91, "5, 5"),
    # -5, -5: EGM2008 17.1068, EGM96 16.8362, EGM84 17.5916
    # wmm2015 -9.03
    (-5, -5, 17.11, -9.03, "-5, -5"),
    # -5, 5: EGM2008 9.3988, EGM96 9.2399, EGM84 9.7948
    # wmm2015 -4.80
    (-5, 5, 9.40, -4.80, "-5, 5"),
    # 5, -5: EGM2008 25.7668, EGM96 25.6144, EGM84 25.1224
    # wmm2015 -4.90
    (5, -5, 25.77, -4.90, "5, 5"),

    # test data for some former corners in the code
    # 0, -78.452222: EGM2008 26.8978, EGM96 25.3457, EGM84 26.1507
    # wmm2015 -3.87
    (0, -78.452222, 15.98, -3.89, "Equatorial Sign Bolivia"),
    # 51.4778067, 0: EGM2008 45.8961, EGM96 45.7976, EGM84 47.2468
    # wmm2015 -0.10
    (51.4778067, 0, 45.46, -0.11, "Lawn Greenwich Observatory UK"),
    # 0, 180: EGM2008 21.2813, EGM96 21.1534, EGM84 21.7089
    # wmm2015 9.75
    (0, 180, 21.28, 9.75, "Far away from Google default"),
    (0, -180, 21.28, 9.75, "Away far from Google default"),
]

# gpsd gpsd_units
test4 = [('GPSD_UNITS', 'imperial', gps.clienthelpers.imperial),
         ('GPSD_UNITS', 'nautical', gps.clienthelpers.nautical),
         ('GPSD_UNITS', 'metric', gps.clienthelpers.metric),
         ('LC_MEASUREMENT', 'en_US', gps.clienthelpers.imperial),
         ('LC_MEASUREMENT', 'C', gps.clienthelpers.imperial),
         ('LC_MEASUREMENT', 'POSIX', gps.clienthelpers.imperial),
         ('LC_MEASUREMENT', 'ru_RU', gps.clienthelpers.metric),
         ('LANG', 'en_US', gps.clienthelpers.imperial),
         ('LANG', 'C', gps.clienthelpers.imperial),
         ('LANG', 'POSIX', gps.clienthelpers.imperial),
         ('LANG', 'ru_RU', gps.clienthelpers.metric),
         ]

# ecef2lla() tests
# lat, lon, altHAE, x, y z
test5 = [(0, 0, 0, 6378137, 0, 0),
         # (90, 0, 0, 0, 0, 6356752),  # broken!
         (0, 90, 0, 0, 6378137, 0),
         (0, 90, 90, 0, 6378227, 0),
         (29.999998, 30.000001, -1.022303, 4787610, 2764128, 3170373),
         (29.999996, 29.999998, 29.356862, 4787633, 2764141, 3170388),
         (60, 60, -0.000068,
          1598552.29346197, 2768773.79083189, 5500477.13386045),
         # a typical GPS sat
         (28.4, 74.4, 20073000.065, 6258290.772, 22414678.807, 12562777.312),
         (-34.2, -172.3, 20240000.113,
          -21822373.137, -2950500.608, -14941384.507),
         (44.0, -155.5, 20240000.201,
          -17430229.809, -7943413.363, 18467977.030),
         ]

# lla2ecef() tests
# lat, lon, altHAE, x, y z
# ideally would be the same as test 5, but life not that simple
test6 = [(0, 0, 0, 6378137, 0, 0),
         # (90, 0, 0, 0, 0, 6356752),  # broken!
         (0, 90, 0, 0, 6378137, 0),
         (0, 90, 90, 0, 6378227, 0),
         (30, 30, 0, 4787610.688268, 2764128.319646, 3170373.735383),
         (30, 30, 30, 4787633.188268, 2764141.310027, 3170388.735383),
         (60, 60, 0, 1598552.29346197, 2768773.79083189, 5500477.13386045),
         # typical GPS sats
         (28.4, 74.4, 20073000, 6258290.772, 22414678.807, 12562777.312),
         (-34.2, -172.3, 20240000, -21822373.137, -2950500.608, -14941384.507),
         (44.0, -155.5, 20240000, -17430229.809, -7943413.363, 18467977.030),
         ]

# ecef2aer().  Which sorta tests ecef2enu() and enu2aer()
# x, y, z, lat, lon, altHAE, e, n, u
test7 = [(1, 0, 0, 0, 0, 0, 0, -90.000, 6378136.000),
         (0, 1, 0, 0, 0, 0, 90.000, -90.000, 6378137.000),
         (0, 0, 1, 0, 0, 0, 0.000, -90.000, 6378137.000),
         (1, 1, 1, 0, 0, 0, 45.000, -90.000, 6378136.000),
         (15363696.95, -9349336.027, 19639882.76,
          44.06876100, -121.31437000, 1115.922,
          51.842, 17.685, 24006768.741),
         ]

errors = 0

for test in test1:
    (deg_type, deg, expected) = test
    result = gps.clienthelpers.deg_to_str(deg_type, deg)
    if result != expected:
        print("fail: deg_to_str(%d, %.3f) got %s expected %s" %
              (deg_type, deg, result, expected))
        errors += 1

for (lat, lon, maidenhead, location) in test2:
    converted = gps.clienthelpers.maidenhead(lat, lon)
    if converted != maidenhead:
        sys.stderr.write(
            "fail: maidenhead test %s, %s (%s)) expected %s got %s\n" %
            (lat, lon, location, maidenhead, converted))
        errors += 1

# check wgs84_separation()
for (lat, lon, wgs84, var, desc) in test3:
    separation = gps.clienthelpers.wgs84_separation(lat, lon)
    # check to 1 millimeter
    diff = separation - wgs84
    if debug:
        print("diff %f sep %f wgs84 %f" % (diff, separation, wgs84))
    if 0.009 < math.fabs(diff):
        sys.stderr.write(
            "fail: wgs84_separation(%s, %s) (%s) expected %.2f got %.2f\n" %
            (lat, lon, desc, wgs84, separation))
        errors += 1

# check mag_var()
for (lat, lon, wgs84, var, desc) in test3:
    magvar = gps.clienthelpers.mag_var(lat, lon)
    # check to 0.1 degree
    diff = magvar - var
    if debug:
        print("diff %f magvar %f s/b %f" % (diff, magvar, var))
    if 0.09 < math.fabs(diff):
        sys.stderr.write(
            "fail: mag_var(%s, %s) (%s) expected %.2f got %.2f\n" %
            (lat, lon, desc, var, magvar))
        errors += 1


savedenv = os.environ
# from the python doc:
# calls to unsetenv() don't update os.environ, so it is actually
# preferable to delete items of os.environ.
for key in ['GPSD_UNITS', 'LC_MEASUREMENT', 'LANG']:
    if key in os.environ:
        del os.environ[key]

for (key, val, expected) in test4:
    os.environ[key] = val

    result = gps.clienthelpers.gpsd_units()
    del os.environ[key]

    if result != expected:
        print("fail: gpsd_units() %s=%s got %s expected %d" %
              (key, val, str(result), expected))
        errors += 1

for (elat, elon, ealtHAE, x, y, z) in test5:
    (lat, lon, altHAE) = gps.ecef2lla(x, y, z)

    if ((0.000001 < abs(elat - lat) or
         0.000001 < abs(elon - lon) or
         0.001 < abs(ealtHAE - altHAE))):
        print("fail: ecef2lla(%f, %f, %f)=(%.6f, %.6f, %.6f) "
              "expected (%.6f, %.6f, %.6f)" %
              (x, y, z, lat, lon, altHAE, elat, elon, ealtHAE))
        errors += 1

for (lat, lon, altHAE, ex, ey, ez) in test6:
    (x, y, z) = gps.lla2ecef(lat, lon, altHAE)

    if ((0.001 < abs(ex - x) or
         0.001 < abs(ey - y) or
         0.001 < abs(ez - z))):
        print("fail: lla2ecef(%f, %f, %f)=(%.6f, %.6f, %.6f) "
              "expected (%.6f, %.6f, %.6f)" %
              (lat, lon, altHAE, x, y, z, ex, ey, ez))
        errors += 1

for (x, y, z, lat, lon, altHAE, eaz, eel, ernge) in test7:
    (az, el, rnge) = gps.ecef2aer(x, y, z, lat, lon, altHAE)

    if ((0.001 < abs(eaz - az) or
         0.001 < abs(eel - el) or
         0.001 < abs(ernge - rnge))):
        print("fail: ecef2aer(%f, %f, %f, %f, %f, %f)=(%.3f, %.3f, %.3f) "
              "expected (%.3f, %.3f, %.3f)" %
              (x, y, z, lat, lon, altHAE, eaz, eel, ernge, az, el, rnge))
        errors += 1


# restore environment
os.environ = savedenv

if errors:
    print("test_clienthelpers.py: %d tests failed" % errors)
    sys.exit(1)
else:
    print("test_clienthelpers.py: OK")
    sys.exit(0)