1 // sg_geodesy.cxx -- routines to convert between geodetic and geocentric
4 // Copied and adapted directly from LaRCsim/ls_geodesy.c
6 // See below for the complete original LaRCsim comments.
10 #include <simgear/compiler.h>
12 #ifdef SG_HAVE_STD_INCLUDES
22 #include <simgear/constants.h>
23 #include <simgear/debug/logstream.hxx>
25 #include "point3d.hxx"
26 #include "sg_geodesy.hxx"
27 #include "localconsts.hxx"
33 // #define DOMAIN_ERR_DEBUG 1
36 // sgGeocToGeod(lat_geoc, radius, *lat_geod, *alt, *sea_level_r)
38 // lat_geoc Geocentric latitude, radians, + = North
39 // radius C.G. radius to earth center (meters)
42 // lat_geod Geodetic latitude, radians, + = North
43 // alt C.G. altitude above mean sea level (meters)
44 // sea_level_r radius from earth center to sea level at
45 // local vertical (surface normal) of C.G. (meters)
48 void sgGeocToGeod( const double& lat_geoc, const double& radius,
49 double *lat_geod, double *alt, double *sea_level_r )
51 #ifdef DOMAIN_ERR_DEBUG
52 errno = 0; // start with error zero'd
55 double t_lat, x_alpha, mu_alpha, delt_mu, r_alpha, l_point, rho_alpha;
56 double sin_mu_a, denom,delt_lambda, lambda_sl, sin_lambda_sl;
58 if( ( (SGD_PI_2 - lat_geoc) < SG_ONE_SECOND ) // near North pole
59 || ( (SGD_PI_2 + lat_geoc) < SG_ONE_SECOND ) ) // near South pole
62 *sea_level_r = SG_EQUATORIAL_RADIUS_M*E;
63 *alt = radius - *sea_level_r;
65 // cout << " lat_geoc = " << lat_geoc << endl;
66 t_lat = tan(lat_geoc);
67 // cout << " tan(t_lat) = " << t_lat << endl;
68 x_alpha = E*SG_EQUATORIAL_RADIUS_M/sqrt(t_lat*t_lat + E*E);
69 #ifdef DOMAIN_ERR_DEBUG
71 perror("fgGeocToGeod()");
72 SG_LOG( SG_GENERAL, SG_ALERT, "sqrt(" << t_lat*t_lat + E*E << ")" );
75 // cout << " x_alpha = " << x_alpha << endl;
76 double tmp = sqrt(SG_EQ_RAD_SQUARE_M - x_alpha * x_alpha);
77 if ( tmp < 0.0 ) { tmp = 0.0; }
78 #ifdef DOMAIN_ERR_DEBUG
80 perror("fgGeocToGeod()");
81 SG_LOG( SG_GENERAL, SG_ALERT, "sqrt(" << SG_EQ_RAD_SQUARE_M - x_alpha * x_alpha
85 mu_alpha = atan2(tmp,E*x_alpha);
86 if (lat_geoc < 0) mu_alpha = - mu_alpha;
87 sin_mu_a = sin(mu_alpha);
88 delt_lambda = mu_alpha - lat_geoc;
89 r_alpha = x_alpha/cos(lat_geoc);
90 l_point = radius - r_alpha;
91 *alt = l_point*cos(delt_lambda);
93 denom = sqrt(1-EPS*EPS*sin_mu_a*sin_mu_a);
94 #ifdef DOMAIN_ERR_DEBUG
96 perror("fgGeocToGeod()");
97 SG_LOG( SG_GENERAL, SG_ALERT, "sqrt(" <<
98 1-EPS*EPS*sin_mu_a*sin_mu_a << ")" );
101 rho_alpha = SG_EQUATORIAL_RADIUS_M*(1-EPS)/
103 delt_mu = atan2(l_point*sin(delt_lambda),rho_alpha + *alt);
104 *lat_geod = mu_alpha - delt_mu;
105 lambda_sl = atan( E*E * tan(*lat_geod) ); // SL geoc. latitude
106 sin_lambda_sl = sin( lambda_sl );
108 sqrt(SG_EQ_RAD_SQUARE_M / (1 + ((1/(E*E))-1)*sin_lambda_sl*sin_lambda_sl));
109 #ifdef DOMAIN_ERR_DEBUG
111 perror("fgGeocToGeod()");
112 SG_LOG( SG_GENERAL, SG_ALERT, "sqrt(" <<
113 SG_EQ_RAD_SQUARE_M / (1 + ((1/(E*E))-1)*sin_lambda_sl*sin_lambda_sl)
123 // sgGeodToGeoc( lat_geod, alt, *sl_radius, *lat_geoc )
125 // lat_geod Geodetic latitude, radians, + = North
126 // alt C.G. altitude above mean sea level (meters)
129 // sl_radius SEA LEVEL radius to earth center (meters)
130 // (add Altitude to get true distance from earth center.
131 // lat_geoc Geocentric latitude, radians, + = North
135 void sgGeodToGeoc( const double& lat_geod, const double& alt, double *sl_radius,
138 double lambda_sl, sin_lambda_sl, cos_lambda_sl, sin_mu, cos_mu, px, py;
140 #ifdef DOMAIN_ERR_DEBUG
144 lambda_sl = atan( E*E * tan(lat_geod) ); // sea level geocentric latitude
145 sin_lambda_sl = sin( lambda_sl );
146 cos_lambda_sl = cos( lambda_sl );
147 sin_mu = sin(lat_geod); // Geodetic (map makers') latitude
148 cos_mu = cos(lat_geod);
150 sqrt(SG_EQ_RAD_SQUARE_M / (1 + ((1/(E*E))-1)*sin_lambda_sl*sin_lambda_sl));
151 #ifdef DOMAIN_ERR_DEBUG
153 perror("fgGeodToGeoc()");
154 SG_LOG( SG_GENERAL, SG_ALERT, "sqrt(" <<
155 SG_EQ_RAD_SQUARE_M / (1 + ((1/(E*E))-1)*sin_lambda_sl*sin_lambda_sl)
159 py = *sl_radius*sin_lambda_sl + alt*sin_mu;
160 px = *sl_radius*cos_lambda_sl + alt*cos_mu;
161 *lat_geoc = atan2( py, px );
165 // Direct and inverse distance functions
167 // Proceedings of the 7th International Symposium on Geodetic
168 // Computations, 1985
170 // "The Nested Coefficient Method for Accurate Solutions of Direct and
171 // Inverse Geodetic Problems With Any Length"
176 // modified for FlightGear to use WGS84 only -- Norman Vine
178 #define GEOD_INV_PI SGD_PI
183 // for WGS_84 a = 6378137.000, rf = 298.257223563;
185 static inline double M0( double e2 ) {
187 return GEOD_INV_PI*(1.0 - e2*( 1.0/4.0 + e2*( 3.0/64.0 +
188 e2*(5.0/256.0) )))/2.0;
192 // given, alt, lat1, lon1, az1 and distance (s), calculate lat2, lon2
193 // and az2. Lat, lon, and azimuth are in degrees. distance in meters
194 int geo_direct_wgs_84 ( const double& alt, const double& lat1,
195 const double& lon1, const double& az1,
196 const double& s, double *lat2, double *lon2,
199 double a = 6378137.000, rf = 298.257223563;
200 double RADDEG = (GEOD_INV_PI)/180.0, testv = 1.0E-10;
201 double f = ( rf > 0.0 ? 1.0/rf : 0.0 );
202 double b = a*(1.0-f);
203 double e2 = f*(2.0-f);
204 double phi1 = lat1*RADDEG, lam1 = lon1*RADDEG;
205 double sinphi1 = sin(phi1), cosphi1 = cos(phi1);
206 double azm1 = az1*RADDEG;
207 double sinaz1 = sin(azm1), cosaz1 = cos(azm1);
210 if( fabs(s) < 0.01 ) { // distance < centimeter => congruency
214 if( *az2 > 360.0 ) *az2 -= 360.0;
216 } else if( cosphi1 ) { // non-polar origin
217 // u1 is reduced latitude
218 double tanu1 = sqrt(1.0-e2)*sinphi1/cosphi1;
219 double sig1 = atan2(tanu1,cosaz1);
220 double cosu1 = 1.0/sqrt( 1.0 + tanu1*tanu1 ), sinu1 = tanu1*cosu1;
221 double sinaz = cosu1*sinaz1, cos2saz = 1.0-sinaz*sinaz;
222 double us = cos2saz*e2/(1.0-e2);
225 double ta = 1.0+us*(4096.0+us*(-768.0+us*(320.0-175.0*us)))/16384.0,
226 tb = us*(256.0+us*(-128.0+us*(74.0-47.0*us)))/1024.0,
229 // FIRST ESTIMATE OF SIGMA (SIG)
230 double first = s/(b*ta); // !!
232 double c2sigm, sinsig,cossig, temp,denom,rnumer, dlams, dlam;
234 c2sigm = cos(2.0*sig1+sig);
235 sinsig = sin(sig); cossig = cos(sig);
238 tb*sinsig*(c2sigm+tb*(cossig*(-1.0+2.0*c2sigm*c2sigm) -
239 tb*c2sigm*(-3.0+4.0*sinsig*sinsig)
240 *(-3.0+4.0*c2sigm*c2sigm)/6.0)
242 } while( fabs(sig-temp) > testv);
244 // LATITUDE OF POINT 2
245 // DENOMINATOR IN 2 PARTS (TEMP ALSO USED LATER)
246 temp = sinu1*sinsig-cosu1*cossig*cosaz1;
247 denom = (1.0-f)*sqrt(sinaz*sinaz+temp*temp);
250 rnumer = sinu1*cossig+cosu1*sinsig*cosaz1;
251 *lat2 = atan2(rnumer,denom)/RADDEG;
253 // DIFFERENCE IN LONGITUDE ON AUXILARY SPHERE (DLAMS )
254 rnumer = sinsig*sinaz1;
255 denom = cosu1*cossig-sinu1*sinsig*cosaz1;
256 dlams = atan2(rnumer,denom);
259 tc = f*cos2saz*(4.0+f*(4.0-3.0*cos2saz))/16.0;
261 // DIFFERENCE IN LONGITUDE
262 dlam = dlams-(1.0-tc)*f*sinaz*(sig+tc*sinsig*
266 *lon2 = (lam1+dlam)/RADDEG;
267 if (*lon2 > 180.0 ) *lon2 -= 360.0;
268 if (*lon2 < -180.0 ) *lon2 += 360.0;
270 // AZIMUTH - FROM NORTH
271 *az2 = atan2(-sinaz,temp)/RADDEG;
272 if ( fabs(*az2) < testv ) *az2 = 0.0;
273 if( *az2 < 0.0) *az2 += 360.0;
275 } else { // phi1 == 90 degrees, polar origin
276 double dM = a*M0(e2) - s;
277 double paz = ( phi1 < 0.0 ? 180.0 : 0.0 );
279 return geo_direct_wgs_84( alt, zero, lon1, paz, dM, lat2, lon2, az2 );
284 // given alt, lat1, lon1, lat2, lon2, calculate starting and ending
285 // az1, az2 and distance (s). Lat, lon, and azimuth are in degrees.
286 // distance in meters
287 int geo_inverse_wgs_84( const double& alt, const double& lat1,
288 const double& lon1, const double& lat2,
289 const double& lon2, double *az1, double *az2,
292 double a = 6378137.000, rf = 298.257223563;
294 double RADDEG = (GEOD_INV_PI)/180.0, testv = 1.0E-10;
295 double f = ( rf > 0.0 ? 1.0/rf : 0.0 );
296 double b = a*(1.0-f);
297 // double e2 = f*(2.0-f); // unused in this routine
298 double phi1 = lat1*RADDEG, lam1 = lon1*RADDEG;
299 double sinphi1 = sin(phi1), cosphi1 = cos(phi1);
300 double phi2 = lat2*RADDEG, lam2 = lon2*RADDEG;
301 double sinphi2 = sin(phi2), cosphi2 = cos(phi2);
303 if( (fabs(lat1-lat2) < testv &&
304 ( fabs(lon1-lon2) < testv) || fabs(lat1-90.0) < testv ) )
306 // TWO STATIONS ARE IDENTICAL : SET DISTANCE & AZIMUTHS TO ZERO */
307 *az1 = 0.0; *az2 = 0.0; *s = 0.0;
309 } else if( fabs(cosphi1) < testv ) {
310 // initial point is polar
311 int k = geo_inverse_wgs_84( alt, lat2,lon2,lat1,lon1, az1,az2,s );
312 k = k; // avoid compiler error since return result is unused
313 b = *az1; *az1 = *az2; *az2 = b;
315 } else if( fabs(cosphi2) < testv ) {
316 // terminal point is polar
317 double _lon1 = lon1 + 180.0f;
318 int k = geo_inverse_wgs_84( alt, lat1, lon1, lat1, _lon1,
320 k = k; // avoid compiler error since return result is unused
323 if( *az2 > 360.0 ) *az2 -= 360.0;
325 } else if( (fabs( fabs(lon1-lon2) - 180 ) < testv) &&
326 (fabs(lat1+lat2) < testv) )
328 // Geodesic passes through the pole (antipodal)
330 geo_inverse_wgs_84( alt, lat1,lon1, lat1,lon2, az1,az2, &s1 );
331 geo_inverse_wgs_84( alt, lat2,lon2, lat1,lon2, az1,az2, &s2 );
336 // antipodal and polar points don't get here
337 double dlam = lam2 - lam1, dlams = dlam;
338 double sdlams,cdlams, sig,sinsig,cossig, sinaz,
340 double tc,temp, us,rnumer,denom, ta,tb;
341 double cosu1,sinu1, sinu2,cosu2;
344 temp = (1.0-f)*sinphi1/cosphi1;
345 cosu1 = 1.0/sqrt(1.0+temp*temp);
347 temp = (1.0-f)*sinphi2/cosphi2;
348 cosu2 = 1.0/sqrt(1.0+temp*temp);
352 sdlams = sin(dlams), cdlams = cos(dlams);
353 sinsig = sqrt(cosu2*cosu2*sdlams*sdlams+
354 (cosu1*sinu2-sinu1*cosu2*cdlams)*
355 (cosu1*sinu2-sinu1*cosu2*cdlams));
356 cossig = sinu1*sinu2+cosu1*cosu2*cdlams;
358 sig = atan2(sinsig,cossig);
359 sinaz = cosu1*cosu2*sdlams/sinsig;
360 cos2saz = 1.0-sinaz*sinaz;
361 c2sigm = (sinu1 == 0.0 || sinu2 == 0.0 ? cossig :
362 cossig-2.0*sinu1*sinu2/cos2saz);
363 tc = f*cos2saz*(4.0+f*(4.0-3.0*cos2saz))/16.0;
365 dlams = dlam+(1.0-tc)*f*sinaz*
367 (c2sigm+tc*cossig*(-1.0+2.0*c2sigm*c2sigm)));
368 if (fabs(dlams) > GEOD_INV_PI && iter++ > 50) {
371 } while ( fabs(temp-dlams) > testv);
373 us = cos2saz*(a*a-b*b)/(b*b); // !!
374 // BACK AZIMUTH FROM NORTH
375 rnumer = -(cosu1*sdlams);
376 denom = sinu1*cosu2-cosu1*sinu2*cdlams;
377 *az2 = atan2(rnumer,denom)/RADDEG;
378 if( fabs(*az2) < testv ) *az2 = 0.0;
379 if(*az2 < 0.0) *az2 += 360.0;
381 // FORWARD AZIMUTH FROM NORTH
382 rnumer = cosu2*sdlams;
383 denom = cosu1*sinu2-sinu1*cosu2*cdlams;
384 *az1 = atan2(rnumer,denom)/RADDEG;
385 if( fabs(*az1) < testv ) *az1 = 0.0;
386 if(*az1 < 0.0) *az1 += 360.0;
389 ta = 1.0+us*(4096.0+us*(-768.0+us*(320.0-175.0*us)))/
391 tb = us*(256.0+us*(-128.0+us*(74.0-47.0*us)))/1024.0;
394 *s = b*ta*(sig-tb*sinsig*
395 (c2sigm+tb*(cossig*(-1.0+2.0*c2sigm*c2sigm)-tb*
396 c2sigm*(-3.0+4.0*sinsig*sinsig)*
397 (-3.0+4.0*c2sigm*c2sigm)/6.0)/
404 /***************************************************************************
408 ----------------------------------------------------------------------------
410 FUNCTION: Converts geocentric coordinates to geodetic positions
412 ----------------------------------------------------------------------------
414 MODULE STATUS: developmental
416 ----------------------------------------------------------------------------
418 GENEALOGY: Written as part of LaRCSim project by E. B. Jackson
420 ----------------------------------------------------------------------------
422 DESIGNED BY: E. B. Jackson
424 CODED BY: E. B. Jackson
426 MAINTAINED BY: E. B. Jackson
428 ----------------------------------------------------------------------------
430 MODIFICATION HISTORY:
434 930208 Modified to avoid singularity near polar region. EBJ
435 930602 Moved backwards calcs here from ls_step. EBJ
436 931214 Changed erroneous Latitude and Altitude variables to
437 *lat_geod and *alt in routine ls_geoc_to_geod. EBJ
438 940111 Changed header files from old ls_eom.h style to ls_types,
439 and ls_constants. Also replaced old DATA type with new
445 * Revision 1.5 1994/01/11 18:47:05 bjax
446 * Changed include files to use types and constants, not ls_eom.h
447 * Also changed DATA type to SCALAR type.
449 * Revision 1.4 1993/12/14 21:06:47 bjax
450 * Removed global variable references Altitude and Latitude. EBJ
452 * Revision 1.3 1993/06/02 15:03:40 bjax
453 * Made new subroutine for calculating geodetic to geocentric; changed name
454 * of forward conversion routine from ls_geodesy to ls_geoc_to_geod.
457 ----------------------------------------------------------------------------
461 [ 1] Stevens, Brian L.; and Lewis, Frank L.: "Aircraft
462 Control and Simulation", Wiley and Sons, 1992.
466 ----------------------------------------------------------------------------
470 ----------------------------------------------------------------------------
474 ----------------------------------------------------------------------------
477 lat_geoc Geocentric latitude, radians, + = North
478 radius C.G. radius to earth center, ft
480 ----------------------------------------------------------------------------
483 lat_geod Geodetic latitude, radians, + = North
484 alt C.G. altitude above mean sea level, ft
485 sea_level_r radius from earth center to sea level at
486 local vertical (surface normal) of C.G.
488 --------------------------------------------------------------------------*/