C ++控制台-使用方位角和范围的新纬度/经度坐标

时间:2018-10-20 08:50:36

标签: c++ geospatial haversine

我希望帮助您了解我在搜索中找到的这段代码。我被困了两个星期试图得到它,这阻碍了我的项目。老实说,我正在尝试学习,而不仅仅是成为一个脚本小子,但这即使我在尝试,也比我项目的其余部分要复杂得多。 (例如,我今天只是在尝试理解此代码时才了解 auto 。)

我正在开发气象应用程序,我知道雷达站点的经纬度。我需要雷达已经根据雷达告诉我的方位角/范围(例如271度和7海里)检测到的特征的纬度/经度。我需要了解如何使用下面的代码将方位角/范围转换为新的纬度坐标。我不需要其他功能,只是能够放置变量(起始坐标,方位角和范围)并得到结果。下面的代码看起来还有很多其他功能,这使我感到困惑。

我在结尾处看到以下代码:

auto coordinate = CoordinateToCoordinate(latitude1, longitude1, angle, meters);

...这似乎是我需要的一部分。我知道它是如何计算的,但是一旦我深入研究,我就会感到困惑。我已经尝试过对代码进行黑客攻击,以至于我放弃了,甚至没有任何示例。

我希望能够手动设置变量(例如cin >>),并将lat和lon输出到可以保存到文本文件的变量中。除了实际的转换本身,我自己可以做所有事情(将起始变量吸收并将结果写入文本文件)。

如何使用下面的代码开始此操作?

我的示例变量是:

Original Latitude = 29.4214

Original Longitude = -98.0142

Azimuth from Origin = 271 degrees 

Range from Origin =   6 nautical miles (I can convert to meters if needed, 
in this case it's 11112 meters)

下面是实际未编辑的代码, a copy at this link.如果得到帮助,我不仅会复制/粘贴,而且我会在完成后返回完整的代码。我真的很想了解我的工作,因此我可以通过这些高级主题变得更好,并且将来不受束缚。下面的代码:

#include<iostream>
#include<iomanip>
#include<cmath>


// Source: // http://w...content-available-to-author-only...o.uk/scripts/latlong.html

static const double PI = 3.14159265358979323846, earthDiameterMeters = 6371.0 * 2 * 1000;

double degreeToRadian (const double degree) { return (degree * PI / 180); };
double radianToDegree (const double radian) { return (radian * 180 / PI); };

double CoordinatesToAngle (double latitude1,
                           const double longitude1,
                           double latitude2,
                           const double longitude2)
{
  const auto longitudeDifference = degreeToRadian(longitude2 - longitude1);
  latitude1 = degreeToRadian(latitude1);
  latitude2 = degreeToRadian(latitude2);

  using namespace std;
  const auto x = (cos(latitude1) * sin(latitude2)) -
                 (sin(latitude1) * cos(latitude2) * cos(longitudeDifference));
  const auto y = sin(longitudeDifference) * cos(latitude2);

  const auto degree = radianToDegree(atan2(y, x));
  return (degree >= 0)? degree : (degree + 360);
}

double CoordinatesToMeters (double latitude1,
                            double longitude1,
                            double latitude2,
                            double longitude2)
{
  latitude1 = degreeToRadian(latitude1);
  longitude1 = degreeToRadian(longitude1);
  latitude2 = degreeToRadian(latitude2);
  longitude2 = degreeToRadian(longitude2);

  using namespace std;
  auto x = sin((latitude2 - latitude1) / 2), y = sin((longitude2 - longitude1) / 2);
#if 1
  return earthDiameterMeters * asin(sqrt((x * x) + (cos(latitude1) * cos(latitude2) * y * y)));
#else
  auto value = (x * x) + (cos(latitude1) * cos(latitude2) * y * y);
  return earthDiameterMeters * atan2(sqrt(value), sqrt(1 - value));
#endif
}

std::pair<double,double> CoordinateToCoordinate (double latitude,
                                                 double longitude,
                                                 double angle,
                                                 double meters)
{
  latitude = degreeToRadian(latitude);
  longitude = degreeToRadian(longitude);
  angle = degreeToRadian(angle);
  meters *= 2 / earthDiameterMeters;

  using namespace std;
  pair<double,double> coordinate;

  coordinate.first = asin((sin(latitude) * cos(meters))
                        + (cos(latitude) * sin(meters) * cos(angle)));
  coordinate.second = longitude + atan2((sin(angle) * sin(meters) * cos(latitude)),
                                        cos(meters) - (sin(latitude) * sin(coordinate.first)));

  coordinate.first = radianToDegree(coordinate.first);
  coordinate.second = radianToDegree(coordinate.second);

  return coordinate;
}

int main ()
{
  using namespace std;
  const auto latitude1 = 12.968460, longitude1 = 77.641308,
             latitude2 = 12.967862, longitude2 = 77.653130;

  cout << std::setprecision(10);
  cout << "(" << latitude1 << "," << longitude1 << ") --- "
          "(" << latitude2 << "," << longitude2 << ")\n";

  auto angle = CoordinatesToAngle(latitude1, longitude1, latitude2, longitude2);
  cout << "Angle =  " << angle << endl;

  auto meters = CoordinatesToMeters(latitude1, longitude1, latitude2, longitude2);
  cout << "Meters = " << meters << endl;

  auto coordinate = CoordinateToCoordinate(latitude1, longitude1, angle, meters);
  cout << "Destination = (" << coordinate.first << "," << coordinate.second << ")\n";
}

1 个答案:

答案 0 :(得分:1)

我认为您只想要这样的东西:

#include <iostream>

std::pair<double,double> CoordinateToCoordinate (double latitude,
                                             double longitude,
                                             double angle,
                                             double meters)
{
...
...
}

using namespace std;

int main() {

   double lat, lon, angle, dist;

   cout << "Enter lat:";   cin >> lat;
   cout << "Enter lon:";   cin >> lon;
   cout << "Enter angle:"; cin >> angle;
   cout << "Enter dist:";  cin >> dist;

   auto coordinate = CoordinateToCoordinate(lat, lon, angle, dist);
   cout << "Destination = (" << coordinate.first << "," << coordinate.second << ")\n";
}