~ubuntu-branches/ubuntu/raring/geotranz/raring

« back to all changes in this revision

Viewing changes to dt_cc/geocent/geocent.c

  • Committer: Bazaar Package Importer
  • Author(s): Roberto Lumbreras
  • Date: 2008-10-17 14:43:09 UTC
  • Revision ID: james.westby@ubuntu.com-20081017144309-jb7uzfi1y1lvez8j
Tags: upstream-2.4.2
ImportĀ upstreamĀ versionĀ 2.4.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************/
 
2
/* RSC IDENTIFIER:  GEOCENTRIC
 
3
 *
 
4
 * ABSTRACT
 
5
 *
 
6
 *    This component provides conversions between Geodetic coordinates (latitude,
 
7
 *    longitude in radians and height in meters) and Geocentric coordinates
 
8
 *    (X, Y, Z) in meters.
 
9
 *
 
10
 * ERROR HANDLING
 
11
 *
 
12
 *    This component checks parameters for valid values.  If an invalid value
 
13
 *    is found, the error code is combined with the current error code using 
 
14
 *    the bitwise or.  This combining allows multiple error codes to be
 
15
 *    returned. The possible error codes are:
 
16
 *
 
17
 *      GEOCENT_NO_ERROR        : No errors occurred in function
 
18
 *      GEOCENT_LAT_ERROR       : Latitude out of valid range
 
19
 *                                 (-90 to 90 degrees)
 
20
 *      GEOCENT_LON_ERROR       : Longitude out of valid range
 
21
 *                                 (-180 to 360 degrees)
 
22
 *      GEOCENT_A_ERROR         : Semi-major axis less than or equal to zero
 
23
 *      GEOCENT_INV_F_ERROR     : Inverse flattening outside of valid range
 
24
 *                                                                               (250 to 350)
 
25
 *
 
26
 *
 
27
 * REUSE NOTES
 
28
 *
 
29
 *    GEOCENTRIC is intended for reuse by any application that performs
 
30
 *    coordinate conversions between geodetic coordinates and geocentric
 
31
 *    coordinates.
 
32
 *    
 
33
 *
 
34
 * REFERENCES
 
35
 *    
 
36
 *    An Improved Algorithm for Geocentric to Geodetic Coordinate Conversion,
 
37
 *    Ralph Toms, February 1996  UCRL-JC-123138.
 
38
 *    
 
39
 *    Further information on GEOCENTRIC can be found in the Reuse Manual.
 
40
 *
 
41
 *    GEOCENTRIC originated from : U.S. Army Topographic Engineering Center
 
42
 *                                 Geospatial Information Division
 
43
 *                                 7701 Telegraph Road
 
44
 *                                 Alexandria, VA  22310-3864
 
45
 *
 
46
 * LICENSES
 
47
 *
 
48
 *    None apply to this component.
 
49
 *
 
50
 * RESTRICTIONS
 
51
 *
 
52
 *    GEOCENTRIC has no restrictions.
 
53
 *
 
54
 * ENVIRONMENT
 
55
 *
 
56
 *    GEOCENTRIC was tested and certified in the following environments:
 
57
 *
 
58
 *    1. Solaris 2.5 with GCC version 2.8.1
 
59
 *    2. Windows 95 with MS Visual C++ version 6
 
60
 *
 
61
 * MODIFICATIONS
 
62
 *
 
63
 *    Date              Description
 
64
 *    ----              -----------
 
65
 *    25-02-97          Original Code
 
66
 *
 
67
 */
 
68
 
 
69
 
 
70
/***************************************************************************/
 
71
/*
 
72
 *                               INCLUDES
 
73
 */
 
74
#include <math.h>
 
75
#include "geocent.h"
 
76
/*
 
77
 *    math.h     - is needed for calls to sin, cos, tan and sqrt.
 
78
 *    geocent.h  - is needed for Error codes and prototype error checking.
 
79
 */
 
