GeographicLib  1.48
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-2016) <charles@karney.com> and licensed
6  * under the MIT/X11 License. For more information, see
7  * https://geographiclib.sourceforge.io/
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://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.
140  * @param[in] stdlat standard parallel (degrees), the circle of tangency.
141  * @param[in] k0 scale on the standard parallel.
142  * @exception GeographicErr if \e a, (1 &minus; \e f) \e a, or \e k0 is
143  * not positive.
144  * @exception GeographicErr if \e stdlat is not in [&minus;90&deg;,
145  * 90&deg;].
146  **********************************************************************/
147  LambertConformalConic(real a, real f, real stdlat, real k0);
148 
149  /**
150  * Constructor with two standard parallels.
151  *
152  * @param[in] a equatorial radius of ellipsoid (meters).
153  * @param[in] f flattening of ellipsoid. Setting \e f = 0 gives a sphere.
154  * Negative \e f gives a prolate ellipsoid.
155  * @param[in] stdlat1 first standard parallel (degrees).
156  * @param[in] stdlat2 second standard parallel (degrees).
157  * @param[in] k1 scale on the standard parallels.
158  * @exception GeographicErr if \e a, (1 &minus; \e f) \e a, or \e k1 is
159  * not positive.
160  * @exception GeographicErr if \e stdlat1 or \e stdlat2 is not in
161  * [&minus;90&deg;, 90&deg;], or if either \e stdlat1 or \e
162  * stdlat2 is a pole and \e stdlat1 is not equal \e stdlat2.
163  **********************************************************************/
164  LambertConformalConic(real a, real f, real stdlat1, real stdlat2, real k1);
165 
166  /**
167  * Constructor with two standard parallels specified by sines and cosines.
168  *
169  * @param[in] a equatorial radius of ellipsoid (meters).
170  * @param[in] f flattening of ellipsoid. Setting \e f = 0 gives a sphere.
171  * Negative \e f gives a prolate ellipsoid.
172  * @param[in] sinlat1 sine of first standard parallel.
173  * @param[in] coslat1 cosine of first standard parallel.
174  * @param[in] sinlat2 sine of second standard parallel.
175  * @param[in] coslat2 cosine of second standard parallel.
176  * @param[in] k1 scale on the standard parallels.
177  * @exception GeographicErr if \e a, (1 &minus; \e f) \e a, or \e k1 is
178  * not positive.
179  * @exception GeographicErr if \e stdlat1 or \e stdlat2 is not in
180  * [&minus;90&deg;, 90&deg;], or if either \e stdlat1 or \e
181  * stdlat2 is a pole and \e stdlat1 is not equal \e stdlat2.
182  *
183  * This allows parallels close to the poles to be specified accurately.
184  * This routine computes the latitude of origin and the scale at this
185  * latitude. In the case where \e lat1 and \e lat2 are different, the
186  * errors in this routines are as follows: if \e dlat = abs(\e lat2 &minus;
187  * \e lat1) &le; 160&deg; and max(abs(\e lat1), abs(\e lat2)) &le; 90
188  * &minus; min(0.0002, 2.2 &times; 10<sup>&minus;6</sup>(180 &minus; \e
189  * dlat), 6 &times 10<sup>&minus;8</sup> <i>dlat</i><sup>2</sup>) (in
190  * degrees), then the error in the latitude of origin is less than 4.5
191  * &times; 10<sup>&minus;14</sup>d and the relative error in the scale is
192  * less than 7 &times; 10<sup>&minus;15</sup>.
193  **********************************************************************/
194  LambertConformalConic(real a, real f,
195  real sinlat1, real coslat1,
196  real sinlat2, real coslat2,
197  real k1);
198 
199  /**
200  * Set the scale for the projection.
201  *
202  * @param[in] lat (degrees).
203  * @param[in] k scale at latitude \e lat (default 1).
204  * @exception GeographicErr \e k is not positive.
205  * @exception GeographicErr if \e lat is not in [&minus;90&deg;,
206  * 90&deg;].
207  **********************************************************************/
208  void SetScale(real lat, real k = real(1));
209 
210  /**
211  * Forward projection, from geographic to Lambert conformal conic.
212  *
213  * @param[in] lon0 central meridian longitude (degrees).
214  * @param[in] lat latitude of point (degrees).
215  * @param[in] lon longitude of point (degrees).
216  * @param[out] x easting of point (meters).
217  * @param[out] y northing of point (meters).
218  * @param[out] gamma meridian convergence at point (degrees).
219  * @param[out] k scale of projection at point.
220  *
221  * The latitude origin is given by LambertConformalConic::LatitudeOrigin().
222  * No false easting or northing is added and \e lat should be in the range
223  * [&minus;90&deg;, 90&deg;]. The error in the projection is less than
224  * about 10 nm (10 nanometers), true distance, and the errors in the
225  * meridian convergence and scale are consistent with this. The values of
226  * \e x and \e y returned for points which project to infinity (i.e., one
227  * or both of the poles) will be large but finite.
228  **********************************************************************/
229  void Forward(real lon0, real lat, real lon,
230  real& x, real& y, real& gamma, real& k) const;
231 
232  /**
233  * Reverse projection, from Lambert conformal conic to geographic.
234  *
235  * @param[in] lon0 central meridian longitude (degrees).
236  * @param[in] x easting of point (meters).
237  * @param[in] y northing of point (meters).
238  * @param[out] lat latitude of point (degrees).
239  * @param[out] lon longitude of point (degrees).
240  * @param[out] gamma meridian convergence at point (degrees).
241  * @param[out] k scale of projection at point.
242  *
243  * The latitude origin is given by LambertConformalConic::LatitudeOrigin().
244  * No false easting or northing is added. The value of \e lon returned is
245  * in the range [&minus;180&deg;, 180&deg;]. The error in the projection
246  * is less than about 10 nm (10 nanometers), true distance, and the errors
247  * in the meridian convergence and scale are consistent with this.
248  **********************************************************************/
249  void Reverse(real lon0, real x, real y,
250  real& lat, real& lon, real& gamma, real& k) const;
251 
252  /**
253  * LambertConformalConic::Forward without returning the convergence and
254  * scale.
255  **********************************************************************/
256  void Forward(real lon0, real lat, real lon,
257  real& x, real& y) const {
258  real gamma, k;
259  Forward(lon0, lat, lon, x, y, gamma, k);
260  }
261 
262  /**
263  * LambertConformalConic::Reverse without returning the convergence and
264  * scale.
265  **********************************************************************/
266  void Reverse(real lon0, real x, real y,
267  real& lat, real& lon) const {
268  real gamma, k;
269  Reverse(lon0, x, y, lat, lon, gamma, k);
270  }
271 
272  /** \name Inspector functions
273  **********************************************************************/
274  ///@{
275  /**
276  * @return \e a the equatorial radius of the ellipsoid (meters). This is
277  * the value used in the constructor.
278  **********************************************************************/
279  Math::real MajorRadius() const { return _a; }
280 
281  /**
282  * @return \e f the flattening of the ellipsoid. This is the
283  * value used in the constructor.
284  **********************************************************************/
285  Math::real Flattening() const { return _f; }
286 
287  /**
288  * @return latitude of the origin for the projection (degrees).
289  *
290  * This is the latitude of minimum scale and equals the \e stdlat in the
291  * 1-parallel constructor and lies between \e stdlat1 and \e stdlat2 in the
292  * 2-parallel constructors.
293  **********************************************************************/
294  Math::real OriginLatitude() const { return _lat0; }
295 
296  /**
297  * @return central scale for the projection. This is the scale on the
298  * latitude of origin.
299  **********************************************************************/
300  Math::real CentralScale() const { return _k0; }
301  ///@}
302 
303  /**
304  * A global instantiation of LambertConformalConic with the WGS84
305  * ellipsoid, \e stdlat = 0, and \e k0 = 1. This degenerates to the
306  * Mercator projection.
307  **********************************************************************/
308  static const LambertConformalConic& Mercator();
309  };
310 
311 } // namespace GeographicLib
312 
313 #endif // GEOGRAPHICLIB_LAMBERTCONFORMALCONIC_HPP
#define GEOGRAPHICLIB_EXPORT
Definition: Constants.hpp:91
GeographicLib::Math::real real
Definition: GeodSolve.cpp:31
void Forward(real lon0, real lat, real lon, real &x, real &y) const
Lambert conformal conic projection.
static T asinh(T x)
Definition: Math.hpp:311
static T hypot(T x, T y)
Definition: Math.hpp:243
static T sq(T x)
Definition: Math.hpp:232
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
static T log1p(T x)
Definition: Math.hpp:288
Header for GeographicLib::Constants class.
static T eatanhe(T x, T es)
Definition: Math.cpp:21
void Reverse(real lon0, real x, real y, real &lat, real &lon) const