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

« back to all changes in this revision

Viewing changes to dt_cc/ellipse/ellipse.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:  Ellipsoid
 
3
 *
 
4
 * ABSTRACT
 
5
 *
 
6
 *    The purpose of ELLIPSOID is to provide access to ellipsoid parameters
 
7
 *    for a collection of common ellipsoids.  A particular ellipsoid can be
 
8
 *    accessed by using its standard 2-letter code to find its index in the
 
9
 *    ellipsoid table.  The index can then be used to retrieve the ellipsoid
 
10
 *    name and parameters.
 
11
 *
 
12
 *    By sequentially retrieving all of the ellipsoid codes and/or names, a
 
13
 *    menu of the available ellipsoids can be constructed.  The index values
 
14
 *    resulting from selections from this menu can then be used to access the
 
15
 *    parameters of the selected ellipsoid.
 
16
 *
 
17
 *    This component depends on a data file named "ellips.dat", which contains
 
18
 *    the ellipsoid parameter values.  A copy of this file must be located in
 
19
 *    the directory specified by the environment variable "GEOTRANS_DATA", if
 
20
 *    defined, or else in the current directory, whenever a program containing
 
21
 *    this component is executed.
 
22
 *
 
23
 *    Additional ellipsoids can be added to this file, either manually or using
 
24
 *    the Create_Ellipsoid function.  However, if a large number of ellipsoids
 
25
 *    are added, the ellipsoid table array size in this component will have to
 
26
 *    be increased.
 
27
 *
 
28
 * ERROR HANDLING
 
29
 *
 
30
 *    This component checks parameters for valid values.  If an invalid value
 
31
 *    is found, the error code is combined with the current error code using
 
32
 *    the bitwise or.  This combining allows multiple error codes to be
 
33
 *    returned. The possible error codes are:
 
34
 *
 
35
 *  ELLIPSE_NO_ERROR             : No errors occured in function
 
36
 *  ELLIPSE_FILE_OPEN_ERROR      : Ellipsoid file opening error
 
37
 *  ELLIPSE_INITIALIZE_ERROR     : Ellipsoid table can not initialize
 
38
 *  ELLIPSE_TABLE_OVERFLOW_ERROR : Ellipsoid table overflow
 
39
 *  ELLIPSE_NOT_INITIALIZED_ERROR: Ellipsoid table not initialized properly
 
40
 *  ELLIPSE_INVALID_INDEX_ERROR  : Index is an invalid value
 
41
 *  ELLIPSE_INVALID_CODE_ERROR   : Code was not found in table
 
42
 *  ELLIPSE_A_ERROR              : Semi-major axis less than or equal to zero
 
43
 *  ELLIPSE_INV_F_ERROR          : Inverse flattening outside of valid range
 
44
 *                                  (250 to 350)
 
45
 *  ELLIPSE_IN_USE_ERROR         : User defined ellipsoid is in use by a user
 
46
 *                                  defined datum
 
47
 *  ELLIPSE_NOT_USERDEF_ERROR    : Ellipsoid is not user defined - cannot be
 
48
 *                                  deleted
 
49
 *
 
50
 * REUSE NOTES
 
51
 *
 
52
 *    Ellipsoid is intended for reuse by any application that requires Earth
 
53
 *    approximating ellipsoids.
 
54
 *
 
55
 * REFERENCES
 
56
 *
 
57
 *    Further information on Ellipsoid can be found in the Reuse Manual.
 
58
 *
 
59
 *    Ellipsoid originated from :  U.S. Army Topographic Engineering Center (USATEC)
 
60
 *                                 Geospatial Information Division (GID)
 
61
 *                                 7701 Telegraph Road
 
62
 *                                 Alexandria, VA  22310-3864
 
63
 *
 
64
 * LICENSES
 
65
 *
 
66
 *    None apply to this component.
 
67
 *
 
68
 * RESTRICTIONS
 
69
 *
 
70
 *    Ellipsoid has no restrictions.
 
71
 *
 
72
 * ENVIRONMENT
 
73
 *
 
74
 *    Ellipsoid was tested and certified in the following environments
 
75
 *
 
76
 *    1. Solaris 2.5
 
77
 *    2. Windows 95
 
78
 *
 
79
 * MODIFICATIONS
 
80
 *
 
81
 *    Date              Description
 
82
 *    ----              -----------
 
83
 *    11-19-95          Original Code
 
84
 *    17-Jan-97         Moved local constants out of public interface
 
85
 *                      Improved efficiency in algorithms (GEOTRANS)
 
86
 *    24-May-99         Added user-defined ellipsoids (GEOTRANS for JMTK)
 
87
 *    06/27/06          Moved data file to data directory
 
88
 *
 
89
 */
 
90
 
 
91
 
 
92
/***************************************************************************/
 
93
/*
 
94
 *                               INCLUDES
 
95
 */
 
96
#include <stdlib.h>
 
97
#include <stdio.h>
 
98
#include <string.h>
 
99
#include "datum.h"
 
