1 // Boost.Geometry - gis-projections (based on PROJ4)
3 // Copyright (c) 2008-2015 Barend Gehrels, Amsterdam, the Netherlands.
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.
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)
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
18 // Last updated version of proj: 5.0.0
20 // Original copyright notice:
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:
29 // The above copyright notice and this permission notice shall be included
30 // in all copies or substantial portions of the Software.
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.
40 #ifndef BOOST_GEOMETRY_PROJECTIONS_NSPER_HPP
41 #define BOOST_GEOMETRY_PROJECTIONS_NSPER_HPP
43 #include <boost/config.hpp>
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>
51 #include <boost/geometry/util/math.hpp>
53 #include <boost/math/special_functions/hypot.hpp>
55 namespace boost { namespace geometry
60 #ifndef DOXYGEN_NO_DETAIL
61 namespace detail { namespace nsper
64 static const double epsilon10 = 1.e-10;
91 template <typename T, typename Parameters>
92 struct base_nsper_spheroid
94 par_nsper<T> m_proj_parm;
96 // FORWARD(s_forward) spheroid
97 // Project coordinates from geographic (lon, lat) to cartesian (x, y)
98 inline void fwd(Parameters const& , T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
100 T coslam, cosphi, sinphi;
102 sinphi = sin(lp_lat);
103 cosphi = cos(lp_lat);
104 coslam = cos(lp_lon);
105 switch (this->m_proj_parm.mode) {
107 xy_y = this->m_proj_parm.sinph0 * sinphi + this->m_proj_parm.cosph0 * cosphi * coslam;
110 xy_y = cosphi * coslam;
119 if (xy_y < this->m_proj_parm.rp) {
120 BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
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) {
126 xy_y *= (this->m_proj_parm.cosph0 * sinphi -
127 this->m_proj_parm.sinph0 * cosphi * coslam);
136 xy_y *= cosphi * coslam;
139 if (this->m_proj_parm.tilt) {
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;
149 // INVERSE(s_inverse) spheroid
150 // Project coordinates from cartesian (x, y) to geographic (lon, lat)
151 inline void inv(Parameters const& par, T xy_x, T xy_y, T& lp_lon, T& lp_lat) const
155 if (this->m_proj_parm.tilt) {
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;
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) );
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) {
174 switch (this->m_proj_parm.mode) {
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;
181 lp_lat = asin(xy_y * sinz / rh);
190 lp_lat = - asin(cosz);
193 lp_lon = atan2(xy_x, xy_y);
197 static inline std::string get_name()
199 return "nsper_spheroid";
204 template <typename Params, typename Parameters, typename T>
205 inline void setup(Params const& params, Parameters& par, par_nsper<T>& proj_parm)
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) );
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;
216 proj_parm.mode = obliq;
217 proj_parm.sinph0 = sin(par.phi0);
218 proj_parm.cosph0 = cos(par.phi0);
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;
229 // Near-sided perspective
230 template <typename Params, typename Parameters, typename T>
231 inline void setup_nsper(Params const& params, Parameters& par, par_nsper<T>& proj_parm)
233 proj_parm.tilt = false;
235 setup(params, par, proj_parm);
238 // Tilted perspective
239 template <typename Params, typename Parameters, typename T>
240 inline void setup_tpers(Params const& params, Parameters& par, par_nsper<T>& proj_parm)
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);
248 setup(params, par, proj_parm);
251 }} // namespace detail::nsper
255 \brief Near-sided perspective projection
257 \tparam Geographic latlong point type
258 \tparam Cartesian xy point type
259 \tparam Parameters parameter type
260 \par Projection characteristics
263 \par Projection parameters
266 \image html ex_nsper.gif
268 template <typename T, typename Parameters>
269 struct nsper_spheroid : public detail::nsper::base_nsper_spheroid<T, Parameters>
271 template <typename Params>
272 inline nsper_spheroid(Params const& params, Parameters & par)
274 detail::nsper::setup_nsper(params, par, this->m_proj_parm);
279 \brief Tilted perspective projection
281 \tparam Geographic latlong point type
282 \tparam Cartesian xy point type
283 \tparam Parameters parameter type
284 \par Projection characteristics
287 \par Projection parameters
288 - tilt: Tilt, or Omega (real)
289 - azi: Azimuth (or Gamma) (real)
292 \image html ex_tpers.gif
294 template <typename T, typename Parameters>
295 struct tpers_spheroid : public detail::nsper::base_nsper_spheroid<T, Parameters>
297 template <typename Params>
298 inline tpers_spheroid(Params const& params, Parameters & par)
300 detail::nsper::setup_tpers(params, par, this->m_proj_parm);
304 #ifndef DOXYGEN_NO_DETAIL
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)
313 BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI(nsper_entry, nsper_spheroid)
314 BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI(tpers_entry, tpers_spheroid)
316 BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(nsper_init)
318 BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(nsper, nsper_entry)
319 BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(tpers, tpers_entry)
322 } // namespace detail
325 } // namespace projections
327 }} // namespace boost::geometry
329 #endif // BOOST_GEOMETRY_PROJECTIONS_NSPER_HPP