GeoTessCPP  2.2.3
Software to facilitate storage and retrieval of 3D information about the Earth.
EarthShape.h
Go to the documentation of this file.
1 //- ****************************************************************************
2 //-
3 //- Copyright 2009 National Technology & Engineering Solutions of Sandia, LLC
4 //- (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S.
5 //- Government 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 //- 1. Redistributions of source code must retain the above copyright notice,
14 //- this list of conditions and the following disclaimer.
15 //-
16 //- 2. Redistributions in binary form must reproduce the above copyright
17 //- notice, this list of conditions and the following disclaimer in the
18 //- documentation and/or other materials provided with the distribution.
19 //-
20 //- 3. Neither the name of the copyright holder nor the names of its
21 //- contributors may be used to endorse or promote products derived from
22 //- this software without specific prior written permission.
23 //-
24 //- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 //- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 //- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 //- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
28 //- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 //- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 //- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 //- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 //- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 //- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 //- POSSIBILITY OF SUCH DAMAGE.
35 //-
36 //- ****************************************************************************
37 
38 #ifndef EARTH_SHAPE_H
39 #define EARTH_SHAPE_H
40 
41 // **** _SYSTEM INCLUDES_ ******************************************************
42 
43 #include <iostream>
44 #include <string>
45 
46 // use standard library objects
47 using namespace std;
48 
49 // **** _LOCAL INCLUDES_ *******************************************************
50 
51 #include "CPPUtils.h"
52 #include "GeoTessException.h"
53 
54 // **** _BEGIN GEOTESS NAMESPACE_ **********************************************
55 
56 namespace geotess
57 {
58 
59 // **** _FORWARD REFERENCES_ ***************************************************
60 
61 // **** _CLASS DEFINITION_ *****************************************************
62 
88 {
89 private:
90 
94  string shapeName;
95 
99  double equatorialRadius;
100 
105  double inverseFlattening;
106 
115  double eccentricitySqr;
116 
120  double e1;
121 
125  double e2;
126 
131  bool constantRadius;
132 
136  bool sphere;
137 
138 public:
139 
160  EarthShape(const string& earthShape = "WGS84") { setEarthShape(earthShape); }
161 
187  void setEarthShape(const string& earthShape)
188  {
189  shapeName = earthShape;
190 
191  if (earthShape == "SPHERE")
192  {
193  equatorialRadius = 6371;
194  inverseFlattening = 1e99;
195  constantRadius = true;
196  }
197  else if (earthShape == "GRS80")
198  {
199  equatorialRadius = 6378.137;
200  inverseFlattening = 298.257222101;
201  constantRadius = false;
202  }
203  else if (earthShape == "GRS80_RCONST")
204  {
205  equatorialRadius = 6371.;
206  inverseFlattening = 298.257222101;
207  constantRadius = true;
208  }
209  else if (earthShape == "WGS84")
210  {
211  equatorialRadius = 6378.137;;
212  inverseFlattening = 298.257223563;
213  constantRadius = false;
214  }
215  else if (earthShape == "WGS84_RCONST")
216  {
217  equatorialRadius = 6371.;
218  inverseFlattening = 298.257223563;
219  constantRadius = true;
220  }
221  else if (earthShape == "IERS2003")
222  {
223  equatorialRadius = 6378.1366;
224  inverseFlattening = 298.25642;
225  constantRadius = false;
226  }
227  else if (earthShape == "IERS2003_RCONST")
228  {
229  equatorialRadius = 6371.;
230  inverseFlattening = 298.25642;
231  constantRadius = true;
232  }
233  else
234  {
235  ostringstream os;
236  os << endl << "ERROR in EarthShape::setEarthShape" << endl
237  << earthShape << " is not a recognized EarthShape" << endl
238  << "Valid EarthShapes include SPHERE, GRS80, GRS80_RCONST, WGS84, WGS84_RCONST, IERS2003 and IERS2003_RCONST" << endl;
239  throw GeoTessException(os, __FILE__, __LINE__, 9001);
240  }
241 
242  sphere = shapeName == "SPHERE";
243  eccentricitySqr = (2.- 1./inverseFlattening)/inverseFlattening;
244  e1 = 1.-eccentricitySqr;
245  e2 = eccentricitySqr/(1.-eccentricitySqr);
246  }
247 
251  virtual ~EarthShape()
252  {
253  }
254 
259  static string class_name()
260  { return "EarthShape"; }
261 
266  virtual int class_size() const
267  { return (int) sizeof(EarthShape); }
268 
276  double getEarthRadius(const double* const v)
277  { return constantRadius ? equatorialRadius : equatorialRadius / sqrt(1 + e2 * v[2] * v[2]); }
278 
286  double getGeocentricLat(const double& lat)
287  { return sphere ? lat : atan(tan(lat) * e1); }
288 
296  double getGeographicLat(const double& lat)
297  { return sphere ? lat : atan(tan(lat) / e1); }
298 
306  double getGeocentricLatDegrees(const double& lat)
307  { return sphere ? lat : CPPUtils::toDegrees(atan(tan(CPPUtils::toRadians(lat)) * e1)); }
308 
316  double getGeographicLatDegrees(const double& lat)
317  { return sphere ? lat : CPPUtils::toDegrees(atan(tan(CPPUtils::toRadians(lat)) / e1)); }
318 
325  double getLat(const double* const v)
326  { return getGeographicLat(asin(v[2])); }
327 
334  double getLatDegrees(const double* const v)
335  { return CPPUtils::toDegrees(getLat(v)); }
336 
343  double getLon(const double* const v)
344  { return atan2(v[1], v[0]); }
345 
352  double getLonDegrees(const double* const v)
353  { return CPPUtils::toDegrees(atan2(v[1], v[0])); }
354 
365  void getVectorDegrees(const double& lat, const double& lon, double* v)
366  { getVector(CPPUtils::toRadians(lat), CPPUtils::toRadians(lon), v); }
367 
378  double* getVectorDegrees(const double& lat, const double& lon)
379  { return getVector(CPPUtils::toRadians(lat), CPPUtils::toRadians(lon)); }
380 
392  double* getVector(const double& lat, const double& lon)
393  {
394  double* v = new double[3];
395  getVector(lat, lon, v);
396  return v;
397  }
398 
409  double* getVector(const double& lat, const double& lon, double* v)
410  {
411  // convert lat from geographic to geocentric latitude.
412  double temp = getGeocentricLat(lat);
413 
414  // z component of v is sin of geocentric latitude.
415  v[2] = sin(temp);
416 
417  // set lat = to cos of geocentric latitude
418  temp = cos(temp);
419 
420  // compute x and y components of v
421  v[0] = temp * cos(lon);
422  v[1] = temp * sin(lon);
423 
424  return v;
425  }
426 
431  string getLatLonString(const double* const v)
432  {
433  char s[300];
434  string frmt("%9.5f %10.5f");
435  sprintf(s, frmt.c_str(), getLatDegrees(v), getLonDegrees(v));
436  return s;
437  }
438 
439  bool isConstantRadius() const { return constantRadius; }
440 
441  double getEccentricitySqr() const { return eccentricitySqr; }
442 
443  double getEquatorialRadius() const { return equatorialRadius; }
444 
445  double getInverseFlattening() const { return inverseFlattening; }
446 
447  bool isSphere() const { return sphere; }
448 
449  const string& getShapeName() const { return shapeName; }
450 
451 };
452 // end class EarthShape
453 
454 } // end namespace geotess
455 
456 #endif // EARTH_SHAPE_H
geotess::EarthShape::isSphere
bool isSphere() const
Definition: EarthShape.h:447
geotess
Definition: ArrayReuse.h:57
GeoTessException.h
geotess::EarthShape::getEarthRadius
double getEarthRadius(const double *const v)
Definition: EarthShape.h:276
geotess::EarthShape::class_size
virtual int class_size() const
Definition: EarthShape.h:266
geotess::EarthShape::getLon
double getLon(const double *const v)
Definition: EarthShape.h:343
geotess::EarthShape::getGeographicLatDegrees
double getGeographicLatDegrees(const double &lat)
Definition: EarthShape.h:316
geotess::EarthShape::EarthShape
EarthShape(const string &earthShape="WGS84")
Definition: EarthShape.h:160
geotess::EarthShape::getVector
double * getVector(const double &lat, const double &lon, double *v)
Definition: EarthShape.h:409
geotess::EarthShape::getVectorDegrees
void getVectorDegrees(const double &lat, const double &lon, double *v)
Definition: EarthShape.h:365
geotess::EarthShape::isConstantRadius
bool isConstantRadius() const
Definition: EarthShape.h:439
geotess::EarthShape::~EarthShape
virtual ~EarthShape()
Definition: EarthShape.h:251
geotess::EarthShape::getInverseFlattening
double getInverseFlattening() const
Definition: EarthShape.h:445
geotess::EarthShape::setEarthShape
void setEarthShape(const string &earthShape)
Definition: EarthShape.h:187
geotess::EarthShape::getLonDegrees
double getLonDegrees(const double *const v)
Definition: EarthShape.h:352
geotess::EarthShape::getShapeName
const string & getShapeName() const
Definition: EarthShape.h:449
geotess::EarthShape::getGeocentricLat
double getGeocentricLat(const double &lat)
Definition: EarthShape.h:286
geotess::EarthShape::getVectorDegrees
double * getVectorDegrees(const double &lat, const double &lon)
Definition: EarthShape.h:378
geotess::EarthShape
Defines the ellipsoid that is to be used to convert between geocentric and geographic latitude and be...
Definition: EarthShape.h:88
geotess::EarthShape::class_name
static string class_name()
Definition: EarthShape.h:259
geotess::EarthShape::getLatDegrees
double getLatDegrees(const double *const v)
Definition: EarthShape.h:334
GEOTESS_EXP_IMP
#define GEOTESS_EXP_IMP
Definition: CPPGlobals.h:73
geotess::EarthShape::getVector
double * getVector(const double &lat, const double &lon)
Definition: EarthShape.h:392
geotess::EarthShape::getEquatorialRadius
double getEquatorialRadius() const
Definition: EarthShape.h:443
geotess::EarthShape::getGeographicLat
double getGeographicLat(const double &lat)
Definition: EarthShape.h:296
geotess::EarthShape::getGeocentricLatDegrees
double getGeocentricLatDegrees(const double &lat)
Definition: EarthShape.h:306
geotess::EarthShape::getEccentricitySqr
double getEccentricitySqr() const
Definition: EarthShape.h:441
geotess::EarthShape::getLatLonString
string getLatLonString(const double *const v)
Definition: EarthShape.h:431
geotess::GeoTessException
An exception class for all GeoTess objects.
Definition: GeoTessException.h:68
geotess::EarthShape::getLat
double getLat(const double *const v)
Definition: EarthShape.h:325
CPPUtils.h