100
#include "ellipse.h"
 
101
#include "threads.h"
 
102
 
 
103
/*
 
104
 *    ctype.h    - standard C character handling library
 
105
 *    stdio.h    - standard C input/output library
 
106
 *    stdlib.h   - standard C general utilities library
 
107
 *    string.h   - standard C string handling library
 
108
 *    datum.h    - used to determine if user defined ellipsoid
 
109
 *                  is in use by a user defined datum
 
110
 *    ellipse.h  - prototype error checking and error codes
 
111
 */
 
112
 
 
113
 
 
114
/***************************************************************************/
 
115
/*
 
116
 *                             GLOBAL DATA STRUCTURES
 
117
 */
 
118
 
 
119
#define MAX_ELLIPSOIDS        32  /* Maximum number of ellipsoids in table */
 
120
#define ELLIPSOID_CODE_LENGTH  3  /* Length of ellipsoid code (including null) */
 
121
#define ELLIPSOID_NAME_LENGTH 30  /* Max length of ellipsoid name */
 
122
#define ELLIPSOID_BUF         90
 
123
#define FILENAME_LENGTH      128
 
124
#define FALSE 0
 
125
#define TRUE  1
 
126
 
 
127
const char *WGS84_Ellipsoid_Code = "WE";
 
128
const char *WGS72_Ellipsoid_Code = "WD";
 
129
 
 
130
typedef struct Ellipsoid_Table_Row
 
131
{
 
132
  char Name[ELLIPSOID_NAME_LENGTH];
 
133
  char Code[ELLIPSOID_CODE_LENGTH];
 
134
  double A;
 
135
  double B;
 
136
  double Recp_F;
 
137
  long User_Defined;  /* Identifies a user defined ellipsoid */
 
138
} Ellipsoid_Row;
 
139
 
 
140
static Ellipsoid_Row Ellipsoid_Table[MAX_ELLIPSOIDS];
 
141
static long WGS84_Index = 0;           /* Index of WGS84 in ellipsoid table */
 
142
static long WGS72_Index = 0;           /* Index of WGS72 in ellipsoid table */
 
143
static long Number_of_Ellipsoids = 0;  /* Number of ellipsoids in table */
 
144
static long Ellipsoid_Initialized = 0; /* Indicates successful initialization */
 
145
Thread_Mutex ellipsoid_mutex;
 
146
 
 
147
/***************************************************************************/
 
148
/*                              FUNCTIONS                                  */
 
149
 
 
150
 
 
151
void Assign_Ellipsoid_Row (Ellipsoid_Row *destination,
 
152
                           const Ellipsoid_Row *source)
 
153
{ /* Begin Assign_Ellipsoid_Row */
 
154
/*
 
155
 *   destination  : The destination of the copy         (output)
 
156
 *   source       : The source for the copy             (input)
 
157
 *
 
158
 * The function Assign_Ellipsoid_Row copies ellipsoid data.
 
159
 */
 
160
 
 
161
  strcpy(destination->Name, source->Name);
 
162
  strcpy(destination->Code, source->Code);
 
163
  destination->A = source->A;
 
164
  destination->B = source->B;
 
165
  destination->Recp_F = source->Recp_F;
 
166
  destination->User_Defined = source->User_Defined;
 
167
} /* End Assign_Ellipsoid_Row */
 
168
 
 
169
 
 
170
long Initialize_Ellipsoids ()
 
