GeographicLib  1.44
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-2015) <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_, ahypover_;
61  real _a, _f, _fm, _e2, _es;
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  // Divided differences
67  // Definition: Df(x,y) = (f(x)-f(y))/(x-y)
68  // See:
69  // W. M. Kahan and R. J. Fateman,
70  // Symbolic computation of divided differences,
71  // SIGSAM Bull. 33(3), 7-28 (1999)
72  // https://dx.doi.org/10.1145/334714.334716
73  // http://www.cs.berkeley.edu/~fateman/papers/divdiff.pdf
74  //
75  // General rules
76  // h(x) = f(g(x)): Dh(x,y) = Df(g(x),g(y))*Dg(x,y)
77  // h(x) = f(x)*g(x):
78  // Dh(x,y) = Df(x,y)*g(x) + Dg(x,y)*f(y)
79  // = Df(x,y)*g(y) + Dg(x,y)*f(x)
80  // = Df(x,y)*(g(x)+g(y))/2 + Dg(x,y)*(f(x)+f(y))/2
81  //
82  // hyp(x) = sqrt(1+x^2): Dhyp(x,y) = (x+y)/(hyp(x)+hyp(y))
83  static inline real Dhyp(real x, real y, real hx, real hy)
84  // hx = hyp(x)
85  { return (x + y) / (hx + hy); }
86  // sn(x) = x/sqrt(1+x^2): Dsn(x,y) = (x+y)/((sn(x)+sn(y))*(1+x^2)*(1+y^2))
87  static inline real Dsn(real x, real y, real sx, real sy) {
88  // sx = x/hyp(x)
89  real t = x * y;
90  return t > 0 ? (x + y) * Math::sq( (sx * sy)/t ) / (sx + sy) :
91  (x - y != 0 ? (sx - sy) / (x - y) : 1);
92  }
93  // Dlog1p(x,y) = log1p((x-y)/(1+y))/(x-y)
94  static inline real Dlog1p(real x, real y) {
95  real t = x - y; if (t < 0) { t = -t; y = x; }
96  return t ? Math::log1p(t / (1 + y)) / t : 1 / (1 + x);
97  }
98  // Dexp(x,y) = exp((x+y)/2) * 2*sinh((x-y)/2)/(x-y)
99  static inline real Dexp(real x, real y) {
100  using std::sinh; using std::exp;
101  real t = (x - y)/2;
102  return (t ? sinh(t)/t : 1) * exp((x + y)/2);
103  }
104  // Dsinh(x,y) = 2*sinh((x-y)/2)/(x-y) * cosh((x+y)/2)
105  // cosh((x+y)/2) = (c+sinh(x)*sinh(y)/c)/2
106  // c=sqrt((1+cosh(x))*(1+cosh(y)))
107  // cosh((x+y)/2) = sqrt( (sinh(x)*sinh(y) + cosh(x)*cosh(y) + 1)/2 )
108  static inline real Dsinh(real x, real y, real sx, real sy, real cx, real cy)
109  // sx = sinh(x), cx = cosh(x)
110  {
111  // real t = (x - y)/2, c = sqrt((1 + cx) * (1 + cy));
112  // return (t ? sinh(t)/t : real(1)) * (c + sx * sy / c) /2;
113  using std::sinh; using std::sqrt;
114  real t = (x - y)/2;
115  return (t ? sinh(t)/t : 1) * sqrt((sx * sy + cx * cy + 1) /2);
116  }
117  // Dasinh(x,y) = asinh((x-y)*(x+y)/(x*sqrt(1+y^2)+y*sqrt(1+x^2)))/(x-y)
118  // = asinh((x*sqrt(1+y^2)-y*sqrt(1+x^2)))/(x-y)
119  static inline real Dasinh(real x, real y, real hx, real hy) {
120  // hx = hyp(x)
121  real t = x - y;
122  return t ?
123  Math::asinh(x*y > 0 ? t * (x+y) / (x*hy + y*hx) : x*hy - y*hx) / t :
124  1/hx;
125  }
126  // Deatanhe(x,y) = eatanhe((x-y)/(1-e^2*x*y))/(x-y)
127  inline real Deatanhe(real x, real y) const {
128  real t = x - y, d = 1 - _e2 * x * y;
129  return t ? Math::eatanhe(t / d, _es) / t : _e2 / d;
130  }
131  void Init(real sphi1, real cphi1, real sphi2, real cphi2, real k1);
132  public:
133 
134  /**
135  * Constructor with a single standard parallel.
136  *
137  * @param[in] a equatorial radius of ellipsoid (meters).
138  * @param[in] f flattening of ellipsoid. Setting \e f = 0 gives a sphere.
139  * Negative \e f gives a prolate ellipsoid. If \e f &gt; 1, set
140  * flattening to 1/\e f.
141  * @param[in] stdlat standard parallel (degrees), the circle of tangency.
142  * @param[in] k0 scale on the standard parallel.
143  * @exception GeographicErr if \e a, (1 &minus; \e f) \e a, or \e k0 is
144  * not positive.
145  * @exception GeographicErr if \e stdlat is not in [&minus;90&deg;,
146  * 90&deg;].
147  **********************************************************************/
148  LambertConformalConic(real a, real f, real stdlat, real k0);
149 
150  /**
151  * Constructor with two standard parallels.
152  *
153  * @param[in] a equatorial radius of ellipsoid (meters).
154  * @param[in] f flattening of ellipsoid. Setting \e f = 0 gives a sphere.
155  * Negative \e f gives a prolate ellipsoid. If \e f &gt; 1, set
156  * flattening to 1/\e f.
157  * @param[in] stdlat1 first standard parallel (degrees).
158  * @param[in] stdlat2 second standard parallel (degrees).
159  * @param[in] k1 scale on the standard parallels.
160  * @exception GeographicErr if \e a, (1 &minus; \e f) \e a, or \e k1 is
161  * not positive.
162  * @exception GeographicErr if \e stdlat1 or \e stdlat2 is not in
163  * [&minus;90&deg;, 90&deg;], or if either \e stdlat1 or \e
164  * stdlat2 is a pole and \e stdlat1 is not equal \e stdlat2.
165  **********************************************************************/
166  LambertConformalConic(real a, real f, real stdlat1, real stdlat2, real k1);
167 
168  /**
169  * Constructor with two standard parallels specified by sines and cosines.
170  *
171  * @param[in] a equatorial radius of ellipsoid (meters).
172  * @param[in] f flattening of ellipsoid. Setting \e f = 0 gives a sphere.
173  * Negative \e f gives a prolate ellipsoid. If \e f &gt; 1, set
174  * flattening to 1/\e f.
175  * @param[in] sinlat1 sine of first standard parallel.
176  * @param[in] coslat1 cosine of first standard parallel.
177  * @param[in] sinlat2 sine of second standard parallel.
178  * @param[in] coslat2 cosine of second standard parallel.
179  * @param[in] k1 scale on the standard parallels.
180  * @exception GeographicErr if \e a, (1 &minus; \e f) \e a, or \e k1 is
181  * not positive.
182  * @exception GeographicErr if \e stdlat1 or \e stdlat2 is not in
183  * [&minus;90&deg;, 90&deg;], or if either \e stdlat1 or \e
184  * stdlat2 is a pole and \e stdlat1 is not equal \e stdlat2.
185  *
186  * This allows parallels close to the poles to be specified accurately.
187  * This routine computes the latitude of origin and the scale at this
188  * latitude. In the case where \e lat1 and \e lat2 are different, the
189  * errors in this routines are as follows: if \e dlat = abs(\e lat2 &minus;
190  * \e lat1) &le; 160&deg; and max(abs(\e lat1), abs(\e lat2)) &le; 90
191  * &minus; min(0.0002, 2.2 &times; 10<sup>&minus;6</sup>(180 &minus; \e
192  * dlat), 6 &times 10<sup>&minus;8</sup> <i>dlat</i><sup>2</sup>) (in
193  * degrees), then the error in the latitude of origin is less than 4.5
194  * &times; 10<sup>&minus;14</sup>d and the relative error in the scale is
195  * less than 7 &times; 10<sup>&minus;15</sup>.
196  **********************************************************************/
197  LambertConformalConic(real a, real f,
198  real sinlat1, real coslat1,
199  real sinlat2, real coslat2,
200  real k1);
201 
202  /**
203  * Set the scale for the projection.
204  *
205  * @param[in] lat (degrees).
206  * @param[in] k scale at latitude \e lat (default 1).
207  * @exception GeographicErr \e k is not positive.
208  * @exception GeographicErr if \e lat is not in [&minus;90&deg;,
209  * 90&deg;].
210  **********************************************************************/
211  void SetScale(real lat, real k = real(1));
212 
213  /**
214  * Forward projection, from geographic to Lambert conformal conic.
215  *
216  * @param[in] lon0 central meridian longitude (degrees).
217  * @param[in] lat latitude of point (degrees).
218  * @param[in] lon longitude of point (degrees).
219  * @param[out] x easting of point (meters).
220  * @param[out] y northing of point (meters).
221  * @param[out] gamma meridian convergence at point (degrees).
222  * @param[out] k scale of projection at point.
223  *
224  * The latitude origin is given by LambertConformalConic::LatitudeOrigin().
225  * No false easting or northing is added and \e lat should be in the range
226  * [&minus;90&deg;, 90&deg;]. The error in the projection is less than
227  * about 10 nm (10 nanometers), true distance, and the errors in the
228  * meridian convergence and scale are consistent with this. The values of
229  * \e x and \e y returned for points which project to infinity (i.e., one
230  * or both of the poles) will be large but finite.
231  **********************************************************************/
232  void Forward(real lon0, real lat, real lon,
233  real& x, real& y, real& gamma, real& k) const;
234 
235  /**
236  * Reverse projection, from Lambert conformal conic to geographic.
237  *
238  * @param[in] lon0 central meridian longitude (degrees).
239  * @param[in] x easting of point (meters).
240  * @param[in] y northing of point (meters).
241  * @param[out] lat latitude of point (degrees).
242  * @param[out] lon longitude of point (degrees).
243  * @param[out] gamma meridian convergence at point (degrees).
244  * @param[out] k scale of projection at point.
245  *
246  * The latitude origin is given by LambertConformalConic::LatitudeOrigin().
247  * No false easting or northing is added. The value of \e lon returned is
248  * in the range [&minus;180&deg;, 180&deg;). The error in the projection
249  * is less than about 10 nm (10 nanometers), true distance, and the errors
250  * in the meridian convergence and scale are consistent with this.
251  **********************************************************************/
252  void Reverse(real lon0, real x, real y,
253  real& lat, real& lon, real& gamma, real& k) const;
254 
255  /**
256  * LambertConformalConic::Forward without returning the convergence and
257  * scale.
258  **********************************************************************/
259  void Forward(real lon0, real lat, real lon,
260  real& x, real& y) const {
261  real gamma, k;
262  Forward(lon0, lat, lon, x, y, gamma, k);
263  }
264 
265  /**
266  * LambertConformalConic::Reverse without returning the convergence and
267  * scale.
268  **********************************************************************/
269  void Reverse(real lon0, real x, real y,
270  real& lat, real& lon) const {
271  real gamma, k;
272  Reverse(lon0, x, y, lat, lon, gamma, k);
273  }
274 
275  /** \name Inspector functions
276  **********************************************************************/
277  ///@{
278  /**
279  * @return \e a the equatorial radius of the ellipsoid (meters). This is
280  * the value used in the constructor.
281  **********************************************************************/
282  Math::real MajorRadius() const { return _a; }
283 
284  /**
285  * @return \e f the flattening of the ellipsoid. This is the
286  * value used in the constructor.
287  **********************************************************************/
288  Math::real Flattening() const { return _f; }
289 
290  /// \cond SKIP
291  /**
292  * <b>DEPRECATED</b>
293  * @return \e r the inverse flattening of the ellipsoid.
294  **********************************************************************/
295  Math::real InverseFlattening() const { return 1/_f; }
296  /// \endcond
297 
298  /**
299  * @return latitude of the origin for the projection (degrees).
300  *
301  * This is the latitude of minimum scale and equals the \e stdlat in the
302  * 1-parallel constructor and lies between \e stdlat1 and \e stdlat2 in the
303  * 2-parallel constructors.
304  **********************************************************************/
305  Math::real OriginLatitude() const { return _lat0; }
306 
307  /**
308  * @return central scale for the projection. This is the scale on the
309  * latitude of origin.
310  **********************************************************************/
311  Math::real CentralScale() const { return _k0; }
312  ///@}
313 
314  /**
315  * A global instantiation of LambertConformalConic with the WGS84
316  * ellipsoid, \e stdlat = 0, and \e k0 = 1. This degenerates to the
317  * Mercator projection.
318  **********************************************************************/
319  static const LambertConformalConic& Mercator();
320  };
321 
322 } // namespace GeographicLib
323 
324 #endif // GEOGRAPHICLIB_LAMBERTCONFORMALCONIC_HPP
#define GEOGRAPHICLIB_EXPORT
Definition: Constants.hpp:90
void Reverse(real lon0, real x, real y, real &lat, real &lon) const
GeographicLib::Math::real real
Definition: GeodSolve.cpp:32
static T eatanhe(T x, T es)
Lambert conformal conic projection.
static T asinh(T x)
Definition: Math.hpp:325
void Forward(real lon0, real lat, real lon, real &x, real &y) const
static T hypot(T x, T y)
Definition: Math.hpp:257
static T sq(T x)
Definition: Math.hpp:246
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
static T log1p(T x)
Definition: Math.hpp:302
Header for GeographicLib::Constants class.