imw_p.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // Boost.Geometry - gis-projections (based on PROJ4)
  2. // Copyright (c) 2008-2015 Barend Gehrels, Amsterdam, the Netherlands.
  3. // This file was modified by Oracle on 2017, 2018, 2019.
  4. // Modifications copyright (c) 2017-2019, Oracle and/or its affiliates.
  5. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle.
  6. // Use, modification and distribution is subject to the Boost Software License,
  7. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. // This file is converted from PROJ4, http://trac.osgeo.org/proj
  10. // PROJ4 is originally written by Gerald Evenden (then of the USGS)
  11. // PROJ4 is maintained by Frank Warmerdam
  12. // PROJ4 is converted to Boost.Geometry by Barend Gehrels
  13. // Last updated version of proj: 5.0.0
  14. // Original copyright notice:
  15. // Permission is hereby granted, free of charge, to any person obtaining a
  16. // copy of this software and associated documentation files (the "Software"),
  17. // to deal in the Software without restriction, including without limitation
  18. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  19. // and/or sell copies of the Software, and to permit persons to whom the
  20. // Software is furnished to do so, subject to the following conditions:
  21. // The above copyright notice and this permission notice shall be included
  22. // in all copies or substantial portions of the Software.
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  24. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  26. // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  28. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  29. // DEALINGS IN THE SOFTWARE.
  30. #ifndef BOOST_GEOMETRY_PROJECTIONS_IMW_P_HPP
  31. #define BOOST_GEOMETRY_PROJECTIONS_IMW_P_HPP
  32. #include <boost/geometry/srs/projections/impl/base_static.hpp>
  33. #include <boost/geometry/srs/projections/impl/base_dynamic.hpp>
  34. #include <boost/geometry/srs/projections/impl/factory_entry.hpp>
  35. #include <boost/geometry/srs/projections/impl/pj_mlfn.hpp>
  36. #include <boost/geometry/srs/projections/impl/pj_param.hpp>
  37. #include <boost/geometry/srs/projections/impl/projects.hpp>
  38. #include <boost/geometry/util/math.hpp>
  39. namespace boost { namespace geometry
  40. {
  41. namespace projections
  42. {
  43. #ifndef DOXYGEN_NO_DETAIL
  44. namespace detail { namespace imw_p
  45. {
  46. static const double tolerance = 1e-10;
  47. static const double epsilon = 1e-10;
  48. template <typename T>
  49. struct point_xy { T x, y; }; // specific for IMW_P
  50. enum mode_type {
  51. none_is_zero = 0, /* phi_1 and phi_2 != 0 */
  52. phi_1_is_zero = 1, /* phi_1 = 0 */
  53. phi_2_is_zero = -1 /* phi_2 = 0 */
  54. };
  55. template <typename T>
  56. struct par_imw_p
  57. {
  58. T P, Pp, Q, Qp, R_1, R_2, sphi_1, sphi_2, C2;
  59. T phi_1, phi_2, lam_1;
  60. detail::en<T> en;
  61. mode_type mode;
  62. };
  63. template <typename Params, typename T>
  64. inline int phi12(Params const& params,
  65. par_imw_p<T> & proj_parm, T *del, T *sig)
  66. {
  67. int err = 0;
  68. if (!pj_param_r<srs::spar::lat_1>(params, "lat_1", srs::dpar::lat_1, proj_parm.phi_1) ||
  69. !pj_param_r<srs::spar::lat_2>(params, "lat_2", srs::dpar::lat_2, proj_parm.phi_2)) {
  70. err = -41;
  71. } else {
  72. //proj_parm.phi_1 = pj_get_param_r(par.params, "lat_1"); // set above
  73. //proj_parm.phi_2 = pj_get_param_r(par.params, "lat_2"); // set above
  74. *del = 0.5 * (proj_parm.phi_2 - proj_parm.phi_1);
  75. *sig = 0.5 * (proj_parm.phi_2 + proj_parm.phi_1);
  76. err = (fabs(*del) < epsilon || fabs(*sig) < epsilon) ? -42 : 0;
  77. }
  78. return err;
  79. }
  80. template <typename Parameters, typename T>
  81. inline point_xy<T> loc_for(T const& lp_lam, T const& lp_phi,
  82. Parameters const& par,
  83. par_imw_p<T> const& proj_parm,
  84. T *yc)
  85. {
  86. point_xy<T> xy;
  87. if (lp_phi == 0.0) {
  88. xy.x = lp_lam;
  89. xy.y = 0.;
  90. } else {
  91. T xa, ya, xb, yb, xc, D, B, m, sp, t, R, C;
  92. sp = sin(lp_phi);
  93. m = pj_mlfn(lp_phi, sp, cos(lp_phi), proj_parm.en);
  94. xa = proj_parm.Pp + proj_parm.Qp * m;
  95. ya = proj_parm.P + proj_parm.Q * m;
  96. R = 1. / (tan(lp_phi) * sqrt(1. - par.es * sp * sp));
  97. C = sqrt(R * R - xa * xa);
  98. if (lp_phi < 0.) C = - C;
  99. C += ya - R;
  100. if (proj_parm.mode == phi_2_is_zero) {
  101. xb = lp_lam;
  102. yb = proj_parm.C2;
  103. } else {
  104. t = lp_lam * proj_parm.sphi_2;
  105. xb = proj_parm.R_2 * sin(t);
  106. yb = proj_parm.C2 + proj_parm.R_2 * (1. - cos(t));
  107. }
  108. if (proj_parm.mode == phi_1_is_zero) {
  109. xc = lp_lam;
  110. *yc = 0.;
  111. } else {
  112. t = lp_lam * proj_parm.sphi_1;
  113. xc = proj_parm.R_1 * sin(t);
  114. *yc = proj_parm.R_1 * (1. - cos(t));
  115. }
  116. D = (xb - xc)/(yb - *yc);
  117. B = xc + D * (C + R - *yc);
  118. xy.x = D * sqrt(R * R * (1 + D * D) - B * B);
  119. if (lp_phi > 0)
  120. xy.x = - xy.x;
  121. xy.x = (B + xy.x) / (1. + D * D);
  122. xy.y = sqrt(R * R - xy.x * xy.x);
  123. if (lp_phi > 0)
  124. xy.y = - xy.y;
  125. xy.y += C + R;
  126. }
  127. return (xy);
  128. }
  129. template <typename Parameters, typename T>
  130. inline void xy(Parameters const& par, par_imw_p<T> const& proj_parm,
  131. T const& phi,
  132. T *x, T *y, T *sp, T *R)
  133. {
  134. T F;
  135. *sp = sin(phi);
  136. *R = 1./(tan(phi) * sqrt(1. - par.es * *sp * *sp ));
  137. F = proj_parm.lam_1 * *sp;
  138. *y = *R * (1 - cos(F));
  139. *x = *R * sin(F);
  140. }
  141. template <typename T, typename Parameters>
  142. struct base_imw_p_ellipsoid
  143. {
  144. par_imw_p<T> m_proj_parm;
  145. // FORWARD(e_forward) ellipsoid
  146. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  147. inline void fwd(Parameters const& par, T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
  148. {
  149. T yc = 0;
  150. point_xy<T> xy = loc_for(lp_lon, lp_lat, par, m_proj_parm, &yc);
  151. xy_x = xy.x; xy_y = xy.y;
  152. }
  153. // INVERSE(e_inverse) ellipsoid
  154. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  155. inline void inv(Parameters const& par, T const& xy_x, T const& xy_y, T& lp_lon, T& lp_lat) const
  156. {
  157. point_xy<T> t;
  158. T yc = 0.0;
  159. int i = 0;
  160. const int n_max_iter = 1000; /* Arbitrarily choosen number... */
  161. lp_lat = this->m_proj_parm.phi_2;
  162. lp_lon = xy_x / cos(lp_lat);
  163. do {
  164. t = loc_for(lp_lon, lp_lat, par, m_proj_parm, &yc);
  165. lp_lat = ((lp_lat - this->m_proj_parm.phi_1) * (xy_y - yc) / (t.y - yc)) + this->m_proj_parm.phi_1;
  166. lp_lon = lp_lon * xy_x / t.x;
  167. i++;
  168. } while (i < n_max_iter &&
  169. (fabs(t.x - xy_x) > tolerance || fabs(t.y - xy_y) > tolerance));
  170. if( i == n_max_iter )
  171. {
  172. lp_lon = lp_lat = HUGE_VAL;
  173. }
  174. }
  175. static inline std::string get_name()
  176. {
  177. return "imw_p_ellipsoid";
  178. }
  179. };
  180. // International Map of the World Polyconic
  181. template <typename Params, typename Parameters, typename T>
  182. inline void setup_imw_p(Params const& params, Parameters const& par, par_imw_p<T>& proj_parm)
  183. {
  184. T del, sig, s, t, x1, x2, T2, y1, m1, m2, y2;
  185. int err;
  186. proj_parm.en = pj_enfn<T>(par.es);
  187. if( (err = phi12(params, proj_parm, &del, &sig)) != 0)
  188. BOOST_THROW_EXCEPTION( projection_exception(err) );
  189. if (proj_parm.phi_2 < proj_parm.phi_1) { /* make sure proj_parm.phi_1 most southerly */
  190. del = proj_parm.phi_1;
  191. proj_parm.phi_1 = proj_parm.phi_2;
  192. proj_parm.phi_2 = del;
  193. }
  194. if (pj_param_r<srs::spar::lon_1>(params, "lon_1", srs::dpar::lon_1, proj_parm.lam_1)) {
  195. /* empty */
  196. } else { /* use predefined based upon latitude */
  197. sig = fabs(sig * geometry::math::r2d<T>());
  198. if (sig <= 60) sig = 2.;
  199. else if (sig <= 76) sig = 4.;
  200. else sig = 8.;
  201. proj_parm.lam_1 = sig * geometry::math::d2r<T>();
  202. }
  203. proj_parm.mode = none_is_zero;
  204. if (proj_parm.phi_1 != 0.0)
  205. xy(par, proj_parm, proj_parm.phi_1, &x1, &y1, &proj_parm.sphi_1, &proj_parm.R_1);
  206. else {
  207. proj_parm.mode = phi_1_is_zero;
  208. y1 = 0.;
  209. x1 = proj_parm.lam_1;
  210. }
  211. if (proj_parm.phi_2 != 0.0)
  212. xy(par, proj_parm, proj_parm.phi_2, &x2, &T2, &proj_parm.sphi_2, &proj_parm.R_2);
  213. else {
  214. proj_parm.mode = phi_2_is_zero;
  215. T2 = 0.;
  216. x2 = proj_parm.lam_1;
  217. }
  218. m1 = pj_mlfn(proj_parm.phi_1, proj_parm.sphi_1, cos(proj_parm.phi_1), proj_parm.en);
  219. m2 = pj_mlfn(proj_parm.phi_2, proj_parm.sphi_2, cos(proj_parm.phi_2), proj_parm.en);
  220. t = m2 - m1;
  221. s = x2 - x1;
  222. y2 = sqrt(t * t - s * s) + y1;
  223. proj_parm.C2 = y2 - T2;
  224. t = 1. / t;
  225. proj_parm.P = (m2 * y1 - m1 * y2) * t;
  226. proj_parm.Q = (y2 - y1) * t;
  227. proj_parm.Pp = (m2 * x1 - m1 * x2) * t;
  228. proj_parm.Qp = (x2 - x1) * t;
  229. }
  230. }} // namespace detail::imw_p
  231. #endif // doxygen
  232. /*!
  233. \brief International Map of the World Polyconic projection
  234. \ingroup projections
  235. \tparam Geographic latlong point type
  236. \tparam Cartesian xy point type
  237. \tparam Parameters parameter type
  238. \par Projection characteristics
  239. - Mod. Polyconic
  240. - Ellipsoid
  241. \par Projection parameters
  242. - lat_1: Latitude of first standard parallel
  243. - lat_2: Latitude of second standard parallel
  244. - lon_1 (degrees)
  245. \par Example
  246. \image html ex_imw_p.gif
  247. */
  248. template <typename T, typename Parameters>
  249. struct imw_p_ellipsoid : public detail::imw_p::base_imw_p_ellipsoid<T, Parameters>
  250. {
  251. template <typename Params>
  252. inline imw_p_ellipsoid(Params const& params, Parameters const& par)
  253. {
  254. detail::imw_p::setup_imw_p(params, par, this->m_proj_parm);
  255. }
  256. };
  257. #ifndef DOXYGEN_NO_DETAIL
  258. namespace detail
  259. {
  260. // Static projection
  261. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_FI(srs::spar::proj_imw_p, imw_p_ellipsoid)
  262. // Factory entry(s)
  263. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI(imw_p_entry, imw_p_ellipsoid)
  264. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(imw_p_init)
  265. {
  266. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(imw_p, imw_p_entry)
  267. }
  268. } // namespace detail
  269. #endif // doxygen
  270. } // namespace projections
  271. }} // namespace boost::geometry
  272. #endif // BOOST_GEOMETRY_PROJECTIONS_IMW_P_HPP