171
{ /* Begin Initialize_Ellipsoids */
 
172
/*
 
173
 * The function Initialize_Ellipsoids reads ellipsoid data from ellips.dat in
 
174
 * the current directory and builds the ellipsoid table from it.  If an
 
175
 * error occurs, the error code is returned, otherwise ELLIPSE_NO_ERROR is
 
176
 * returned.
 
177
 */
 
178
 
 
179
  char* PathName = NULL;
 
180
  char* FileName = 0;
 
181
  FILE* fp = NULL;                    /* File pointer to file ellips.dat     */
 
182
  char buffer[ELLIPSOID_BUF];
 
183
  long index = 0;                     /* Array index                         */
 
184
  long error_code = ELLIPSE_NO_ERROR;
 
185
  long mutex_error = THREADS_NO_ERROR;
 
186
 
 
187
  if (Ellipsoid_Initialized)
 
188
  {
 
189
    return error_code;
 
190
  }
 
191
 
 
192
#ifdef WIN32
 
193
  mutex_error = Threads_Create_Mutex("Ellipsoid_Mutex", &ellipsoid_mutex);
 
194
#else
 
195
  mutex_error = Threads_Create_Mutex(&ellipsoid_mutex);
 
196
#endif
 
197
 
 
198
  if( !mutex_error )
 
199
    mutex_error = Threads_Lock_Mutex(ellipsoid_mutex);
 
200
 
 
201
  /*  Check the environment for a user provided path, else current directory;   */
 
202
  /*  Build a File Name, including specified or default path:                   */
 
203
 
 
204
  PathName = getenv( "GEOTRANS_DATA" );
 
205
  if (PathName != NULL)
 
206
  {
 
207
    FileName = calloc( strlen( PathName ) + 12, sizeof( char ) );
 
208
    strcpy( FileName, PathName );
 
209
    strcat( FileName, "/" );
 
210
  }
 
211
  else
 
212
  {
 
213
    FileName = calloc( 19, sizeof( char ) );
 
214
    strcpy( FileName, "../data/" );
 
215
  }
 
216
  strcat( FileName, "ellips.dat" );
 
217
 
 
218
  /*  Open the File READONLY, or Return Error Condition:                        */
 
219
 
 
220
  if (( fp = fopen( FileName, "r" ) ) == NULL)
 
221
  {
 
222
    free( FileName );
 
223
    FileName = 0;
 
224
 
 
225
    return ELLIPSE_FILE_OPEN_ERROR;
 
226
  }
 
227
 
 
228
  /* read file */
 
229
  while ((!feof(fp)) && (!error_code))
 
230
  {
 
231
    if (index <= MAX_ELLIPSOIDS)
 
232
    {
 
233
      if (fgets(buffer, ELLIPSOID_BUF, fp))
 
234
      {
 
235
        sscanf(buffer, "%30c %s %lf %lf %lf",
 
236
               Ellipsoid_Table[index].Name,
 
237
               Ellipsoid_Table[index].Code,
 
238
               &(Ellipsoid_Table[index].A),
 
239
               &(Ellipsoid_Table[index].B),
 
240
               &(Ellipsoid_Table[index].Recp_F));
 
241
        if (Ellipsoid_Table[index].Name[0] == '*')
 
242
        {
 
243
          int i;
 
244
          Ellipsoid_Table[index].User_Defined = TRUE;
 
245
          for (i = 0; i < ELLIPSOID_NAME_LENGTH; i++)
 
246
            Ellipsoid_Table[index].Name[i] = Ellipsoid_Table[index].Name[i+1];
 
247
        }
 
248
        else
 
249
          Ellipsoid_Table[index].User_Defined = FALSE;
 
250
        Ellipsoid_Table[index].Name[ELLIPSOID_NAME_LENGTH - 1] = '\0'; /* null terminate */
 
251
        index++;
 
252
      }
 
253
    }
 
254
    else
 
255
      error_code |= ELLIPSE_TABLE_OVERFLOW_ERROR;
 
256
  }
 
257
  
 
258
  fclose(fp);
 
259
  Number_of_Ellipsoids = index;
 
260
 
 
261
  if (error_code)
 
262
  {
 
263
    Ellipsoid_Initialized = 0;
 
264
    Number_of_Ellipsoids = 0;
 
265
  }
 
266
  else
 
267
    Ellipsoid_Initialized = 1;
 
268
 
 
269
  /* Store WGS84 Index*/
 
270
  if (Ellipsoid_Index(WGS84_Ellipsoid_Code, &WGS84_Index))
 
271
    error_code |= ELLIPSE_INITIALIZE_ERROR;
 
272
 
 
273
  /* Store WGS72 Index*/
 
274
  if (Ellipsoid_Index(WGS72_Ellipsoid_Code, &WGS72_Index))
 
275
    error_code |= ELLIPSE_INITIALIZE_ERROR;
 
276
 
 
277
  if( !mutex_error )
 
278
    mutex_error = Threads_Unlock_Mutex(ellipsoid_mutex);
 
279
 
 
280
  if( !mutex_error )
 
281
    Threads_Destroy_Mutex(ellipsoid_mutex);
 
282
 
 
283
  free( FileName );
 
284
  FileName = 0;
 
285
 
 
286
  return (error_code);
 
287
} /* End of Initialize_Ellipsoids */
 
288
 
 
289
 
 
290
long Create_Ellipsoid (const char* Code,
 
291
                       const char* Name,
 
292
                       double a,
 
293
                       double f)
 
