station.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from typing import List, Union
  2. from plib import Point
  3. class StationError(Exception):
  4. pass
  5. class Station:
  6. def __init__(self, code: Union[str, None] = None,
  7. height: Union[float, None] = None,
  8. location: Union[List[float], None] = None,
  9. status: Union[str, None] = None,
  10. xyz: Union[List[float], None] = None):
  11. if code is None or height is None or location is None or status is None or xyz is None:
  12. raise StationError
  13. self.code = code
  14. self.height = height
  15. self.location = Point(location['lat'], location['lon'])
  16. self.status = status
  17. self.xyz = xyz
  18. def distance_to(self, other: "Station") -> float:
  19. return self.location.distance_to(other.location)
  20. class StationMap:
  21. def __init__(self, stations: Union[List[Station], None] = None):
  22. if stations is None:
  23. raise StationError
  24. self.stations = stations
  25. @classmethod
  26. def from_json(cls, text):
  27. stations = []
  28. for st in text:
  29. stations.append(dict(st))
  30. return cls(stations)
  31. def to_json(self, stations):
  32. return stations