GeoTessCPP  2.2
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 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 
86 {
87 private:
88 
92  string shapeName;
93 
97  double equatorialRadius;
98 
103  double inverseFlattening;
104 
113  double eccentricitySqr;
114 
118  double e1;
119 
123  double e2;
124 
129  bool constantRadius;
130 
134  bool sphere;
135 
136 public:
137 
158  EarthShape(const string& earthShape = "WGS84") { setEarthShape(earthShape); }
159 
185  void setEarthShape(const string& earthShape)
186  {
187  shapeName = earthShape;
188 
189  if (earthShape == "SPHERE")
190  {
191  equatorialRadius = 6371;
192  inverseFlattening = 1e99;
193  constantRadius = true;
194  }
195  else if (earthShape == "GRS80")
196  {
197  equatorialRadius = 6378.137;
198  inverseFlattening = 298.257222101;
199  constantRadius = false;
200  }
201  else if (earthShape == "GRS80_RCONST")
202  {
203  equatorialRadius = 6371.;
204  inverseFlattening = 298.257222101;
205  constantRadius = true;
206  }
207  else if (earthShape == "WGS84")
208  {
209  equatorialRadius = 6378.137;;
210  inverseFlattening = 298.257223563;
211  constantRadius = false;
212  }
213  else if (earthShape == "WGS84_RCONST")
214  {
215  equatorialRadius = 6371.;
216  inverseFlattening = 298.257223563;
217  constantRadius = true;
218  }
219  else if (earthShape == "IERS2003")
220  {
221  equatorialRadius = 6378.1366;
222  inverseFlattening = 298.25642;
223  constantRadius = false;
224  }
225  else if (earthShape == "IERS2003_RCONST")
226  {
227  equatorialRadius = 6371.;
228  inverseFlattening = 298.25642;
229  constantRadius = true;
230  }
231  else
232  {
233  ostringstream os;
234  os << endl << "ERROR in EarthShape::setEarthShape" << endl
235  << earthShape << " is not a recognized EarthShape" << endl
236  << "Valid EarthShapes include SPHERE, GRS80, GRS80_RCONST, WGS84, WGS84_RCONST, IERS2003 and IERS2003_RCONST" << endl;
237  throw GeoTessException(os, __FILE__, __LINE__, 9001);
238  }
239 
240  sphere = shapeName == "SPHERE";
241  eccentricitySqr = (2.- 1./inverseFlattening)/inverseFlattening;
242  e1 = 1.-eccentricitySqr;
243  e2 = eccentricitySqr/(1.-eccentricitySqr);
244  }
245 
249  virtual ~EarthShape()
250  {
251  }
252 
257  static string class_name()
258  { return "EarthShape"; }
259 
264  virtual int class_size() const
265  { return (int) sizeof(EarthShape); }
266 
274  double getEarthRadius(const double* const v)
275  { return constantRadius ? equatorialRadius : equatorialRadius / sqrt(1 + e2 * v[2] * v[2]); }
276 
284  double getGeocentricLat(const double& lat)
285  { return sphere ? lat : atan(tan(lat) * e1); }
286 
294  double getGeographicLat(const double& lat)
295  { return sphere ? lat : atan(tan(lat) / e1); }
296 
304  double getGeocentricLatDegrees(const double& lat)
305  { return sphere ? lat : CPPUtils::toDegrees(atan(tan(CPPUtils::toRadians(lat)) * e1)); }
306 
314  double getGeographicLatDegrees(const double& lat)
315  { return sphere ? lat : CPPUtils::toDegrees(atan(tan(CPPUtils::toRadians(lat)) / e1)); }
316 
323  double getLat(const double* const v)
324  { return getGeographicLat(asin(v[2])); }
325 
332  double getLatDegrees(const double* const v)
333  { return CPPUtils::toDegrees(getLat(v)); }
334 
341  double getLon(const double* const v)
342  { return atan2(v[1], v[0]); }
343 
350  double getLonDegrees(const double* const v)
351  { return CPPUtils::toDegrees(atan2(v[1], v[0])); }
352 
363  void getVectorDegrees(const double& lat, const double& lon, double* v)
364  { getVector(CPPUtils::toRadians(lat), CPPUtils::toRadians(lon), v); }
365 
375  double* getVectorDegrees(const double& lat, const double& lon)
376  { return getVector(CPPUtils::toRadians(lat), CPPUtils::toRadians(lon)); }
377 
389  double* getVector(const double& lat, const double& lon)
390  {
391  double* v = new double[3];
392  getVector(lat, lon, v);
393  return v;
394  }
395 
406  double* getVector(const double& lat, const double& lon, double* v)
407  {
408  // convert lat from geographic to geocentric latitude.
409  double temp = getGeocentricLat(lat);
410 
411  // z component of v is sin of geocentric latitude.
412  v[2] = sin(temp);
413 
414  // set lat = to cos of geocentric latitude
415  temp = cos(temp);
416 
417  // compute x and y components of v
418  v[0] = temp * cos(lon);
419  v[1] = temp * sin(lon);
420 
421  return v;
422  }
423 
428  string getLatLonString(const double* const v)
429  {
430  char s[300];
431  string frmt("%9.5f %10.5f");
432  sprintf(s, frmt.c_str(), getLatDegrees(v), getLonDegrees(v));
433  return s;
434  }
435 
436  bool isConstantRadius() const { return constantRadius; }
437 
438  double getEccentricitySqr() const { return eccentricitySqr; }
439 
440  double getEquatorialRadius() const { return equatorialRadius; }
441 
442  double getInverseFlattening() const { return inverseFlattening; }
443 
444  bool isSphere() const { return sphere; }
445 
446  const string& getShapeName() const { return shapeName; }
447 
448 };
449 // end class EarthShape
450 
451 } // end namespace geotess
452 
453 #endif // EARTH_SHAPE_H
double getLon(const double *const v)
Convert a 3-component unit vector to a longitude, in radians.
Definition: EarthShape.h:341
Definition: ArrayReuse.h:55
double getEarthRadius(const double *const v)
Retrieve the radius of the Earth in km at the position specified by an Earth-centered unit vector...
Definition: EarthShape.h:274
double * getVector(const double &lat, const double &lon, double *v)
Convert geographic lat, lon into a geocentric unit vector.
Definition: EarthShape.h:406
void setEarthShape(const string &earthShape)
Define the shape of the Earth that is to be used to convert between geocentric and geographic latitud...
Definition: EarthShape.h:185
const string & getShapeName() const
Definition: EarthShape.h:446
double getGeocentricLat(const double &lat)
Return geocentric latitude given a geographic latitude.
Definition: EarthShape.h:284
void getVectorDegrees(const double &lat, const double &lon, double *v)
Convert geographic lat, lon into a geocentric unit vector.
Definition: EarthShape.h:363
double * getVectorDegrees(const double &lat, const double &lon)
Convert geographic lat, lon into a geocentric unit vector.
Definition: EarthShape.h:375
double getLatDegrees(const double *const v)
Convert a 3-component unit vector to geographic latitude, in degrees.
Definition: EarthShape.h:332
double getLonDegrees(const double *const v)
Convert a 3-component unit vector to a longitude, in degrees.
Definition: EarthShape.h:350
An exception class for all GeoTess objects.
Definition: GeoTessException.h:65
static string class_name()
Returns the class name.
Definition: EarthShape.h:257
virtual ~EarthShape()
Destructor.
Definition: EarthShape.h:249
double * getVector(const double &lat, const double &lon)
Convert geographic lat, lon into a geocentric unit vector.
Definition: EarthShape.h:389
Defines the ellipsoid that is to be used to convert between geocentric and geographic latitude and be...
Definition: EarthShape.h:85
virtual int class_size() const
Returns the class size.
Definition: EarthShape.h:264
double getInverseFlattening() const
Definition: EarthShape.h:442
string getLatLonString(const double *const v)
Definition: EarthShape.h:428
bool isConstantRadius() const
Definition: EarthShape.h:436
bool isSphere() const
Definition: EarthShape.h:444
double getLat(const double *const v)
Convert a 3-component unit vector to geographic latitude, in radians.
Definition: EarthShape.h:323
#define GEOTESS_EXP_IMP
Definition: CPPGlobals.h:71
double getGeocentricLatDegrees(const double &lat)
Return geocentric latitude given a geographic latitude.
Definition: EarthShape.h:304
double getEquatorialRadius() const
Definition: EarthShape.h:440
double getEccentricitySqr() const
Definition: EarthShape.h:438
double getGeographicLat(const double &lat)
Return geographic latitude given a geocentric latitude.
Definition: EarthShape.h:294
EarthShape(const string &earthShape="WGS84")
Define the shape of the Earth that is to be used to convert between geocentric and geographic latitud...
Definition: EarthShape.h:158
double getGeographicLatDegrees(const double &lat)
Return geographic latitude given a geocentric latitude.
Definition: EarthShape.h:314