Waypoint.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env python
  2. import numpy as np
  3. import pyproj
  4. import sys
  5. class Waypoint:
  6. def dms2dd(coordinate : str):
  7. split = coordinate.split('.')
  8. if 4 != len(split):
  9. return 0.0
  10. direction = split[0][1]
  11. degrees = float(split[0][1:])
  12. minutes = float(split[1])
  13. seconds = float(split[2]) * (float(split[3]) / 1000.0)
  14. dd = degrees + minutes / 60.0 + seconds / (60 * 60)
  15. if 'E' == direction or 'S' == direction:
  16. dd *= -1.0
  17. return dd
  18. def coordinateArgument(value):
  19. if True == isinstance(value, str):
  20. return Waypoint.dms2dd(value)
  21. elif True == isinstance(value, (float, int)):
  22. return float(value)
  23. else:
  24. raise Exception('Invalid constructor argument')
  25. def __init__(self, **kwargs):
  26. self.Name = None
  27. self.Coordinate = None
  28. self.Altitude = None
  29. self.Speed = None
  30. self.BaseTurn = False
  31. self.FinalTurn = False
  32. latitude = None
  33. longitude = None
  34. for key, value in kwargs.items():
  35. if 'name' == key.lower():
  36. self.Name = str(value)
  37. elif 'latitude' == key.lower():
  38. latitude = Waypoint.coordinateArgument(value)
  39. elif 'longitude' == key.lower():
  40. longitude = Waypoint.coordinateArgument(value)
  41. elif 'altitude' == key.lower():
  42. self.Altitude = int(value)
  43. elif 'speed' == key.lower():
  44. self.Speed = int(value)
  45. elif 'base' == key:
  46. self.BaseTurn = bool(value)
  47. elif 'final' == key:
  48. self.FinalTurn = bool(value)
  49. else:
  50. raise Exception('Invalid constructor argument: ' + key)
  51. if None != latitude and None != longitude:
  52. self.Coordinate = np.array([ latitude, longitude ])
  53. def __str__(self):
  54. return 'Name: ' + self.Name + ', Lat: ' + str(self.Coordinate[0]) + ', Lon: ' + str(self.Coordinate[1])
  55. def haversine(self, other):
  56. geodesic = pyproj.Geod(ellps='WGS84')
  57. forward, backward, distance = geodesic.inv(self.Coordinate[1], self.Coordinate[0], other.Coordinate[1], other.Coordinate[0])
  58. return distance / 1000.0
  59. def bearing(self, other):
  60. geodesic = pyproj.Geod(ellps='WGS84')
  61. forward, backward, distance = geodesic.inv(self.Coordinate[1], self.Coordinate[0], other.Coordinate[1], other.Coordinate[0])
  62. return forward