chamb.hpp 11 KB

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