-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocalXY.h
96 lines (72 loc) · 2.21 KB
/
LocalXY.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#ifndef LOCAL_XY
#define LOCAL_XY
#include <math.h>
#include <cmath>
#define M_PI 3.14159265358979323846
// Earth equatorial radius in meters according to WGS84.
const static double _earth_equator_radius = 6378137.0;
// Earth 'first' eccentricity according to WGS84.
const static double _earth_eccentricity = 0.08181919084261;
class LocalXYUtil
{
double rho_lat_ = 0.0;
double rho_lon_ = 0.0;
double origin_lat_ = 0.0;// note this is in radians
double origin_lon_ = 0.0;// note this is in radians
public:
LocalXYUtil()
{
}
bool Initialized()
{
return !(origin_lat_ == 0.0 && origin_lon_ == 0.0);
}
LocalXYUtil(double origin_lat, double origin_lon)
{
origin_lat_ = DegreesToRadians(origin_lat);
origin_lon_ = DegreesToRadians(origin_lon);
double altitude = 0;
double depth = -altitude;
double p = _earth_eccentricity * sin(origin_lat_);
p = 1.0 - p * p;
double rho_e = _earth_equator_radius * (1.0 - _earth_eccentricity * _earth_eccentricity) / (sqrt(p) * p);
double rho_n = _earth_equator_radius / sqrt(p);
rho_lat_ = rho_e - depth;
rho_lon_ = (rho_n - depth) * cos(origin_lat_);
}
void ToLatLon(double x, double y, double& lat, double& lon)
{
double dLon = 1.0 * x - 0.0 * y;
double dLat = 0.0 * x + 1.0 * y;
double rlat = (dLat / rho_lat_) + origin_lat_;
double rlon = (dLon / rho_lon_) + origin_lon_;
lat = RadiansToDegrees(rlat);
lon = RadiansToDegrees(rlon);
}
void FromLatLon(double lat, double lon, double& x, double& y)
{
double rLat = DegreesToRadians(lat);
double rLon = DegreesToRadians(lon);
double dLat = (rLat - origin_lat_) * rho_lat_;
double dLon = (rLon - origin_lon_) * rho_lon_;
x = 1.0 * dLon + 0.0 * dLat;
y = -0.0 * dLon + 1.0 * dLat;
}
inline double OriginLatitude()
{
return RadiansToDegrees(origin_lat_);
}
inline double OriginLongitude()
{
return RadiansToDegrees(origin_lon_);
}
inline double DegreesToRadians(double angle)
{
return angle * M_PI / 180.0;
}
inline double RadiansToDegrees(double radians)
{
return (180.0 / M_PI) * radians;
}
};
#endif