]> git.mxchange.org Git - simgear.git/blob - simgear/math/fg_geodesy.cxx
Added simgear/magvar which impliments WMM 2000 world magnetic variance model.
[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         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);
70
71         // check for domain error
72         if ( errno == EDOM ) {
73             FG_LOG( FG_GENERAL, FG_ALERT, "Domain ERROR in fgGeocToGeod!!!!" );
74             *alt = 0.0;
75         }
76
77         denom = sqrt(1-EPS*EPS*sin_mu_a*sin_mu_a);
78         rho_alpha = EQUATORIAL_RADIUS_M*(1-EPS)/
79             (denom*denom*denom);
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 );
84         *sea_level_r = 
85             sqrt(RESQ_M / (1 + ((1/(E*E))-1)*sin_lambda_sl*sin_lambda_sl));
86
87         // check for domain error
88         if ( errno == EDOM ) {
89             FG_LOG( FG_GENERAL, FG_ALERT, "Domain ERROR in fgGeocToGeod!!!!" );
90             *sea_level_r = 0.0;
91         }
92     }
93
94 }
95
96
97 // fgGeodToGeoc( lat_geod, alt, *sl_radius, *lat_geoc )
98 //     INPUTS:  
99 //         lat_geod     Geodetic latitude, radians, + = North
100 //         alt          C.G. altitude above mean sea level (meters)
101 //
102 //     OUTPUTS:
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
106 //
107
108
109 void fgGeodToGeoc( double lat_geod, double alt, double *sl_radius,
110                       double *lat_geoc )
111 {
112     double lambda_sl, sin_lambda_sl, cos_lambda_sl, sin_mu, cos_mu, px, py;
113     
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);
119     *sl_radius = 
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 );
124 }
125
126
127 // Direct and inverse distance functions 
128 //
129 // Proceedings of the 7th International Symposium on Geodetic
130 // Computations, 1985
131 //
132 // "The Nested Coefficient Method for Accurate Solutions of Direct and
133 // Inverse Geodetic Problems With Any Length"
134 //
135 // Zhang Xue-Lian
136 // pp 747-763
137 //
138 // modified for FlightGear to use WGS84 only -- Norman Vine
139
140 #define GEOD_INV_PI FG_PI
141
142 // s == distance
143 // az = azimuth
144
145 // for WGS_84 a = 6378137.000, rf = 298.257223563;
146
147 static double M0( double e2 ) {
148     //double e4 = e2*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;
151 }
152
153
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 )
158 {
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);
168         
169         
170     if( fabs(s) < 0.01 ) {      // distance < centimeter => congruency
171         *lat2 = lat1;
172         *lon2 = lon1;
173         *az2 = 180.0 + az1;
174         if( *az2 > 360.0 ) *az2 -= 360.0;
175         return 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);
183
184         // Terms
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,
187             tc = 0;
188
189         // FIRST ESTIMATE OF SIGMA (SIG)
190         double first = s/(b*ta);  // !!
191         double sig = first;
192         double c2sigm, sinsig,cossig, temp,denom,rnumer, dlams, dlam;
193         do {
194             c2sigm = cos(2.0*sig1+sig);
195             sinsig = sin(sig); cossig = cos(sig);
196             temp = sig;
197             sig = first + 
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)
201                            /4.0);
202         } while( fabs(sig-temp) > testv);
203
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);
208
209         // NUMERATOR
210         rnumer = sinu1*cossig+cosu1*sinsig*cosaz1;
211         *lat2 = atan2(rnumer,denom)/RADDEG;
212
213         // DIFFERENCE IN LONGITUDE ON AUXILARY SPHERE (DLAMS )
214         rnumer = sinsig*sinaz1;
215         denom = cosu1*cossig-sinu1*sinsig*cosaz1;
216         dlams = atan2(rnumer,denom);
217
218         // TERM C
219         tc = f*cos2saz*(4.0+f*(4.0-3.0*cos2saz))/16.0;
220
221         // DIFFERENCE IN LONGITUDE
222         dlam = dlams-(1.0-tc)*f*sinaz*(sig+tc*sinsig*
223                                        (c2sigm+
224                                         tc*cossig*(-1.0+2.0*
225                                                    c2sigm*c2sigm)));
226         *lon2 = (lam1+dlam)/RADDEG;
227         if (*lon2 > 180.0  ) *lon2 -= 360.0;
228         if (*lon2 < -180.0 ) *lon2 += 360.0;
229
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;
234         return 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 );
239     } 
240 }
241
242
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 )
248 {
249     double a = 6378137.000, rf = 298.257223563;
250     int iter=0;
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);
259         
260     if( (fabs(lat1-lat2) < testv && 
261          ( fabs(lon1-lon2) < testv) || fabs(lat1-90.0) < testv ) )
262     {   
263         // TWO STATIONS ARE IDENTICAL : SET DISTANCE & AZIMUTHS TO ZERO */
264         *az1 = 0.0; *az2 = 0.0; *s = 0.0;
265         return 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;
271         return 0;
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, 
275                                     az1,az2,s );
276         k = k; // avoid compiler error since return result is unused
277         *s /= 2.0;
278         *az2 = *az1 + 180.0;
279         if( *az2 > 360.0 ) *az2 -= 360.0; 
280         return 0;
281     } else if( (fabs( fabs(lon1-lon2) - 180 ) < testv) && 
282                (fabs(lat1+lat2) < testv) ) 
283     {
284         // Geodesic passes through the pole (antipodal)
285         double s1,s2;
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 );
288         *az2 = *az1;
289         *s = s1 + s2;
290         return 0;
291     } else {
292         // antipodal and polar points don't get here
293         double dlam = lam2 - lam1, dlams = dlam;
294         double sdlams,cdlams, sig,sinsig,cossig, sinaz,
295             cos2saz, c2sigm;
296         double tc,temp, us,rnumer,denom, ta,tb;
297         double cosu1,sinu1, sinu2,cosu2;
298
299         // Reduced latitudes
300         temp = (1.0-f)*sinphi1/cosphi1;
301         cosu1 = 1.0/sqrt(1.0+temp*temp);
302         sinu1 = temp*cosu1;
303         temp = (1.0-f)*sinphi2/cosphi2;
304         cosu2 = 1.0/sqrt(1.0+temp*temp);
305         sinu2 = temp*cosu2;
306     
307         do {
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;
313             
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;
320             temp = dlams;
321             dlams = dlam+(1.0-tc)*f*sinaz*
322                 (sig+tc*sinsig*
323                  (c2sigm+tc*cossig*(-1.0+2.0*c2sigm*c2sigm)));
324             if (fabs(dlams) > GEOD_INV_PI && iter++ > 50) {
325                 return iter;
326             }
327         } while ( fabs(temp-dlams) > testv);
328
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;
336
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;
343
344         // Terms a & b
345         ta = 1.0+us*(4096.0+us*(-768.0+us*(320.0-175.0*us)))/
346             16384.0;
347         tb = us*(256.0+us*(-128.0+us*(74.0-47.0*us)))/1024.0;
348
349         // GEODETIC DISTANCE
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)/
354                     4.0));
355         return 0;
356     }
357 }
358
359
360 /***************************************************************************
361
362         TITLE:  ls_geodesy
363         
364 ----------------------------------------------------------------------------
365
366         FUNCTION:       Converts geocentric coordinates to geodetic positions
367
368 ----------------------------------------------------------------------------
369
370         MODULE STATUS:  developmental
371
372 ----------------------------------------------------------------------------
373
374         GENEALOGY:      Written as part of LaRCSim project by E. B. Jackson
375
376 ----------------------------------------------------------------------------
377
378         DESIGNED BY:    E. B. Jackson
379         
380         CODED BY:       E. B. Jackson
381         
382         MAINTAINED BY:  E. B. Jackson
383
384 ----------------------------------------------------------------------------
385
386         MODIFICATION HISTORY:
387         
388         DATE    PURPOSE                                         BY
389         
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
396                 SCALAR type.                                            EBJ
397
398         CURRENT RCS HEADER:
399
400 $Header$
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.
404  *
405  * Revision 1.4  1993/12/14  21:06:47  bjax
406  * Removed global variable references Altitude and Latitude.   EBJ
407  *
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.
411  *
412
413 ----------------------------------------------------------------------------
414
415         REFERENCES:
416
417                 [ 1]    Stevens, Brian L.; and Lewis, Frank L.: "Aircraft 
418                         Control and Simulation", Wiley and Sons, 1992.
419                         ISBN 0-471-61397-5                    
420
421
422 ----------------------------------------------------------------------------
423
424         CALLED BY:      ls_aux
425
426 ----------------------------------------------------------------------------
427
428         CALLS TO:
429
430 ----------------------------------------------------------------------------
431
432         INPUTS: 
433                 lat_geoc        Geocentric latitude, radians, + = North
434                 radius          C.G. radius to earth center, ft
435
436 ----------------------------------------------------------------------------
437
438         OUTPUTS:
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.
443
444 --------------------------------------------------------------------------*/
445
446