294
{ /* Begin Create_Ellipsoid */
 
295
/*
 
296
 *   Code     : 2-letter ellipsoid code.                      (input)
 
297
 *   Name     : Name of the new ellipsoid                     (input)
 
298
 *   a        : Semi-major axis, in meters, of new ellipsoid  (input)
 
299
 *   f        : Flattening of new ellipsoid.                  (input)
 
300
 *
 
301
 * The function Create_Ellipsoid creates a new ellipsoid with the specified
 
302
 * Code, name, and axes.  If the ellipsoid table has not been initialized,
 
303
 * the specified code is already in use, or a new version of the ellips.dat
 
304
 * file cannot be created, an error code is returned, otherwise ELLIPSE_NO_ERROR
 
305
 * is returned.  Note that the indexes of all ellipsoids in the ellipsoid
 
306
 * table may be changed by this function.
 
307
 */
 
308
 
 
309
  long error_code = ELLIPSE_NO_ERROR;
 
310
  long index = 0;
 
311
  long code_length = 0;
 
312
  char *PathName = NULL;
 
313
  char FileName[FILENAME_LENGTH];
 
314
  char ellipsoid_code[ELLIPSOID_CODE_LENGTH];
 
315
  FILE *fp = NULL;                    /* File pointer to file ellips.dat     */
 
316
  double inv_f = 1 / f;
 
317
 
 
318
  if (!Ellipsoid_Initialized)
 
319
    error_code |= ELLIPSE_NOT_INITIALIZED_ERROR;
 
320
  else if (!(Number_of_Ellipsoids < MAX_ELLIPSOIDS))
 
321
    error_code |= ELLIPSE_TABLE_OVERFLOW_ERROR;
 
322
  else
 
323
  {
 
324
    code_length = strlen(Code);
 
325
    if ((!Ellipsoid_Index(Code, &index)) || (code_length > (ELLIPSOID_CODE_LENGTH-1)))
 
326
      error_code |= ELLIPSE_INVALID_CODE_ERROR;
 
327
    if (a <= 0.0)
 
328
      error_code |= ELLIPSE_A_ERROR;
 
329
    if ((inv_f < 250) || (inv_f > 350))
 
330
    { /* Inverse flattening must be between 250 and 350 */
 
331
      error_code |= ELLIPSE_INV_F_ERROR;
 
332
    }
 
333
    if (!error_code)
 
334
    {
 
335
      long i;
 
336
      strcpy(ellipsoid_code,Code);
 
337
      /* Convert code to upper case */
 
338
      for (i = 0; i < code_length; i++)
 
339
        ellipsoid_code[i] = (char)toupper(ellipsoid_code[i]);
 
340
      index = Number_of_Ellipsoids;
 
341
      strcpy(Ellipsoid_Table[index].Name, Name);
 
342
      strcpy(Ellipsoid_Table[index].Code, ellipsoid_code);
 
343
      Ellipsoid_Table[index].A = a;
 
344
      Ellipsoid_Table[index].B = a * (1 - f);
 
345
      Ellipsoid_Table[index].Recp_F = inv_f;
 
346
      Ellipsoid_Table[index].User_Defined = TRUE;
 
347
      Number_of_Ellipsoids++;
 
348
      /*output updated ellipsoid table*/
 
349
      PathName = getenv( "GEOTRANS_DATA" );
 
350
      if (PathName != NULL)
 
351
      {
 
352
        strcpy( FileName, PathName );
 
353
        strcat( FileName, "/" );
 
354
      }
 
355
      else
 
356
      {
 
357
        strcpy( FileName, "../data/" );
 
358
      }
 
359
      strcat( FileName, "ellips.dat" );
 
360
 
 
361
      if ((fp = fopen(FileName, "w")) == NULL)
 
362
      { /* fatal error */
 
363
        return ELLIPSE_FILE_OPEN_ERROR;
 
364
      }
 
365
      /* write file */
 
366
      index = 0;
 
367
      while (index < Number_of_Ellipsoids)
 
368
      {
 
369
        if (Ellipsoid_Table[index].User_Defined)
 
370
          fprintf(fp, "*%-29s %-2s %11.3f %12.4f %13.9f \n",
 
371
                  Ellipsoid_Table[index].Name,
 
372
                  Ellipsoid_Table[index].Code,
 
373
                  Ellipsoid_Table[index].A,
 
374
                  Ellipsoid_Table[index].B,
 
375
                  Ellipsoid_Table[index].Recp_F);
 
376
        else
 
377
          fprintf(fp, "%-29s  %-2s %11.3f %12.4f %13.9f \n",
 
378
                  Ellipsoid_Table[index].Name,
 
379
                  Ellipsoid_Table[index].Code,
 
380
                  Ellipsoid_Table[index].A,
 
381
                  Ellipsoid_Table[index].B,
 
382
                  Ellipsoid_Table[index].Recp_F);
 
383
        index++;
 
384
      }
 
385
      fclose(fp);
 
386
      /* Store WGS84 */
 
387
      Ellipsoid_Index(WGS84_Ellipsoid_Code, &WGS84_Index);
 
388
      /* Store WGS72 */
 
389
      Ellipsoid_Index(WGS72_Ellipsoid_Code, &WGS72_Index);
 
390
    }
 
391
  }
 
392
  return (error_code);
 
393
} /* End Create_Ellipsoid */
 
394
 
 
395
 
 
396
long Delete_Ellipsoid (const char* Code)
 
