test_misc.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/env python
  2. #
  3. # This code runs compatibly under Python 2 and 3.x for x >= 2.
  4. # Preserve this property!
  5. """Test gps/misc.py."""
  6. from __future__ import absolute_import, print_function, division
  7. import sys
  8. import gps.misc
  9. errors = 0
  10. # values from here: https://en.wikipedia.org/wiki/Decimal_degrees#Precision
  11. # Note the wikipedia numbers are NOT ellipsoid corrected:
  12. # EarthDistanceSmall() is ellipsoid corrected.
  13. tests = [
  14. # slow convergence
  15. (0.00000000, 0.00000000, 0.50000000, 179.5000000, 19936288),
  16. # failure to converge, happens at antipodal points
  17. # fall back to EarthDistanceSmall()
  18. # the real answer should be 19944127
  19. (0.00000000, 0.00000000, 0.50000000, 179.7000000, 19870094),
  20. # boston to new york
  21. (42.3541165, -71.0693514, 40.7791472, -73.9680804, 298396),
  22. # equator 10 degree
  23. (0.00000000, 0.00000000, 10.00000000, 0.00000000, 1105854),
  24. (0.00000000, 0.00000000, 0.00000000, 10.00000000, 1113194),
  25. (0.00000000, 0.00000000, 10.00000000, 10.00000000, 1565109),
  26. # 23N 10 degree
  27. (23.00000000, 0.00000000, 33.00000000, 0.00000000, 1108210),
  28. (23.00000000, 0.00000000, 23.00000000, 10.00000000, 1025026),
  29. (23.00000000, 0.00000000, 33.00000000, 10.00000000, 1480049),
  30. # 45N 10 degree
  31. (45.00000000, 0.00000000, 55.00000000, 0.00000000, 1112285),
  32. (45.00000000, 0.00000000, 45.00000000, 10.00000000, 787967),
  33. (45.00000000, 0.00000000, 55.00000000, 10.00000000, 1320437),
  34. # 67N 10 degree
  35. (67.00000000, 0.00000000, 77.00000000, 0.00000000, 1115839),
  36. (67.00000000, 0.00000000, 67.00000000, 10.00000000, 435729),
  37. (67.00000000, 0.00000000, 77.00000000, 10.00000000, 1164036),
  38. # equator 5 degree
  39. (0.00000000, 0.00000000, 5.00000000, 0.00000000, 552885),
  40. (0.00000000, 0.00000000, 0.00000000, 5.00000000, 556597),
  41. (0.00000000, 0.00000000, 5.00000010, 5.00000000, 784028),
  42. # 23N 5 degree
  43. (23.00000000, 0.00000000, 28.00000000, 0.00000000, 553904),
  44. (23.00000000, 0.00000000, 23.00000000, 5.00000000, 512587),
  45. (23.00000000, 0.00000000, 28.00000000, 5.00000000, 747800),
  46. # 45N 5 degree
  47. (45.00000000, 0.00000000, 50.00000000, 0.00000000, 555902),
  48. (45.00000000, 0.00000000, 45.00000000, 5.00000000, 394171),
  49. (45.00000000, 0.00000000, 50.00000000, 5.00000000, 671179),
  50. # 67N 5 degree
  51. (67.00000000, 0.00000000, 72.00000000, 0.00000000, 557775),
  52. (67.00000000, 0.00000000, 67.00000000, 5.00000000, 218040),
  53. (67.00000000, 0.00000000, 72.00000000, 5.00000000, 590565),
  54. # equator 1 degree
  55. (0.00000000, 0.00000000, 1.00000000, 0.00000000, 110574),
  56. (0.00000000, 0.00000000, 0.00000000, 1.00000000, 111319),
  57. (0.00000000, 0.00000000, 1.00000010, 1.00000000, 156899),
  58. # 23N 1 degree
  59. (23.00000000, 0.00000000, 24.00000000, 0.00000000, 110751),
  60. (23.00000000, 0.00000000, 23.00000000, 1.00000000, 102522),
  61. (23.00000000, 0.00000000, 24.00000000, 1.00000000, 150659),
  62. # 45N 1 degree
  63. (45.00000000, 0.00000000, 46.00000000, 0.00000000, 111141),
  64. (45.00000000, 0.00000000, 45.00000000, 1.00000000, 78846),
  65. (45.00000000, 0.00000000, 46.00000000, 1.00000000, 135869),
  66. # 67N 1 degree
  67. (67.00000000, 0.00000000, 68.00000000, 0.00000000, 111528),
  68. (67.00000000, 0.00000000, 67.00000000, 1.00000000, 43619),
  69. (67.00000000, 0.00000000, 68.00000000, 1.00000000, 119427),
  70. # equator 10e-7
  71. (0.00000000, 0.00000000, 0.00000010, 0.00000000, 0.011057),
  72. (0.00000000, 0.00000000, 0.00000000, 0.00000010, 0.011132),
  73. (0.00000000, 0.00000000, 0.00000010, 0.00000010, 0.015690),
  74. # 23N 10e-7
  75. (23.00000000, 0.00000000, 23.00000010, 0.00000000, 0.011074),
  76. (23.00000000, 0.00000000, 23.00000000, 0.00000010, 0.010252),
  77. (23.00000000, 0.00000000, 23.00000010, 0.00000010, 0.015091),
  78. # 45N 10e-7
  79. (45.00000000, 0.00000000, 45.00000010, 0.00000000, 0.011113),
  80. (45.00000000, 0.00000000, 45.00000000, 0.00000010, 0.007885),
  81. (45.00000000, 0.00000000, 45.00000010, 0.00000010, 0.013626),
  82. # 67N 10e-7
  83. (67.00000000, 0.00000000, 67.00000010, 0.00000000, 0.011152),
  84. (67.00000000, 0.00000000, 67.00000000, 0.00000010, 0.004360),
  85. (67.00000000, 0.00000000, 67.00000010, 0.00000010, 0.011974),
  86. ]
  87. # EarthDistanceSmall
  88. for (lat1, lon1, lat2, lon2, dist) in tests:
  89. distance = gps.misc.EarthDistanceSmall((lat1, lon1), (lat2, lon2))
  90. # compare to 1%
  91. diff = dist - distance
  92. max_diff = dist * 0.01
  93. if abs(diff) > max_diff:
  94. sys.stderr.write(
  95. "misc small: %.8f %.8f, %.8f %.8f, expected %.7f got %.7f\n"
  96. % (lat1, lon1, lat2, lon2, dist, distance))
  97. errors += 1
  98. # EarthDistance
  99. for (lat1, lon1, lat2, lon2, dist) in tests:
  100. distance = gps.misc.EarthDistance((lat1, lon1), (lat2, lon2))
  101. # compare to 0.001%
  102. diff = dist - distance
  103. max_diff = dist * 0.00001
  104. if abs(diff) > max_diff:
  105. sys.stderr.write(
  106. "misc large: %.8f %.8f, %.8f %.8f, expected %.7f got %.7f\n"
  107. % (lat1, lon1, lat2, lon2, dist, distance))
  108. errors += 1
  109. if errors:
  110. print("test_misc.py: failed")
  111. sys.exit(1)
  112. else:
  113. print("test_misc.py: OK")
  114. sys.exit(0)