LambertConformalConic.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /**
  2. * \file LambertConformalConic.hpp
  3. * \brief Header for GeographicLib::LambertConformalConic class
  4. *
  5. * Copyright (c) Charles Karney (2010-2020) <charles@karney.com> and licensed
  6. * under the MIT/X11 License. For more information, see
  7. * https://geographiclib.sourceforge.io/
  8. **********************************************************************/
  9. #if !defined(GEOGRAPHICLIB_LAMBERTCONFORMALCONIC_HPP)
  10. #define GEOGRAPHICLIB_LAMBERTCONFORMALCONIC_HPP 1
  11. #include <GeographicLib/Constants.hpp>
  12. namespace GeographicLib {
  13. /**
  14. * \brief Lambert conformal conic projection
  15. *
  16. * Implementation taken from the report,
  17. * - J. P. Snyder,
  18. * <a href="http://pubs.er.usgs.gov/usgspubs/pp/pp1395"> Map Projections: A
  19. * Working Manual</a>, USGS Professional Paper 1395 (1987),
  20. * pp. 107--109.
  21. *
  22. * This is a implementation of the equations in Snyder except that divided
  23. * differences have been used to transform the expressions into ones which
  24. * may be evaluated accurately and that Newton's method is used to invert the
  25. * projection. In this implementation, the projection correctly becomes the
  26. * Mercator projection or the polar stereographic projection when the
  27. * standard latitude is the equator or a pole. The accuracy of the
  28. * projections is about 10 nm (10 nanometers).
  29. *
  30. * The ellipsoid parameters, the standard parallels, and the scale on the
  31. * standard parallels are set in the constructor. Internally, the case with
  32. * two standard parallels is converted into a single standard parallel, the
  33. * latitude of tangency (also the latitude of minimum scale), with a scale
  34. * specified on this parallel. This latitude is also used as the latitude of
  35. * origin which is returned by LambertConformalConic::OriginLatitude. The
  36. * scale on the latitude of origin is given by
  37. * LambertConformalConic::CentralScale. The case with two distinct standard
  38. * parallels where one is a pole is singular and is disallowed. The central
  39. * meridian (which is a trivial shift of the longitude) is specified as the
  40. * \e lon0 argument of the LambertConformalConic::Forward and
  41. * LambertConformalConic::Reverse functions.
  42. *
  43. * This class also returns the meridian convergence \e gamma and scale \e k.
  44. * The meridian convergence is the bearing of grid north (the \e y axis)
  45. * measured clockwise from true north.
  46. *
  47. * There is no provision in this
  48. * class for specifying a false easting or false northing or a different
  49. * latitude of origin. However these are can be simply included by the
  50. * calling function. For example the Pennsylvania South state coordinate
  51. * system (<a href="https://www.spatialreference.org/ref/epsg/3364/">
  52. * EPSG:3364</a>) is obtained by:
  53. * \include example-LambertConformalConic.cpp
  54. *
  55. * <a href="ConicProj.1.html">ConicProj</a> is a command-line utility
  56. * providing access to the functionality of LambertConformalConic and
  57. * AlbersEqualArea.
  58. **********************************************************************/
  59. class GEOGRAPHICLIB_EXPORT LambertConformalConic {
  60. private:
  61. typedef Math::real real;
  62. real eps_, epsx_, ahypover_;
  63. real _a, _f, _fm, _e2, _es;
  64. real _sign, _n, _nc, _t0nm1, _scale, _lat0, _k0;
  65. real _scbet0, _tchi0, _scchi0, _psi0, _nrho0, _drhomax;
  66. static const int numit_ = 5;
  67. static real hyp(real x) {
  68. using std::hypot;
  69. return hypot(real(1), x);
  70. }
  71. // Divided differences
  72. // Definition: Df(x,y) = (f(x)-f(y))/(x-y)
  73. // See:
  74. // W. M. Kahan and R. J. Fateman,
  75. // Symbolic computation of divided differences,
  76. // SIGSAM Bull. 33(3), 7-28 (1999)
  77. // https://doi.org/10.1145/334714.334716
  78. // http://www.cs.berkeley.edu/~fateman/papers/divdiff.pdf
  79. //
  80. // General rules
  81. // h(x) = f(g(x)): Dh(x,y) = Df(g(x),g(y))*Dg(x,y)
  82. // h(x) = f(x)*g(x):
  83. // Dh(x,y) = Df(x,y)*g(x) + Dg(x,y)*f(y)
  84. // = Df(x,y)*g(y) + Dg(x,y)*f(x)
  85. // = Df(x,y)*(g(x)+g(y))/2 + Dg(x,y)*(f(x)+f(y))/2
  86. //
  87. // hyp(x) = sqrt(1+x^2): Dhyp(x,y) = (x+y)/(hyp(x)+hyp(y))
  88. static real Dhyp(real x, real y, real hx, real hy)
  89. // hx = hyp(x)
  90. { return (x + y) / (hx + hy); }
  91. // sn(x) = x/sqrt(1+x^2): Dsn(x,y) = (x+y)/((sn(x)+sn(y))*(1+x^2)*(1+y^2))
  92. static real Dsn(real x, real y, real sx, real sy) {
  93. // sx = x/hyp(x)
  94. real t = x * y;
  95. return t > 0 ? (x + y) * Math::sq( (sx * sy)/t ) / (sx + sy) :
  96. (x - y != 0 ? (sx - sy) / (x - y) : 1);
  97. }
  98. // Dlog1p(x,y) = log1p((x-y)/(1+y))/(x-y)
  99. static real Dlog1p(real x, real y) {
  100. using std::log1p;
  101. real t = x - y; if (t < 0) { t = -t; y = x; }
  102. return t != 0 ? 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. using std::asinh;
  128. real t = x - y;
  129. return t != 0 ?
  130. asinh(x*y > 0 ? t * (x + y) / (x*hy + y*hx) : x*hy - y*hx) / t :
  131. 1 / hx;
  132. }
  133. // Deatanhe(x,y) = eatanhe((x-y)/(1-e^2*x*y))/(x-y)
  134. real Deatanhe(real x, real y) const {
  135. real t = x - y, d = 1 - _e2 * x * y;
  136. return t != 0 ? Math::eatanhe(t / d, _es) / t : _e2 / d;
  137. }
  138. void Init(real sphi1, real cphi1, real sphi2, real cphi2, real k1);
  139. public:
  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. * Constructor with two standard parallels.
  156. *
  157. * @param[in] a equatorial radius of ellipsoid (meters).
  158. * @param[in] f flattening of ellipsoid. Setting \e f = 0 gives a sphere.
  159. * Negative \e f gives a prolate ellipsoid.
  160. * @param[in] stdlat1 first standard parallel (degrees).
  161. * @param[in] stdlat2 second standard parallel (degrees).
  162. * @param[in] k1 scale on the standard parallels.
  163. * @exception GeographicErr if \e a, (1 &minus; \e f) \e a, or \e k1 is
  164. * not positive.
  165. * @exception GeographicErr if \e stdlat1 or \e stdlat2 is not in
  166. * [&minus;90&deg;, 90&deg;], or if either \e stdlat1 or \e
  167. * stdlat2 is a pole and \e stdlat1 is not equal \e stdlat2.
  168. **********************************************************************/
  169. LambertConformalConic(real a, real f, real stdlat1, real stdlat2, real k1);
  170. /**
  171. * Constructor with two standard parallels specified by sines and cosines.
  172. *
  173. * @param[in] a equatorial radius of ellipsoid (meters).
  174. * @param[in] f flattening of ellipsoid. Setting \e f = 0 gives a sphere.
  175. * Negative \e f gives a prolate ellipsoid.
  176. * @param[in] sinlat1 sine of first standard parallel.
  177. * @param[in] coslat1 cosine of first standard parallel.
  178. * @param[in] sinlat2 sine of second standard parallel.
  179. * @param[in] coslat2 cosine of second standard parallel.
  180. * @param[in] k1 scale on the standard parallels.
  181. * @exception GeographicErr if \e a, (1 &minus; \e f) \e a, or \e k1 is
  182. * not positive.
  183. * @exception GeographicErr if \e stdlat1 or \e stdlat2 is not in
  184. * [&minus;90&deg;, 90&deg;], or if either \e stdlat1 or \e
  185. * stdlat2 is a pole and \e stdlat1 is not equal \e stdlat2.
  186. *
  187. * This allows parallels close to the poles to be specified accurately.
  188. * This routine computes the latitude of origin and the scale at this
  189. * latitude. In the case where \e lat1 and \e lat2 are different, the
  190. * errors in this routines are as follows: if \e dlat = abs(\e lat2 &minus;
  191. * \e lat1) &le; 160&deg; and max(abs(\e lat1), abs(\e lat2)) &le; 90
  192. * &minus; min(0.0002, 2.2 &times; 10<sup>&minus;6</sup>(180 &minus; \e
  193. * dlat), 6 &times 10<sup>&minus;8</sup> <i>dlat</i><sup>2</sup>) (in
  194. * degrees), then the error in the latitude of origin is less than 4.5
  195. * &times; 10<sup>&minus;14</sup>d and the relative error in the scale is
  196. * less than 7 &times; 10<sup>&minus;15</sup>.
  197. **********************************************************************/
  198. LambertConformalConic(real a, real f,
  199. real sinlat1, real coslat1,
  200. real sinlat2, real coslat2,
  201. real k1);
  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. * Forward projection, from geographic to Lambert conformal conic.
  214. *
  215. * @param[in] lon0 central meridian longitude (degrees).
  216. * @param[in] lat latitude of point (degrees).
  217. * @param[in] lon longitude of point (degrees).
  218. * @param[out] x easting of point (meters).
  219. * @param[out] y northing of point (meters).
  220. * @param[out] gamma meridian convergence at point (degrees).
  221. * @param[out] k scale of projection at point.
  222. *
  223. * The latitude origin is given by LambertConformalConic::LatitudeOrigin().
  224. * No false easting or northing is added and \e lat should be in the range
  225. * [&minus;90&deg;, 90&deg;]. The error in the projection is less than
  226. * about 10 nm (10 nanometers), true distance, and the errors in the
  227. * meridian convergence and scale are consistent with this. The values of
  228. * \e x and \e y returned for points which project to infinity (i.e., one
  229. * or both of the poles) will be large but finite.
  230. **********************************************************************/
  231. void Forward(real lon0, real lat, real lon,
  232. real& x, real& y, real& gamma, real& k) const;
  233. /**
  234. * Reverse projection, from Lambert conformal conic to geographic.
  235. *
  236. * @param[in] lon0 central meridian longitude (degrees).
  237. * @param[in] x easting of point (meters).
  238. * @param[in] y northing of point (meters).
  239. * @param[out] lat latitude of point (degrees).
  240. * @param[out] lon longitude of point (degrees).
  241. * @param[out] gamma meridian convergence at point (degrees).
  242. * @param[out] k scale of projection at point.
  243. *
  244. * The latitude origin is given by LambertConformalConic::LatitudeOrigin().
  245. * No false easting or northing is added. The value of \e lon returned is
  246. * in the range [&minus;180&deg;, 180&deg;]. The error in the projection
  247. * is less than about 10 nm (10 nanometers), true distance, and the errors
  248. * in the meridian convergence and scale are consistent with this.
  249. **********************************************************************/
  250. void Reverse(real lon0, real x, real y,
  251. real& lat, real& lon, real& gamma, real& k) const;
  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. * LambertConformalConic::Reverse without returning the convergence and
  263. * scale.
  264. **********************************************************************/
  265. void Reverse(real lon0, real x, real y,
  266. real& lat, real& lon) const {
  267. real gamma, k;
  268. Reverse(lon0, x, y, lat, lon, gamma, k);
  269. }
  270. /** \name Inspector functions
  271. **********************************************************************/
  272. ///@{
  273. /**
  274. * @return \e a the equatorial radius of the ellipsoid (meters). This is
  275. * the value used in the constructor.
  276. **********************************************************************/
  277. Math::real EquatorialRadius() const { return _a; }
  278. /**
  279. * @return \e f the flattening of the ellipsoid. This is the
  280. * value used in the constructor.
  281. **********************************************************************/
  282. Math::real Flattening() const { return _f; }
  283. /**
  284. * @return latitude of the origin for the projection (degrees).
  285. *
  286. * This is the latitude of minimum scale and equals the \e stdlat in the
  287. * 1-parallel constructor and lies between \e stdlat1 and \e stdlat2 in the
  288. * 2-parallel constructors.
  289. **********************************************************************/
  290. Math::real OriginLatitude() const { return _lat0; }
  291. /**
  292. * @return central scale for the projection. This is the scale on the
  293. * latitude of origin.
  294. **********************************************************************/
  295. Math::real CentralScale() const { return _k0; }
  296. /**
  297. * \deprecated An old name for EquatorialRadius().
  298. **********************************************************************/
  299. GEOGRAPHICLIB_DEPRECATED("Use EquatorialRadius()")
  300. Math::real MajorRadius() const { return EquatorialRadius(); }
  301. ///@}
  302. /**
  303. * A global instantiation of LambertConformalConic with the WGS84
  304. * ellipsoid, \e stdlat = 0, and \e k0 = 1. This degenerates to the
  305. * Mercator projection.
  306. **********************************************************************/
  307. static const LambertConformalConic& Mercator();
  308. };
  309. } // namespace GeographicLib
  310. #endif // GEOGRAPHICLIB_LAMBERTCONFORMALCONIC_HPP