397
{/* Begin Delete_Ellipsoid */
 
398
/*
 
399
 *   Code     : 2-letter ellipsoid code.                      (input)
 
400
 *
 
401
 * The function Delete_Ellipsoid deletes a user defined ellipsoid with
 
402
 * the specified Code.  If the ellipsoid table has not been created,
 
403
 * the specified code is in use by a user defined datum, or a new version
 
404
 * of the ellips.dat file cannot be created, an error code is returned,
 
405
 * otherwise ELLIPSE_NO_ERROR is returned.  Note that the indexes of all
 
406
 * ellipsoids in the ellipsoid table may be changed by this function.
 
407
 */
 
408
 
 
409
  long error_code = ELLIPSE_NO_ERROR;
 
410
  long index = 0;
 
411
  char *PathName = NULL;
 
412
  char FileName[FILENAME_LENGTH];
 
413
  FILE *fp = NULL;                    /* File pointer to file ellips.dat     */
 
414
 
 
415
  if (!Ellipsoid_Initialized)
 
416
    error_code |= ELLIPSE_NOT_INITIALIZED_ERROR;
 
417
  else
 
418
  {
 
419
    if (!Ellipsoid_Index(Code, &index))
 
420
    {
 
421
      if (Ellipsoid_Table[index-1].User_Defined)
 
422
      {
 
423
        if (Datum_Uses_Ellipsoid(Code))
 
424
          error_code |= ELLIPSE_IN_USE_ERROR;
 
425
      }
 
426
      else
 
427
        error_code |= ELLIPSE_NOT_USERDEF_ERROR;
 
428
    }
 
429
    else
 
430
      error_code |= ELLIPSE_NOT_USERDEF_ERROR;
 
431
  }
 
432
  if (!error_code)
 
433
  {
 
434
    long i = 0;
 
435
    for (i = index-1; i < Number_of_Ellipsoids-1; i++)
 
436
      Ellipsoid_Table[i] = Ellipsoid_Table[i+1];
 
437
 
 
438
    if (Number_of_Ellipsoids != MAX_ELLIPSOIDS)
 
439
      Ellipsoid_Table[i] = Ellipsoid_Table[i+1];
 
440
    else
 
441
    {
 
442
      strcpy(Ellipsoid_Table[i].Name, "");
 
443
      strcpy(Ellipsoid_Table[i].Code, "");
 
444
      Ellipsoid_Table[i].A = 0;
 
445
      Ellipsoid_Table[i].B = 0;
 
446
      Ellipsoid_Table[i].Recp_F = 0;
 
447
      Ellipsoid_Table[i].User_Defined = ' ';
 
448
    }
 
449
    Number_of_Ellipsoids--;
 
450
    /*output updated ellipsoid table*/
 
451
    PathName = getenv( "GEOTRANS_DATA" );
 
452
    if (PathName != NULL)
 
453
    {
 
454
      strcpy( FileName, PathName );
 
455
      strcat( FileName, "/" );
 
456
    }
 
457
    else
 
458
    {
 
459
      strcpy( FileName, "../data/" );
 
460
    }
 
461
    strcat( FileName, "ellips.dat" );
 
462
    if ((fp = fopen(FileName, "w")) == NULL)
 
463
    { /* fatal error */
 
464
      return ELLIPSE_FILE_OPEN_ERROR;
 
465
    }
 
466
    /* write file */
 
467
    index = 0;
 
468
    while (index < Number_of_Ellipsoids)
 
469
    {
 
470
      if (Ellipsoid_Table[index].User_Defined)
 
471
        fprintf(fp, "*%-29s %-2s %11.3f %12.4f %13.9f \n",
 
472
                Ellipsoid_Table[index].Name,
 
473
                Ellipsoid_Table[index].Code,
 
474
                Ellipsoid_Table[index].A,
 
475
                Ellipsoid_Table[index].B,
 
476
                Ellipsoid_Table[index].Recp_F);
 
477
      else
 
478
        fprintf(fp, "%-29s  %-2s %11.3f %12.4f %13.9f \n",
 
479
                Ellipsoid_Table[index].Name,
 
480
                Ellipsoid_Table[index].Code,
 
481
                Ellipsoid_Table[index].A,
 
482
                Ellipsoid_Table[index].B,
 
483
                Ellipsoid_Table[index].Recp_F);
 
484
      index++;
 
485
    }
 
486
    fclose(fp);
 
487
    /* Store WGS84 */
 
488
    Ellipsoid_Index(WGS84_Ellipsoid_Code, &WGS84_Index);
 
489
    /* Store WGS72 */
 
490
    Ellipsoid_Index(WGS72_Ellipsoid_Code, &WGS72_Index);
 
491
  }
 
492
  return (error_code);
 
493
}/* End Delete_Ellipsoid */
 
494
 
 
495
 
 
496
long Ellipsoid_Count ( long *Count )
 
497
{ /* Begin Ellipsoid_Count */
 
498
/*
 
499
 *   Count    : The number of ellipsoids in the ellipsoid table. (output)
 
500
 *
 
501
 * The function Ellipsoid_Count returns the number of ellipsoids in the
 
502
 * ellipsoid table.  If the ellipsoid table has been initialized without error,
 
503
 * ELLIPSE_NO_ERROR is returned, otherwise ELLIPSE_NOT_INITIALIZED_ERROR
 
504
 * is returned.
 
505
 */
 
506
  long error_code = ELLIPSE_NO_ERROR;
 
507
  if (!Ellipsoid_Initialized)
 
508
    error_code |= ELLIPSE_NOT_INITIALIZED_ERROR;
 
509
  *Count = Number_of_Ellipsoids;
 
510
  return (error_code);
 
511
} /* End Ellipsoid_Count */
 
