nzmg.hpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. // Purpose: Implementation of the nzmg (New Zealand Map Grid) projection.
  16. // Very loosely based upon DMA code by Bradford W. Drew
  17. // Author: Gerald Evenden
  18. // Copyright (c) 1995, Gerald Evenden
  19. // Permission is hereby granted, free of charge, to any person obtaining a
  20. // copy of this software and associated documentation files (the "Software"),
  21. // to deal in the Software without restriction, including without limitation
  22. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  23. // and/or sell copies of the Software, and to permit persons to whom the
  24. // Software is furnished to do so, subject to the following conditions:
  25. // The above copyright notice and this permission notice shall be included
  26. // in all copies or substantial portions of the Software.
  27. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  28. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  29. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  30. // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  31. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  32. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  33. // DEALINGS IN THE SOFTWARE.
  34. #ifndef BOOST_GEOMETRY_PROJECTIONS_NZMG_HPP
  35. #define BOOST_GEOMETRY_PROJECTIONS_NZMG_HPP
  36. #include <boost/geometry/util/math.hpp>
  37. #include <boost/geometry/srs/projections/impl/base_static.hpp>
  38. #include <boost/geometry/srs/projections/impl/base_dynamic.hpp>
  39. #include <boost/geometry/srs/projections/impl/projects.hpp>
  40. #include <boost/geometry/srs/projections/impl/factory_entry.hpp>
  41. #include <boost/geometry/srs/projections/impl/pj_zpoly1.hpp>
  42. namespace boost { namespace geometry
  43. {
  44. namespace srs { namespace par4
  45. {
  46. struct nzmg {}; // New Zealand Map Grid
  47. }} //namespace srs::par4
  48. namespace projections
  49. {
  50. #ifndef DOXYGEN_NO_DETAIL
  51. namespace detail { namespace nzmg
  52. {
  53. static const double epsilon = 1e-10;
  54. static const int Nbf = 5;
  55. static const int Ntpsi = 9;
  56. static const int Ntphi = 8;
  57. template <typename T>
  58. inline T sec5_to_rad() { return 0.4848136811095359935899141023; }
  59. template <typename T>
  60. inline T rad_to_sec5() { return 2.062648062470963551564733573; }
  61. template <typename T>
  62. inline const pj_complex<T> * bf()
  63. {
  64. static const pj_complex<T> result[] = {
  65. {.7557853228, 0.0},
  66. {.249204646, .003371507},
  67. {-.001541739, .041058560},
  68. {-.10162907, .01727609},
  69. {-.26623489, -.36249218},
  70. {-.6870983, -1.1651967}
  71. };
  72. return result;
  73. }
  74. template <typename T>
  75. inline const T * tphi()
  76. {
  77. static const T result[] = { 1.5627014243, .5185406398, -.03333098,
  78. -.1052906, -.0368594, .007317,
  79. .01220, .00394, -.0013 };
  80. return result;
  81. }
  82. template <typename T>
  83. inline const T * tpsi()
  84. {
  85. static const T result[] = { .6399175073, -.1358797613, .063294409, -.02526853, .0117879,
  86. -.0055161, .0026906, -.001333, .00067, -.00034 };
  87. return result;
  88. }
  89. // template class, using CRTP to implement forward/inverse
  90. template <typename T, typename Parameters>
  91. struct base_nzmg_ellipsoid
  92. : public base_t_fi<base_nzmg_ellipsoid<T, Parameters>, T, Parameters>
  93. {
  94. inline base_nzmg_ellipsoid(const Parameters& par)
  95. : base_t_fi<base_nzmg_ellipsoid<T, Parameters>, T, Parameters>(*this, par)
  96. {}
  97. // FORWARD(e_forward) ellipsoid
  98. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  99. inline void fwd(T& lp_lon, T& lp_lat, T& xy_x, T& xy_y) const
  100. {
  101. static const T rad_to_sec5 = nzmg::rad_to_sec5<T>();
  102. pj_complex<T> p;
  103. const T * C;
  104. int i;
  105. lp_lat = (lp_lat - this->m_par.phi0) * rad_to_sec5;
  106. for (p.r = *(C = tpsi<T>() + (i = Ntpsi)); i ; --i)
  107. p.r = *--C + lp_lat * p.r;
  108. p.r *= lp_lat;
  109. p.i = lp_lon;
  110. p = pj_zpoly1(p, bf<T>(), Nbf);
  111. xy_x = p.i;
  112. xy_y = p.r;
  113. }
  114. // INVERSE(e_inverse) ellipsoid
  115. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  116. inline void inv(T& xy_x, T& xy_y, T& lp_lon, T& lp_lat) const
  117. {
  118. static const T sec5_to_rad = nzmg::sec5_to_rad<T>();
  119. int nn, i;
  120. pj_complex<T> p, f, fp, dp;
  121. T den;
  122. const T* C;
  123. p.r = xy_y;
  124. p.i = xy_x;
  125. for (nn = 20; nn ;--nn) {
  126. f = pj_zpolyd1(p, bf<T>(), Nbf, &fp);
  127. f.r -= xy_y;
  128. f.i -= xy_x;
  129. den = fp.r * fp.r + fp.i * fp.i;
  130. p.r += dp.r = -(f.r * fp.r + f.i * fp.i) / den;
  131. p.i += dp.i = -(f.i * fp.r - f.r * fp.i) / den;
  132. if ((fabs(dp.r) + fabs(dp.i)) <= epsilon)
  133. break;
  134. }
  135. if (nn) {
  136. lp_lon = p.i;
  137. for (lp_lat = *(C = tphi<T>() + (i = Ntphi)); i ; --i)
  138. lp_lat = *--C + p.r * lp_lat;
  139. lp_lat = this->m_par.phi0 + p.r * lp_lat * sec5_to_rad;
  140. } else
  141. lp_lon = lp_lat = HUGE_VAL;
  142. }
  143. static inline std::string get_name()
  144. {
  145. return "nzmg_ellipsoid";
  146. }
  147. };
  148. // New Zealand Map Grid
  149. template <typename Parameters>
  150. inline void setup_nzmg(Parameters& par)
  151. {
  152. typedef typename Parameters::type calc_t;
  153. static const calc_t d2r = geometry::math::d2r<calc_t>();
  154. /* force to International major axis */
  155. par.ra = 1. / (par.a = 6378388.0);
  156. par.lam0 = 173. * d2r;
  157. par.phi0 = -41. * d2r;
  158. par.x0 = 2510000.;
  159. par.y0 = 6023150.;
  160. }
  161. }} // namespace detail::nzmg
  162. #endif // doxygen
  163. /*!
  164. \brief New Zealand Map Grid projection
  165. \ingroup projections
  166. \tparam Geographic latlong point type
  167. \tparam Cartesian xy point type
  168. \tparam Parameters parameter type
  169. \par Projection characteristics
  170. - Fixed Earth
  171. \par Example
  172. \image html ex_nzmg.gif
  173. */
  174. template <typename T, typename Parameters>
  175. struct nzmg_ellipsoid : public detail::nzmg::base_nzmg_ellipsoid<T, Parameters>
  176. {
  177. inline nzmg_ellipsoid(const Parameters& par) : detail::nzmg::base_nzmg_ellipsoid<T, Parameters>(par)
  178. {
  179. detail::nzmg::setup_nzmg(this->m_par);
  180. }
  181. };
  182. #ifndef DOXYGEN_NO_DETAIL
  183. namespace detail
  184. {
  185. // Static projection
  186. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION(srs::par4::nzmg, nzmg_ellipsoid, nzmg_ellipsoid)
  187. // Factory entry(s)
  188. template <typename T, typename Parameters>
  189. class nzmg_entry : public detail::factory_entry<T, Parameters>
  190. {
  191. public :
  192. virtual base_v<T, Parameters>* create_new(const Parameters& par) const
  193. {
  194. return new base_v_fi<nzmg_ellipsoid<T, Parameters>, T, Parameters>(par);
  195. }
  196. };
  197. template <typename T, typename Parameters>
  198. inline void nzmg_init(detail::base_factory<T, Parameters>& factory)
  199. {
  200. factory.add_to_factory("nzmg", new nzmg_entry<T, Parameters>);
  201. }
  202. } // namespace detail
  203. #endif // doxygen
  204. } // namespace projections
  205. }} // namespace boost::geometry
  206. #endif // BOOST_GEOMETRY_PROJECTIONS_NZMG_HPP