chamb.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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_CHAMB_HPP
  31. #define BOOST_GEOMETRY_PROJECTIONS_CHAMB_HPP
  32. #include <cstdio>
  33. #include <boost/geometry/srs/projections/impl/aasincos.hpp>
  34. #include <boost/geometry/srs/projections/impl/base_static.hpp>
  35. #include <boost/geometry/srs/projections/impl/base_dynamic.hpp>
  36. #include <boost/geometry/srs/projections/impl/factory_entry.hpp>
  37. #include <boost/geometry/srs/projections/impl/pj_param.hpp>
  38. #include <boost/geometry/srs/projections/impl/projects.hpp>
  39. #include <boost/geometry/util/math.hpp>
  40. namespace boost { namespace geometry
  41. {
  42. namespace projections
  43. {
  44. #ifndef DOXYGEN_NO_DETAIL
  45. namespace detail { namespace chamb
  46. {
  47. //static const double third = 0.333333333333333333;
  48. static const double tolerance = 1e-9;
  49. // specific for 'chamb'
  50. template <typename T>
  51. struct vect_ra { T r, Az; };
  52. template <typename T>
  53. struct point_xy { T x, y; };
  54. template <typename T>
  55. struct par_chamb
  56. {
  57. struct { /* control point data */
  58. T phi, lam;
  59. T cosphi, sinphi;
  60. vect_ra<T> v;
  61. point_xy<T> p;
  62. T Az;
  63. } c[3];
  64. point_xy<T> p;
  65. T beta_0, beta_1, beta_2;
  66. };
  67. /* distance and azimuth from point 1 to point 2 */
  68. template <typename T>
  69. inline vect_ra<T> vect(T const& dphi, T const& c1, T const& s1, T const& c2, T const& s2, T const& dlam)
  70. {
  71. vect_ra<T> v;
  72. T cdl, dp, dl;
  73. cdl = cos(dlam);
  74. if (fabs(dphi) > 1. || fabs(dlam) > 1.)
  75. v.r = aacos(s1 * s2 + c1 * c2 * cdl);
  76. else { /* more accurate for smaller distances */
  77. dp = sin(.5 * dphi);
  78. dl = sin(.5 * dlam);
  79. v.r = 2. * aasin(sqrt(dp * dp + c1 * c2 * dl * dl));
  80. }
  81. if (fabs(v.r) > tolerance)
  82. v.Az = atan2(c2 * sin(dlam), c1 * s2 - s1 * c2 * cdl);
  83. else
  84. v.r = v.Az = 0.;
  85. return v;
  86. }
  87. /* law of cosines */
  88. template <typename T>
  89. inline T lc(T const& b, T const& c, T const& a)
  90. {
  91. return aacos(.5 * (b * b + c * c - a * a) / (b * c));
  92. }
  93. template <typename T, typename Parameters>
  94. struct base_chamb_spheroid
  95. {
  96. par_chamb<T> m_proj_parm;
  97. // FORWARD(s_forward) spheroid
  98. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  99. inline void fwd(Parameters const& , T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
  100. {
  101. static const T third = detail::third<T>();
  102. T sinphi, cosphi, a;
  103. vect_ra<T> v[3];
  104. int i, j;
  105. sinphi = sin(lp_lat);
  106. cosphi = cos(lp_lat);
  107. for (i = 0; i < 3; ++i) { /* dist/azimiths from control */
  108. v[i] = vect(lp_lat - this->m_proj_parm.c[i].phi, this->m_proj_parm.c[i].cosphi, this->m_proj_parm.c[i].sinphi,
  109. cosphi, sinphi, lp_lon - this->m_proj_parm.c[i].lam);
  110. if (v[i].r == 0.0)
  111. break;
  112. v[i].Az = adjlon(v[i].Az - this->m_proj_parm.c[i].v.Az);
  113. }
  114. if (i < 3) /* current point at control point */
  115. { xy_x = this->m_proj_parm.c[i].p.x; xy_y = this->m_proj_parm.c[i].p.y; }
  116. else { /* point mean of intersepts */
  117. { xy_x = this->m_proj_parm.p.x; xy_y = this->m_proj_parm.p.y; }
  118. for (i = 0; i < 3; ++i) {
  119. j = i == 2 ? 0 : i + 1;
  120. a = lc(this->m_proj_parm.c[i].v.r, v[i].r, v[j].r);
  121. if (v[i].Az < 0.)
  122. a = -a;
  123. if (! i) { /* coord comp unique to each arc */
  124. xy_x += v[i].r * cos(a);
  125. xy_y -= v[i].r * sin(a);
  126. } else if (i == 1) {
  127. a = this->m_proj_parm.beta_1 - a;
  128. xy_x -= v[i].r * cos(a);
  129. xy_y -= v[i].r * sin(a);
  130. } else {
  131. a = this->m_proj_parm.beta_2 - a;
  132. xy_x += v[i].r * cos(a);
  133. xy_y += v[i].r * sin(a);
  134. }
  135. }
  136. xy_x *= third; /* mean of arc intercepts */
  137. xy_y *= third;
  138. }
  139. }
  140. static inline std::string get_name()
  141. {
  142. return "chamb_spheroid";
  143. }
  144. };
  145. template <typename T>
  146. inline T chamb_init_lat(srs::detail::proj4_parameters const& params, int i)
  147. {
  148. static const std::string lat[3] = {"lat_1", "lat_2", "lat_3"};
  149. return _pj_get_param_r<T>(params, lat[i]);
  150. }
  151. template <typename T>
  152. inline T chamb_init_lat(srs::dpar::parameters<T> const& params, int i)
  153. {
  154. static const srs::dpar::name_r lat[3] = {srs::dpar::lat_1, srs::dpar::lat_2, srs::dpar::lat_3};
  155. return _pj_get_param_r<T>(params, lat[i]);
  156. }
  157. template <typename T>
  158. inline T chamb_init_lon(srs::detail::proj4_parameters const& params, int i)
  159. {
  160. static const std::string lon[3] = {"lon_1", "lon_2", "lon_3"};
  161. return _pj_get_param_r<T>(params, lon[i]);
  162. }
  163. template <typename T>
  164. inline T chamb_init_lon(srs::dpar::parameters<T> const& params, int i)
  165. {
  166. static const srs::dpar::name_r lon[3] = {srs::dpar::lon_1, srs::dpar::lon_2, srs::dpar::lon_3};
  167. return _pj_get_param_r<T>(params, lon[i]);
  168. }
  169. // Chamberlin Trimetric
  170. template <typename Params, typename Parameters, typename T>
  171. inline void setup_chamb(Params const& params, Parameters& par, par_chamb<T>& proj_parm)
  172. {
  173. static const T pi = detail::pi<T>();
  174. int i, j;
  175. for (i = 0; i < 3; ++i) { /* get control point locations */
  176. proj_parm.c[i].phi = chamb_init_lat<T>(params, i);
  177. proj_parm.c[i].lam = chamb_init_lon<T>(params, i);
  178. proj_parm.c[i].lam = adjlon(proj_parm.c[i].lam - par.lam0);
  179. proj_parm.c[i].cosphi = cos(proj_parm.c[i].phi);
  180. proj_parm.c[i].sinphi = sin(proj_parm.c[i].phi);
  181. }
  182. for (i = 0; i < 3; ++i) { /* inter ctl pt. distances and azimuths */
  183. j = i == 2 ? 0 : i + 1;
  184. proj_parm.c[i].v = vect(proj_parm.c[j].phi - proj_parm.c[i].phi, proj_parm.c[i].cosphi, proj_parm.c[i].sinphi,
  185. proj_parm.c[j].cosphi, proj_parm.c[j].sinphi, proj_parm.c[j].lam - proj_parm.c[i].lam);
  186. if (proj_parm.c[i].v.r == 0.0)
  187. BOOST_THROW_EXCEPTION( projection_exception(error_control_point_no_dist) );
  188. /* co-linearity problem ignored for now */
  189. }
  190. proj_parm.beta_0 = lc(proj_parm.c[0].v.r, proj_parm.c[2].v.r, proj_parm.c[1].v.r);
  191. proj_parm.beta_1 = lc(proj_parm.c[0].v.r, proj_parm.c[1].v.r, proj_parm.c[2].v.r);
  192. proj_parm.beta_2 = pi - proj_parm.beta_0;
  193. proj_parm.p.y = 2. * (proj_parm.c[0].p.y = proj_parm.c[1].p.y = proj_parm.c[2].v.r * sin(proj_parm.beta_0));
  194. proj_parm.c[2].p.y = 0.;
  195. proj_parm.c[0].p.x = - (proj_parm.c[1].p.x = 0.5 * proj_parm.c[0].v.r);
  196. proj_parm.p.x = proj_parm.c[2].p.x = proj_parm.c[0].p.x + proj_parm.c[2].v.r * cos(proj_parm.beta_0);
  197. par.es = 0.;
  198. }
  199. }} // namespace detail::chamb
  200. #endif // doxygen
  201. /*!
  202. \brief Chamberlin Trimetric projection
  203. \ingroup projections
  204. \tparam Geographic latlong point type
  205. \tparam Cartesian xy point type
  206. \tparam Parameters parameter type
  207. \par Projection characteristics
  208. - Miscellaneous
  209. - Spheroid
  210. - no inverse
  211. \par Projection parameters
  212. - lat_1: Latitude of control point 1 (degrees)
  213. - lon_1: Longitude of control point 1 (degrees)
  214. - lat_2: Latitude of control point 2 (degrees)
  215. - lon_2: Longitude of control point 2 (degrees)
  216. - lat_3: Latitude of control point 3 (degrees)
  217. - lon_3: Longitude of control point 3 (degrees)
  218. \par Example
  219. \image html ex_chamb.gif
  220. */
  221. template <typename T, typename Parameters>
  222. struct chamb_spheroid : public detail::chamb::base_chamb_spheroid<T, Parameters>
  223. {
  224. template <typename Params>
  225. inline chamb_spheroid(Params const& params, Parameters & par)
  226. {
  227. detail::chamb::setup_chamb(params, par, this->m_proj_parm);
  228. }
  229. };
  230. #ifndef DOXYGEN_NO_DETAIL
  231. namespace detail
  232. {
  233. // Static projection
  234. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_F(srs::spar::proj_chamb, chamb_spheroid)
  235. // Factory entry(s)
  236. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_F(chamb_entry, chamb_spheroid)
  237. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(chamb_init)
  238. {
  239. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(chamb, chamb_entry);
  240. }
  241. } // namespace detail
  242. #endif // doxygen
  243. } // namespace projections
  244. }} // namespace boost::geometry
  245. #endif // BOOST_GEOMETRY_PROJECTIONS_CHAMB_HPP