512
 
 
513
 
 
514
long Ellipsoid_Index ( const char *Code,
 
515
                       long *Index )
 
516
{ /* Begin Ellipsoid_Index */
 
517
/*
 
518
 *    Code     : 2-letter ellipsoid code.                      (input)
 
519
 *    Index    : Index of the ellipsoid in the ellipsoid table with the
 
520
 *                  specified code                             (output)
 
521
 *
 
522
 *  The function Ellipsoid_Index returns the index of the ellipsoid in
 
523
 *  the ellipsoid table with the specified code.  If ellipsoid code is found,
 
524
 *  ELLIPSE_NO_ERROR is returned, otherwise ELLIPSE_INVALID_CODE_ERROR is
 
525
 *  returned.
 
526
 */
 
527
  char temp_code[3];
 
528
  long error_code = ELLIPSE_NO_ERROR;
 
529
  long i = 0;                   /* index for ellipsoid table */
 
530
  long j = 0;
 
531
  *Index = 0;
 
532
  if (Ellipsoid_Initialized)
 
533
  {
 
534
    while (j < ELLIPSOID_CODE_LENGTH)
 
535
    {
 
536
      temp_code[j] = (char)toupper(Code[j]);
 
537
      j++;
 
538
    }
 
539
    temp_code[ELLIPSOID_CODE_LENGTH - 1] = 0;
 
540
    while ((i < Number_of_Ellipsoids)
 
541
           && strcmp(temp_code, Ellipsoid_Table[i].Code))
 
542
    {
 
543
      i++;
 
544
    }
 
545
    if (strcmp(temp_code, Ellipsoid_Table[i].Code))
 
546
      error_code |= ELLIPSE_INVALID_CODE_ERROR;
 
547
    else
 
548
      *Index = i+1;
 
549
  }
 
550
  else
 
551
  {
 
552
    error_code |= ELLIPSE_NOT_INITIALIZED_ERROR;
 
553
  }
 
554
  return (error_code);
 
555
} /* End Ellipsoid_Index */
 
556
 
 
557
 
 
558
long Ellipsoid_Name ( const long Index,
 
559
                      char *Name )
 
560
{ /* Begin Ellipsoid_Name */
 
561
/*
 
562
 *    Index   : Index of a given ellipsoid.in the ellipsoid table with the
 
563
 *                 specified index                             (input)
 
564
 *    Name    : Name of the ellipsoid referencd by index       (output)
 
565
 *
 
566
 *  The Function Ellipsoid_Name returns the name of the ellipsoid in
 
567
 *  the ellipsoid table with the specified index.  If index is valid,
 
568
 *  ELLIPSE_NO_ERROR is returned, otherwise ELLIPSE_INVALID_INDEX_ERROR is
 
569
 *  returned.
 
570
 */
 
571
 
 
572
  long error_code = ELLIPSE_NO_ERROR;
 
573
 
 
574
  strcpy(Name,"");
 
575
  if (Ellipsoid_Initialized)
 
576
  {
 
577
    if ((Index < 1) || (Index > Number_of_Ellipsoids))
 
578
      error_code |= ELLIPSE_INVALID_INDEX_ERROR;
 
579
    else
 
580
      strcpy(Name, Ellipsoid_Table[Index-1].Name);
 
581
  }
 
582
  else
 
583
    error_code |= ELLIPSE_NOT_INITIALIZED_ERROR;
 
584
  return (error_code);
 
585
} /* End Ellipsoid_Name */
 
586
 
 
587
 
 
588
long Ellipsoid_Code ( const long Index,
 
589
                      char *Code )
 
590
{ /* Begin Ellipsoid_Code */
 
591
/*
 
592
 *    Index    : Index of a given ellipsoid in the ellipsoid table (input)
 
593
 *    Code     : 2-letter ellipsoid code.                          (output)
 
594
 *
 
595
 *  The Function Ellipsoid_Code returns the 2-letter code for the
 
596
 *  ellipsoid in the ellipsoid table with the specified index.  If index is
 
597
 *  valid, ELLIPSE_NO_ERROR is returned, otherwise ELLIPSE_INVALID_INDEX_ERROR
 
598
 *  is returned.
 
599
 */
 
600
 
 
601
  long error_code = ELLIPSE_NO_ERROR;
 
602
 
 
603
  strcpy(Code,"");
 
604
  if (Ellipsoid_Initialized)
 
605
  {
 
606
    if ((Index < 1) || (Index > Number_of_Ellipsoids))
 
607
      error_code |= ELLIPSE_INVALID_INDEX_ERROR;
 
608
    else
 
609
      strcpy(Code, Ellipsoid_Table[Index-1].Code);
 
610
  }
 
611
  else
 
612
    error_code |= ELLIPSE_NOT_INITIALIZED_ERROR;
 
613
  return (error_code);
 
614
} /* End Ellipsoid_Code */
 
