GeographicLib  1.50.1
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-2019) <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.
46  *
47  * This class also returns the meridian convergence \e gamma and scale \e k.
48  * The meridian convergence is the bearing of grid north (the \e y axis)
49  * measured clockwise from true north.
50  *
51  * There is no provision in this
52  * class for specifying a false easting or false northing or a different
53  * latitude of origin. However these are can be simply included by the
54  * calling function. For example the Pennsylvania South state coordinate
55  * system (<a href="https://www.spatialreference.org/ref/epsg/3364/">
56  * EPSG:3364</a>) is obtained by:
57  * \include example-LambertConformalConic.cpp
58  *
59  * <a href="ConicProj.1.html">ConicProj</a> is a command-line utility
60  * providing access to the functionality of LambertConformalConic and
61  * AlbersEqualArea.
62  **********************************************************************/
64  private:
65  typedef Math::real real;
66  real eps_, epsx_, ahypover_;
67  real _a, _f, _fm, _e2, _es;
68  real _sign, _n, _nc, _t0nm1, _scale, _lat0, _k0;
69  real _scbet0, _tchi0, _scchi0, _psi0, _nrho0, _drhomax;
70  static const int numit_ = 5;
71  static real hyp(real x) { return Math::hypot(real(1), x); }
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://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 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 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 real Dlog1p(real x, real y) {
101  real t = x - y; if (t < 0) { t = -t; y = x; }
102  return t != 0 ? 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 real Dexp(real x, real y) {
106  using std::sinh; using std::exp;
107  real t = (x - y)/2;
108  return (t != 0 ? 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 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 != 0 ? 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 real Dasinh(real x, real y, real hx, real hy) {
126  // hx = hyp(x)
127  real t = x - y;
128  return t != 0 ?
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  real Deatanhe(real x, real y) const {
134  real t = x - y, d = 1 - _e2 * x * y;
135  return t != 0 ? Math::eatanhe(t / d, _es) / 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.
146  * @param[in] stdlat standard parallel (degrees), the circle of tangency.
147  * @param[in] k0 scale on the standard parallel.
148  * @exception GeographicErr if \e a, (1 &minus; \e f) \e a, or \e k0 is
149  * not positive.
150  * @exception GeographicErr if \e stdlat is not in [&minus;90&deg;,
151  * 90&deg;].
152  **********************************************************************/
153  LambertConformalConic(real a, real f, real stdlat, real k0);
154 
155  /**
156  * Constructor with two standard parallels.
157  *
158  * @param[in] a equatorial radius of ellipsoid (meters).
159  * @param[in] f flattening of ellipsoid. Setting \e f = 0 gives a sphere.
160  * Negative \e f gives a prolate ellipsoid.
161  * @param[in] stdlat1 first standard parallel (degrees).
162  * @param[in] stdlat2 second standard parallel (degrees).
163  * @param[in] k1 scale on the standard parallels.
164  * @exception GeographicErr if \e a, (1 &minus; \e f) \e a, or \e k1 is
165  * not positive.
166  * @exception GeographicErr if \e stdlat1 or \e stdlat2 is not in
167  * [&minus;90&deg;, 90&deg;], or if either \e stdlat1 or \e
168  * stdlat2 is a pole and \e stdlat1 is not equal \e stdlat2.
169  **********************************************************************/
170  LambertConformalConic(real a, real f, real stdlat1, real stdlat2, real k1);
171 
172  /**
173  * Constructor with two standard parallels specified by sines and cosines.
174  *
175  * @param[in] a equatorial radius of ellipsoid (meters).
176  * @param[in] f flattening of ellipsoid. Setting \e f = 0 gives a sphere.
177  * Negative \e f gives a prolate ellipsoid.
178  * @param[in] sinlat1 sine of first standard parallel.
179  * @param[in] coslat1 cosine of first standard parallel.
180  * @param[in] sinlat2 sine of second standard parallel.
181  * @param[in] coslat2 cosine of second standard parallel.
182  * @param[in] k1 scale on the standard parallels.
183  * @exception GeographicErr if \e a, (1 &minus; \e f) \e a, or \e k1 is
184  * not positive.
185  * @exception GeographicErr if \e stdlat1 or \e stdlat2 is not in
186  * [&minus;90&deg;, 90&deg;], or if either \e stdlat1 or \e
187  * stdlat2 is a pole and \e stdlat1 is not equal \e stdlat2.
188  *
189  * This allows parallels close to the poles to be specified accurately.
190  * This routine computes the latitude of origin and the scale at this
191  * latitude. In the case where \e lat1 and \e lat2 are different, the
192  * errors in this routines are as follows: if \e dlat = abs(\e lat2 &minus;
193  * \e lat1) &le; 160&deg; and max(abs(\e lat1), abs(\e lat2)) &le; 90
194  * &minus; min(0.0002, 2.2 &times; 10<sup>&minus;6</sup>(180 &minus; \e
195  * dlat), 6 &times 10<sup>&minus;8</sup> <i>dlat</i><sup>2</sup>) (in
196  * degrees), then the error in the latitude of origin is less than 4.5
197  * &times; 10<sup>&minus;14</sup>d and the relative error in the scale is
198  * less than 7 &times; 10<sup>&minus;15</sup>.
199  **********************************************************************/
200  LambertConformalConic(real a, real f,
201  real sinlat1, real coslat1,
202  real sinlat2, real coslat2,
203  real k1);
204 
205  /**
206  * Set the scale for the projection.
207  *
208  * @param[in] lat (degrees).
209  * @param[in] k scale at latitude \e lat (default 1).
210  * @exception GeographicErr \e k is not positive.
211  * @exception GeographicErr if \e lat is not in [&minus;90&deg;,
212  * 90&deg;].
213  **********************************************************************/
214  void SetScale(real lat, real k = real(1));
215 
216  /**
217  * Forward projection, from geographic to Lambert conformal conic.
218  *
219  * @param[in] lon0 central meridian longitude (degrees).
220  * @param[in] lat latitude of point (degrees).
221  * @param[in] lon longitude of point (degrees).
222  * @param[out] x easting of point (meters).
223  * @param[out] y northing of point (meters).
224  * @param[out] gamma meridian convergence at point (degrees).
225  * @param[out] k scale of projection at point.
226  *
227  * The latitude origin is given by LambertConformalConic::LatitudeOrigin().
228  * No false easting or northing is added and \e lat should be in the range
229  * [&minus;90&deg;, 90&deg;]. The error in the projection is less than
230  * about 10 nm (10 nanometers), true distance, and the errors in the
231  * meridian convergence and scale are consistent with this. The values of
232  * \e x and \e y returned for points which project to infinity (i.e., one
233  * or both of the poles) will be large but finite.
234  **********************************************************************/
235  void Forward(real lon0, real lat, real lon,
236  real& x, real& y, real& gamma, real& k) const;
237 
238  /**
239  * Reverse projection, from Lambert conformal conic to geographic.
240  *
241  * @param[in] lon0 central meridian longitude (degrees).
242  * @param[in] x easting of point (meters).
243  * @param[in] y northing of point (meters).
244  * @param[out] lat latitude of point (degrees).
245  * @param[out] lon longitude of point (degrees).
246  * @param[out] gamma meridian convergence at point (degrees).
247  * @param[out] k scale of projection at point.
248  *
249  * The latitude origin is given by LambertConformalConic::LatitudeOrigin().
250  * No false easting or northing is added. The value of \e lon returned is
251  * in the range [&minus;180&deg;, 180&deg;]. The error in the projection
252  * is less than about 10 nm (10 nanometers), true distance, and the errors
253  * in the meridian convergence and scale are consistent with this.
254  **********************************************************************/
255  void Reverse(real lon0, real x, real y,
256  real& lat, real& lon, real& gamma, real& k) const;
257 
258  /**
259  * LambertConformalConic::Forward without returning the convergence and
260  * scale.
261  **********************************************************************/
262  void Forward(real lon0, real lat, real lon,
263  real& x, real& y) const {
264  real gamma, k;
265  Forward(lon0, lat, lon, x, y, gamma, k);
266  }
267 
268  /**
269  * LambertConformalConic::Reverse without returning the convergence and
270  * scale.
271  **********************************************************************/
272  void Reverse(real lon0, real x, real y,
273  real& lat, real& lon) const {
274  real gamma, k;
275  Reverse(lon0, x, y, lat, lon, gamma, k);
276  }
277 
278  /** \name Inspector functions
279  **********************************************************************/
280  ///@{
281  /**
282  * @return \e a the equatorial radius of the ellipsoid (meters). This is
283  * the value used in the constructor.
284  **********************************************************************/
285  Math::real EquatorialRadius() const { return _a; }
286 
287  /**
288  * @return \e f the flattening of the ellipsoid. This is the
289  * value used in the constructor.
290  **********************************************************************/
291  Math::real Flattening() const { return _f; }
292 
293  /**
294  * @return latitude of the origin for the projection (degrees).
295  *
296  * This is the latitude of minimum scale and equals the \e stdlat in the
297  * 1-parallel constructor and lies between \e stdlat1 and \e stdlat2 in the
298  * 2-parallel constructors.
299  **********************************************************************/
300  Math::real OriginLatitude() const { return _lat0; }
301 
302  /**
303  * @return central scale for the projection. This is the scale on the
304  * latitude of origin.
305  **********************************************************************/
306  Math::real CentralScale() const { return _k0; }
307 
308  /**
309  * \deprecated An old name for EquatorialRadius().
310  **********************************************************************/
311  // GEOGRAPHICLIB_DEPRECATED("Use EquatorialRadius()")
312  Math::real MajorRadius() const { return EquatorialRadius(); }
313  ///@}
314 
315  /**
316  * A global instantiation of LambertConformalConic with the WGS84
317  * ellipsoid, \e stdlat = 0, and \e k0 = 1. This degenerates to the
318  * Mercator projection.
319  **********************************************************************/
320  static const LambertConformalConic& Mercator();
321  };
322 
323 } // namespace GeographicLib
324 
325 #endif // GEOGRAPHICLIB_LAMBERTCONFORMALCONIC_HPP
real
GeographicLib::Math::real real
Definition: GeodSolve.cpp:31
GeographicLib::LambertConformalConic::CentralScale
Math::real CentralScale() const
Definition: LambertConformalConic.hpp:306
GeographicLib
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
GeographicLib::Math::log1p
static T log1p(T x)
Definition: Math.cpp:87
GEOGRAPHICLIB_EXPORT
#define GEOGRAPHICLIB_EXPORT
Definition: Constants.hpp:92
GeographicLib::Math::real
double real
Definition: Math.hpp:121
GeographicLib::Math::hypot
static T hypot(T x, T y)
Definition: Math.cpp:58
GeographicLib::LambertConformalConic::EquatorialRadius
Math::real EquatorialRadius() const
Definition: LambertConformalConic.hpp:285
GeographicLib::LambertConformalConic::OriginLatitude
Math::real OriginLatitude() const
Definition: LambertConformalConic.hpp:300
Constants.hpp
Header for GeographicLib::Constants class.
GeographicLib::LambertConformalConic::Forward
void Forward(real lon0, real lat, real lon, real &x, real &y) const
Definition: LambertConformalConic.hpp:262
GeographicLib::LambertConformalConic::MajorRadius
Math::real MajorRadius() const
Definition: LambertConformalConic.hpp:312
GeographicLib::LambertConformalConic
Lambert conformal conic projection.
Definition: LambertConformalConic.hpp:63
GeographicLib::LambertConformalConic::Flattening
Math::real Flattening() const
Definition: LambertConformalConic.hpp:291
GeographicLib::Math::eatanhe
static T eatanhe(T x, T es)
Definition: Math.cpp:337
GeographicLib::Math::sq
static T sq(T x)
Definition: Math.hpp:201
GeographicLib::Math::asinh
static T asinh(T x)
Definition: Math.cpp:102
GeographicLib::LambertConformalConic::Reverse
void Reverse(real lon0, real x, real y, real &lat, real &lon) const
Definition: LambertConformalConic.hpp:272