| 1 |
// Boost.Geometry - gis-projections (based on PROJ4) |
| 2 |
|
| 3 |
// Copyright (c) 2008-2015 Barend Gehrels, Amsterdam, the Netherlands. |
| 4 |
|
| 5 |
// This file was modified by Oracle on 2017, 2018, 2019, 2022. |
| 6 |
// Modifications Copyright (c) 2017, 2026, Oracle and/or its affiliates. |
| 7 |
// Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle. |
| 8 |
// Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle. |
| 9 |
|
| 10 |
// Use, modification and distribution is subject to the Boost Software License, |
| 11 |
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
| 12 |
// http://www.boost.org/LICENSE_1_0.txt) |
| 13 |
|
| 14 |
// This file is converted from PROJ4, http://trac.osgeo.org/proj |
| 15 |
// PROJ4 is originally written by Gerald Evenden (then of the USGS) |
| 16 |
// PROJ4 is maintained by Frank Warmerdam |
| 17 |
// PROJ4 is converted to Boost.Geometry by Barend Gehrels |
| 18 |
|
| 19 |
// Last updated version of proj: 5.0.0 |
| 20 |
|
| 21 |
// Original copyright notice: |
| 22 |
|
| 23 |
// Copyright (c) 2003, 2006 Gerald I. Evenden |
| 24 |
|
| 25 |
// Permission is hereby granted, free of charge, to any person obtaining a |
| 26 |
// copy of this software and associated documentation files (the "Software"), |
| 27 |
// to deal in the Software without restriction, including without limitation |
| 28 |
// the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 29 |
// and/or sell copies of the Software, and to permit persons to whom the |
| 30 |
// Software is furnished to do so, subject to the following conditions: |
| 31 |
|
| 32 |
// The above copyright notice and this permission notice shall be included |
| 33 |
// in all copies or substantial portions of the Software. |
| 34 |
|
| 35 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 36 |
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 37 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 38 |
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 39 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 40 |
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 41 |
// DEALINGS IN THE SOFTWARE. |
| 42 |
|
| 43 |
#ifndef BOOST_GEOMETRY_PROJECTIONS_OMERC_HPP |
| 44 |
#define BOOST_GEOMETRY_PROJECTIONS_OMERC_HPP |
| 45 |
|
| 46 |
#include <boost/geometry/util/math.hpp> |
| 47 |
|
| 48 |
#include <boost/geometry/srs/projections/impl/base_static.hpp> |
| 49 |
#include <boost/geometry/srs/projections/impl/base_dynamic.hpp> |
| 50 |
#include <boost/geometry/srs/projections/impl/factory_entry.hpp> |
| 51 |
#include <boost/geometry/srs/projections/impl/pj_param.hpp> |
| 52 |
#include <boost/geometry/srs/projections/impl/pj_phi2.hpp> |
| 53 |
#include <boost/geometry/srs/projections/impl/pj_tsfn.hpp> |
| 54 |
#include <boost/geometry/srs/projections/impl/projects.hpp> |
| 55 |
|
| 56 |
namespace boost { namespace geometry |
| 57 |
{ |
| 58 |
|
| 59 |
namespace projections |
| 60 |
{ |
| 61 |
#ifndef DOXYGEN_NO_DETAIL |
| 62 |
namespace detail { namespace omerc |
| 63 |
{ |
| 64 |
template <typename T> |
| 65 |
struct par_omerc |
| 66 |
{ |
| 67 |
T A, B, E, AB, ArB, BrA, rB, singam, cosgam, sinrot, cosrot; |
| 68 |
T v_pole_n, v_pole_s, u_0; |
| 69 |
bool no_rot; |
| 70 |
}; |
| 71 |
|
| 72 |
static const double tolerance = 1.e-7; |
| 73 |
static const double epsilon = 1.e-10; |
| 74 |
|
| 75 |
template <typename T, typename Parameters> |
| 76 |
struct base_omerc_ellipsoid |
| 77 |
{ |
| 78 |
par_omerc<T> m_proj_parm; |
| 79 |
|
| 80 |
// FORWARD(e_forward) ellipsoid |
| 81 |
// Project coordinates from geographic (lon, lat) to cartesian (x, y) |
| 82 |
inline void fwd(Parameters const& par, T const& lp_lon, T const& lp_lat, T& xy_x, |
| 83 |
T& xy_y) const |
| 84 |
{ |
| 85 |
static const T half_pi = detail::half_pi<T>(); |
| 86 |
|
| 87 |
T s, t, U, V, W, temp, u, v; |
| 88 |
|
| 89 |
if (fabs(fabs(lp_lat) - half_pi) > epsilon) { |
| 90 |
W = this->m_proj_parm.E / math::pow(pj_tsfn(lp_lat, sin(lp_lat), par.e), |
| 91 |
this->m_proj_parm.B); |
| 92 |
temp = 1. / W; |
| 93 |
s = .5 * (W - temp); |
| 94 |
t = .5 * (W + temp); |
| 95 |
V = sin(this->m_proj_parm.B * lp_lon); |
| 96 |
U = (s * this->m_proj_parm.singam - V * this->m_proj_parm.cosgam) / t; |
| 97 |
if (fabs(fabs(U) - 1.0) < epsilon) { |
| 98 |
BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) ); |
| 99 |
} |
| 100 |
v = 0.5 * this->m_proj_parm.ArB * log((1. - U)/(1. + U)); |
| 101 |
temp = cos(this->m_proj_parm.B * lp_lon); |
| 102 |
if(fabs(temp) < tolerance) { |
| 103 |
u = this->m_proj_parm.A * lp_lon; |
| 104 |
} else { |
| 105 |
u = this->m_proj_parm.ArB * atan2((s * this->m_proj_parm.cosgam |
| 106 |
+ V * this->m_proj_parm.singam), temp); |
| 107 |
} |
| 108 |
} else { |
| 109 |
v = lp_lat > 0 ? this->m_proj_parm.v_pole_n : this->m_proj_parm.v_pole_s; |
| 110 |
u = this->m_proj_parm.ArB * lp_lat; |
| 111 |
} |
| 112 |
if (this->m_proj_parm.no_rot) { |
| 113 |
xy_x = u; |
| 114 |
xy_y = v; |
| 115 |
} else { |
| 116 |
u -= this->m_proj_parm.u_0; |
| 117 |
xy_x = v * this->m_proj_parm.cosrot + u * this->m_proj_parm.sinrot; |
| 118 |
xy_y = u * this->m_proj_parm.cosrot - v * this->m_proj_parm.sinrot; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
// INVERSE(e_inverse) ellipsoid |
| 123 |
// Project coordinates from cartesian (x, y) to geographic (lon, lat) |
| 124 |
inline void inv(Parameters const& par, T const& xy_x, T const& xy_y, T& lp_lon, |
| 125 |
T& lp_lat) const |
| 126 |
{ |
| 127 |
static const T half_pi = detail::half_pi<T>(); |
| 128 |
|
| 129 |
T u, v, Qp, Sp, Tp, Vp, Up; |
| 130 |
|
| 131 |
if (this->m_proj_parm.no_rot) { |
| 132 |
v = xy_y; |
| 133 |
u = xy_x; |
| 134 |
} else { |
| 135 |
v = xy_x * this->m_proj_parm.cosrot - xy_y * this->m_proj_parm.sinrot; |
| 136 |
u = xy_y * this->m_proj_parm.cosrot + xy_x * this->m_proj_parm.sinrot |
| 137 |
+ this->m_proj_parm.u_0; |
| 138 |
} |
| 139 |
Qp = exp(- this->m_proj_parm.BrA * v); |
| 140 |
Sp = .5 * (Qp - 1. / Qp); |
| 141 |
Tp = .5 * (Qp + 1. / Qp); |
| 142 |
Vp = sin(this->m_proj_parm.BrA * u); |
| 143 |
Up = (Vp * this->m_proj_parm.cosgam + Sp * this->m_proj_parm.singam) / Tp; |
| 144 |
if (fabs(fabs(Up) - 1.) < epsilon) { |
| 145 |
lp_lon = 0.; |
| 146 |
lp_lat = Up < 0. ? -half_pi : half_pi; |
| 147 |
} else { |
| 148 |
lp_lat = this->m_proj_parm.E / sqrt((1. + Up) / (1. - Up)); |
| 149 |
if ((lp_lat = pj_phi2(math::pow(lp_lat, T(1) / this->m_proj_parm.B), par.e)) |
| 150 |
== HUGE_VAL) { |
| 151 |
BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) ); |
| 152 |
} |
| 153 |
lp_lon = - this->m_proj_parm.rB * atan2((Sp * this->m_proj_parm.cosgam - |
| 154 |
Vp * this->m_proj_parm.singam), cos(this->m_proj_parm.BrA * u)); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
static inline std::string get_name() |
| 159 |
{ |
| 160 |
return "omerc_ellipsoid"; |
| 161 |
} |
| 162 |
|
| 163 |
}; |
| 164 |
|
| 165 |
// Oblique Mercator |
| 166 |
template <typename Params, typename Parameters, typename T> |
| 167 |
inline void setup_omerc(Params const& params, Parameters & par, par_omerc<T>& proj_parm) |
| 168 |
{ |
| 169 |
static const T fourth_pi = detail::fourth_pi<T>(); |
| 170 |
static const T half_pi = detail::half_pi<T>(); |
| 171 |
static const T pi = detail::pi<T>(); |
| 172 |
static const T two_pi = detail::two_pi<T>(); |
| 173 |
|
| 174 |
T con, com, cosph0, D, F, H, L, sinph0, p, J, gamma=0, |
| 175 |
gamma0, lamc=0, lam1=0, lam2=0, phi1=0, phi2=0, alpha_c=0; |
| 176 |
int alp, gam, no_off = 0; |
| 177 |
|
| 178 |
proj_parm.no_rot = pj_get_param_b<srs::spar::no_rot>(params, "no_rot", |
| 179 |
srs::dpar::no_rot); |
| 180 |
alp = pj_param_r<srs::spar::alpha>(params, "alpha", srs::dpar::alpha, alpha_c); |
| 181 |
gam = pj_param_r<srs::spar::gamma>(params, "gamma", srs::dpar::gamma, gamma); |
| 182 |
if (alp || gam) { |
| 183 |
lamc = pj_get_param_r<T, srs::spar::lonc>(params, "lonc", srs::dpar::lonc); |
| 184 |
// NOTE: This is needed for Hotline Oblique Mercator variant A projection |
| 185 |
no_off = pj_get_param_b<srs::spar::no_off>(params, "no_off", srs::dpar::no_off); |
| 186 |
} else { |
| 187 |
lam1 = pj_get_param_r<T, srs::spar::lon_1>(params, "lon_1", srs::dpar::lon_1); |
| 188 |
phi1 = pj_get_param_r<T, srs::spar::lat_1>(params, "lat_1", srs::dpar::lat_1); |
| 189 |
lam2 = pj_get_param_r<T, srs::spar::lon_2>(params, "lon_2", srs::dpar::lon_2); |
| 190 |
phi2 = pj_get_param_r<T, srs::spar::lat_2>(params, "lat_2", srs::dpar::lat_2); |
| 191 |
if (fabs(phi1 - phi2) <= tolerance || |
| 192 |
(con = fabs(phi1)) <= tolerance || |
| 193 |
fabs(con - half_pi) <= tolerance || |
| 194 |
fabs(fabs(par.phi0) - half_pi) <= tolerance || |
| 195 |
fabs(fabs(phi2) - half_pi) <= tolerance) |
| 196 |
BOOST_THROW_EXCEPTION( projection_exception(error_lat_0_or_alpha_eq_90) ); |
| 197 |
} |
| 198 |
com = sqrt(par.one_es); |
| 199 |
if (fabs(par.phi0) > epsilon) { |
| 200 |
sinph0 = sin(par.phi0); |
| 201 |
cosph0 = cos(par.phi0); |
| 202 |
con = 1. - par.es * sinph0 * sinph0; |
| 203 |
proj_parm.B = cosph0 * cosph0; |
| 204 |
proj_parm.B = sqrt(1. + par.es * proj_parm.B * proj_parm.B / par.one_es); |
| 205 |
proj_parm.A = proj_parm.B * par.k0 * com / con; |
| 206 |
D = proj_parm.B * com / (cosph0 * sqrt(con)); |
| 207 |
if ((F = D * D - 1.) <= 0.) |
| 208 |
F = 0.; |
| 209 |
else { |
| 210 |
F = sqrt(F); |
| 211 |
if (par.phi0 < 0.) |
| 212 |
F = -F; |
| 213 |
} |
| 214 |
proj_parm.E = F += D; |
| 215 |
proj_parm.E *= math::pow(pj_tsfn(par.phi0, sinph0, par.e), proj_parm.B); |
| 216 |
} else { |
| 217 |
proj_parm.B = 1. / com; |
| 218 |
proj_parm.A = par.k0; |
| 219 |
proj_parm.E = D = F = 1.; |
| 220 |
} |
| 221 |
if (alp || gam) { |
| 222 |
if (alp) { |
| 223 |
gamma0 = aasin(sin(alpha_c) / D); |
| 224 |
if (!gam) |
| 225 |
gamma = alpha_c; |
| 226 |
} else |
| 227 |
alpha_c = aasin(D*sin(gamma0 = gamma)); |
| 228 |
par.lam0 = lamc - aasin(.5 * (F - 1. / F) * |
| 229 |
tan(gamma0)) / proj_parm.B; |
| 230 |
} else { |
| 231 |
H = math::pow(pj_tsfn(phi1, sin(phi1), par.e), proj_parm.B); |
| 232 |
L = math::pow(pj_tsfn(phi2, sin(phi2), par.e), proj_parm.B); |
| 233 |
F = proj_parm.E / H; |
| 234 |
p = (L - H) / (L + H); |
| 235 |
J = proj_parm.E * proj_parm.E; |
| 236 |
J = (J - L * H) / (J + L * H); |
| 237 |
if ((con = lam1 - lam2) < -pi) |
| 238 |
lam2 -= two_pi; |
| 239 |
else if (con > pi) |
| 240 |
lam2 += two_pi; |
| 241 |
par.lam0 = adjlon(.5 * (lam1 + lam2) - atan( |
| 242 |
J * tan(.5 * proj_parm.B * (lam1 - lam2)) / p) / proj_parm.B); |
| 243 |
gamma0 = atan(2. * sin(proj_parm.B * adjlon(lam1 - par.lam0)) / |
| 244 |
(F - 1. / F)); |
| 245 |
gamma = alpha_c = aasin(D * sin(gamma0)); |
| 246 |
} |
| 247 |
proj_parm.singam = sin(gamma0); |
| 248 |
proj_parm.cosgam = cos(gamma0); |
| 249 |
proj_parm.sinrot = sin(gamma); |
| 250 |
proj_parm.cosrot = cos(gamma); |
| 251 |
proj_parm.BrA = 1. / (proj_parm.ArB = proj_parm.A * (proj_parm.rB = 1. / proj_parm.B)); |
| 252 |
proj_parm.AB = proj_parm.A * proj_parm.B; |
| 253 |
if (no_off) |
| 254 |
proj_parm.u_0 = 0; |
| 255 |
else { |
| 256 |
proj_parm.u_0 = fabs(proj_parm.ArB * atan(sqrt(D * D - 1.) / cos(alpha_c))); |
| 257 |
if (par.phi0 < 0.) |
| 258 |
proj_parm.u_0 = - proj_parm.u_0; |
| 259 |
} |
| 260 |
F = 0.5 * gamma0; |
| 261 |
proj_parm.v_pole_n = proj_parm.ArB * log(tan(fourth_pi - F)); |
| 262 |
proj_parm.v_pole_s = proj_parm.ArB * log(tan(fourth_pi + F)); |
| 263 |
} |
| 264 |
|
| 265 |
}} // namespace detail::omerc |
| 266 |
#endif // doxygen |
| 267 |
|
| 268 |
/*! |
| 269 |
\brief Oblique Mercator projection |
| 270 |
\ingroup projections |
| 271 |
\tparam Geographic latlong point type |
| 272 |
\tparam Cartesian xy point type |
| 273 |
\tparam Parameters parameter type |
| 274 |
\par Projection characteristics |
| 275 |
- Cylindrical |
| 276 |
- Spheroid |
| 277 |
- Ellipsoid |
| 278 |
\par Projection parameters |
| 279 |
- no_rot: No rotation |
| 280 |
- alpha: Alpha (degrees) |
| 281 |
- gamma: Gamma (degrees) |
| 282 |
- no_off: Do not offset origin to center of projection |
| 283 |
(useful for Hotline Oblique Mercator variant A). |
| 284 |
- lonc: Longitude (only used if alpha (or gamma) is specified) (degrees) |
| 285 |
- lon_1 (degrees) |
| 286 |
- lat_1: Latitude of first standard parallel (degrees) |
| 287 |
- lon_2 (degrees) |
| 288 |
- lat_2: Latitude of second standard parallel (degrees) |
| 289 |
- no_uoff: deprecated (string) |
| 290 |
\par Example |
| 291 |
\image html ex_omerc.gif |
| 292 |
*/ |
| 293 |
template <typename T, typename Parameters> |
| 294 |
struct omerc_ellipsoid : public detail::omerc::base_omerc_ellipsoid<T, Parameters> |
| 295 |
{ |
| 296 |
template <typename Params> |
| 297 |
inline omerc_ellipsoid(Params const& params, Parameters & par) |
| 298 |
{ |
| 299 |
detail::omerc::setup_omerc(params, par, this->m_proj_parm); |
| 300 |
} |
| 301 |
}; |
| 302 |
|
| 303 |
#ifndef DOXYGEN_NO_DETAIL |
| 304 |
namespace detail |
| 305 |
{ |
| 306 |
|
| 307 |
// Static projection |
| 308 |
BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_FI(srs::spar::proj_omerc, omerc_ellipsoid) |
| 309 |
|
| 310 |
// Factory entry(s) |
| 311 |
BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI(omerc_entry, omerc_ellipsoid) |
| 312 |
|
| 313 |
BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(omerc_init) |
| 314 |
{ |
| 315 |
BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(omerc, omerc_entry) |
| 316 |
} |
| 317 |
|
| 318 |
} // namespace detail |
| 319 |
#endif // doxygen |
| 320 |
|
| 321 |
} // namespace projections |
| 322 |
|
| 323 |
}} // namespace boost::geometry |
| 324 |
|
| 325 |
#endif // BOOST_GEOMETRY_PROJECTIONS_OMERC_HPP |
| 326 |
|