1 // fg_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 FG_HAVE_STD_INCLUDES
20 #include <simgear/constants.h>
21 #include <simgear/debug/logstream.hxx>
23 #include "point3d.hxx"
24 #include "fg_geodesy.hxx"
26 #ifndef FG_HAVE_NATIVE_SGI_COMPILERS
30 // ONE_SECOND is pi/180/60/60, or about 100 feet at earths' equator
31 #define ONE_SECOND 4.848136811E-6
34 // fgGeocToGeod(lat_geoc, radius, *lat_geod, *alt, *sea_level_r)
36 // lat_geoc Geocentric latitude, radians, + = North
37 // radius C.G. radius to earth center (meters)
40 // lat_geod Geodetic latitude, radians, + = North
41 // alt C.G. altitude above mean sea level (meters)
42 // sea_level_r radius from earth center to sea level at
43 // local vertical (surface normal) of C.G. (meters)
46 void fgGeocToGeod( double lat_geoc, double radius, double
47 *lat_geod, double *alt, double *sea_level_r )
49 double t_lat, x_alpha, mu_alpha, delt_mu, r_alpha, l_point, rho_alpha;
50 double sin_mu_a, denom,delt_lambda, lambda_sl, sin_lambda_sl;
52 if( ( (FG_PI_2 - lat_geoc) < ONE_SECOND ) // near North pole
53 || ( (FG_PI_2 + lat_geoc) < ONE_SECOND ) ) // near South pole
56 *sea_level_r = EQUATORIAL_RADIUS_M*E;
57 *alt = radius - *sea_level_r;
59 // cout << " lat_geoc = " << lat_geoc << endl;
60 t_lat = tan(lat_geoc);
61 // cout << " tan(t_lat) = " << t_lat << endl;
62 x_alpha = E*EQUATORIAL_RADIUS_M/sqrt(t_lat*t_lat + E*E);
63 // cout << " x_alpha = " << x_alpha << endl;
64 double tmp = RESQ_M - x_alpha * x_alpha;
65 if ( tmp < 0.0 ) { tmp = 0.0; }
66 mu_alpha = atan2(sqrt(tmp),E*x_alpha);
67 if (lat_geoc < 0) mu_alpha = - mu_alpha;
68 sin_mu_a = sin(mu_alpha);
69 delt_lambda = mu_alpha - lat_geoc;
70 r_alpha = x_alpha/cos(lat_geoc);
71 l_point = radius - r_alpha;
72 *alt = l_point*cos(delt_lambda);
74 // check for domain error
75 if ( errno == EDOM ) {
76 FG_LOG( FG_GENERAL, FG_ALERT, "Domain ERROR in fgGeocToGeod!!!!" );
80 denom = sqrt(1-EPS*EPS*sin_mu_a*sin_mu_a);
81 rho_alpha = EQUATORIAL_RADIUS_M*(1-EPS)/
83 delt_mu = atan2(l_point*sin(delt_lambda),rho_alpha + *alt);
84 *lat_geod = mu_alpha - delt_mu;
85 lambda_sl = atan( E*E * tan(*lat_geod) ); // SL geoc. latitude
86 sin_lambda_sl = sin( lambda_sl );
88 sqrt(RESQ_M / (1 + ((1/(E*E))-1)*sin_lambda_sl*sin_lambda_sl));
90 // check for domain error
91 if ( errno == EDOM ) {
92 FG_LOG( FG_GENERAL, FG_ALERT, "Domain ERROR in fgGeocToGeod!!!!" );
100 // fgGeodToGeoc( lat_geod, alt, *sl_radius, *lat_geoc )
102 // lat_geod Geodetic latitude, radians, + = North
103 // alt C.G. altitude above mean sea level (meters)
106 // sl_radius SEA LEVEL radius to earth center (meters)
107 // (add Altitude to get true distance from earth center.
108 // lat_geoc Geocentric latitude, radians, + = North
112 void fgGeodToGeoc( double lat_geod, double alt, double *sl_radius,
115 double lambda_sl, sin_lambda_sl, cos_lambda_sl, sin_mu, cos_mu, px, py;
117 lambda_sl = atan( E*E * tan(lat_geod) ); // sea level geocentric latitude
118 sin_lambda_sl = sin( lambda_sl );
119 cos_lambda_sl = cos( lambda_sl );
120 sin_mu = sin(lat_geod); // Geodetic (map makers') latitude
121 cos_mu = cos(lat_geod);
123 sqrt(RESQ_M / (1 + ((1/(E*E))-1)*sin_lambda_sl*sin_lambda_sl));
124 py = *sl_radius*sin_lambda_sl + alt*sin_mu;
125 px = *sl_radius*cos_lambda_sl + alt*cos_mu;
126 *lat_geoc = atan2( py, px );
130 // Direct and inverse distance functions
132 // Proceedings of the 7th International Symposium on Geodetic
133 // Computations, 1985
135 // "The Nested Coefficient Method for Accurate Solutions of Direct and
136 // Inverse Geodetic Problems With Any Length"
141 // modified for FlightGear to use WGS84 only -- Norman Vine
143 #define GEOD_INV_PI FG_PI
148 // for WGS_84 a = 6378137.000, rf = 298.257223563;
150 static double M0( double e2 ) {
152 return GEOD_INV_PI*(1.0 - e2*( 1.0/4.0 + e2*( 3.0/64.0 +
153 e2*(5.0/256.0) )))/2.0;
157 // given, alt, lat1, lon1, az1 and distance (s), calculate lat2, lon2
158 // and az2. Lat, lon, and azimuth are in degrees. distance in meters
159 int geo_direct_wgs_84 ( double alt, double lat1, double lon1, double az1,
160 double s, double *lat2, double *lon2, double *az2 )
162 double a = 6378137.000, rf = 298.257223563;
163 double RADDEG = (GEOD_INV_PI)/180.0, testv = 1.0E-10;
164 double f = ( rf > 0.0 ? 1.0/rf : 0.0 );
165 double b = a*(1.0-f);
166 double e2 = f*(2.0-f);
167 double phi1 = lat1*RADDEG, lam1 = lon1*RADDEG;
168 double sinphi1 = sin(phi1), cosphi1 = cos(phi1);
169 double azm1 = az1*RADDEG;
170 double sinaz1 = sin(azm1), cosaz1 = cos(azm1);
173 if( fabs(s) < 0.01 ) { // distance < centimeter => congruency
177 if( *az2 > 360.0 ) *az2 -= 360.0;
179 } else if( cosphi1 ) { // non-polar origin
180 // u1 is reduced latitude
181 double tanu1 = sqrt(1.0-e2)*sinphi1/cosphi1;
182 double sig1 = atan2(tanu1,cosaz1);
183 double cosu1 = 1.0/sqrt( 1.0 + tanu1*tanu1 ), sinu1 = tanu1*cosu1;
184 double sinaz = cosu1*sinaz1, cos2saz = 1.0-sinaz*sinaz;
185 double us = cos2saz*e2/(1.0-e2);
188 double ta = 1.0+us*(4096.0+us*(-768.0+us*(320.0-175.0*us)))/16384.0,
189 tb = us*(256.0+us*(-128.0+us*(74.0-47.0*us)))/1024.0,
192 // FIRST ESTIMATE OF SIGMA (SIG)
193 double first = s/(b*ta); // !!
195 double c2sigm, sinsig,cossig, temp,denom,rnumer, dlams, dlam;
197 c2sigm = cos(2.0*sig1+sig);
198 sinsig = sin(sig); cossig = cos(sig);
201 tb*sinsig*(c2sigm+tb*(cossig*(-1.0+2.0*c2sigm*c2sigm) -
202 tb*c2sigm*(-3.0+4.0*sinsig*sinsig)
203 *(-3.0+4.0*c2sigm*c2sigm)/6.0)
205 } while( fabs(sig-temp) > testv);
207 // LATITUDE OF POINT 2
208 // DENOMINATOR IN 2 PARTS (TEMP ALSO USED LATER)
209 temp = sinu1*sinsig-cosu1*cossig*cosaz1;
210 denom = (1.0-f)*sqrt(sinaz*sinaz+temp*temp);
213 rnumer = sinu1*cossig+cosu1*sinsig*cosaz1;
214 *lat2 = atan2(rnumer,denom)/RADDEG;
216 // DIFFERENCE IN LONGITUDE ON AUXILARY SPHERE (DLAMS )
217 rnumer = sinsig*sinaz1;
218 denom = cosu1*cossig-sinu1*sinsig*cosaz1;
219 dlams = atan2(rnumer,denom);
222 tc = f*cos2saz*(4.0+f*(4.0-3.0*cos2saz))/16.0;
224 // DIFFERENCE IN LONGITUDE
225 dlam = dlams-(1.0-tc)*f*sinaz*(sig+tc*sinsig*
229 *lon2 = (lam1+dlam)/RADDEG;
230 if (*lon2 > 180.0 ) *lon2 -= 360.0;
231 if (*lon2 < -180.0 ) *lon2 += 360.0;
233 // AZIMUTH - FROM NORTH
234 *az2 = atan2(-sinaz,temp)/RADDEG;
235 if ( fabs(*az2) < testv ) *az2 = 0.0;
236 if( *az2 < 0.0) *az2 += 360.0;
238 } else { // phi1 == 90 degrees, polar origin
239 double dM = a*M0(e2) - s;
240 double paz = ( phi1 < 0.0 ? 180.0 : 0.0 );
241 return geo_direct_wgs_84( alt, 0.0, lon1, paz, dM,lat2,lon2,az2 );
246 // given alt, lat1, lon1, lat2, lon2, calculate starting and ending
247 // az1, az2 and distance (s). Lat, lon, and azimuth are in degrees.
248 // distance in meters
249 int geo_inverse_wgs_84( double alt, double lat1, double lon1, double lat2,
250 double lon2, double *az1, double *az2, double *s )
252 double a = 6378137.000, rf = 298.257223563;
254 double RADDEG = (GEOD_INV_PI)/180.0, testv = 1.0E-10;
255 double f = ( rf > 0.0 ? 1.0/rf : 0.0 );
256 double b = a*(1.0-f);
257 // double e2 = f*(2.0-f); // unused in this routine
258 double phi1 = lat1*RADDEG, lam1 = lon1*RADDEG;
259 double sinphi1 = sin(phi1), cosphi1 = cos(phi1);
260 double phi2 = lat2*RADDEG, lam2 = lon2*RADDEG;
261 double sinphi2 = sin(phi2), cosphi2 = cos(phi2);
263 if( (fabs(lat1-lat2) < testv &&
264 ( fabs(lon1-lon2) < testv) || fabs(lat1-90.0) < testv ) )
266 // TWO STATIONS ARE IDENTICAL : SET DISTANCE & AZIMUTHS TO ZERO */
267 *az1 = 0.0; *az2 = 0.0; *s = 0.0;
269 } else if( fabs(cosphi1) < testv ) {
270 // initial point is polar
271 int k = geo_inverse_wgs_84( alt, lat2,lon2,lat1,lon1, az1,az2,s );
272 k = k; // avoid compiler error since return result is unused
273 b = *az1; *az1 = *az2; *az2 = b;
275 } else if( fabs(cosphi2) < testv ) {
276 // terminal point is polar
277 int k = geo_inverse_wgs_84( alt, lat1,lon1,lat1,lon1+180.0,
279 k = k; // avoid compiler error since return result is unused
282 if( *az2 > 360.0 ) *az2 -= 360.0;
284 } else if( (fabs( fabs(lon1-lon2) - 180 ) < testv) &&
285 (fabs(lat1+lat2) < testv) )
287 // Geodesic passes through the pole (antipodal)
289 geo_inverse_wgs_84( alt, lat1,lon1, lat1,lon2, az1,az2, &s1 );
290 geo_inverse_wgs_84( alt, lat2,lon2, lat1,lon2, az1,az2, &s2 );
295 // antipodal and polar points don't get here
296 double dlam = lam2 - lam1, dlams = dlam;
297 double sdlams,cdlams, sig,sinsig,cossig, sinaz,
299 double tc,temp, us,rnumer,denom, ta,tb;
300 double cosu1,sinu1, sinu2,cosu2;
303 temp = (1.0-f)*sinphi1/cosphi1;
304 cosu1 = 1.0/sqrt(1.0+temp*temp);
306 temp = (1.0-f)*sinphi2/cosphi2;
307 cosu2 = 1.0/sqrt(1.0+temp*temp);
311 sdlams = sin(dlams), cdlams = cos(dlams);
312 sinsig = sqrt(cosu2*cosu2*sdlams*sdlams+
313 (cosu1*sinu2-sinu1*cosu2*cdlams)*
314 (cosu1*sinu2-sinu1*cosu2*cdlams));
315 cossig = sinu1*sinu2+cosu1*cosu2*cdlams;
317 sig = atan2(sinsig,cossig);
318 sinaz = cosu1*cosu2*sdlams/sinsig;
319 cos2saz = 1.0-sinaz*sinaz;
320 c2sigm = (sinu1 == 0.0 || sinu2 == 0.0 ? cossig :
321 cossig-2.0*sinu1*sinu2/cos2saz);
322 tc = f*cos2saz*(4.0+f*(4.0-3.0*cos2saz))/16.0;
324 dlams = dlam+(1.0-tc)*f*sinaz*
326 (c2sigm+tc*cossig*(-1.0+2.0*c2sigm*c2sigm)));
327 if (fabs(dlams) > GEOD_INV_PI && iter++ > 50) {
330 } while ( fabs(temp-dlams) > testv);
332 us = cos2saz*(a*a-b*b)/(b*b); // !!
333 // BACK AZIMUTH FROM NORTH
334 rnumer = -(cosu1*sdlams);
335 denom = sinu1*cosu2-cosu1*sinu2*cdlams;
336 *az2 = atan2(rnumer,denom)/RADDEG;
337 if( fabs(*az2) < testv ) *az2 = 0.0;
338 if(*az2 < 0.0) *az2 += 360.0;
340 // FORWARD AZIMUTH FROM NORTH
341 rnumer = cosu2*sdlams;
342 denom = cosu1*sinu2-sinu1*cosu2*cdlams;
343 *az1 = atan2(rnumer,denom)/RADDEG;
344 if( fabs(*az1) < testv ) *az1 = 0.0;
345 if(*az1 < 0.0) *az1 += 360.0;
348 ta = 1.0+us*(4096.0+us*(-768.0+us*(320.0-175.0*us)))/
350 tb = us*(256.0+us*(-128.0+us*(74.0-47.0*us)))/1024.0;
353 *s = b*ta*(sig-tb*sinsig*
354 (c2sigm+tb*(cossig*(-1.0+2.0*c2sigm*c2sigm)-tb*
355 c2sigm*(-3.0+4.0*sinsig*sinsig)*
356 (-3.0+4.0*c2sigm*c2sigm)/6.0)/
363 /***************************************************************************
367 ----------------------------------------------------------------------------
369 FUNCTION: Converts geocentric coordinates to geodetic positions
371 ----------------------------------------------------------------------------
373 MODULE STATUS: developmental
375 ----------------------------------------------------------------------------
377 GENEALOGY: Written as part of LaRCSim project by E. B. Jackson
379 ----------------------------------------------------------------------------
381 DESIGNED BY: E. B. Jackson
383 CODED BY: E. B. Jackson
385 MAINTAINED BY: E. B. Jackson
387 ----------------------------------------------------------------------------
389 MODIFICATION HISTORY:
393 930208 Modified to avoid singularity near polar region. EBJ
394 930602 Moved backwards calcs here from ls_step. EBJ
395 931214 Changed erroneous Latitude and Altitude variables to
396 *lat_geod and *alt in routine ls_geoc_to_geod. EBJ
397 940111 Changed header files from old ls_eom.h style to ls_types,
398 and ls_constants. Also replaced old DATA type with new
404 * Revision 1.5 1994/01/11 18:47:05 bjax
405 * Changed include files to use types and constants, not ls_eom.h
406 * Also changed DATA type to SCALAR type.
408 * Revision 1.4 1993/12/14 21:06:47 bjax
409 * Removed global variable references Altitude and Latitude. EBJ
411 * Revision 1.3 1993/06/02 15:03:40 bjax
412 * Made new subroutine for calculating geodetic to geocentric; changed name
413 * of forward conversion routine from ls_geodesy to ls_geoc_to_geod.
416 ----------------------------------------------------------------------------
420 [ 1] Stevens, Brian L.; and Lewis, Frank L.: "Aircraft
421 Control and Simulation", Wiley and Sons, 1992.
425 ----------------------------------------------------------------------------
429 ----------------------------------------------------------------------------
433 ----------------------------------------------------------------------------
436 lat_geoc Geocentric latitude, radians, + = North
437 radius C.G. radius to earth center, ft
439 ----------------------------------------------------------------------------
442 lat_geod Geodetic latitude, radians, + = North
443 alt C.G. altitude above mean sea level, ft
444 sea_level_r radius from earth center to sea level at
445 local vertical (surface normal) of C.G.
447 --------------------------------------------------------------------------*/