GeographicLib  1.40
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LambertConformalConic.hpp
Go to the documentation of this file.
1 /**
2  * \file LambertConformalConic.hpp
3  * \brief Header for GeographicLib::LambertConformalConic class
4  *
5  * Copyright (c) Charles Karney (2010-2014) <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_LAMBERTCONFORMALCONIC_HPP)
11 #define GEOGRAPHICLIB_LAMBERTCONFORMALCONIC_HPP 1
12 
14 
15 namespace GeographicLib {
16 
17  /**
18  * \brief Lambert conformal conic projection
19  *
20  * Implementation taken from the report,
21  * - J. P. Snyder,
22  * <a href="http://pubs.er.usgs.gov/usgspubs/pp/pp1395"> Map Projections: A
23  * Working Manual</a>, USGS Professional Paper 1395 (1987),
24  * pp. 107--109.
25  *
26  * This is a implementation of the equations in Snyder except that divided
27  * differences have been used to transform the expressions into ones which
28  * may be evaluated accurately and that Newton's method is used to invert the
29  * projection. In this implementation, the projection correctly becomes the
30  * Mercator projection or the polar stereographic projection when the
31  * standard latitude is the equator or a pole. The accuracy of the
32  * projections is about 10 nm (10 nanometers).
33  *
34  * The ellipsoid parameters, the standard parallels, and the scale on the
35  * standard parallels are set in the constructor. Internally, the case with
36  * two standard parallels is converted into a single standard parallel, the
37  * latitude of tangency (also the latitude of minimum scale), with a scale
38  * specified on this parallel. This latitude is also used as the latitude of
39  * origin which is returned by LambertConformalConic::OriginLatitude. The
40  * scale on the latitude of origin is given by
41  * LambertConformalConic::CentralScale. The case with two distinct standard
42  * parallels where one is a pole is singular and is disallowed. The central
43  * meridian (which is a trivial shift of the longitude) is specified as the
44  * \e lon0 argument of the LambertConformalConic::Forward and
45  * LambertConformalConic::Reverse functions. There is no provision in this
46  * class for specifying a false easting or false northing or a different
47  * latitude of origin. However these are can be simply included by the
48  * calling function. For example the Pennsylvania South state coordinate
49  * system (<a href="http://www.spatialreference.org/ref/epsg/3364/">
50  * EPSG:3364</a>) is obtained by:
51  * \include example-LambertConformalConic.cpp
52  *
53  * <a href="ConicProj.1.html">ConicProj</a> is a command-line utility
54  * providing access to the functionality of LambertConformalConic and
55  * AlbersEqualArea.
56  **********************************************************************/
58  private:
59  typedef Math::real real;
60  real eps_, epsx_, tol_, ahypover_;
61  real _a, _f, _fm, _e2, _e, _e2m;
62  real _sign, _n, _nc, _t0nm1, _scale, _lat0, _k0;
63  real _scbet0, _tchi0, _scchi0, _psi0, _nrho0, _drhomax;
64  static const int numit_ = 5;
65  static inline real hyp(real x) { return Math::hypot(real(1), x); }
66  // e * atanh(e * x) = log( ((1 + e*x)/(1 - e*x))^(e/2) ) if f >= 0
67  // - sqrt(-e2) * atan( sqrt(-e2) * x) if f < 0
68  inline real eatanhe(real x) const {
69  using std::atan;
70  return _f >= 0 ? _e * Math::atanh(_e * x) : - _e * atan(_e * x);
71  }
72  // Divided differences
73  // Definition: Df(x,y) = (f(x)-f(y))/(x-y)
74  // See:
75  // W. M. Kahan and R. J. Fateman,
76  // Symbolic computation of divided differences,
77  // SIGSAM Bull. 33(3), 7-28 (1999)
78  // https://dx.doi.org/10.1145/334714.334716
79  // http://www.cs.berkeley.edu/~fateman/papers/divdiff.pdf
80  //
81  // General rules
82  // h(x) = f(g(x)): Dh(x,y) = Df(g(x),g(y))*Dg(x,y)
83  // h(x) = f(x)*g(x):
84  // Dh(x,y) = Df(x,y)*g(x) + Dg(x,y)*f(y)
85  // = Df(x,y)*g(y) + Dg(x,y)*f(x)
86  // = Df(x,y)*(g(x)+g(y))/2 + Dg(x,y)*(f(x)+f(y))/2
87  //
88  // hyp(x) = sqrt(1+x^2): Dhyp(x,y) = (x+y)/(hyp(x)+hyp(y))
89  static inline real Dhyp(real x, real y, real hx, real hy)
90  // hx = hyp(x)
91  { return (x + y) / (hx + hy); }
92  // sn(x) = x/sqrt(1+x^2): Dsn(x,y) = (x+y)/((sn(x)+sn(y))*(1+x^2)*(1+y^2))
93  static inline real Dsn(real x, real y, real sx, real sy) {
94  // sx = x/hyp(x)
95  real t = x * y;
96  return t > 0 ? (x + y) * Math::sq( (sx * sy)/t ) / (sx + sy) :
97  (x - y != 0 ? (sx - sy) / (x - y) : 1);
98  }
99  // Dlog1p(x,y) = log1p((x-y)/(1+y))/(x-y)
100  static inline real Dlog1p(real x, real y) {
101  real t = x - y; if (t < 0) { t = -t; y = x; }
102  return t ? Math::log1p(t / (1 + y)) / t : 1 / (1 + x);
103  }
104  // Dexp(x,y) = exp((x+y)/2) * 2*sinh((x-y)/2)/(x-y)
105  static inline real Dexp(real x, real y) {
106  using std::sinh; using std::exp;
107  real t = (x - y)/2;
108  return (t ? sinh(t)/t : 1) * exp((x + y)/2);
109  }
110  // Dsinh(x,y) = 2*sinh((x-y)/2)/(x-y) * cosh((x+y)/2)
111  // cosh((x+y)/2) = (c+sinh(x)*sinh(y)/c)/2
112  // c=sqrt((1+cosh(x))*(1+cosh(y)))
113  // cosh((x+y)/2) = sqrt( (sinh(x)*sinh(y) + cosh(x)*cosh(y) + 1)/2 )
114  static inline real Dsinh(real x, real y, real sx, real sy, real cx, real cy)
115  // sx = sinh(x), cx = cosh(x)
116  {
117  // real t = (x - y)/2, c = sqrt((1 + cx) * (1 + cy));
118  // return (t ? sinh(t)/t : real(1)) * (c + sx * sy / c) /2;
119  using std::sinh; using std::sqrt;
120  real t = (x - y)/2;
121  return (t ? sinh(t)/t : 1) * sqrt((sx * sy + cx * cy + 1) /2);
122  }
123  // Dasinh(x,y) = asinh((x-y)*(x+y)/(x*sqrt(1+y^2)+y*sqrt(1+x^2)))/(x-y)
124  // = asinh((x*sqrt(1+y^2)-y*sqrt(1+x^2)))/(x-y)
125  static inline real Dasinh(real x, real y, real hx, real hy) {
126  // hx = hyp(x)
127  real t = x - y;
128  return t ?
129  Math::asinh(x*y > 0 ? t * (x+y) / (x*hy + y*hx) : x*hy - y*hx) / t :
130  1/hx;
131  }
132  // Deatanhe(x,y) = eatanhe((x-y)/(1-e^2*x*y))/(x-y)
133  inline real Deatanhe(real x, real y) const {
134  real t = x - y, d = 1 - _e2 * x * y;
135  return t ? eatanhe(t / d) / t : _e2 / d;
136  }
137  void Init(real sphi1, real cphi1, real sphi2, real cphi2, real k1);
138  public:
139 
140  /**
141  * Constructor with a single standard parallel.
142  *
143  * @param[in] a equatorial radius of ellipsoid (meters).
144  * @param[in] f flattening of ellipsoid. Setting \e f = 0 gives a sphere.
145  * Negative \e f gives a prolate ellipsoid. If \e f &gt; 1, set
146  * flattening to 1/\e f.
147  * @param[in] stdlat standard parallel (degrees), the circle of tangency.
148  * @param[in] k0 scale on the standard parallel.
149  * @exception GeographicErr if \e a, (1 &minus; \e f) \e a, or \e k0 is
150  * not positive.
151  * @exception GeographicErr if \e stdlat is not in [&minus;90&deg;,
152  * 90&deg;].
153  **********************************************************************/
154  LambertConformalConic(real a, real f, real stdlat, real k0);
155 
156  /**
157  * Constructor with two standard parallels.
158  *
159  * @param[in] a equatorial radius of ellipsoid (meters).
160  * @param[in] f flattening of ellipsoid. Setting \e f = 0 gives a sphere.
161  * Negative \e f gives a prolate ellipsoid. If \e f &gt; 1, set
162  * flattening to 1/\e f.
163  * @param[in] stdlat1 first standard parallel (degrees).
164  * @param[in] stdlat2 second standard parallel (degrees).
165  * @param[in] k1 scale on the standard parallels.
166  * @exception GeographicErr if \e a, (1 &minus; \e f) \e a, or \e k1 is
167  * not positive.
168  * @exception GeographicErr if \e stdlat1 or \e stdlat2 is not in
169  * [&minus;90&deg;, 90&deg;], or if either \e stdlat1 or \e
170  * stdlat2 is a pole and \e stdlat1 is not equal \e stdlat2.
171  **********************************************************************/
172  LambertConformalConic(real a, real f, real stdlat1, real stdlat2, real k1);
173 
174  /**
175  * Constructor with two standard parallels specified by sines and cosines.
176  *
177  * @param[in] a equatorial radius of ellipsoid (meters).
178  * @param[in] f flattening of ellipsoid. Setting \e f = 0 gives a sphere.
179  * Negative \e f gives a prolate ellipsoid. If \e f &gt; 1, set
180  * flattening to 1/\e f.
181  * @param[in] sinlat1 sine of first standard parallel.
182  * @param[in] coslat1 cosine of first standard parallel.
183  * @param[in] sinlat2 sine of second standard parallel.
184  * @param[in] coslat2 cosine of second standard parallel.
185  * @param[in] k1 scale on the standard parallels.
186  * @exception GeographicErr if \e a, (1 &minus; \e f) \e a, or \e k1 is
187  * not positive.
188  * @exception GeographicErr if \e stdlat1 or \e stdlat2 is not in
189  * [&minus;90&deg;, 90&deg;], or if either \e stdlat1 or \e
190  * stdlat2 is a pole and \e stdlat1 is not equal \e stdlat2.
191  *
192  * This allows parallels close to the poles to be specified accurately.
193  * This routine computes the latitude of origin and the scale at this
194  * latitude. In the case where \e lat1 and \e lat2 are different, the
195  * errors in this routines are as follows: if \e dlat = abs(\e lat2 &minus;
196  * \e lat1) &le; 160&deg; and max(abs(\e lat1), abs(\e lat2)) &le; 90
197  * &minus; min(0.0002, 2.2 &times; 10<sup>&minus;6</sup>(180 &minus; \e
198  * dlat), 6 &times 10<sup>&minus;8</sup> <i>dlat</i><sup>2</sup>) (in
199  * degrees), then the error in the latitude of origin is less than 4.5
200  * &times; 10<sup>&minus;14</sup>d and the relative error in the scale is
201  * less than 7 &times; 10<sup>&minus;15</sup>.
202  **********************************************************************/
203  LambertConformalConic(real a, real f,
204  real sinlat1, real coslat1,
205  real sinlat2, real coslat2,
206  real k1);
207 
208  /**
209  * Set the scale for the projection.
210  *
211  * @param[in] lat (degrees).
212  * @param[in] k scale at latitude \e lat (default 1).
213  * @exception GeographicErr \e k is not positive.
214  * @exception GeographicErr if \e lat is not in [&minus;90&deg;,
215  * 90&deg;].
216  **********************************************************************/
217  void SetScale(real lat, real k = real(1));
218 
219  /**
220  * Forward projection, from geographic to Lambert conformal conic.
221  *
222  * @param[in] lon0 central meridian longitude (degrees).
223  * @param[in] lat latitude of point (degrees).
224  * @param[in] lon longitude of point (degrees).
225  * @param[out] x easting of point (meters).
226  * @param[out] y northing of point (meters).
227  * @param[out] gamma meridian convergence at point (degrees).
228  * @param[out] k scale of projection at point.
229  *
230  * The latitude origin is given by LambertConformalConic::LatitudeOrigin().
231  * No false easting or northing is added and \e lat should be in the range
232  * [&minus;90&deg;, 90&deg;]; \e lon and \e lon0 should be in the
233  * range [&minus;540&deg;, 540&deg;). The error in the projection
234  * is less than about 10 nm (10 nanometers), true distance, and the errors
235  * in the meridian convergence and scale are consistent with this. The
236  * values of \e x and \e y returned for points which project to infinity
237  * (i.e., one or both of the poles) will be large but finite.
238  **********************************************************************/
239  void Forward(real lon0, real lat, real lon,
240  real& x, real& y, real& gamma, real& k) const;
241 
242  /**
243  * Reverse projection, from Lambert conformal conic to geographic.
244  *
245  * @param[in] lon0 central meridian longitude (degrees).
246  * @param[in] x easting of point (meters).
247  * @param[in] y northing of point (meters).
248  * @param[out] lat latitude of point (degrees).
249  * @param[out] lon longitude of point (degrees).
250  * @param[out] gamma meridian convergence at point (degrees).
251  * @param[out] k scale of projection at point.
252  *
253  * The latitude origin is given by LambertConformalConic::LatitudeOrigin().
254  * No false easting or northing is added. \e lon0 should be in the range
255  * [&minus;540&deg;, 540&deg;). The value of \e lon returned is in
256  * the range [&minus;180&deg;, 180&deg;). The error in the
257  * projection is less than about 10 nm (10 nanometers), true distance, and
258  * the errors in the meridian convergence and scale are consistent with
259  * this.
260  **********************************************************************/
261  void Reverse(real lon0, real x, real y,
262  real& lat, real& lon, real& gamma, real& k) const;
263 
264  /**
265  * LambertConformalConic::Forward without returning the convergence and
266  * scale.
267  **********************************************************************/
268  void Forward(real lon0, real lat, real lon,
269  real& x, real& y) const {
270  real gamma, k;
271  Forward(lon0, lat, lon, x, y, gamma, k);
272  }
273 
274  /**
275  * LambertConformalConic::Reverse without returning the convergence and
276  * scale.
277  **********************************************************************/
278  void Reverse(real lon0, real x, real y,
279  real& lat, real& lon) const {
280  real gamma, k;
281  Reverse(lon0, x, y, lat, lon, gamma, k);
282  }
283 
284  /** \name Inspector functions
285  **********************************************************************/
286  ///@{
287  /**
288  * @return \e a the equatorial radius of the ellipsoid (meters). This is
289  * the value used in the constructor.
290  **********************************************************************/
291  Math::real MajorRadius() const { return _a; }
292 
293  /**
294  * @return \e f the flattening of the ellipsoid. This is the
295  * value used in the constructor.
296  **********************************************************************/
297  Math::real Flattening() const { return _f; }
298 
299  /// \cond SKIP
300  /**
301  * <b>DEPRECATED</b>
302  * @return \e r the inverse flattening of the ellipsoid.
303  **********************************************************************/
304  Math::real InverseFlattening() const { return 1/_f; }
305  /// \endcond
306 
307  /**
308  * @return latitude of the origin for the projection (degrees).
309  *
310  * This is the latitude of minimum scale and equals the \e stdlat in the
311  * 1-parallel constructor and lies between \e stdlat1 and \e stdlat2 in the
312  * 2-parallel constructors.
313  **********************************************************************/
314  Math::real OriginLatitude() const { return _lat0; }
315 
316  /**
317  * @return central scale for the projection. This is the scale on the
318  * latitude of origin.
319  **********************************************************************/
320  Math::real CentralScale() const { return _k0; }
321  ///@}
322 
323  /**
324  * A global instantiation of LambertConformalConic with the WGS84
325  * ellipsoid, \e stdlat = 0, and \e k0 = 1. This degenerates to the
326  * Mercator projection.
327  **********************************************************************/
328  static const LambertConformalConic& Mercator();
329  };
330 
331 } // namespace GeographicLib
332 
333 #endif // GEOGRAPHICLIB_LAMBERTCONFORMALCONIC_HPP
#define GEOGRAPHICLIB_EXPORT
Definition: Constants.hpp:69
void Reverse(real lon0, real x, real y, real &lat, real &lon) const
GeographicLib::Math::real real
Definition: GeodSolve.cpp:32
Lambert conformal conic projection.
static T atanh(T x)
Definition: Math.hpp:340
static T asinh(T x)
Definition: Math.hpp:323
void Forward(real lon0, real lat, real lon, real &x, real &y) const
static T hypot(T x, T y)
Definition: Math.hpp:255
static T sq(T x)
Definition: Math.hpp:244
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
static T log1p(T x)
Definition: Math.hpp:300
Header for GeographicLib::Constants class.