You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

geography.cpp 1.9 kB

2 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * @file geography.cpp
  3. * @author donkey (anjingyu_ws@foxmail.com)
  4. * @brief Implementation of basic geography functions in \c ns::geo
  5. * @version 0.1.0
  6. * @date 2020-05-15
  7. *
  8. * @copyright Copyright (c) 2020- donkey <anjingyu_ws@foxmail.com>.
  9. *
  10. */
  11. #include "geo/geography.h"
  12. #include "geo/math.h"
  13. #include <cmath> /**< std::sqrt std::fabs */
  14. #include <cstdio> /**< sprintf */
  15. #include "geodesic.h" /**< GeographicLib::Geodesic::WGS84 */
  16. namespace ns {
  17. namespace geo {
  18. double Geography::distanceUnitConvertFactor(DistanceUnit from, DistanceUnit to)
  19. {
  20. static const double cvtArray[] = {1.0, 1000.0, 100000.0, 100000000.0};
  21. return (cvtArray[static_cast<int>(from)] / cvtArray[static_cast<int>(to)]);
  22. }
  23. double Geography::distance(double lng1, double lat1, double lng2, double lat2)
  24. {
  25. double distance = 0.0;
  26. GeographicLib::Geodesic::WGS84().Inverse(lat1, lng1, lat2, lng2, distance);
  27. return distance;
  28. }
  29. double Geography::angleToNorth(double lng1, double lat1, double lng2,
  30. double lat2)
  31. {
  32. double ec = Wgs84MinorRadius +
  33. (Wgs84MajorRadius - Wgs84MinorRadius) * (90.0 - lat1) / 90.0;
  34. double ed = ec * cos(ns::geo::Math::degree2Radian(lat1));
  35. double dx = (ns::geo::Math::degree2Radian(lng2) -
  36. ns::geo::Math::degree2Radian(lng1)) *
  37. ed;
  38. double dy = (ns::geo::Math::degree2Radian(lat2) -
  39. ns::geo::Math::degree2Radian(lat1)) *
  40. ec;
  41. double angle = atan(fabs(dx / dy)) * 180.0 / ns::geo::Math::pi();
  42. double dLng = lng2 - lng1;
  43. double dLat = lat2 - lat1;
  44. if (dLng > 0.0 && dLat <= 0.0)
  45. {
  46. angle = (90.0 - angle) + 90.0;
  47. }
  48. else if (dLng <= 0.0 && dLat < 0.0)
  49. {
  50. angle = angle + 180.0;
  51. }
  52. else if (dLng < 0.0 && dLat >= 0.0)
  53. {
  54. angle = (90.0 - angle) + 270.0;
  55. }
  56. return angle;
  57. }
  58. } // namespace geo
  59. } // namespace ns