615
 
 
616
 
 
617
long Ellipsoid_Parameters ( const long Index,
 
618
                            double *a,
 
619
                            double *f )
 
620
{ /* Begin Ellipsoid_Parameters */
 
621
/*
 
622
 *    Index    : Index of a given ellipsoid in the ellipsoid table (input)
 
623
 *    a        : Semi-major axis, in meters, of ellipsoid          (output)
 
624
 *    f        : Flattening of ellipsoid.                          (output)
 
625
 *
 
626
 *  The function Ellipsoid_Parameters returns the semi-major axis and flattening
 
627
 *  for the ellipsoid with the specified index.  If index is valid,
 
628
 *  ELLIPSE_NO_ERROR is returned, otherwise ELLIPSE_INVALID_INDEX_ERROR is
 
629
 *  returned.
 
630
 */
 
631
 
 
632
  long error_code = ELLIPSE_NO_ERROR;
 
633
 
 
634
  *a = 0;
 
635
  *f = 0;
 
636
  if (Ellipsoid_Initialized)
 
637
  {
 
638
    if ((Index < 1) || (Index > Number_of_Ellipsoids))
 
639
    {
 
640
      error_code |= ELLIPSE_INVALID_INDEX_ERROR;
 
641
    }
 
642
    else
 
643
    {
 
644
      *a = Ellipsoid_Table[Index-1].A;
 
645
      *f = 1 / Ellipsoid_Table[Index-1].Recp_F;
 
646
    }
 
647
  }
 
648
  else
 
649
    error_code |= ELLIPSE_NOT_INITIALIZED_ERROR;
 
650
  return (error_code);
 
651
} /* End Ellipsoid_Parameters */
 
652
 
 
653
 
 
654
long Ellipsoid_Eccentricity2 ( const long Index,
 
655
                               double *e2 )
 
656
{ /* Begin Ellipsoid_Eccentricity2 */
 
657
/*
 
658
 *    Index    : Index of a given ellipsoid in the ellipsoid table (input)
 
659
 *    e2       : Square of eccentricity of ellipsoid               (output)
 
660
 *
 
661
 *  The function Ellipsoid_Eccentricity2 returns the square of the
 
662
 *  eccentricity for the ellipsoid with the specified index.  If index is
 
663
 *  valid, ELLIPSE_NO_ERROR is returned, otherwise ELLIPSE_INVALID_INDEX_ERROR
 
664
 *  is returned.
 
665
 */
 
666
  double f;
 
667
  long error_code = ELLIPSE_NO_ERROR;
 
668
 
 
669
  *e2 = 0;
 
670
  if (Ellipsoid_Initialized)
 
671
  {
 
672
    if ((Index < 1) || (Index > Number_of_Ellipsoids))
 
673
    {
 
674
      error_code |= ELLIPSE_INVALID_INDEX_ERROR;
 
675
    }
 
676
    else
 
677
    {
 
678
      f = 1 / Ellipsoid_Table[Index-1].Recp_F;
 
679
      *e2 = 2 * f - f * f;
 
680
    }
 
681
  }
 
682
  else
 
683
    error_code |= ELLIPSE_NOT_INITIALIZED_ERROR;
 
684
  return (error_code);
 
685
} /* End Ellipsoid_Eccentricity2 */
 
686
 
 
687
 
 
688
long Ellipsoid_User_Defined ( const long Index,
 
689
                              long *result )
 
690
 
 
691
{ /* Begin Ellipsoid_User_Defined */
 
692
/*
 
693
 *    Index    : Index of a given ellipsoid in the ellipsoid table (input)
 
694
 *    result   : Indicates whether specified ellipsoid is user defined (1)
 
695
 *               or not (0)                                        (output)
 
696
 *
 
697
 *  The function Ellipsoid_User_Defined returns 1 if the ellipsoid is user
 
698
 *  defined.  Otherwise, 0 is returned.  If index is valid ELLIPSE_NO_ERROR is
 
699
 *  returned, otherwise ELLIPSE_INVALID_INDEX_ERROR is returned.
 
700
 */
 
701
 
 
702
  long error_code = ELLIPSE_NO_ERROR;
 
703
 
 
704
  *result = FALSE;
 
705
 
 
706
  if (Ellipsoid_Initialized)
 
707
  {
 
708
    if ((Index < 1) || (Index > Number_of_Ellipsoids))
 
709
      error_code |= ELLIPSE_INVALID_INDEX_ERROR;
 
710
    else
 
711
    {
 
712
      if (Ellipsoid_Table[Index-1].User_Defined)
 
713
        *result = TRUE;
 
714
    }
 
715
  }
 
716
  else
 
717
    error_code |= ELLIPSE_NOT_INITIALIZED_ERROR;
 
718
  return (error_code);
 
719
} /* End Ellipsoid_User_Defined */
 
720
 
 
721
 
 
722
long WGS84_Parameters ( double *a,
 
723
                        double *f )
 
