GeographicLib  1.35
Constants.hpp
Go to the documentation of this file.
1 /**
2  * \file Constants.hpp
3  * \brief Header for GeographicLib::Constants class
4  *
5  * Copyright (c) Charles Karney (2008-2011) <charles@karney.com> and licensed
6  * under the MIT/X11 License. For more information, see
7  * http://geographiclib.sourceforge.net/
8  **********************************************************************/
9 
10 #if !defined(GEOGRAPHICLIB_CONSTANTS_HPP)
11 #define GEOGRAPHICLIB_CONSTANTS_HPP 1
12 
13 #include <GeographicLib/Config.h>
14 
15 /**
16  * A compile-time assert. Use C++11 static_assert, if available.
17  **********************************************************************/
18 #if !defined(STATIC_ASSERT)
19 # if __cplusplus >= 201103
20 # define STATIC_ASSERT static_assert
21 # elif defined(__GXX_EXPERIMENTAL_CXX0X__)
22 # define STATIC_ASSERT static_assert
23 # elif defined(_MSC_VER) && _MSC_VER >= 1600
24 // For reference, here is a table of Visual Studio and _MSC_VER
25 // correspondences:
26 //
27 // _MSC_VER Visual Studio
28 // 1300 vc7
29 // 1311 vc7.1 (2003)
30 // 1400 vc8 (2005)
31 // 1500 vc9 (2008)
32 // 1600 vc10 (2010)
33 // 1700 vc11 (2012)
34 // 1800 vc12 (2013)
35 # define STATIC_ASSERT static_assert
36 # else
37 # define STATIC_ASSERT(cond,reason) \
38  { enum{ STATIC_ASSERT_ENUM = 1/int(cond) }; }
39 # endif
40 #endif
41 
42 #if defined(_MSC_VER) && defined(GEOGRAPHICLIB_SHARED_LIB) && \
43  GEOGRAPHICLIB_SHARED_LIB
44 # if GEOGRAPHICLIB_SHARED_LIB > 1
45 # error GEOGRAPHICLIB_SHARED_LIB must be 0 or 1
46 # elif defined(GeographicLib_EXPORTS)
47 # define GEOGRAPHICLIB_EXPORT __declspec(dllexport)
48 # else
49 # define GEOGRAPHICLIB_EXPORT __declspec(dllimport)
50 # endif
51 #else
52 # define GEOGRAPHICLIB_EXPORT
53 #endif
54 
55 #include <stdexcept>
56 #include <string>
57 #include <GeographicLib/Math.hpp>
58 
59 /**
60  * \brief Namespace for %GeographicLib
61  *
62  * All of %GeographicLib is defined within the GeographicLib namespace. In
63  * addition all the header files are included via %GeographicLib/Class.hpp.
64  * This minimizes the likelihood of conflicts with other packages.
65  **********************************************************************/
66 namespace GeographicLib {
67 
68  /**
69  * \brief %Constants needed by %GeographicLib
70  *
71  * Define constants specifying the WGS84 ellipsoid, the UTM and UPS
72  * projections, and various unit conversions.
73  *
74  * Example of use:
75  * \include example-Constants.cpp
76  **********************************************************************/
78  private:
79  typedef Math::real real;
80  Constants(); // Disable constructor
81 
82  public:
83  /**
84  * A synonym for Math::degree<real>().
85  **********************************************************************/
86  static inline Math::real degree() throw() { return Math::degree<real>(); }
87  /**
88  * @return the number of radians in an arcminute.
89  **********************************************************************/
90  static inline Math::real arcminute() throw()
91  { return Math::degree<real>() / 60; }
92  /**
93  * @return the number of radians in an arcsecond.
94  **********************************************************************/
95  static inline Math::real arcsecond() throw()
96  { return Math::degree<real>() / 3600; }
97 
98  /** \name Ellipsoid parameters
99  **********************************************************************/
100  ///@{
101  /**
102  * @tparam T the type of the returned value.
103  * @return the equatorial radius of WGS84 ellipsoid (6378137 m).
104  **********************************************************************/
105  template<typename T> static inline T WGS84_a() throw()
106  { return T(6378137) * meter<T>(); }
107  /**
108  * A synonym for WGS84_a<real>().
109  **********************************************************************/
110  static inline Math::real WGS84_a() throw() { return WGS84_a<real>(); }
111  /**
112  * @tparam T the type of the returned value.
113  * @return the flattening of WGS84 ellipsoid (1/298.257223563).
114  **********************************************************************/
115  template<typename T> static inline T WGS84_f() throw()
116  { return T(1) / ( T(298) + T(257223563) / T(1000000000) ); }
117  /**
118  * A synonym for WGS84_f<real>().
119  **********************************************************************/
120  static inline Math::real WGS84_f() throw() { return WGS84_f<real>(); }
121  /**
122  * @tparam T the type of the returned value.
123  * @return the gravitational constant of the WGS84 ellipsoid, \e GM, in
124  * m<sup>3</sup> s<sup>&minus;2</sup>.
125  **********************************************************************/
126  template<typename T> static inline T WGS84_GM() throw()
127  { return T(3986004) * T(100000000) + T(41800000); }
128  /**
129  * @tparam T the type of the returned value.
130  * @return the angular velocity of the WGS84 ellipsoid, &omega;, in rad
131  * s<sup>&minus;1</sup>.
132  **********************************************************************/
133  template<typename T> static inline T WGS84_omega() throw()
134  { return T(7292115) / (T(1000000) * T(100000)); }
135  /// \cond SKIP
136  /**
137  * <b>DEPRECATED</b>
138  * @return the reciprocal flattening of WGS84 ellipsoid.
139  **********************************************************************/
140  template<typename T> static inline T WGS84_r() throw()
141  { return 1/WGS84_f<T>(); }
142  /**
143  * <b>DEPRECATED</b>
144  * A synonym for WGS84_r<real>().
145  **********************************************************************/
146  static inline Math::real WGS84_r() throw() { return WGS84_r<real>(); }
147  /// \endcond
148  /**
149  * @tparam T the type of the returned value.
150  * @return the equatorial radius of GRS80 ellipsoid, \e a, in m.
151  **********************************************************************/
152  template<typename T> static inline T GRS80_a() throw()
153  { return T(6378137); }
154  /**
155  * @tparam T the type of the returned value.
156  * @return the gravitational constant of the GRS80 ellipsoid, \e GM, in
157  * m<sup>3</sup> s<sup>&minus;2</sup>.
158  **********************************************************************/
159  template<typename T> static inline T GRS80_GM() throw()
160  { return T(3986005) * T(100000000); }
161  /**
162  * @tparam T the type of the returned value.
163  * @return the angular velocity of the GRS80 ellipsoid, &omega;, in rad
164  * s<sup>&minus;1</sup>.
165  *
166  * This is about 2 &pi; 366.25 / (365.25 &times; 24 &times; 3600) rad
167  * s<sup>&minus;1</sup>. 365.25 is the number of days in a Julian year and
168  * 365.35/366.25 converts from solar days to sidereal days. Using the
169  * number of days in a Gregorian year (365.2425) results in a worse
170  * approximation (because the Gregorian year includes the precession of the
171  * earth's axis).
172  **********************************************************************/
173  template<typename T> static inline T GRS80_omega() throw()
174  { return T(7292115) / (T(1000000) * T(100000)); }
175  /**
176  * @tparam T the type of the returned value.
177  * @return the dynamical form factor of the GRS80 ellipsoid,
178  * <i>J</i><sub>2</sub>.
179  **********************************************************************/
180  template<typename T> static inline T GRS80_J2() throw()
181  { return T(108263) / T(100000000); }
182  /**
183  * @tparam T the type of the returned value.
184  * @return the central scale factor for UTM (0.9996).
185  **********************************************************************/
186  template<typename T> static inline T UTM_k0() throw()
187  {return T(9996) / T(10000); }
188  /**
189  * A synonym for UTM_k0<real>().
190  **********************************************************************/
191  static inline Math::real UTM_k0() throw() { return UTM_k0<real>(); }
192  /**
193  * @tparam T the type of the returned value.
194  * @return the central scale factor for UPS (0.994).
195  **********************************************************************/
196  template<typename T> static inline T UPS_k0() throw()
197  { return T(994) / T(1000); }
198  /**
199  * A synonym for UPS_k0<real>().
200  **********************************************************************/
201  static inline Math::real UPS_k0() throw() { return UPS_k0<real>(); }
202  ///@}
203 
204  /** \name SI units
205  **********************************************************************/
206  ///@{
207  /**
208  * @tparam T the type of the returned value.
209  * @return the number of meters in a meter.
210  *
211  * This is unity, but this lets the internal system of units be changed if
212  * necessary.
213  **********************************************************************/
214  template<typename T> static inline T meter() throw() { return T(1); }
215  /**
216  * A synonym for meter<real>().
217  **********************************************************************/
218  static inline Math::real meter() throw() { return meter<real>(); }
219  /**
220  * @return the number of meters in a kilometer.
221  **********************************************************************/
222  static inline Math::real kilometer() throw()
223  { return 1000 * meter<real>(); }
224  /**
225  * @return the number of meters in a nautical mile (approximately 1 arc
226  * minute)
227  **********************************************************************/
228  static inline Math::real nauticalmile() throw()
229  { return 1852 * meter<real>(); }
230 
231  /**
232  * @tparam T the type of the returned value.
233  * @return the number of square meters in a square meter.
234  *
235  * This is unity, but this lets the internal system of units be changed if
236  * necessary.
237  **********************************************************************/
238  template<typename T> static inline T square_meter() throw()
239  { return meter<real>() * meter<real>(); }
240  /**
241  * A synonym for square_meter<real>().
242  **********************************************************************/
243  static inline Math::real square_meter() throw()
244  { return square_meter<real>(); }
245  /**
246  * @return the number of square meters in a hectare.
247  **********************************************************************/
248  static inline Math::real hectare() throw()
249  { return 10000 * square_meter<real>(); }
250  /**
251  * @return the number of square meters in a square kilometer.
252  **********************************************************************/
253  static inline Math::real square_kilometer() throw()
254  { return kilometer() * kilometer(); }
255  /**
256  * @return the number of square meters in a square nautical mile.
257  **********************************************************************/
258  static inline Math::real square_nauticalmile() throw()
259  { return nauticalmile() * nauticalmile(); }
260  ///@}
261 
262  /** \name Anachronistic British units
263  **********************************************************************/
264  ///@{
265  /**
266  * @return the number of meters in an international foot.
267  **********************************************************************/
268  static inline Math::real foot() throw()
269  { return real(0.0254L) * 12 * meter<real>(); }
270  /**
271  * @return the number of meters in a yard.
272  **********************************************************************/
273  static inline Math::real yard() throw() { return 3 * foot(); }
274  /**
275  * @return the number of meters in a fathom.
276  **********************************************************************/
277  static inline Math::real fathom() throw() { return 2 * yard(); }
278  /**
279  * @return the number of meters in a chain.
280  **********************************************************************/
281  static inline Math::real chain() throw() { return 22 * yard(); }
282  /**
283  * @return the number of meters in a furlong.
284  **********************************************************************/
285  static inline Math::real furlong() throw() { return 10 * chain(); }
286  /**
287  * @return the number of meters in a statute mile.
288  **********************************************************************/
289  static inline Math::real mile() throw() { return 8 * furlong(); }
290  /**
291  * @return the number of square meters in an acre.
292  **********************************************************************/
293  static inline Math::real acre() throw() { return chain() * furlong(); }
294  /**
295  * @return the number of square meters in a square statute mile.
296  **********************************************************************/
297  static inline Math::real square_mile() throw() { return mile() * mile(); }
298  ///@}
299 
300  /** \name Anachronistic US units
301  **********************************************************************/
302  ///@{
303  /**
304  * @return the number of meters in a US survey foot.
305  **********************************************************************/
306  static inline Math::real surveyfoot() throw()
307  { return real(1200) / real(3937) * meter<real>(); }
308  ///@}
309  };
310 
311  /**
312  * \brief Exception handling for %GeographicLib
313  *
314  * A class to handle exceptions. It's derived from std::runtime_error so it
315  * can be caught by the usual catch clauses.
316  *
317  * Example of use:
318  * \include example-GeographicErr.cpp
319  **********************************************************************/
320  class GeographicErr : public std::runtime_error {
321  public:
322 
323  /**
324  * Constructor
325  *
326  * @param[in] msg a string message, which is accessible in the catch
327  * clause via what().
328  **********************************************************************/
329  GeographicErr(const std::string& msg) : std::runtime_error(msg) {}
330  };
331 
332 } // namespace GeographicLib
333 
334 #endif // GEOGRAPHICLIB_CONSTANTS_HPP
static Math::real arcminute()
Definition: Constants.hpp:90
static Math::real mile()
Definition: Constants.hpp:289
static Math::real kilometer()
Definition: Constants.hpp:222
static Math::real yard()
Definition: Constants.hpp:273
#define GEOGRAPHICLIB_EXPORT
Definition: Constants.hpp:52
static Math::real UPS_k0()
Definition: Constants.hpp:201
static Math::real square_nauticalmile()
Definition: Constants.hpp:258
GeographicLib::Math::real real
Definition: GeodSolve.cpp:40
static Math::real nauticalmile()
Definition: Constants.hpp:228
static Math::real arcsecond()
Definition: Constants.hpp:95
static Math::real foot()
Definition: Constants.hpp:268
static Math::real surveyfoot()
Definition: Constants.hpp:306
static Math::real furlong()
Definition: Constants.hpp:285
static Math::real hectare()
Definition: Constants.hpp:248
static Math::real meter()
Definition: Constants.hpp:218
static Math::real degree()
Definition: Constants.hpp:86
static Math::real fathom()
Definition: Constants.hpp:277
static Math::real UTM_k0()
Definition: Constants.hpp:191
static Math::real acre()
Definition: Constants.hpp:293
Header for GeographicLib::Math class.
static Math::real chain()
Definition: Constants.hpp:281
static Math::real square_meter()
Definition: Constants.hpp:243
Constants needed by GeographicLib
Definition: Constants.hpp:77
static Math::real WGS84_a()
Definition: Constants.hpp:110
Exception handling for GeographicLib.
Definition: Constants.hpp:320
static Math::real square_kilometer()
Definition: Constants.hpp:253
static Math::real square_mile()
Definition: Constants.hpp:297
static Math::real WGS84_f()
Definition: Constants.hpp:120
GeographicErr(const std::string &msg)
Definition: Constants.hpp:329