Waypoint.py 2.8 KB

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