GeoCoordinate.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Author:
  3. * Sven Czarnian <devel@svcz.de>
  4. * Brief:
  5. * Implements the coordinate to abstract global coordinates
  6. * Copyright:
  7. * 2020-2021 Sven Czarnian
  8. * License:
  9. * GNU General Public License v3 (GPLv3)
  10. */
  11. #include <algorithm>
  12. #include <GeographicLib/Geodesic.hpp>
  13. #include <gsl/gsl>
  14. #include <aman/helper/Math.h>
  15. #include <aman/helper/String.h>
  16. #include <aman/types/GeoCoordinate.h>
  17. using namespace aman;
  18. static constexpr float MinutesToHours = 60.0f;
  19. static constexpr float SecondsToHours = 3600.0f;
  20. static constexpr float MillisecondsToHours = 3600000.0f;
  21. static constexpr float MaxAngle = 360.0f;
  22. GeoCoordinate::GeoCoordinate() noexcept :
  23. m_longitude(0.0f),
  24. m_latitude(0.0f) { }
  25. GeoCoordinate::GeoCoordinate(const Angle& longitude, const Angle& latitude) noexcept :
  26. m_longitude(longitude),
  27. m_latitude(latitude) { }
  28. bool GeoCoordinate::operator==(const GeoCoordinate& other) const {
  29. return this->m_longitude == other.m_longitude && this->m_latitude == other.m_latitude;
  30. }
  31. bool GeoCoordinate::operator!=(const GeoCoordinate& other) const {
  32. return false == this->operator==(other);
  33. }
  34. const Angle& GeoCoordinate::longitude() const noexcept {
  35. return this->m_longitude;
  36. }
  37. Angle& GeoCoordinate::longitude() noexcept {
  38. return this->m_longitude;
  39. }
  40. const Angle& GeoCoordinate::latitude() const noexcept {
  41. return this->m_latitude;
  42. }
  43. Angle& GeoCoordinate::latitude() noexcept {
  44. return this->m_latitude;
  45. }
  46. GeoCoordinate GeoCoordinate::projection(const Angle& heading, const Length& distance) const {
  47. float lat = 0.0f, lon = 0.0f;
  48. GeographicLib::Geodesic::WGS84().Direct(this->latitude().convert(degree), this->longitude().convert(degree),
  49. heading.convert(degree), distance.convert(metre), lat, lon);
  50. return GeoCoordinate(lon * degree, lat * degree);
  51. }
  52. Length GeoCoordinate::distanceTo(const GeoCoordinate& other) const {
  53. float distance = 0.0f;
  54. GeographicLib::Geodesic::WGS84().Inverse(this->latitude().convert(degree), this->longitude().convert(degree),
  55. other.latitude().convert(degree), other.longitude().convert(degree),
  56. distance);
  57. return distance * metre;
  58. }
  59. Angle GeoCoordinate::bearingTo(const GeoCoordinate& other) const {
  60. float azimuth0 = 0.0f, azimuth1 = 0.0f;
  61. GeographicLib::Geodesic::WGS84().Inverse(this->latitude().convert(degree), this->longitude().convert(degree),
  62. other.latitude().convert(degree), other.longitude().convert(degree),
  63. azimuth0, azimuth1);
  64. std::ignore = azimuth1;
  65. while (0.0f > azimuth0)
  66. azimuth0 += MaxAngle;
  67. while (MaxAngle < azimuth0)
  68. azimuth0 += MaxAngle;
  69. return azimuth0 * degree;
  70. }