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/logstream.hxx>
21 #include <simgear/constants.h>
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 t_lat = tan(lat_geoc);
60 x_alpha = E*EQUATORIAL_RADIUS_M/sqrt(t_lat*t_lat + E*E);
61 double tmp = RESQ_M - x_alpha * x_alpha;
62 if ( tmp < 0.0 ) { tmp = 0.0; }
63 mu_alpha = atan2(sqrt(tmp),E*x_alpha);
64 if (lat_geoc < 0) mu_alpha = - mu_alpha;
65 sin_mu_a = sin(mu_alpha);
66 delt_lambda = mu_alpha - lat_geoc;
67 r_alpha = x_alpha/cos(lat_geoc);
68 l_point = radius - r_alpha;
69 *alt = l_point*cos(delt_lambda);
71 // check for domain error
72 if ( errno == EDOM ) {
73 FG_LOG( FG_GENERAL, FG_ALERT, "Domain ERROR in fgGeocToGeod!!!!" );
77 denom = sqrt(1-EPS*EPS*sin_mu_a*sin_mu_a);
78 rho_alpha = EQUATORIAL_RADIUS_M*(1-EPS)/
80 delt_mu = atan2(l_point*sin(delt_lambda),rho_alpha + *alt);
81 *lat_geod = mu_alpha - delt_mu;
82 lambda_sl = atan( E*E * tan(*lat_geod) ); // SL geoc. latitude
83 sin_lambda_sl = sin( lambda_sl );
85 sqrt(RESQ_M / (1 + ((1/(E*E))-1)*sin_lambda_sl*sin_lambda_sl));
87 // check for domain error
88 if ( errno == EDOM ) {
89 FG_LOG( FG_GENERAL, FG_ALERT, "Domain ERROR in fgGeocToGeod!!!!" );
97 // fgGeodToGeoc( lat_geod, alt, *sl_radius, *lat_geoc )
99 // lat_geod Geodetic latitude, radians, + = North
100 // alt C.G. altitude above mean sea level (meters)
103 // sl_radius SEA LEVEL radius to earth center (meters)
104 // (add Altitude to get true distance from earth center.
105 // lat_geoc Geocentric latitude, radians, + = North
109 void fgGeodToGeoc( double lat_geod, double alt, double *sl_radius,
112 double lambda_sl, sin_lambda_sl, cos_lambda_sl, sin_mu, cos_mu, px, py;
114 lambda_sl = atan( E*E * tan(lat_geod) ); // sea level geocentric latitude
115 sin_lambda_sl = sin( lambda_sl );
116 cos_lambda_sl = cos( lambda_sl );
117 sin_mu = sin(lat_geod); // Geodetic (map makers') latitude
118 cos_mu = cos(lat_geod);
120 sqrt(RESQ_M / (1 + ((1/(E*E))-1)*sin_lambda_sl*sin_lambda_sl));
121 py = *sl_radius*sin_lambda_sl + alt*sin_mu;
122 px = *sl_radius*cos_lambda_sl + alt*cos_mu;
123 *lat_geoc = atan2( py, px );
127 // Direct and inverse distance functions
129 // Proceedings of the 7th International Symposium on Geodetic
130 // Computations, 1985
132 // "The Nested Coefficient Method for Accurate Solutions of Direct and
133 // Inverse Geodetic Problems With Any Length"
138 // modified for FlightGear to use WGS84 only -- Norman Vine
140 #define GEOD_INV_PI FG_PI
145 // for WGS_84 a = 6378137.000, rf = 298.257223563;
147 static double M0( double e2 ) {
149 return GEOD_INV_PI*(1.0 - e2*( 1.0/4.0 + e2*( 3.0/64.0 +
150 e2*(5.0/256.0) )))/2.0;
154 // given, alt, lat1, lon1, az1 and distance (s), calculate lat2, lon2
155 // and az2. Lat, lon, and azimuth are in degrees. distance in meters
156 int geo_direct_wgs_84 ( double alt, double lat1, double lon1, double az1,
157 double s, double *lat2, double *lon2, double *az2 )
159 double a = 6378137.000, rf = 298.257223563;
160 double RADDEG = (GEOD_INV_PI)/180.0, testv = 1.0E-10;
161 double f = ( rf > 0.0 ? 1.0/rf : 0.0 );
162 double b = a*(1.0-f);
163 double e2 = f*(2.0-f);
164 double phi1 = lat1*RADDEG, lam1 = lon1*RADDEG;
165 double sinphi1 = sin(phi1), cosphi1 = cos(phi1);
166 double azm1 = az1*RADDEG;
167 double sinaz1 = sin(azm1), cosaz1 = cos(azm1);
170 if( fabs(s) < 0.01 ) { // distance < centimeter => congruency
174 if( *az2 > 360.0 ) *az2 -= 360.0;
176 } else if( cosphi1 ) { // non-polar origin
177 // u1 is reduced latitude
178 double tanu1 = sqrt(1.0-e2)*sinphi1/cosphi1;
179 double sig1 = atan2(tanu1,cosaz1);
180 double cosu1 = 1.0/sqrt( 1.0 + tanu1*tanu1 ), sinu1 = tanu1*cosu1;
181 double sinaz = cosu1*sinaz1, cos2saz = 1.0-sinaz*sinaz;
182 double us = cos2saz*e2/(1.0-e2);
185 double ta = 1.0+us*(4096.0+us*(-768.0+us*(320.0-175.0*us)))/16384.0,
186 tb = us*(256.0+us*(-128.0+us*(74.0-47.0*us)))/1024.0,
189 // FIRST ESTIMATE OF SIGMA (SIG)
190 double first = s/(b*ta); // !!
192 double c2sigm, sinsig,cossig, temp,denom,rnumer, dlams, dlam;
194 c2sigm = cos(2.0*sig1+sig);
195 sinsig = sin(sig); cossig = cos(sig);
198 tb*sinsig*(c2sigm+tb*(cossig*(-1.0+2.0*c2sigm*c2sigm) -
199 tb*c2sigm*(-3.0+4.0*sinsig*sinsig)
200 *(-3.0+4.0*c2sigm*c2sigm)/6.0)
202 } while( fabs(sig-temp) > testv);
204 // LATITUDE OF POINT 2
205 // DENOMINATOR IN 2 PARTS (TEMP ALSO USED LATER)
206 temp = sinu1*sinsig-cosu1*cossig*cosaz1;
207 denom = (1.0-f)*sqrt(sinaz*sinaz+temp*temp);
210 rnumer = sinu1*cossig+cosu1*sinsig*cosaz1;
211 *lat2 = atan2(rnumer,denom)/RADDEG;
213 // DIFFERENCE IN LONGITUDE ON AUXILARY SPHERE (DLAMS )
214 rnumer = sinsig*sinaz1;
215 denom = cosu1*cossig-sinu1*sinsig*cosaz1;
216 dlams = atan2(rnumer,denom);
219 tc = f*cos2saz*(4.0+f*(4.0-3.0*cos2saz))/16.0;
221 // DIFFERENCE IN LONGITUDE
222 dlam = dlams-(1.0-tc)*f*sinaz*(sig+tc*sinsig*
226 *lon2 = (lam1+dlam)/RADDEG;
227 if (*lon2 > 180.0 ) *lon2 -= 360.0;
228 if (*lon2 < -180.0 ) *lon2 += 360.0;
230 // AZIMUTH - FROM NORTH
231 *az2 = atan2(-sinaz,temp)/RADDEG;
232 if ( fabs(*az2) < testv ) *az2 = 0.0;
233 if( *az2 < 0.0) *az2 += 360.0;
235 } else { // phi1 == 90 degrees, polar origin
236 double dM = a*M0(e2) - s;
237 double paz = ( phi1 < 0.0 ? 180.0 : 0.0 );
238 return geo_direct_wgs_84( alt, 0.0, lon1, paz, dM,lat2,lon2,az2 );
243 // given alt, lat1, lon1, lat2, lon2, calculate starting and ending
244 // az1, az2 and distance (s). Lat, lon, and azimuth are in degrees.
245 // distance in meters
246 int geo_inverse_wgs_84( double alt, double lat1, double lon1, double lat2,
247 double lon2, double *az1, double *az2, double *s )
249 double a = 6378137.000, rf = 298.257223563;
251 double RADDEG = (GEOD_INV_PI)/180.0, testv = 1.0E-10;
252 double f = ( rf > 0.0 ? 1.0/rf : 0.0 );
253 double b = a*(1.0-f);
254 // double e2 = f*(2.0-f); // unused in this routine
255 double phi1 = lat1*RADDEG, lam1 = lon1*RADDEG;
256 double sinphi1 = sin(phi1), cosphi1 = cos(phi1);
257 double phi2 = lat2*RADDEG, lam2 = lon2*RADDEG;
258 double sinphi2 = sin(phi2), cosphi2 = cos(phi2);
260 if( (fabs(lat1-lat2) < testv &&
261 ( fabs(lon1-lon2) < testv) || fabs(lat1-90.0) < testv ) )
263 // TWO STATIONS ARE IDENTICAL : SET DISTANCE & AZIMUTHS TO ZERO */
264 *az1 = 0.0; *az2 = 0.0; *s = 0.0;
266 } else if( fabs(cosphi1) < testv ) {
267 // initial point is polar
268 int k = geo_inverse_wgs_84( alt, lat2,lon2,lat1,lon1, az1,az2,s );
269 k = k; // avoid compiler error since return result is unused
270 b = *az1; *az1 = *az2; *az2 = b;
272 } else if( fabs(cosphi2) < testv ) {
273 // terminal point is polar
274 int k = geo_inverse_wgs_84( alt, lat1,lon1,lat1,lon1+180.0,
276 k = k; // avoid compiler error since return result is unused
279 if( *az2 > 360.0 ) *az2 -= 360.0;
281 } else if( (fabs( fabs(lon1-lon2) - 180 ) < testv) &&
282 (fabs(lat1+lat2) < testv) )
284 // Geodesic passes through the pole (antipodal)
286 geo_inverse_wgs_84( alt, lat1,lon1, lat1,lon2, az1,az2, &s1 );
287 geo_inverse_wgs_84( alt, lat2,lon2, lat1,lon2, az1,az2, &s2 );
292 // antipodal and polar points don't get here
293 double dlam = lam2 - lam1, dlams = dlam;
294 double sdlams,cdlams, sig,sinsig,cossig, sinaz,
296 double tc,temp, us,rnumer,denom, ta,tb;
297 double cosu1,sinu1, sinu2,cosu2;
300 temp = (1.0-f)*sinphi1/cosphi1;
301 cosu1 = 1.0/sqrt(1.0+temp*temp);
303 temp = (1.0-f)*sinphi2/cosphi2;
304 cosu2 = 1.0/sqrt(1.0+temp*temp);
308 sdlams = sin(dlams), cdlams = cos(dlams);
309 sinsig = sqrt(cosu2*cosu2*sdlams*sdlams+
310 (cosu1*sinu2-sinu1*cosu2*cdlams)*
311 (cosu1*sinu2-sinu1*cosu2*cdlams));
312 cossig = sinu1*sinu2+cosu1*cosu2*cdlams;
314 sig = atan2(sinsig,cossig);
315 sinaz = cosu1*cosu2*sdlams/sinsig;
316 cos2saz = 1.0-sinaz*sinaz;
317 c2sigm = (sinu1 == 0.0 || sinu2 == 0.0 ? cossig :
318 cossig-2.0*sinu1*sinu2/cos2saz);
319 tc = f*cos2saz*(4.0+f*(4.0-3.0*cos2saz))/16.0;
321 dlams = dlam+(1.0-tc)*f*sinaz*
323 (c2sigm+tc*cossig*(-1.0+2.0*c2sigm*c2sigm)));
324 if (fabs(dlams) > GEOD_INV_PI && iter++ > 50) {
327 } while ( fabs(temp-dlams) > testv);
329 us = cos2saz*(a*a-b*b)/(b*b); // !!
330 // BACK AZIMUTH FROM NORTH
331 rnumer = -(cosu1*sdlams);
332 denom = sinu1*cosu2-cosu1*sinu2*cdlams;
333 *az2 = atan2(rnumer,denom)/RADDEG;
334 if( fabs(*az2) < testv ) *az2 = 0.0;
335 if(*az2 < 0.0) *az2 += 360.0;
337 // FORWARD AZIMUTH FROM NORTH
338 rnumer = cosu2*sdlams;
339 denom = cosu1*sinu2-sinu1*cosu2*cdlams;
340 *az1 = atan2(rnumer,denom)/RADDEG;
341 if( fabs(*az1) < testv ) *az1 = 0.0;
342 if(*az1 < 0.0) *az1 += 360.0;
345 ta = 1.0+us*(4096.0+us*(-768.0+us*(320.0-175.0*us)))/
347 tb = us*(256.0+us*(-128.0+us*(74.0-47.0*us)))/1024.0;
350 *s = b*ta*(sig-tb*sinsig*
351 (c2sigm+tb*(cossig*(-1.0+2.0*c2sigm*c2sigm)-tb*
352 c2sigm*(-3.0+4.0*sinsig*sinsig)*
353 (-3.0+4.0*c2sigm*c2sigm)/6.0)/
360 /***************************************************************************
364 ----------------------------------------------------------------------------
366 FUNCTION: Converts geocentric coordinates to geodetic positions
368 ----------------------------------------------------------------------------
370 MODULE STATUS: developmental
372 ----------------------------------------------------------------------------
374 GENEALOGY: Written as part of LaRCSim project by E. B. Jackson
376 ----------------------------------------------------------------------------
378 DESIGNED BY: E. B. Jackson
380 CODED BY: E. B. Jackson
382 MAINTAINED BY: E. B. Jackson
384 ----------------------------------------------------------------------------
386 MODIFICATION HISTORY:
390 930208 Modified to avoid singularity near polar region. EBJ
391 930602 Moved backwards calcs here from ls_step. EBJ
392 931214 Changed erroneous Latitude and Altitude variables to
393 *lat_geod and *alt in routine ls_geoc_to_geod. EBJ
394 940111 Changed header files from old ls_eom.h style to ls_types,
395 and ls_constants. Also replaced old DATA type with new
401 * Revision 1.5 1994/01/11 18:47:05 bjax
402 * Changed include files to use types and constants, not ls_eom.h
403 * Also changed DATA type to SCALAR type.
405 * Revision 1.4 1993/12/14 21:06:47 bjax
406 * Removed global variable references Altitude and Latitude. EBJ
408 * Revision 1.3 1993/06/02 15:03:40 bjax
409 * Made new subroutine for calculating geodetic to geocentric; changed name
410 * of forward conversion routine from ls_geodesy to ls_geoc_to_geod.
413 ----------------------------------------------------------------------------
417 [ 1] Stevens, Brian L.; and Lewis, Frank L.: "Aircraft
418 Control and Simulation", Wiley and Sons, 1992.
422 ----------------------------------------------------------------------------
426 ----------------------------------------------------------------------------
430 ----------------------------------------------------------------------------
433 lat_geoc Geocentric latitude, radians, + = North
434 radius C.G. radius to earth center, ft
436 ----------------------------------------------------------------------------
439 lat_geod Geodetic latitude, radians, + = North
440 alt C.G. altitude above mean sea level, ft
441 sea_level_r radius from earth center to sea level at
442 local vertical (surface normal) of C.G.
444 --------------------------------------------------------------------------*/