724
{ /* Begin WGS84_Parameters */
 
725
/*
 
726
 *    a      : Semi-major axis, in meters, of ellipsoid       (output)
 
727
 *    f      : Flattening of ellipsoid                        (output)
 
728
 *
 
729
 *  The function WGS84_Parameters returns the semi-major axis and the
 
730
 *  flattening for the WGS84 ellipsoid.  If the ellipsoid table was
 
731
 *  initialized successfully, ELLIPSE_NO_ERROR is returned, otherwise
 
732
 *  ELLIPSE_NOT_INITIALIZED_ERROR is returned.
 
733
 */
 
734
 
 
735
  long error_code = ELLIPSE_NO_ERROR;
 
736
 
 
737
  *a = 0;
 
738
  *f = 0;
 
739
  if (Ellipsoid_Initialized)
 
740
  {
 
741
    *a = Ellipsoid_Table[WGS84_Index-1].A;
 
742
    *f = 1 / Ellipsoid_Table[WGS84_Index-1].Recp_F;
 
743
  }
 
744
  else
 
745
    error_code |= ELLIPSE_NOT_INITIALIZED_ERROR;
 
746
  return (error_code);
 
747
} /* End WGS84_Parameters */
 
748
 
 
749
 
 
750
long WGS84_Eccentricity2 ( double *e2 )
 
751
{ /* Begin WGS84_Eccentricity2 */
 
752
/*
 
753
 *    e2    : Square of eccentricity of WGS84 ellipsoid      (output)
 
754
 *
 
755
 *  The function WGS84_Eccentricity2 returns the square of the
 
756
 *  eccentricity for the WGS84 ellipsoid.  If the ellipsoid table was
 
757
 *  initialized successfully, ELLIPSE_NO_ERROR is returned, otherwise
 
758
 *  ELLIPSE_NOT_INITIALIZED_ERROR is returned.
 
759
 */
 
760
 
 
761
  long error_code = ELLIPSE_NO_ERROR;
 
762
  double f;
 
763
 
 
764
  *e2 = 0;
 
765
  if (Ellipsoid_Initialized)
 
766
  {
 
767
    f = 1 / Ellipsoid_Table[WGS84_Index-1].Recp_F;
 
768
    *e2 = 2 * f - f * f;
 
769
  }
 
770
  else
 
771
    error_code |= ELLIPSE_NOT_INITIALIZED_ERROR;
 
772
  return (error_code);
 
773
} /* End WGS84_Eccentricity2 */
 
774
 
 
775
 
 
776
long WGS72_Parameters( double *a,
 
777
                       double *f )
 
778
{ /* Begin WGS72_Parameters */
 
779
/*
 
780
 *    a    : Semi-major axis, in meters, of ellipsoid        (output)
 
781
 *    f    : Flattening of ellipsoid                         (output)
 
782
 *
 
783
 *  The function WGS72_Parameters returns the semi-major axis and the
 
784
 *  flattening for the WGS72 ellipsoid.  If the ellipsoid table was
 
785
 *  initialized successfully, ELLIPSE_NO_ERROR is returned, otherwise
 
786
 *  ELLIPSE_NOT_INITIALIZED_ERROR is returned.
 
787
 */
 
788
 
 
789
  long error_code = ELLIPSE_NO_ERROR;
 
790
 
 
791
  *a = 0;
 
792
  *f = 0;
 
793
  if (Ellipsoid_Initialized)
 
794
  {
 
795
    *a = Ellipsoid_Table[WGS72_Index-1].A;
 
796
    *f = 1 / Ellipsoid_Table[WGS72_Index-1].Recp_F;
 
797
  }
 
798
  else
 
799
    error_code |= ELLIPSE_NOT_INITIALIZED_ERROR;
 
800
  return (error_code);
 
801
} /* End WGS72_Parameters */
 
802
 
 
803
 
 
804
long WGS72_Eccentricity2 ( double *e2 )
 
805
{ /* Begin WGS72_Eccentricity2 */
 
806
/*
 
807
 *    e2     : Square of eccentricity of WGS84 ellipsoid     (output)
 
808
 *
 
809
 *  The function WGS72_Eccentricity2 returns the square of the
 
810
 *  eccentricity for the WGS72 ellipsoid.  If the ellipsoid table was
 
811
 *  initialized successfully, ELLIPSE_NO_ERROR is returned, otherwise
 
812
 *  ELLIPSE_NOT_INITIALIZED_ERROR is returned.
 
813
 */
 
814
 
 
815
  long error_code = ELLIPSE_NO_ERROR;
 
816
  double f;
 
817
 
 
818
  *e2 = 0;
 
819
  if (Ellipsoid_Initialized)
 
820
  {
 
821
    f = 1 / Ellipsoid_Table[WGS72_Index-1].Recp_F;
 
822
    *e2 = 2 * f - f * f;
 
823
  }
 
824
  else
 
825
    error_code |= ELLIPSE_NOT_INITIALIZED_ERROR;
 
826
  return (error_code);
 
827
} /* End WGS72_Eccentricity2 */
 
828
 
 
829