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