GeoTessCPP  2.1
Software to facilitate storage and retrieval of 3D information about the Earth.
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros
EarthShape.h
Go to the documentation of this file.
1 //- ****************************************************************************
2 //-
3 //- Copyright 2009 Sandia Corporation. Under the terms of Contract
4 //- DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
5 //- retains certain rights in this software.
6 //-
7 //- BSD Open Source License.
8 //- All rights reserved.
9 //-
10 //- Redistribution and use in source and binary forms, with or without
11 //- modification, are permitted provided that the following conditions are met:
12 //-
13 //- * Redistributions of source code must retain the above copyright notice,
14 //- this list of conditions and the following disclaimer.
15 //- * Redistributions in binary form must reproduce the above copyright
16 //- notice, this list of conditions and the following disclaimer in the
17 //- documentation and/or other materials provided with the distribution.
18 //- * Neither the name of Sandia National Laboratories nor the names of its
19 //- contributors may be used to endorse or promote products derived from
20 //- this software without specific prior written permission.
21 //-
22 //- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 //- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 //- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 //- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26 //- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 //- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 //- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 //- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 //- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 //- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 //- POSSIBILITY OF SUCH DAMAGE.
33 //-
34 //- ****************************************************************************
35 
36 #ifndef EARTH_SHAPE_H
37 #define EARTH_SHAPE_H
38 
39 // **** _SYSTEM INCLUDES_ ******************************************************
40 
41 #include <iostream>
42 #include <string>
43 
44 // use standard library objects
45 using namespace std;
46 
47 // **** _LOCAL INCLUDES_ *******************************************************
48 
49 #include "CPPUtils.h"
50 #include "GeoTessException.h"
51 
52 // **** _BEGIN GEOTESS NAMESPACE_ **********************************************
53 
54 namespace geotess
55 {
56 
57 // **** _FORWARD REFERENCES_ ***************************************************
58 
59 // **** _CLASS DEFINITION_ *****************************************************
60 
65 {
66 private:
67 
71  string shapeName;
72 
76  double equatorialRadius;
77 
82  double inverseFlattening;
83 
92  double eccentricitySqr;
93 
97  double e1;
98 
102  double e2;
103 
108  bool constantRadius;
109 
113  bool sphere;
114 
115 public:
116 
137  EarthShape(const string& earthShape = "WGS84") { setEarthShape(earthShape); }
138 
164  void setEarthShape(const string& earthShape)
165  {
166  shapeName = earthShape;
167 
168  if (earthShape == "SPHERE")
169  {
170  equatorialRadius = 6371;
171  inverseFlattening = 1e99;
172  constantRadius = true;
173  }
174  else if (earthShape == "GRS80")
175  {
176  equatorialRadius = 6378.137;
177  inverseFlattening = 298.257222101;
178  constantRadius = false;
179  }
180  else if (earthShape == "GRS80_RCONST")
181  {
182  equatorialRadius = 6371.;
183  inverseFlattening = 298.257222101;
184  constantRadius = true;
185  }
186  else if (earthShape == "WGS84")
187  {
188  equatorialRadius = 6378.137;;
189  inverseFlattening = 298.257223563;
190  constantRadius = false;
191  }
192  else if (earthShape == "WGS84_RCONST")
193  {
194  equatorialRadius = 6371.;
195  inverseFlattening = 298.257223563;
196  constantRadius = true;
197  }
198  else if (earthShape == "IERS2003")
199  {
200  equatorialRadius = 6378.1366;
201  inverseFlattening = 298.25642;
202  constantRadius = false;
203  }
204  else if (earthShape == "IERS2003_RCONST")
205  {
206  equatorialRadius = 6371.;
207  inverseFlattening = 298.25642;
208  constantRadius = true;
209  }
210  else
211  {
212  ostringstream os;
213  os << endl << "ERROR in EarthShape::setEarthShape" << endl
214  << earthShape << " is not a recognized EarthShape" << endl
215  << "Valid EarthShapes include SPHERE, GRS80, GRS80_RCONST, WGS84, WGS84_RCONST, IERS2003 and IERS2003_RCONST" << endl;
216  throw GeoTessException(os, __FILE__, __LINE__, 9001);
217  }
218 
219  sphere = shapeName == "SPHERE";
220  eccentricitySqr = (2.- 1./inverseFlattening)/inverseFlattening;
221  e1 = 1.-eccentricitySqr;
222  e2 = eccentricitySqr/(1.-eccentricitySqr);
223  }
224 
228  virtual ~EarthShape()
229  {
230  }
231 
236  static string class_name()
237  { return "EarthShape"; }
238 
243  virtual int class_size() const
244  { return (int) sizeof(EarthShape); }
245 
253  double getEarthRadius(const double* const v)
254  {
255  return constantRadius ? equatorialRadius
256  : equatorialRadius / sqrt(1 + e2 * v[2] * v[2]);
257  }
258 
266  double getGeoCentricLatitude(const double& lat)
267  {
268  // convert lat from geographic to geocentric latitude.
269  return sphere ? lat : atan(tan(lat) * e1);
270  }
271 
279  double getGeoGraphicLatitude(const double& lat)
280  {
281  return sphere ? lat : atan(tan(lat) / e1);
282  }
283 
284 
291  double getLat(const double* const v)
292  {
293  return getGeoGraphicLatitude(asin(v[2]));
294  }
295 
302  double getLatDegrees(const double* const v)
303  {
304  return CPPUtils::toDegrees(getLat(v));
305  }
306 
313  double getLon(const double* const v)
314  {
315  return atan2(v[1], v[0]);
316  }
317 
324  double getLonDegrees(const double* const v)
325  {
326  return CPPUtils::toDegrees(atan2(v[1], v[0]));
327  }
328 
340  double* getVectorDegrees(const double& lat,
341  const double& lon)
342  {
343  return getVector(CPPUtils::toRadians(lat), CPPUtils::toRadians(lon));
344  }
345 
356  double* getVectorDegrees(const double& lat,
357  const double& lon, double* v)
358  {
359  return getVector(CPPUtils::toRadians(lat), CPPUtils::toRadians(lon), v);
360  }
361 
373  double* getVector(const double& lat, const double& lon)
374  {
375  double* v = new double[3];
376  getVector(lat, lon, v);
377  return v;
378  }
379 
390  double* getVector(const double& lat, const double& lon, double* v)
391  {
392  // convert lat from geographic to geocentric latitude.
393  double temp = getGeoCentricLatitude(lat);
394 
395  // z component of v is sin of geocentric latitude.
396  v[2] = sin(temp);
397 
398  // set lat = to cos of geocentric latitude
399  temp = cos(temp);
400 
401  // compute x and y components of v
402  v[0] = temp * cos(lon);
403  v[1] = temp * sin(lon);
404 
405  return v;
406  }
407 
412  string getLatLonString(const double* const v)
413  {
414  char s[300];
415  string frmt("%9.5f %10.5f");
416  sprintf(s, frmt.c_str(), getLatDegrees(v), getLonDegrees(v));
417  return s;
418  }
419 
420  bool isConstantRadius() const { return constantRadius; }
421 
422  double getEccentricitySqr() const { return eccentricitySqr; }
423 
424  double getEquatorialRadius() const { return equatorialRadius; }
425 
426  double getInverseFlattening() const { return inverseFlattening; }
427 
428  bool isSphere() const { return sphere; }
429 
430  const string& getShapeName() const { return shapeName; }
431 
432 };
433 // end class EarthShape
434 
435 } // end namespace geotess
436 
437 #endif // EARTH_SHAPE_H
double getLon(const double *const v)
Definition: EarthShape.h:313
double getGeoCentricLatitude(const double &lat)
Definition: EarthShape.h:266
double getEarthRadius(const double *const v)
Definition: EarthShape.h:253
double getGeoGraphicLatitude(const double &lat)
Definition: EarthShape.h:279
double * getVector(const double &lat, const double &lon, double *v)
Definition: EarthShape.h:390
void setEarthShape(const string &earthShape)
Definition: EarthShape.h:164
const string & getShapeName() const
Definition: EarthShape.h:430
double * getVectorDegrees(const double &lat, const double &lon)
Definition: EarthShape.h:340
double getLatDegrees(const double *const v)
Definition: EarthShape.h:302
double getLonDegrees(const double *const v)
Definition: EarthShape.h:324
An exception class for all GeoTess objects.
Definition: GeoTessException.h:65
static string class_name()
Definition: EarthShape.h:236
virtual ~EarthShape()
Definition: EarthShape.h:228
double * getVector(const double &lat, const double &lon)
Definition: EarthShape.h:373
Definition: EarthShape.h:64
virtual int class_size() const
Definition: EarthShape.h:243
double getInverseFlattening() const
Definition: EarthShape.h:426
string getLatLonString(const double *const v)
Definition: EarthShape.h:412
bool isConstantRadius() const
Definition: EarthShape.h:420
bool isSphere() const
Definition: EarthShape.h:428
double getLat(const double *const v)
Definition: EarthShape.h:291
#define GEOTESS_EXP_IMP
Definition: CPPGlobals.h:71
double * getVectorDegrees(const double &lat, const double &lon, double *v)
Definition: EarthShape.h:356
double getEquatorialRadius() const
Definition: EarthShape.h:424
double getEccentricitySqr() const
Definition: EarthShape.h:422
EarthShape(const string &earthShape="WGS84")
Definition: EarthShape.h:137