]> git.mxchange.org Git - simgear.git/blob - simgear/math/fg_geodesy.cxx
Converted to the LGPL licencing terms.
[simgear.git] / simgear / math / fg_geodesy.cxx
1 // fg_geodesy.cxx -- routines to convert between geodetic and geocentric 
2 //                   coordinate systems.
3 //
4 // Copied and adapted directly from LaRCsim/ls_geodesy.c
5 //
6 // See below for the complete original LaRCsim comments.
7 //
8 // $Id$
9
10 #include <simgear/compiler.h>
11
12 #ifdef FG_HAVE_STD_INCLUDES
13 # include <cmath>
14 # include <cerrno>
15 #else
16 # include <math.h>
17 # include <errno.h>
18 #endif
19
20 #include <simgear/constants.h>
21 #include <simgear/debug/logstream.hxx>
22
23 #include "point3d.hxx"
24 #include "fg_geodesy.hxx"
25
26 #ifndef FG_HAVE_NATIVE_SGI_COMPILERS
27 FG_USING_STD(cout);
28 #endif
29
30 // ONE_SECOND is pi/180/60/60, or about 100 feet at earths' equator
31 #define ONE_SECOND 4.848136811E-6
32
33
34 // fgGeocToGeod(lat_geoc, radius, *lat_geod, *alt, *sea_level_r)
35 //     INPUTS:  
36 //         lat_geoc     Geocentric latitude, radians, + = North
37 //         radius       C.G. radius to earth center (meters)
38 //
39 //     OUTPUTS:
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)
44
45
46 void fgGeocToGeod( double lat_geoc, double radius, double
47                    *lat_geod, double *alt, double *sea_level_r )
48 {
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;
51
52     if( ( (FG_PI_2 - lat_geoc) < ONE_SECOND )        // near North pole
53         || ( (FG_PI_2 + lat_geoc) < ONE_SECOND ) )   // near South pole
54     {
55         *lat_geod = lat_geoc;
56         *sea_level_r = EQUATORIAL_RADIUS_M*E;
57         *alt = radius - *sea_level_r;
58     } else {
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);
73
74         // check for domain error
75         if ( errno == EDOM ) {
76             FG_LOG( FG_GENERAL, FG_ALERT, "Domain ERROR in fgGeocToGeod!!!!" );
77             *alt = 0.0;
78         }
79
80         denom = sqrt(1-EPS*EPS*sin_mu_a*sin_mu_a);
81         rho_alpha = EQUATORIAL_RADIUS_M*(1-EPS)/
82             (denom*denom*denom);
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 );
87         *sea_level_r = 
88             sqrt(RESQ_M / (1 + ((1/(E*E))-1)*sin_lambda_sl*sin_lambda_sl));
89
90         // check for domain error
91         if ( errno == EDOM ) {
92             FG_LOG( FG_GENERAL, FG_ALERT, "Domain ERROR in fgGeocToGeod!!!!" );
93             *sea_level_r = 0.0;
94         }
95     }
96
97 }
98
99
100 // fgGeodToGeoc( lat_geod, alt, *sl_radius, *lat_geoc )
101 //     INPUTS:  
102 //         lat_geod     Geodetic latitude, radians, + = North
103 //         alt          C.G. altitude above mean sea level (meters)
104 //
105 //     OUTPUTS:
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
109 //
110
111
112 void fgGeodToGeoc( double lat_geod, double alt, double *sl_radius,
113                       double *lat_geoc )
114 {
115     double lambda_sl, sin_lambda_sl, cos_lambda_sl, sin_mu, cos_mu, px, py;
116     
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);
122     *sl_radius = 
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 );
127 }
128
129
130 // Direct and inverse distance functions 
131 //
132 // Proceedings of the 7th International Symposium on Geodetic
133 // Computations, 1985
134 //
135 // "The Nested Coefficient Method for Accurate Solutions of Direct and
136 // Inverse Geodetic Problems With Any Length"
137 //
138 // Zhang Xue-Lian
139 // pp 747-763
140 //
141 // modified for FlightGear to use WGS84 only -- Norman Vine
142
143 #define GEOD_INV_PI FG_PI
144
145 // s == distance
146 // az = azimuth
147
148 // for WGS_84 a = 6378137.000, rf = 298.257223563;
149
150 static double M0( double e2 ) {
151     //double e4 = e2*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;
154 }
155
156
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 )
161 {
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);
171         
172         
173     if( fabs(s) < 0.01 ) {      // distance < centimeter => congruency
174         *lat2 = lat1;
175         *lon2 = lon1;
176         *az2 = 180.0 + az1;
177         if( *az2 > 360.0 ) *az2 -= 360.0;
178         return 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);
186
187         // Terms
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,
190             tc = 0;
191
192         // FIRST ESTIMATE OF SIGMA (SIG)
193         double first = s/(b*ta);  // !!
194         double sig = first;
195         double c2sigm, sinsig,cossig, temp,denom,rnumer, dlams, dlam;
196         do {
197             c2sigm = cos(2.0*sig1+sig);
198             sinsig = sin(sig); cossig = cos(sig);
199             temp = sig;
200             sig = first + 
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)
204                            /4.0);
205         } while( fabs(sig-temp) > testv);
206
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);
211
212         // NUMERATOR
213         rnumer = sinu1*cossig+cosu1*sinsig*cosaz1;
214         *lat2 = atan2(rnumer,denom)/RADDEG;
215
216         // DIFFERENCE IN LONGITUDE ON AUXILARY SPHERE (DLAMS )
217         rnumer = sinsig*sinaz1;
218         denom = cosu1*cossig-sinu1*sinsig*cosaz1;
219         dlams = atan2(rnumer,denom);
220
221         // TERM C
222         tc = f*cos2saz*(4.0+f*(4.0-3.0*cos2saz))/16.0;
223
224         // DIFFERENCE IN LONGITUDE
225         dlam = dlams-(1.0-tc)*f*sinaz*(sig+tc*sinsig*
226                                        (c2sigm+
227                                         tc*cossig*(-1.0+2.0*
228                                                    c2sigm*c2sigm)));
229         *lon2 = (lam1+dlam)/RADDEG;
230         if (*lon2 > 180.0  ) *lon2 -= 360.0;
231         if (*lon2 < -180.0 ) *lon2 += 360.0;
232
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;
237         return 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 );
242     } 
243 }
244
245
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 )
251 {
252     double a = 6378137.000, rf = 298.257223563;
253     int iter=0;
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);
262         
263     if( (fabs(lat1-lat2) < testv && 
264          ( fabs(lon1-lon2) < testv) || fabs(lat1-90.0) < testv ) )
265     {   
266         // TWO STATIONS ARE IDENTICAL : SET DISTANCE & AZIMUTHS TO ZERO */
267         *az1 = 0.0; *az2 = 0.0; *s = 0.0;
268         return 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;
274         return 0;
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, 
278                                     az1,az2,s );
279         k = k; // avoid compiler error since return result is unused
280         *s /= 2.0;
281         *az2 = *az1 + 180.0;
282         if( *az2 > 360.0 ) *az2 -= 360.0; 
283         return 0;
284     } else if( (fabs( fabs(lon1-lon2) - 180 ) < testv) && 
285                (fabs(lat1+lat2) < testv) ) 
286     {
287         // Geodesic passes through the pole (antipodal)
288         double s1,s2;
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 );
291         *az2 = *az1;
292         *s = s1 + s2;
293         return 0;
294     } else {
295         // antipodal and polar points don't get here
296         double dlam = lam2 - lam1, dlams = dlam;
297         double sdlams,cdlams, sig,sinsig,cossig, sinaz,
298             cos2saz, c2sigm;
299         double tc,temp, us,rnumer,denom, ta,tb;
300         double cosu1,sinu1, sinu2,cosu2;
301
302         // Reduced latitudes
303         temp = (1.0-f)*sinphi1/cosphi1;
304         cosu1 = 1.0/sqrt(1.0+temp*temp);
305         sinu1 = temp*cosu1;
306         temp = (1.0-f)*sinphi2/cosphi2;
307         cosu2 = 1.0/sqrt(1.0+temp*temp);
308         sinu2 = temp*cosu2;
309     
310         do {
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;
316             
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;
323             temp = dlams;
324             dlams = dlam+(1.0-tc)*f*sinaz*
325                 (sig+tc*sinsig*
326                  (c2sigm+tc*cossig*(-1.0+2.0*c2sigm*c2sigm)));
327             if (fabs(dlams) > GEOD_INV_PI && iter++ > 50) {
328                 return iter;
329             }
330         } while ( fabs(temp-dlams) > testv);
331
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;
339
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;
346
347         // Terms a & b
348         ta = 1.0+us*(4096.0+us*(-768.0+us*(320.0-175.0*us)))/
349             16384.0;
350         tb = us*(256.0+us*(-128.0+us*(74.0-47.0*us)))/1024.0;
351
352         // GEODETIC DISTANCE
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)/
357                     4.0));
358         return 0;
359     }
360 }
361
362
363 /***************************************************************************
364
365         TITLE:  ls_geodesy
366         
367 ----------------------------------------------------------------------------
368
369         FUNCTION:       Converts geocentric coordinates to geodetic positions
370
371 ----------------------------------------------------------------------------
372
373         MODULE STATUS:  developmental
374
375 ----------------------------------------------------------------------------
376
377         GENEALOGY:      Written as part of LaRCSim project by E. B. Jackson
378
379 ----------------------------------------------------------------------------
380
381         DESIGNED BY:    E. B. Jackson
382         
383         CODED BY:       E. B. Jackson
384         
385         MAINTAINED BY:  E. B. Jackson
386
387 ----------------------------------------------------------------------------
388
389         MODIFICATION HISTORY:
390         
391         DATE    PURPOSE                                         BY
392         
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
399                 SCALAR type.                                            EBJ
400
401         CURRENT RCS HEADER:
402
403 $Header$
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.
407  *
408  * Revision 1.4  1993/12/14  21:06:47  bjax
409  * Removed global variable references Altitude and Latitude.   EBJ
410  *
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.
414  *
415
416 ----------------------------------------------------------------------------
417
418         REFERENCES:
419
420                 [ 1]    Stevens, Brian L.; and Lewis, Frank L.: "Aircraft 
421                         Control and Simulation", Wiley and Sons, 1992.
422                         ISBN 0-471-61397-5                    
423
424
425 ----------------------------------------------------------------------------
426
427         CALLED BY:      ls_aux
428
429 ----------------------------------------------------------------------------
430
431         CALLS TO:
432
433 ----------------------------------------------------------------------------
434
435         INPUTS: 
436                 lat_geoc        Geocentric latitude, radians, + = North
437                 radius          C.G. radius to earth center, ft
438
439 ----------------------------------------------------------------------------
440
441         OUTPUTS:
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.
446
447 --------------------------------------------------------------------------*/
448
449