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. 6 // Modifications copyright (c) 2017-2019, Oracle and/or its affiliates. 7 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle. 8 9 // Use, modification and distribution is subject to the Boost Software License, 10 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at 11 // http://www.boost.org/LICENSE_1_0.txt) 12 13 // This file is converted from PROJ4, http://trac.osgeo.org/proj 14 // PROJ4 is originally written by Gerald Evenden (then of the USGS) 15 // PROJ4 is maintained by Frank Warmerdam 16 // PROJ4 is converted to Boost.Geometry by Barend Gehrels 17 18 // Last updated version of proj: 5.0.0 19 20 // Original copyright notice: 21 22 // Permission is hereby granted, free of charge, to any person obtaining a 23 // copy of this software and associated documentation files (the "Software"), 24 // to deal in the Software without restriction, including without limitation 25 // the rights to use, copy, modify, merge, publish, distribute, sublicense, 26 // and/or sell copies of the Software, and to permit persons to whom the 27 // Software is furnished to do so, subject to the following conditions: 28 29 // The above copyright notice and this permission notice shall be included 30 // in all copies or substantial portions of the Software. 31 32 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 33 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 34 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 35 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 36 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 37 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 38 // DEALINGS IN THE SOFTWARE. 39 40 #ifndef BOOST_GEOMETRY_PROJECTIONS_NSPER_HPP 41 #define BOOST_GEOMETRY_PROJECTIONS_NSPER_HPP 42 43 #include <boost/config.hpp> 44 45 #include <boost/geometry/srs/projections/impl/base_static.hpp> 46 #include <boost/geometry/srs/projections/impl/base_dynamic.hpp> 47 #include <boost/geometry/srs/projections/impl/factory_entry.hpp> 48 #include <boost/geometry/srs/projections/impl/pj_param.hpp> 49 #include <boost/geometry/srs/projections/impl/projects.hpp> 50 51 #include <boost/geometry/util/math.hpp> 52 53 #include <boost/math/special_functions/hypot.hpp> 54 55 namespace boost { namespace geometry 56 { 57 58 namespace projections 59 { 60 #ifndef DOXYGEN_NO_DETAIL 61 namespace detail { namespace nsper 62 { 63 64 static const double epsilon10 = 1.e-10; 65 enum mode_type { 66 n_pole = 0, 67 s_pole = 1, 68 equit = 2, 69 obliq = 3 70 }; 71 72 template <typename T> 73 struct par_nsper 74 { 75 T height; 76 T sinph0; 77 T cosph0; 78 T p; 79 T rp; 80 T pn1; 81 T pfact; 82 T h; 83 T cg; 84 T sg; 85 T sw; 86 T cw; 87 mode_type mode; 88 bool tilt; 89 }; 90 91 template <typename T, typename Parameters> 92 struct base_nsper_spheroid 93 { 94 par_nsper<T> m_proj_parm; 95 96 // FORWARD(s_forward) spheroid 97 // Project coordinates from geographic (lon, lat) to cartesian (x, y) fwdboost::geometry::projections::detail::nsper::base_nsper_spheroid98 inline void fwd(Parameters const& , T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const 99 { 100 T coslam, cosphi, sinphi; 101 102 sinphi = sin(lp_lat); 103 cosphi = cos(lp_lat); 104 coslam = cos(lp_lon); 105 switch (this->m_proj_parm.mode) { 106 case obliq: 107 xy_y = this->m_proj_parm.sinph0 * sinphi + this->m_proj_parm.cosph0 * cosphi * coslam; 108 break; 109 case equit: 110 xy_y = cosphi * coslam; 111 break; 112 case s_pole: 113 xy_y = - sinphi; 114 break; 115 case n_pole: 116 xy_y = sinphi; 117 break; 118 } 119 if (xy_y < this->m_proj_parm.rp) { 120 BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) ); 121 } 122 xy_y = this->m_proj_parm.pn1 / (this->m_proj_parm.p - xy_y); 123 xy_x = xy_y * cosphi * sin(lp_lon); 124 switch (this->m_proj_parm.mode) { 125 case obliq: 126 xy_y *= (this->m_proj_parm.cosph0 * sinphi - 127 this->m_proj_parm.sinph0 * cosphi * coslam); 128 break; 129 case equit: 130 xy_y *= sinphi; 131 break; 132 case n_pole: 133 coslam = - coslam; 134 BOOST_FALLTHROUGH; 135 case s_pole: 136 xy_y *= cosphi * coslam; 137 break; 138 } 139 if (this->m_proj_parm.tilt) { 140 T yt, ba; 141 142 yt = xy_y * this->m_proj_parm.cg + xy_x * this->m_proj_parm.sg; 143 ba = 1. / (yt * this->m_proj_parm.sw * this->m_proj_parm.h + this->m_proj_parm.cw); 144 xy_x = (xy_x * this->m_proj_parm.cg - xy_y * this->m_proj_parm.sg) * this->m_proj_parm.cw * ba; 145 xy_y = yt * ba; 146 } 147 } 148 149 // INVERSE(s_inverse) spheroid 150 // Project coordinates from cartesian (x, y) to geographic (lon, lat) invboost::geometry::projections::detail::nsper::base_nsper_spheroid151 inline void inv(Parameters const& par, T xy_x, T xy_y, T& lp_lon, T& lp_lat) const 152 { 153 T rh, cosz, sinz; 154 155 if (this->m_proj_parm.tilt) { 156 T bm, bq, yt; 157 158 yt = 1./(this->m_proj_parm.pn1 - xy_y * this->m_proj_parm.sw); 159 bm = this->m_proj_parm.pn1 * xy_x * yt; 160 bq = this->m_proj_parm.pn1 * xy_y * this->m_proj_parm.cw * yt; 161 xy_x = bm * this->m_proj_parm.cg + bq * this->m_proj_parm.sg; 162 xy_y = bq * this->m_proj_parm.cg - bm * this->m_proj_parm.sg; 163 } 164 rh = boost::math::hypot(xy_x, xy_y); 165 if ((sinz = 1. - rh * rh * this->m_proj_parm.pfact) < 0.) { 166 BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) ); 167 } 168 sinz = (this->m_proj_parm.p - sqrt(sinz)) / (this->m_proj_parm.pn1 / rh + rh / this->m_proj_parm.pn1); 169 cosz = sqrt(1. - sinz * sinz); 170 if (fabs(rh) <= epsilon10) { 171 lp_lon = 0.; 172 lp_lat = par.phi0; 173 } else { 174 switch (this->m_proj_parm.mode) { 175 case obliq: 176 lp_lat = asin(cosz * this->m_proj_parm.sinph0 + xy_y * sinz * this->m_proj_parm.cosph0 / rh); 177 xy_y = (cosz - this->m_proj_parm.sinph0 * sin(lp_lat)) * rh; 178 xy_x *= sinz * this->m_proj_parm.cosph0; 179 break; 180 case equit: 181 lp_lat = asin(xy_y * sinz / rh); 182 xy_y = cosz * rh; 183 xy_x *= sinz; 184 break; 185 case n_pole: 186 lp_lat = asin(cosz); 187 xy_y = -xy_y; 188 break; 189 case s_pole: 190 lp_lat = - asin(cosz); 191 break; 192 } 193 lp_lon = atan2(xy_x, xy_y); 194 } 195 } 196 get_nameboost::geometry::projections::detail::nsper::base_nsper_spheroid197 static inline std::string get_name() 198 { 199 return "nsper_spheroid"; 200 } 201 202 }; 203 204 template <typename Params, typename Parameters, typename T> setup(Params const & params,Parameters & par,par_nsper<T> & proj_parm)205 inline void setup(Params const& params, Parameters& par, par_nsper<T>& proj_parm) 206 { 207 proj_parm.height = pj_get_param_f<T, srs::spar::h>(params, "h", srs::dpar::h); 208 if (proj_parm.height <= 0.) 209 BOOST_THROW_EXCEPTION( projection_exception(error_h_less_than_zero) ); 210 211 if (fabs(fabs(par.phi0) - geometry::math::half_pi<T>()) < epsilon10) 212 proj_parm.mode = par.phi0 < 0. ? s_pole : n_pole; 213 else if (fabs(par.phi0) < epsilon10) 214 proj_parm.mode = equit; 215 else { 216 proj_parm.mode = obliq; 217 proj_parm.sinph0 = sin(par.phi0); 218 proj_parm.cosph0 = cos(par.phi0); 219 } 220 proj_parm.pn1 = proj_parm.height / par.a; /* normalize by radius */ 221 proj_parm.p = 1. + proj_parm.pn1; 222 proj_parm.rp = 1. / proj_parm.p; 223 proj_parm.h = 1. / proj_parm.pn1; 224 proj_parm.pfact = (proj_parm.p + 1.) * proj_parm.h; 225 par.es = 0.; 226 } 227 228 229 // Near-sided perspective 230 template <typename Params, typename Parameters, typename T> setup_nsper(Params const & params,Parameters & par,par_nsper<T> & proj_parm)231 inline void setup_nsper(Params const& params, Parameters& par, par_nsper<T>& proj_parm) 232 { 233 proj_parm.tilt = false; 234 235 setup(params, par, proj_parm); 236 } 237 238 // Tilted perspective 239 template <typename Params, typename Parameters, typename T> setup_tpers(Params const & params,Parameters & par,par_nsper<T> & proj_parm)240 inline void setup_tpers(Params const& params, Parameters& par, par_nsper<T>& proj_parm) 241 { 242 T const omega = pj_get_param_r<T, srs::spar::tilt>(params, "tilt", srs::dpar::tilt); 243 T const gamma = pj_get_param_r<T, srs::spar::azi>(params, "azi", srs::dpar::azi); 244 proj_parm.tilt = true; 245 proj_parm.cg = cos(gamma); proj_parm.sg = sin(gamma); 246 proj_parm.cw = cos(omega); proj_parm.sw = sin(omega); 247 248 setup(params, par, proj_parm); 249 } 250 251 }} // namespace detail::nsper 252 #endif // doxygen 253 254 /*! 255 \brief Near-sided perspective projection 256 \ingroup projections 257 \tparam Geographic latlong point type 258 \tparam Cartesian xy point type 259 \tparam Parameters parameter type 260 \par Projection characteristics 261 - Azimuthal 262 - Spheroid 263 \par Projection parameters 264 - h: Height 265 \par Example 266 \image html ex_nsper.gif 267 */ 268 template <typename T, typename Parameters> 269 struct nsper_spheroid : public detail::nsper::base_nsper_spheroid<T, Parameters> 270 { 271 template <typename Params> nsper_spheroidboost::geometry::projections::nsper_spheroid272 inline nsper_spheroid(Params const& params, Parameters & par) 273 { 274 detail::nsper::setup_nsper(params, par, this->m_proj_parm); 275 } 276 }; 277 278 /*! 279 \brief Tilted perspective projection 280 \ingroup projections 281 \tparam Geographic latlong point type 282 \tparam Cartesian xy point type 283 \tparam Parameters parameter type 284 \par Projection characteristics 285 - Azimuthal 286 - Spheroid 287 \par Projection parameters 288 - tilt: Tilt, or Omega (real) 289 - azi: Azimuth (or Gamma) (real) 290 - h: Height 291 \par Example 292 \image html ex_tpers.gif 293 */ 294 template <typename T, typename Parameters> 295 struct tpers_spheroid : public detail::nsper::base_nsper_spheroid<T, Parameters> 296 { 297 template <typename Params> tpers_spheroidboost::geometry::projections::tpers_spheroid298 inline tpers_spheroid(Params const& params, Parameters & par) 299 { 300 detail::nsper::setup_tpers(params, par, this->m_proj_parm); 301 } 302 }; 303 304 #ifndef DOXYGEN_NO_DETAIL 305 namespace detail 306 { 307 308 // Static projection BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_F(srs::spar::proj_nsper,nsper_spheroid)309 BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_F(srs::spar::proj_nsper, nsper_spheroid) 310 BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_F(srs::spar::proj_tpers, tpers_spheroid) 311 312 // Factory entry(s) 313 BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI(nsper_entry, nsper_spheroid) 314 BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI(tpers_entry, tpers_spheroid) 315 316 BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(nsper_init) 317 { 318 BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(nsper, nsper_entry) 319 BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(tpers, tpers_entry) 320 } 321 322 } // namespace detail 323 #endif // doxygen 324 325 } // namespace projections 326 327 }} // namespace boost::geometry 328 329 #endif // BOOST_GEOMETRY_PROJECTIONS_NSPER_HPP 330 331