80
 
 
81
 
 
82
/***************************************************************************/
 
83
/*
 
84
 *                               DEFINES
 
85
 */
 
86
#define PI         3.14159265358979323e0
 
87
#define PI_OVER_2  (PI / 2.0e0)
 
88
#define FALSE      0
 
89
#define TRUE       1
 
90
#define COS_67P5   0.38268343236508977  /* cosine of 67.5 degrees */
 
91
#define AD_C       1.0026000            /* Toms region 1 constant */
 
92
 
 
93
 
 
94
/***************************************************************************/
 
95
/*
 
96
 *                              GLOBAL DECLARATIONS
 
97
 */
 
98
/* Ellipsoid parameters, default to WGS 84 */
 
99
double Geocent_a = 6378137.0;          /* Semi-major axis of ellipsoid in meters */
 
100
double Geocent_f = 1 / 298.257223563;  /* Flattening of ellipsoid           */
 
101
 
 
102
double Geocent_e2 = 0.0066943799901413800;   /* Eccentricity squared  */
 
103
double Geocent_ep2 = 0.00673949675658690300; /* 2nd eccentricity squared */
 
104
/*
 
105
 * These state variables are for optimization purposes.  The only function
 
106
 * that should modify them is Set_Geocentric_Parameters.
 
107
 */
 
108
 
 
109
 
 
110
/***************************************************************************/
 
111
/*
 
112
 *                              FUNCTIONS     
 
113
 */
 
114
 
 
115
 
 
116
long Set_Geocentric_Parameters (double a, 
 
117
                                double f) 
 
118
{ /* BEGIN Set_Geocentric_Parameters */
 
119
/*
 
120
 * The function Set_Geocentric_Parameters receives the ellipsoid parameters
 
121
 * as inputs and sets the corresponding state variables.
 
122
 *
 
123
 *    a  : Semi-major axis of ellipsoid, in meters.          (input)
 
124
 *    f  : Flattening of ellipsoid.                                                            (input)
 
125
 */
 
126
 
 
127
  double inv_f = 1 / f;
 
128
  long Error_Code = GEOCENT_NO_ERROR;
 
129
 
 
130
  if (a <= 0.0)
 
131
    Error_Code |= GEOCENT_A_ERROR;
 
132
  if ((inv_f < 250) || (inv_f > 350))
 
133
  { /* Inverse flattening must be between 250 and 350 */
 
134
    Error_Code |= GEOCENT_INV_F_ERROR;
 
135
  }
 
136
  if (!Error_Code)
 
137
  {
 
138
    Geocent_a = a;
 
139
    Geocent_f = f;
 
140
    Geocent_e2 = 2 * Geocent_f - Geocent_f * Geocent_f;
 
141
    Geocent_ep2 = (1 / (1 - Geocent_e2)) - 1;
 
142
  }
 
143
  return (Error_Code);
 
144
} /* END OF Set_Geocentric_Parameters */
 
145
 
 
146
 
 
147
void Get_Geocentric_Parameters (double *a, 
 
148
                                double *f)
 
149
{ /* BEGIN Get_Geocentric_Parameters */
 
150
/*
 
151
 * The function Get_Geocentric_Parameters returns the ellipsoid parameters
 
152
 * to be used in geocentric coordinate conversions.
 
153
 *
 
154
 *    a  : Semi-major axis of ellipsoid, in meters.          (output)
 
155
 *    f  : Flattening of ellipsoid.                                                            (output)
 
156
 */
 
157
 
 
158
  *a = Geocent_a;
 
159
  *f = Geocent_f;
 
160
} /* END OF Get_Geocentric_Parameters */
 
161
 
 
162
 
 
163
long Convert_Geodetic_To_Geocentric (double Latitude,
 
164
                                     double Longitude,
 
165
                                     double Height,
 
166
                                     double *X,
 
167
                                     double *Y,
 
168
                                     double *Z) 
 
