pj_generic_inverse.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Boost.Geometry
  2. // Copyright (c) 2022 Adam Wulkiewicz, Lodz, Poland.
  3. // Copyright (c) 2022, Oracle and/or its affiliates.
  4. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  5. // Use, modification and distribution is subject to the Boost Software License,
  6. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. // This file is converted from PROJ, https://github.com/OSGeo/PROJ
  9. // Last updated version of proj: 9.0.0
  10. // Original copyright notice:
  11. /******************************************************************************
  12. *
  13. * Project: PROJ
  14. * Purpose: Generic method to compute inverse projection from forward method
  15. * Author: Even Rouault <even dot rouault at spatialys dot com>
  16. *
  17. ******************************************************************************
  18. * Copyright (c) 2018, Even Rouault <even dot rouault at spatialys dot com>
  19. *
  20. * Permission is hereby granted, free of charge, to any person obtaining a
  21. * copy of this software and associated documentation files (the "Software"),
  22. * to deal in the Software without restriction, including without limitation
  23. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  24. * and/or sell copies of the Software, and to permit persons to whom the
  25. * Software is furnished to do so, subject to the following conditions:
  26. *
  27. * The above copyright notice and this permission notice shall be included
  28. * in all copies or substantial portions of the Software.
  29. *
  30. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  31. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  32. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  33. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  34. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  35. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  36. * DEALINGS IN THE SOFTWARE.
  37. ****************************************************************************/
  38. #ifndef BOOST_GEOMETRY_PROJECTIONS_IMPL_PJ_GENERIC_INVERSE_HPP
  39. #define BOOST_GEOMETRY_PROJECTIONS_IMPL_PJ_GENERIC_INVERSE_HPP
  40. #include <algorithm>
  41. #include <cmath>
  42. #include <boost/geometry/util/math.hpp>
  43. /** Compute (lam, phi) corresponding to input (xy_x, xy_y) for projection P.
  44. *
  45. * Uses Newton-Raphson method, extended to 2D variables, that is using
  46. * inversion of the Jacobian 2D matrix of partial derivatives. The derivatives
  47. * are estimated numerically from the P->fwd method evaluated at close points.
  48. *
  49. * Note: thresholds used have been verified to work with adams_ws2 and wink2
  50. *
  51. * Starts with initial guess provided by user in lpInitial
  52. */
  53. namespace boost { namespace geometry { namespace projections
  54. {
  55. namespace detail
  56. {
  57. template <typename T, typename Parameters, typename Projection>
  58. void pj_generic_inverse_2d(T const& xy_x,
  59. T const& xy_y,
  60. Parameters const& par,
  61. Projection const& proj,
  62. T& lp_lat,
  63. T& lp_lon)
  64. {
  65. T deriv_lam_X = 0;
  66. T deriv_lam_Y = 0;
  67. T deriv_phi_X = 0;
  68. T deriv_phi_Y = 0;
  69. for (int i = 0; i < 15; i++)
  70. {
  71. T xyApprox_x;
  72. T xyApprox_y;
  73. proj->fwd(par, lp_lon, lp_lat, xyApprox_x, xyApprox_y);
  74. T const deltaX = xyApprox_x - xy_x;
  75. T const deltaY = xyApprox_y - xy_y;
  76. if (fabs(deltaX) < 1e-10 && fabs(deltaY) < 1e-10) return;
  77. if (fabs(deltaX) > 1e-6 || fabs(deltaY) > 1e-6)
  78. {
  79. // Compute Jacobian matrix (only if we aren't close to the final
  80. // result to speed things a bit)
  81. T lp2_lat;
  82. T lp2_lon;
  83. T xy2_x;
  84. T xy2_y;
  85. T const dLam = lp_lat > 0 ? -1e-6 : 1e-6;
  86. lp2_lat = lp_lat + dLam;
  87. lp2_lon = lp_lon;
  88. proj->fwd(par, lp2_lon, lp2_lat, xy2_x, xy2_y);
  89. //xy2 = P->fwd(lp2, P);
  90. T const deriv_X_lam = (xy2_x - xyApprox_x) / dLam;
  91. T const deriv_Y_lam = (xy2_y - xyApprox_y) / dLam;
  92. T const dPhi = lp_lon > 0 ? -1e-6 : 1e-6;
  93. lp2_lat = lp_lat;
  94. lp2_lon = lp_lon + dPhi;
  95. proj->fwd(par, lp2_lon, lp2_lat, xy2_x, xy2_y);
  96. //xy2 = P->fwd(lp2, P);
  97. T const deriv_X_phi = (xy2_x - xyApprox_x) / dPhi;
  98. T const deriv_Y_phi = (xy2_y - xyApprox_y) / dPhi;
  99. // Inverse of Jacobian matrix
  100. T const det = deriv_X_lam * deriv_Y_phi - deriv_X_phi * deriv_Y_lam;
  101. if (det != 0)
  102. {
  103. deriv_lam_X = deriv_Y_phi / det;
  104. deriv_lam_Y = -deriv_X_phi / det;
  105. deriv_phi_X = -deriv_Y_lam / det;
  106. deriv_phi_Y = deriv_X_lam / det;
  107. }
  108. }
  109. if (xy_x != 0)
  110. {
  111. // Limit the amplitude of correction to avoid overshoots due to
  112. // bad initial guess
  113. T const delta_lam = (std::max)(
  114. (std::min)(deltaX * deriv_lam_X + deltaY * deriv_lam_Y, 0.3),
  115. -0.3);
  116. lp_lat -= delta_lam;
  117. if (lp_lat < -math::pi<T>())
  118. lp_lat = -math::pi<T>();
  119. else if (lp_lat > math::pi<T>())
  120. lp_lat = math::pi<T>();
  121. }
  122. if (xy_y != 0)
  123. {
  124. T const delta_phi = (std::max)(
  125. (std::min)(deltaX * deriv_phi_X + deltaY * deriv_phi_Y, 0.3),
  126. -0.3);
  127. lp_lon -= delta_phi;
  128. static T const half_pi = math::half_pi<T>();
  129. if (lp_lon < -half_pi)
  130. lp_lon = -half_pi;
  131. else if (lp_lon > half_pi)
  132. lp_lon = half_pi;
  133. }
  134. }
  135. //pj_ctx_set_errno(P->ctx, PJD_ERR_NON_CONVERGENT);
  136. }
  137. } // namespace detail
  138. }}} // namespace boost::geometry::projections
  139. #endif // BOOST_GEOMETRY_PROJECTIONS_IMPL_PJ_GENERIC_INVERSE_HPP