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 
83 {
84 private:
85 
89  string shapeName;
90 
94  double equatorialRadius;
95 
100  double inverseFlattening;
101 
110  double eccentricitySqr;
111 
115  double e1;
116 
120  double e2;
121 
126  bool constantRadius;
127 
131  bool sphere;
132 
133 public:
134 
155  EarthShape(const string& earthShape = "WGS84") { setEarthShape(earthShape); }
156 
182  void setEarthShape(const string& earthShape)
183  {
184  shapeName = earthShape;
185 
186  if (earthShape == "SPHERE")
187  {
188  equatorialRadius = 6371;
189  inverseFlattening = 1e99;
190  constantRadius = true;
191  }
192  else if (earthShape == "GRS80")
193  {
194  equatorialRadius = 6378.137;
195  inverseFlattening = 298.257222101;
196  constantRadius = false;
197  }
198  else if (earthShape == "GRS80_RCONST")
199  {
200  equatorialRadius = 6371.;
201  inverseFlattening = 298.257222101;
202  constantRadius = true;
203  }
204  else if (earthShape == "WGS84")
205  {
206  equatorialRadius = 6378.137;;
207  inverseFlattening = 298.257223563;
208  constantRadius = false;
209  }
210  else if (earthShape == "WGS84_RCONST")
211  {
212  equatorialRadius = 6371.;
213  inverseFlattening = 298.257223563;
214  constantRadius = true;
215  }
216  else if (earthShape == "IERS2003")
217  {
218  equatorialRadius = 6378.1366;
219  inverseFlattening = 298.25642;
220  constantRadius = false;
221  }
222  else if (earthShape == "IERS2003_RCONST")
223  {
224  equatorialRadius = 6371.;
225  inverseFlattening = 298.25642;
226  constantRadius = true;
227  }
228  else
229  {
230  ostringstream os;
231  os << endl << "ERROR in EarthShape::setEarthShape" << endl
232  << earthShape << " is not a recognized EarthShape" << endl
233  << "Valid EarthShapes include SPHERE, GRS80, GRS80_RCONST, WGS84, WGS84_RCONST, IERS2003 and IERS2003_RCONST" << endl;
234  throw GeoTessException(os, __FILE__, __LINE__, 9001);
235  }
236 
237  sphere = shapeName == "SPHERE";
238  eccentricitySqr = (2.- 1./inverseFlattening)/inverseFlattening;
239  e1 = 1.-eccentricitySqr;
240  e2 = eccentricitySqr/(1.-eccentricitySqr);
241  }
242 
246  virtual ~EarthShape()
247  {
248  }
249 
254  static string class_name()
255  { return "EarthShape"; }
256 
261  virtual int class_size() const
262  { return (int) sizeof(EarthShape); }
263 
271  double getEarthRadius(const double* const v)
272  { return constantRadius ? equatorialRadius : equatorialRadius / sqrt(1 + e2 * v[2] * v[2]); }
273 
281  double getGeocentricLat(const double& lat)
282  { return sphere ? lat : atan(tan(lat) * e1); }
283 
291  double getGeographicLat(const double& lat)
292  { return sphere ? lat : atan(tan(lat) / e1); }
293 
301  double getGeocentricLatDegrees(const double& lat)
302  { return sphere ? lat : CPPUtils::toDegrees(atan(tan(CPPUtils::toRadians(lat)) * e1)); }
303 
311  double getGeographicLatDegrees(const double& lat)
312  { return sphere ? lat : CPPUtils::toDegrees(atan(tan(CPPUtils::toRadians(lat)) / e1)); }
313 
320  double getLat(const double* const v)
321  { return getGeographicLat(asin(v[2])); }
322 
329  double getLatDegrees(const double* const v)
330  { return CPPUtils::toDegrees(getLat(v)); }
331 
338  double getLon(const double* const v)
339  { return atan2(v[1], v[0]); }
340 
347  double getLonDegrees(const double* const v)
348  { return CPPUtils::toDegrees(atan2(v[1], v[0])); }
349 
360  void getVectorDegrees(const double& lat, const double& lon, double* v)
361  { getVector(CPPUtils::toRadians(lat), CPPUtils::toRadians(lon), v); }
362 
373  double* getVectorDegrees(const double& lat, const double& lon)
374  { return getVector(CPPUtils::toRadians(lat), CPPUtils::toRadians(lon)); }
375 
387  double* getVector(const double& lat, const double& lon)
388  {
389  double* v = new double[3];
390  getVector(lat, lon, v);
391  return v;
392  }
393 
404  double* getVector(const double& lat, const double& lon, double* v)
405  {
406  // convert lat from geographic to geocentric latitude.
407  double temp = getGeocentricLat(lat);
408 
409  // z component of v is sin of geocentric latitude.
410  v[2] = sin(temp);
411 
412  // set lat = to cos of geocentric latitude
413  temp = cos(temp);
414 
415  // compute x and y components of v
416  v[0] = temp * cos(lon);
417  v[1] = temp * sin(lon);
418 
419  return v;
420  }
421 
426  string getLatLonString(const double* const v)
427  {
428  char s[300];
429  string frmt("%9.5f %10.5f");
430  sprintf(s, frmt.c_str(), getLatDegrees(v), getLonDegrees(v));
431  return s;
432  }
433 
434  bool isConstantRadius() const { return constantRadius; }
435 
436  double getEccentricitySqr() const { return eccentricitySqr; }
437 
438  double getEquatorialRadius() const { return equatorialRadius; }
439 
440  double getInverseFlattening() const { return inverseFlattening; }
441 
442  bool isSphere() const { return sphere; }
443 
444  const string& getShapeName() const { return shapeName; }
445 
446 };
447 // end class EarthShape
448 
449 } // end namespace geotess
450 
451 #endif // EARTH_SHAPE_H