169
{ /* BEGIN Convert_Geodetic_To_Geocentric */
 
170
/*
 
171
 * The function Convert_Geodetic_To_Geocentric converts geodetic coordinates
 
172
 * (latitude, longitude, and height) to geocentric coordinates (X, Y, Z),
 
173
 * according to the current ellipsoid parameters.
 
174
 *
 
175
 *    Latitude  : Geodetic latitude in radians                     (input)
 
176
 *    Longitude : Geodetic longitude in radians                    (input)
 
177
 *    Height    : Geodetic height, in meters                       (input)
 
178
 *    X         : Calculated Geocentric X coordinate, in meters    (output)
 
179
 *    Y         : Calculated Geocentric Y coordinate, in meters    (output)
 
180
 *    Z         : Calculated Geocentric Z coordinate, in meters    (output)
 
181
 *
 
182
 */
 
183
  long Error_Code = GEOCENT_NO_ERROR;
 
184
  double Rn;            /*  Earth radius at location  */
 
185
  double Sin_Lat;       /*  sin(Latitude)  */
 
186
  double Sin2_Lat;      /*  Square of sin(Latitude)  */
 
187
  double Cos_Lat;       /*  cos(Latitude)  */
 
188
 
 
189
  if ((Latitude < -PI_OVER_2) || (Latitude > PI_OVER_2))
 
190
  { /* Latitude out of range */
 
191
    Error_Code |= GEOCENT_LAT_ERROR;
 
192
  }
 
193
  if ((Longitude < -PI) || (Longitude > (2*PI)))
 
194
  { /* Longitude out of range */
 
195
    Error_Code |= GEOCENT_LON_ERROR;
 
196
  }
 
197
  if (!Error_Code)
 
198
  { /* no errors */
 
199
    if (Longitude > PI)
 
200
      Longitude -= (2*PI);
 
201
    Sin_Lat = sin(Latitude);
 
202
    Cos_Lat = cos(Latitude);
 
203
    Sin2_Lat = Sin_Lat * Sin_Lat;
 
204
    Rn = Geocent_a / (sqrt(1.0e0 - Geocent_e2 * Sin2_Lat));
 
205
    *X = (Rn + Height) * Cos_Lat * cos(Longitude);
 
206
    *Y = (Rn + Height) * Cos_Lat * sin(Longitude);
 
207
    *Z = ((Rn * (1 - Geocent_e2)) + Height) * Sin_Lat;
 
208
 
 
209
  }
 
210
  return (Error_Code);
 
211
} /* END OF Convert_Geodetic_To_Geocentric */
 
212
 
 
213
 
 
214
void Convert_Geocentric_To_Geodetic (double X,
 
215
                                     double Y, 
 
216
                                     double Z,
 
217
                                     double *Latitude,
 
218
                                     double *Longitude,
 
219
                                     double *Height)
 
