AlbersEqualArea.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /**
  2. * \file AlbersEqualArea.hpp
  3. * \brief Header for GeographicLib::AlbersEqualArea class
  4. *
  5. * Copyright (c) Charles Karney (2010-2021) <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_ALBERSEQUALAREA_HPP)
  10. #define GEOGRAPHICLIB_ALBERSEQUALAREA_HPP 1
  11. #include <GeographicLib/Constants.hpp>
  12. namespace GeographicLib {
  13. /**
  14. * \brief Albers equal area 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. 101--102.
  21. *
  22. * This is a implementation of the equations in Snyder except that divided
  23. * differences will be [have been] used to transform the expressions into
  24. * ones which may be evaluated accurately. [In this implementation, the
  25. * projection correctly becomes the cylindrical equal area or the azimuthal
  26. * equal area projection when the standard latitude is the equator or a
  27. * pole.]
  28. *
  29. * The ellipsoid parameters, the standard parallels, and the scale on the
  30. * standard parallels are set in the constructor. Internally, the case with
  31. * two standard parallels is converted into a single standard parallel, the
  32. * latitude of minimum azimuthal scale, with an azimuthal scale specified on
  33. * this parallel. This latitude is also used as the latitude of origin which
  34. * is returned by AlbersEqualArea::OriginLatitude. The azimuthal scale on
  35. * the latitude of origin is given by AlbersEqualArea::CentralScale. The
  36. * case with two standard parallels at opposite poles is singular and is
  37. * disallowed. The central meridian (which is a trivial shift of the
  38. * longitude) is specified as the \e lon0 argument of the
  39. * AlbersEqualArea::Forward and AlbersEqualArea::Reverse functions.
  40. * AlbersEqualArea::Forward and AlbersEqualArea::Reverse also return the
  41. * meridian convergence, &gamma;, and azimuthal scale, \e k. A small square
  42. * aligned with the cardinal directions is projected to a rectangle with
  43. * dimensions \e k (in the E-W direction) and 1/\e k (in the N-S direction).
  44. * The E-W sides of the rectangle are oriented &gamma; degrees
  45. * counter-clockwise from the \e x axis. There is no provision in this class
  46. * for specifying a false easting or false northing or a different latitude
  47. * of origin.
  48. *
  49. * Example of use:
  50. * \include example-AlbersEqualArea.cpp
  51. *
  52. * <a href="ConicProj.1.html">ConicProj</a> is a command-line utility
  53. * providing access to the functionality of LambertConformalConic and
  54. * AlbersEqualArea.
  55. **********************************************************************/
  56. class GEOGRAPHICLIB_EXPORT AlbersEqualArea {
  57. private:
  58. typedef Math::real real;
  59. real eps_, epsx_, epsx2_, tol_, tol0_;
  60. real _a, _f, _fm, _e2, _e, _e2m, _qZ, _qx;
  61. real _sign, _lat0, _k0;
  62. real _n0, _m02, _nrho0, _k2, _txi0, _scxi0, _sxi0;
  63. static const int numit_ = 5; // Newton iterations in Reverse
  64. static const int numit0_ = 20; // Newton iterations in Init
  65. static real hyp(real x) {
  66. using std::hypot;
  67. return hypot(real(1), x);
  68. }
  69. // atanh( e * x)/ e if f > 0
  70. // atan (sqrt(-e2) * x)/sqrt(-e2) if f < 0
  71. // x if f = 0
  72. real atanhee(real x) const {
  73. using std::atan; using std::abs; using std::atanh;
  74. return _f > 0 ? atanh(_e * x)/_e : (_f < 0 ? (atan(_e * x)/_e) : x);
  75. }
  76. // return atanh(sqrt(x))/sqrt(x) - 1, accurate for small x
  77. static real atanhxm1(real x);
  78. // Divided differences
  79. // Definition: Df(x,y) = (f(x)-f(y))/(x-y)
  80. // See:
  81. // W. M. Kahan and R. J. Fateman,
  82. // Symbolic computation of divided differences,
  83. // SIGSAM Bull. 33(3), 7-28 (1999)
  84. // https://doi.org/10.1145/334714.334716
  85. // http://www.cs.berkeley.edu/~fateman/papers/divdiff.pdf
  86. //
  87. // General rules
  88. // h(x) = f(g(x)): Dh(x,y) = Df(g(x),g(y))*Dg(x,y)
  89. // h(x) = f(x)*g(x):
  90. // Dh(x,y) = Df(x,y)*g(x) + Dg(x,y)*f(y)
  91. // = Df(x,y)*g(y) + Dg(x,y)*f(x)
  92. // = Df(x,y)*(g(x)+g(y))/2 + Dg(x,y)*(f(x)+f(y))/2
  93. //
  94. // sn(x) = x/sqrt(1+x^2): Dsn(x,y) = (x+y)/((sn(x)+sn(y))*(1+x^2)*(1+y^2))
  95. static real Dsn(real x, real y, real sx, real sy) {
  96. // sx = x/hyp(x)
  97. real t = x * y;
  98. return t > 0 ? (x + y) * Math::sq( (sx * sy)/t ) / (sx + sy) :
  99. (x - y != 0 ? (sx - sy) / (x - y) : 1);
  100. }
  101. // Datanhee(x,y) = (atanee(x)-atanee(y))/(x-y)
  102. // = atanhee((x-y)/(1-e^2*x*y))/(x-y)
  103. real Datanhee(real x, real y) const {
  104. real t = x - y, d = 1 - _e2 * x * y;
  105. return t == 0 ? 1 / d :
  106. (x*y < 0 ? atanhee(x) - atanhee(y) : atanhee(t / d)) / t;
  107. }
  108. // DDatanhee(x,y) = (Datanhee(1,y) - Datanhee(1,x))/(y-x)
  109. real DDatanhee(real x, real y) const;
  110. real DDatanhee0(real x, real y) const;
  111. real DDatanhee1(real x, real y) const;
  112. real DDatanhee2(real x, real y) const;
  113. void Init(real sphi1, real cphi1, real sphi2, real cphi2, real k1);
  114. real txif(real tphi) const;
  115. real tphif(real txi) const;
  116. friend class Ellipsoid; // For access to txif, tphif, etc.
  117. public:
  118. /**
  119. * Constructor with a single standard parallel.
  120. *
  121. * @param[in] a equatorial radius of ellipsoid (meters).
  122. * @param[in] f flattening of ellipsoid. Setting \e f = 0 gives a sphere.
  123. * Negative \e f gives a prolate ellipsoid.
  124. * @param[in] stdlat standard parallel (degrees), the circle of tangency.
  125. * @param[in] k0 azimuthal scale on the standard parallel.
  126. * @exception GeographicErr if \e a, (1 &minus; \e f) \e a, or \e k0 is
  127. * not positive.
  128. * @exception GeographicErr if \e stdlat is not in [&minus;90&deg;,
  129. * 90&deg;].
  130. **********************************************************************/
  131. AlbersEqualArea(real a, real f, real stdlat, real k0);
  132. /**
  133. * Constructor with two standard parallels.
  134. *
  135. * @param[in] a equatorial radius of ellipsoid (meters).
  136. * @param[in] f flattening of ellipsoid. Setting \e f = 0 gives a sphere.
  137. * Negative \e f gives a prolate ellipsoid.
  138. * @param[in] stdlat1 first standard parallel (degrees).
  139. * @param[in] stdlat2 second standard parallel (degrees).
  140. * @param[in] k1 azimuthal scale on the standard parallels.
  141. * @exception GeographicErr if \e a, (1 &minus; \e f) \e a, or \e k1 is
  142. * not positive.
  143. * @exception GeographicErr if \e stdlat1 or \e stdlat2 is not in
  144. * [&minus;90&deg;, 90&deg;], or if \e stdlat1 and \e stdlat2 are
  145. * opposite poles.
  146. **********************************************************************/
  147. AlbersEqualArea(real a, real f, real stdlat1, real stdlat2, real k1);
  148. /**
  149. * Constructor with two standard parallels specified by sines and cosines.
  150. *
  151. * @param[in] a equatorial radius of ellipsoid (meters).
  152. * @param[in] f flattening of ellipsoid. Setting \e f = 0 gives a sphere.
  153. * Negative \e f gives a prolate ellipsoid.
  154. * @param[in] sinlat1 sine of first standard parallel.
  155. * @param[in] coslat1 cosine of first standard parallel.
  156. * @param[in] sinlat2 sine of second standard parallel.
  157. * @param[in] coslat2 cosine of second standard parallel.
  158. * @param[in] k1 azimuthal scale on the standard parallels.
  159. * @exception GeographicErr if \e a, (1 &minus; \e f) \e a, or \e k1 is
  160. * not positive.
  161. * @exception GeographicErr if \e stdlat1 or \e stdlat2 is not in
  162. * [&minus;90&deg;, 90&deg;], or if \e stdlat1 and \e stdlat2 are
  163. * opposite poles.
  164. *
  165. * This allows parallels close to the poles to be specified accurately.
  166. * This routine computes the latitude of origin and the azimuthal scale at
  167. * this latitude. If \e dlat = abs(\e lat2 &minus; \e lat1) &le; 160&deg;,
  168. * then the error in the latitude of origin is less than 4.5 &times;
  169. * 10<sup>&minus;14</sup>d;.
  170. **********************************************************************/
  171. AlbersEqualArea(real a, real f,
  172. real sinlat1, real coslat1,
  173. real sinlat2, real coslat2,
  174. real k1);
  175. /**
  176. * Set the azimuthal scale for the projection.
  177. *
  178. * @param[in] lat (degrees).
  179. * @param[in] k azimuthal scale at latitude \e lat (default 1).
  180. * @exception GeographicErr \e k is not positive.
  181. * @exception GeographicErr if \e lat is not in (&minus;90&deg;,
  182. * 90&deg;).
  183. *
  184. * This allows a "latitude of conformality" to be specified.
  185. **********************************************************************/
  186. void SetScale(real lat, real k = real(1));
  187. /**
  188. * Forward projection, from geographic to Lambert conformal conic.
  189. *
  190. * @param[in] lon0 central meridian longitude (degrees).
  191. * @param[in] lat latitude of point (degrees).
  192. * @param[in] lon longitude of point (degrees).
  193. * @param[out] x easting of point (meters).
  194. * @param[out] y northing of point (meters).
  195. * @param[out] gamma meridian convergence at point (degrees).
  196. * @param[out] k azimuthal scale of projection at point; the radial
  197. * scale is the 1/\e k.
  198. *
  199. * The latitude origin is given by AlbersEqualArea::LatitudeOrigin(). No
  200. * false easting or northing is added and \e lat should be in the range
  201. * [&minus;90&deg;, 90&deg;]. The values of \e x and \e y returned for
  202. * points which project to infinity (i.e., one or both of the poles) will
  203. * be large but finite.
  204. **********************************************************************/
  205. void Forward(real lon0, real lat, real lon,
  206. real& x, real& y, real& gamma, real& k) const;
  207. /**
  208. * Reverse projection, from Lambert conformal conic to geographic.
  209. *
  210. * @param[in] lon0 central meridian longitude (degrees).
  211. * @param[in] x easting of point (meters).
  212. * @param[in] y northing of point (meters).
  213. * @param[out] lat latitude of point (degrees).
  214. * @param[out] lon longitude of point (degrees).
  215. * @param[out] gamma meridian convergence at point (degrees).
  216. * @param[out] k azimuthal scale of projection at point; the radial
  217. * scale is the 1/\e k.
  218. *
  219. * The latitude origin is given by AlbersEqualArea::LatitudeOrigin(). No
  220. * false easting or northing is added. The value of \e lon returned is in
  221. * the range [&minus;180&deg;, 180&deg;]. The value of \e lat returned is
  222. * in the range [&minus;90&deg;, 90&deg;]. If the input point is outside
  223. * the legal projected space the nearest pole is returned.
  224. **********************************************************************/
  225. void Reverse(real lon0, real x, real y,
  226. real& lat, real& lon, real& gamma, real& k) const;
  227. /**
  228. * AlbersEqualArea::Forward without returning the convergence and
  229. * scale.
  230. **********************************************************************/
  231. void Forward(real lon0, real lat, real lon,
  232. real& x, real& y) const {
  233. real gamma, k;
  234. Forward(lon0, lat, lon, x, y, gamma, k);
  235. }
  236. /**
  237. * AlbersEqualArea::Reverse without returning the convergence and
  238. * scale.
  239. **********************************************************************/
  240. void Reverse(real lon0, real x, real y,
  241. real& lat, real& lon) const {
  242. real gamma, k;
  243. Reverse(lon0, x, y, lat, lon, gamma, k);
  244. }
  245. /** \name Inspector functions
  246. **********************************************************************/
  247. ///@{
  248. /**
  249. * @return \e a the equatorial radius of the ellipsoid (meters). This is
  250. * the value used in the constructor.
  251. **********************************************************************/
  252. Math::real EquatorialRadius() const { return _a; }
  253. /**
  254. * @return \e f the flattening of the ellipsoid. This is the value used in
  255. * the constructor.
  256. **********************************************************************/
  257. Math::real Flattening() const { return _f; }
  258. /**
  259. * @return latitude of the origin for the projection (degrees).
  260. *
  261. * This is the latitude of minimum azimuthal scale and equals the \e stdlat
  262. * in the 1-parallel constructor and lies between \e stdlat1 and \e stdlat2
  263. * in the 2-parallel constructors.
  264. **********************************************************************/
  265. Math::real OriginLatitude() const { return _lat0; }
  266. /**
  267. * @return central scale for the projection. This is the azimuthal scale
  268. * on the latitude of origin.
  269. **********************************************************************/
  270. Math::real CentralScale() const { return _k0; }
  271. /**
  272. * \deprecated An old name for EquatorialRadius().
  273. **********************************************************************/
  274. GEOGRAPHICLIB_DEPRECATED("Use EquatorialRadius()")
  275. Math::real MajorRadius() const { return EquatorialRadius(); }
  276. ///@}
  277. /**
  278. * A global instantiation of AlbersEqualArea with the WGS84 ellipsoid, \e
  279. * stdlat = 0, and \e k0 = 1. This degenerates to the cylindrical equal
  280. * area projection.
  281. **********************************************************************/
  282. static const AlbersEqualArea& CylindricalEqualArea();
  283. /**
  284. * A global instantiation of AlbersEqualArea with the WGS84 ellipsoid, \e
  285. * stdlat = 90&deg;, and \e k0 = 1. This degenerates to the
  286. * Lambert azimuthal equal area projection.
  287. **********************************************************************/
  288. static const AlbersEqualArea& AzimuthalEqualAreaNorth();
  289. /**
  290. * A global instantiation of AlbersEqualArea with the WGS84 ellipsoid, \e
  291. * stdlat = &minus;90&deg;, and \e k0 = 1. This degenerates to the
  292. * Lambert azimuthal equal area projection.
  293. **********************************************************************/
  294. static const AlbersEqualArea& AzimuthalEqualAreaSouth();
  295. };
  296. } // namespace GeographicLib
  297. #endif // GEOGRAPHICLIB_ALBERSEQUALAREA_HPP