GPSUtils.h
Go to the documentation of this file.
1 #pragma once
2 #include <math.h>
3 #include <cmath>
4 
5 namespace GPSUtils
6 {
7  constexpr double EARTHRADIUS_KM = 6378.137;
8 
9  double coordsToMeter(double lat1, double lon1, double lat2, double lon2)
10  {
11  double dLat = (lat2 - lat1) * (M_PI / 180.0);
12  double dLon = (lon2 - lon1) * (M_PI / 180.0);
13  lat1 = lat1 * (M_PI/180.0);
14  lat2 = lat2 * (M_PI/180.0);
15  double a = sin(dLat/2.0) * sin(dLat/2.0) +
16  cos(lat1) * cos(lat2) * sin(dLon/2.0) * sin(dLon/2.0);
17  double c = 2.0 * atan2(sqrt(a), sqrt(1.0-a));
18  double d = GPSUtils::EARTHRADIUS_KM * c;
19  return d * 1000.0; // meters
20  }
21 
22  void coordAfterMotion(double lat1, double lon1, double d, double a, double &newLat, double &newLon)
23  {
24  //angle in radians
25  //dist in meters
26  d = d/(EARTHRADIUS_KM*1000.0); //angle corresponding to arc length/dist on earth
27  //tc = tc * (M_PI/180.0); //angle from initial point to current location
28  lat1 = lat1 * (M_PI/180.0);
29  lon1 = lon1 * (M_PI/180.0);
30  newLat=asin(sin(lat1)*cos(d)+cos(lat1)*sin(d)*cos(a));
31  if (cos(newLat)==0)
32  newLon=lon1; // endpoint a pole
33  else
34  newLon=fmod(lon1-asin(sin(a)*sin(d)/cos(newLat))+M_PI,2.0*M_PI)-M_PI;
35  newLat = newLat * (180.0/M_PI);
36  newLon = newLon * (180.0/M_PI);
37  }
38 
42  void coordsToMetricXY(double lat1, double lon1, double lat2, double lon2, double &dX, double &dY)
43  {
44  dX = std::copysign(1.0, lon2-lon1) * coordsToMeter(lat1, lon1, lat1, lon2);
45  dY = std::copysign(1.0, lat2-lat1) * coordsToMeter(lat1, lon1, lat2, lon1);
46  }
47 };
constexpr double EARTHRADIUS_KM
Definition: GPSUtils.h:7
void coordsToMetricXY(double lat1, double lon1, double lat2, double lon2, double &dX, double &dY)
Takes two GPS coordinates and outputs X and Y displacements.
Definition: GPSUtils.h:42
double coordsToMeter(double lat1, double lon1, double lat2, double lon2)
Definition: GPSUtils.h:9
void coordAfterMotion(double lat1, double lon1, double d, double a, double &newLat, double &newLon)
Definition: GPSUtils.h:22


igvc
Author(s): Matthew Barulic , Al Chaussee
autogenerated on Sun May 10 2015 16:18:45