220
{ /* BEGIN Convert_Geocentric_To_Geodetic */
 
221
/*
 
222
 * The function Convert_Geocentric_To_Geodetic converts geocentric
 
223
 * coordinates (X, Y, Z) to geodetic coordinates (latitude, longitude, 
 
224
 * and height), according to the current ellipsoid parameters.
 
225
 *
 
226
 *    X         : Geocentric X coordinate, in meters.         (input)
 
227
 *    Y         : Geocentric Y coordinate, in meters.         (input)
 
228
 *    Z         : Geocentric Z coordinate, in meters.         (input)
 
229
 *    Latitude  : Calculated latitude value in radians.       (output)
 
230
 *    Longitude : Calculated longitude value in radians.      (output)
 
231
 *    Height    : Calculated height value, in meters.         (output)
 
232
 *
 
233
 * The method used here is derived from 'An Improved Algorithm for
 
234
 * Geocentric to Geodetic Coordinate Conversion', by Ralph Toms, Feb 1996
 
235
 */
 
236
 
 
237
/* Note: Variable names follow the notation used in Toms, Feb 1996 */
 
238
 
 
239
  double W;        /* distance from Z axis */
 
240
  double W2;       /* square of distance from Z axis */
 
241
  double T0;       /* initial estimate of vertical component */
 
242
  double T1;       /* corrected estimate of vertical component */
 
243
  double S0;       /* initial estimate of horizontal component */
 
244
  double S1;       /* corrected estimate of horizontal component */
 
245
  double Sin_B0;   /* sin(B0), B0 is estimate of Bowring aux variable */
 
246
  double Sin3_B0;  /* cube of sin(B0) */
 
247
  double Cos_B0;   /* cos(B0) */
 
248
  double Sin_p1;   /* sin(phi1), phi1 is estimated latitude */
 
249
  double Cos_p1;   /* cos(phi1) */
 
250
  double Rn;       /* Earth radius at location */
 
251
  double Sum;      /* numerator of cos(phi1) */
 
252
  int At_Pole;     /* indicates location is in polar region */
 
253
  double Geocent_b = Geocent_a * (1 - Geocent_f); /* Semi-minor axis of ellipsoid, in meters */
 
254
 
 
255
  At_Pole = FALSE;
 
256
  if (X != 0.0)
 
257
  {
 
258
    *Longitude = atan2(Y,X);
 
259
  }
 
260
  else
 
261
  {
 
262
    if (Y > 0)
 
263
    {
 
264
      *Longitude = PI_OVER_2;
 
265
    }
 
266
    else if (Y < 0)
 
267
    {
 
268
      *Longitude = -PI_OVER_2;
 
269
    }
 
270
    else
 
271
    {
 
272
      At_Pole = TRUE;
 
273
      *Longitude = 0.0;
 
274
      if (Z > 0.0)
 
275
      {  /* north pole */
 
276
        *Latitude = PI_OVER_2;
 
277
      }
 
278
      else if (Z < 0.0)
 
279
      {  /* south pole */
 
280
        *Latitude = -PI_OVER_2;
 
281
      }
 
282
      else
 
283
      {  /* center of earth */
 
284
        *Latitude = PI_OVER_2;
 
285
        *Height = -Geocent_b;
 
286
        return;
 
287
      } 
 
288
    }
 
289
  }
 
290
  W2 = X*X + Y*Y;
 
291
  W = sqrt(W2);
 
292
  T0 = Z * AD_C;
 
293
  S0 = sqrt(T0 * T0 + W2);
 
294
  Sin_B0 = T0 / S0;
 
295
  Cos_B0 = W / S0;
 
296
  Sin3_B0 = Sin_B0 * Sin_B0 * Sin_B0;
 
297
  T1 = Z + Geocent_b * Geocent_ep2 * Sin3_B0;
 
298
  Sum = W - Geocent_a * Geocent_e2 * Cos_B0 * Cos_B0 * Cos_B0;
 
299
  S1 = sqrt(T1*T1 + Sum * Sum);
 
300
  Sin_p1 = T1 / S1;
 
301
  Cos_p1 = Sum / S1;
 
302
  Rn = Geocent_a / sqrt(1.0 - Geocent_e2 * Sin_p1 * Sin_p1);
 
303
  if (Cos_p1 >= COS_67P5)
 
304
  {
 
305
    *Height = W / Cos_p1 - Rn;
 
306
  }
 
307
  else if (Cos_p1 <= -COS_67P5)
 
308
  {
 
309
    *Height = W / -Cos_p1 - Rn;
 
310
  }
 
311
  else
 
312
  {
 
313
    *Height = Z / Sin_p1 + Rn * (Geocent_e2 - 1.0);
 
314
  }
 
315
  if (At_Pole == FALSE)
 
316
  {
 
317
    *Latitude = atan(Sin_p1 / Cos_p1);
 
318
  }
 
319
} /* END OF Convert_Geocentric_To_Geodetic */