mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
Loading...
Searching...
No Matches
gps_conversions.h
Go to the documentation of this file.
1/* Taken from utexas-art-ros-pkg:art_vehicle/applanix */
2
3/*
4 * Conversions between coordinate systems.
5 *
6 * Includes LatLong<->UTM.
7 */
8
9#ifndef _UTM_H
10#define _UTM_H
11
19#include <cmath>
20#include <cstdio>
21#include <cstdlib>
22#include <string>
23
24namespace mrs_lib
25{
26
27 const double RADIANS_PER_DEGREE = M_PI / 180.0;
28 const double DEGREES_PER_RADIAN = 180.0 / M_PI;
29
30 // WGS84 Parameters
31 const double WGS84_A = 6378137.0; // major axis
32 const double WGS84_B = 6356752.31424518; // minor axis
33 const double WGS84_F = 0.0033528107; // ellipsoid flattening
34 const double WGS84_E = 0.0818191908; // first eccentricity
35 const double WGS84_EP = 0.0820944379; // second eccentricity
36
37 // UTM Parameters
38 const double UTM_K0 = 0.9996; // scale factor
39 const double UTM_FE = 500000.0; // false easting
40 const double UTM_FN_N = 0.0; // false northing on north hemisphere
41 const double UTM_FN_S = 10000000.0; // false northing on south hemisphere
42 const double UTM_E2 = (WGS84_E * WGS84_E); // e^2
43 const double UTM_E4 = (UTM_E2 * UTM_E2); // e^4
44 const double UTM_E6 = (UTM_E4 * UTM_E2); // e^6
45 const double UTM_EP2 = (UTM_E2 / (1 - UTM_E2)); // e'^2
46
54 static inline void UTM(double lat, double lon, double* x, double* y)
55 {
56 // constants
57 const static double m0 = (1 - UTM_E2 / 4 - 3 * UTM_E4 / 64 - 5 * UTM_E6 / 256);
58 const static double m1 = -(3 * UTM_E2 / 8 + 3 * UTM_E4 / 32 + 45 * UTM_E6 / 1024);
59 const static double m2 = (15 * UTM_E4 / 256 + 45 * UTM_E6 / 1024);
60 const static double m3 = -(35 * UTM_E6 / 3072);
61
62 // compute the central meridian
63 int cm = ((lon >= 0.0) ? ((int)lon - ((int)lon) % 6 + 3) : ((int)lon - ((int)lon) % 6 - 3));
64
65 // convert degrees into radians
66 double rlat = lat * RADIANS_PER_DEGREE;
67 double rlon = lon * RADIANS_PER_DEGREE;
68 double rlon0 = cm * RADIANS_PER_DEGREE;
69
70 // compute trigonometric functions
71 double slat = sin(rlat);
72 double clat = cos(rlat);
73 double tlat = tan(rlat);
74
75 // decide the false northing at origin
76 double fn = (lat > 0) ? UTM_FN_N : UTM_FN_S;
77
78 double T = tlat * tlat;
79 double C = UTM_EP2 * clat * clat;
80 double A = (rlon - rlon0) * clat;
81 double M = WGS84_A * (m0 * rlat + m1 * sin(2 * rlat) + m2 * sin(4 * rlat) + m3 * sin(6 * rlat));
82 double V = WGS84_A / sqrt(1 - UTM_E2 * slat * slat);
83
84 // compute the easting-northing coordinates
85 *x = UTM_FE + UTM_K0 * V * (A + (1 - T + C) * pow(A, 3) / 6 + (5 - 18 * T + T * T + 72 * C - 58 * UTM_EP2) * pow(A, 5) / 120);
86 *y =
87 fn
88 + UTM_K0
89 * (M
90 + V * tlat * (A * A / 2 + (5 - T + 9 * C + 4 * C * C) * pow(A, 4) / 24 + ((61 - 58 * T + T * T + 600 * C - 330 * UTM_EP2) * pow(A, 6) / 720)));
91
92 return;
93 }
94
95
104 static inline char UTMLetterDesignator(double Lat)
105 {
106 char LetterDesignator;
107
108 if ((84 >= Lat) && (Lat >= 72))
109 LetterDesignator = 'X';
110 else if ((72 > Lat) && (Lat >= 64))
111 LetterDesignator = 'W';
112 else if ((64 > Lat) && (Lat >= 56))
113 LetterDesignator = 'V';
114 else if ((56 > Lat) && (Lat >= 48))
115 LetterDesignator = 'U';
116 else if ((48 > Lat) && (Lat >= 40))
117 LetterDesignator = 'T';
118 else if ((40 > Lat) && (Lat >= 32))
119 LetterDesignator = 'S';
120 else if ((32 > Lat) && (Lat >= 24))
121 LetterDesignator = 'R';
122 else if ((24 > Lat) && (Lat >= 16))
123 LetterDesignator = 'Q';
124 else if ((16 > Lat) && (Lat >= 8))
125 LetterDesignator = 'P';
126 else if ((8 > Lat) && (Lat >= 0))
127 LetterDesignator = 'N';
128 else if ((0 > Lat) && (Lat >= -8))
129 LetterDesignator = 'M';
130 else if ((-8 > Lat) && (Lat >= -16))
131 LetterDesignator = 'L';
132 else if ((-16 > Lat) && (Lat >= -24))
133 LetterDesignator = 'K';
134 else if ((-24 > Lat) && (Lat >= -32))
135 LetterDesignator = 'J';
136 else if ((-32 > Lat) && (Lat >= -40))
137 LetterDesignator = 'H';
138 else if ((-40 > Lat) && (Lat >= -48))
139 LetterDesignator = 'G';
140 else if ((-48 > Lat) && (Lat >= -56))
141 LetterDesignator = 'F';
142 else if ((-56 > Lat) && (Lat >= -64))
143 LetterDesignator = 'E';
144 else if ((-64 > Lat) && (Lat >= -72))
145 LetterDesignator = 'D';
146 else if ((-72 > Lat) && (Lat >= -80))
147 LetterDesignator = 'C';
148 // 'Z' is an error flag, the Latitude is outside the UTM limits
149 else
150 LetterDesignator = 'Z';
151 return LetterDesignator;
152 }
153
163 static inline void LLtoUTM(const double Lat, const double Long, double& UTMNorthing, double& UTMEasting, char* UTMZone)
164 {
165 double a = WGS84_A;
166 double eccSquared = UTM_E2;
167 double k0 = UTM_K0;
168
169 double LongOrigin;
170 double eccPrimeSquared;
171 double N, T, C, A, M;
172
173 // Make sure the longitude is between -180.00 .. 179.9
174 double LongTemp = (Long + 180) - int((Long + 180) / 360) * 360 - 180;
175
176 double LatRad = Lat * RADIANS_PER_DEGREE;
177 double LongRad = LongTemp * RADIANS_PER_DEGREE;
178 double LongOriginRad;
179 int ZoneNumber;
180
181 ZoneNumber = int((LongTemp + 180) / 6) + 1;
182 // range clamping to shut up some compiler warnings
183 // (the UTM Zone number should in reality be in the range <1, 60>)
184 if (ZoneNumber > 99)
185 ZoneNumber = 99;
186 if (ZoneNumber < -9)
187 ZoneNumber = -9;
188
189 if (Lat >= 56.0 && Lat < 64.0 && LongTemp >= 3.0 && LongTemp < 12.0)
190 ZoneNumber = 32;
191
192 // Special zones for Svalbard
193 if (Lat >= 72.0 && Lat < 84.0)
194 {
195 if (LongTemp >= 0.0 && LongTemp < 9.0)
196 ZoneNumber = 31;
197 else if (LongTemp >= 9.0 && LongTemp < 21.0)
198 ZoneNumber = 33;
199 else if (LongTemp >= 21.0 && LongTemp < 33.0)
200 ZoneNumber = 35;
201 else if (LongTemp >= 33.0 && LongTemp < 42.0)
202 ZoneNumber = 37;
203 }
204 // +3 puts origin in middle of zone
205 LongOrigin = (ZoneNumber - 1) * 6 - 180 + 3;
206 LongOriginRad = LongOrigin * RADIANS_PER_DEGREE;
207
208 // compute the UTM Zone from the latitude and longitude
209 snprintf(UTMZone, 4, "%d%c", ZoneNumber, UTMLetterDesignator(Lat));
210
211 eccPrimeSquared = (eccSquared) / (1 - eccSquared);
212
213 N = a / sqrt(1 - eccSquared * sin(LatRad) * sin(LatRad));
214 T = tan(LatRad) * tan(LatRad);
215 C = eccPrimeSquared * cos(LatRad) * cos(LatRad);
216 A = cos(LatRad) * (LongRad - LongOriginRad);
217
218 M = a
219 * ((1 - eccSquared / 4 - 3 * eccSquared * eccSquared / 64 - 5 * eccSquared * eccSquared * eccSquared / 256) * LatRad
220 - (3 * eccSquared / 8 + 3 * eccSquared * eccSquared / 32 + 45 * eccSquared * eccSquared * eccSquared / 1024) * sin(2 * LatRad)
221 + (15 * eccSquared * eccSquared / 256 + 45 * eccSquared * eccSquared * eccSquared / 1024) * sin(4 * LatRad)
222 - (35 * eccSquared * eccSquared * eccSquared / 3072) * sin(6 * LatRad));
223
224 UTMEasting =
225 (double)(k0 * N * (A + (1 - T + C) * A * A * A / 6 + (5 - 18 * T + T * T + 72 * C - 58 * eccPrimeSquared) * A * A * A * A * A / 120) + 500000.0);
226
227 UTMNorthing = (double)(k0
228 * (M
229 + N * tan(LatRad)
230 * (A * A / 2 + (5 - T + 9 * C + 4 * C * C) * A * A * A * A / 24
231 + (61 - 58 * T + T * T + 600 * C - 330 * eccPrimeSquared) * A * A * A * A * A * A / 720)));
232 if (Lat < 0)
233 UTMNorthing += 10000000.0; // 10000000 meter offset for southern hemisphere
234 }
235
236 static inline void LLtoUTM(const double Lat, const double Long, double& UTMNorthing, double& UTMEasting, std::string& UTMZone)
237 {
238 char zone_buf[] = {0, 0, 0, 0};
239
240 LLtoUTM(Lat, Long, UTMNorthing, UTMEasting, zone_buf);
241
242 UTMZone = zone_buf;
243 }
244
245
255 static inline void UTMtoLL(const double UTMNorthing, const double UTMEasting, const char* UTMZone, double& Lat, double& Long)
256 {
257 double k0 = UTM_K0;
258 double a = WGS84_A;
259 double eccSquared = UTM_E2;
260 double eccPrimeSquared;
261 double e1 = (1 - sqrt(1 - eccSquared)) / (1 + sqrt(1 - eccSquared));
262 double N1, T1, C1, R1, D, M;
263 double LongOrigin;
264 double mu, phi1Rad;
265 [[maybe_unused]] double phi1;
266 double x, y;
267 int ZoneNumber;
268 char* ZoneLetter;
269 [[maybe_unused]] int NorthernHemisphere; // 1 for northern hemispher, 0 for southern
270
271 x = UTMEasting - 500000.0; // remove 500,000 meter offset for longitude
272 y = UTMNorthing;
273
274 ZoneNumber = strtoul(UTMZone, &ZoneLetter, 10);
275 if ((*ZoneLetter - 'N') >= 0)
276 NorthernHemisphere = 1; // point is in northern hemisphere
277 else
278 {
279 NorthernHemisphere = 0; // point is in southern hemisphere
280 y -= 10000000.0; // remove 10,000,000 meter offset used for southern hemisphere
281 }
282
283 LongOrigin = (ZoneNumber - 1) * 6 - 180 + 3; //+3 puts origin in middle of zone
284
285 eccPrimeSquared = (eccSquared) / (1 - eccSquared);
286
287 M = y / k0;
288 mu = M / (a * (1 - eccSquared / 4 - 3 * eccSquared * eccSquared / 64 - 5 * eccSquared * eccSquared * eccSquared / 256));
289
290 phi1Rad = mu + (3 * e1 / 2 - 27 * e1 * e1 * e1 / 32) * sin(2 * mu) + (21 * e1 * e1 / 16 - 55 * e1 * e1 * e1 * e1 / 32) * sin(4 * mu)
291 + (151 * e1 * e1 * e1 / 96) * sin(6 * mu);
292 phi1 = phi1Rad * DEGREES_PER_RADIAN;
293
294 N1 = a / sqrt(1 - eccSquared * sin(phi1Rad) * sin(phi1Rad));
295 T1 = tan(phi1Rad) * tan(phi1Rad);
296 C1 = eccPrimeSquared * cos(phi1Rad) * cos(phi1Rad);
297 R1 = a * (1 - eccSquared) / pow(1 - eccSquared * sin(phi1Rad) * sin(phi1Rad), 1.5);
298 D = x / (N1 * k0);
299
300 Lat = phi1Rad
301 - (N1 * tan(phi1Rad) / R1)
302 * (D * D / 2 - (5 + 3 * T1 + 10 * C1 - 4 * C1 * C1 - 9 * eccPrimeSquared) * D * D * D * D / 24
303 + (61 + 90 * T1 + 298 * C1 + 45 * T1 * T1 - 252 * eccPrimeSquared - 3 * C1 * C1) * D * D * D * D * D * D / 720);
304 Lat = Lat * DEGREES_PER_RADIAN;
305
306 Long = (D - (1 + 2 * T1 + C1) * D * D * D / 6 + (5 - 2 * C1 + 28 * T1 - 3 * C1 * C1 + 8 * eccPrimeSquared + 24 * T1 * T1) * D * D * D * D * D / 120)
307 / cos(phi1Rad);
308 Long = LongOrigin + Long * DEGREES_PER_RADIAN;
309 }
310
311 static inline void UTMtoLL(const double UTMNorthing, const double UTMEasting, std::string UTMZone, double& Lat, double& Long)
312 {
313 UTMtoLL(UTMNorthing, UTMEasting, UTMZone.c_str(), Lat, Long);
314 }
315
316} // namespace mrs_lib
317
318#endif // _UTM_H
All mrs_lib functions, classes, variables and definitions are contained in this namespace.
Definition attitude_converter.h:24