1 : /******************************************************************************
2 : * $Id: atlsci_spheroid.cpp 22381 2011-05-16 21:14:22Z rouault $
3 : *
4 : * Project: Spheroid classes
5 : * Purpose: Provide spheroid lookup table base classes.
6 : * Author: Gillian Walter
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 1999, Frank Warmerdam
10 : *
11 : * Permission is hereby granted, free of charge, to any person obtaining a
12 : * copy of this software and associated documentation files (the "Software"),
13 : * to deal in the Software without restriction, including without limitation
14 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 : * and/or sell copies of the Software, and to permit persons to whom the
16 : * Software is furnished to do so, subject to the following conditions:
17 : *
18 : * The above copyright notice and this permission notice shall be included
19 : * in all copies or substantial portions of the Software.
20 : *
21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 : * DEALINGS IN THE SOFTWARE.
28 : ****************************************************************************/
29 :
30 : #include "atlsci_spheroid.h"
31 : #include "cpl_string.h"
32 :
33 : CPL_CVSID("$Id: atlsci_spheroid.cpp 22381 2011-05-16 21:14:22Z rouault $");
34 :
35 : /**********************************************************************/
36 : /* ================================================================== */
37 : /* Spheroid definitions */
38 : /* ================================================================== */
39 : /**********************************************************************/
40 :
41 :
42 432 : void SpheroidItem :: SetValuesByRadii(const char *spheroidname, double eq_radius, double p_radius)
43 : {
44 432 : spheroid_name = CPLStrdup(spheroidname);
45 432 : equitorial_radius=eq_radius;
46 432 : polar_radius=p_radius;
47 432 : inverse_flattening=(eq_radius == polar_radius) ? 0 : eq_radius/(eq_radius - polar_radius);
48 432 : }
49 :
50 4756 : void SpheroidItem :: SetValuesByEqRadiusAndInvFlattening(const char *spheroidname, double eq_radius, double inverseflattening)
51 : {
52 4756 : spheroid_name = CPLStrdup(spheroidname);
53 4756 : equitorial_radius=eq_radius;
54 4756 : inverse_flattening=inverseflattening;
55 4756 : polar_radius=(inverse_flattening == 0) ? eq_radius : eq_radius*(1.0 - (1.0/inverse_flattening));
56 4756 : }
57 27136 : SpheroidItem :: SpheroidItem()
58 : {
59 27136 : spheroid_name=NULL;
60 27136 : equitorial_radius=-1.0;
61 27136 : polar_radius=-1.0;
62 27136 : inverse_flattening=-1.0;
63 27136 : }
64 :
65 27136 : SpheroidItem :: ~SpheroidItem()
66 : {
67 27136 : if (spheroid_name != NULL)
68 5188 : CPLFree(spheroid_name);
69 27136 : }
70 :
71 106 : SpheroidList :: SpheroidList()
72 : {
73 106 : num_spheroids=0;
74 106 : }
75 :
76 106 : SpheroidList :: ~SpheroidList()
77 : {
78 106 : }
79 :
80 0 : char *SpheroidList :: GetSpheroidNameByRadii( double eq_radius, double polar_radius )
81 : {
82 0 : int index=0;
83 0 : double er=0.0;
84 0 : double pr=0.0;
85 :
86 0 : for(index=0;index<num_spheroids;index++)
87 : {
88 0 : er = spheroids[index].equitorial_radius;
89 0 : pr = spheroids[index].polar_radius;
90 0 : if ((fabs(er - eq_radius) < epsilonR) && (fabs(pr - polar_radius) < epsilonR))
91 0 : return CPLStrdup(spheroids[index].spheroid_name);
92 : }
93 :
94 0 : return NULL;
95 :
96 : }
97 :
98 68 : char *SpheroidList :: GetSpheroidNameByEqRadiusAndInvFlattening( double eq_radius, double inverse_flattening )
99 : {
100 68 : int index=0;
101 68 : double er=0.0;
102 68 : double invf=0.0;
103 :
104 1742 : for(index=0;index<num_spheroids;index++)
105 : {
106 1724 : er = spheroids[index].equitorial_radius;
107 1724 : invf = spheroids[index].inverse_flattening;
108 1724 : if ((fabs(er - eq_radius) < epsilonR) && (fabs(invf - inverse_flattening) < epsilonI))
109 50 : return CPLStrdup(spheroids[index].spheroid_name);
110 : }
111 :
112 18 : return NULL;
113 :
114 : }
115 :
116 40 : double SpheroidList :: GetSpheroidEqRadius( const char *spheroid_name )
117 : {
118 40 : int index=0;
119 :
120 906 : for(index=0;index<num_spheroids;index++)
121 : {
122 906 : if EQUAL(spheroids[index].spheroid_name,spheroid_name)
123 40 : return spheroids[index].equitorial_radius;
124 : }
125 :
126 0 : return -1.0;
127 :
128 : }
129 :
130 36 : int SpheroidList :: SpheroidInList( const char *spheroid_name )
131 : {
132 : /* Return 1 if the spheroid name is recognized; 0 otherwise */
133 36 : int index=0;
134 :
135 880 : for(index=0;index<num_spheroids;index++)
136 : {
137 880 : if EQUAL(spheroids[index].spheroid_name,spheroid_name)
138 36 : return 1;
139 : }
140 :
141 0 : return 0;
142 : }
143 :
144 40 : double SpheroidList :: GetSpheroidInverseFlattening( const char *spheroid_name )
145 : {
146 40 : int index=0;
147 :
148 906 : for(index=0;index<num_spheroids;index++)
149 : {
150 906 : if EQUAL(spheroids[index].spheroid_name,spheroid_name)
151 40 : return spheroids[index].inverse_flattening;
152 : }
153 :
154 0 : return -1.0;
155 :
156 : }
157 :
158 0 : double SpheroidList :: GetSpheroidPolarRadius( const char *spheroid_name )
159 : {
160 0 : int index=0;
161 :
162 0 : for(index=0;index<num_spheroids;index++)
163 : {
164 0 : if (strcmp(spheroids[index].spheroid_name,spheroid_name) == 0)
165 0 : return spheroids[index].polar_radius;
166 : }
167 :
168 0 : return -1.0;
169 :
170 : }
171 :
|