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_POLY_HPP
41 #define BOOST_GEOMETRY_PROJECTIONS_POLY_HPP
43 #include <boost/geometry/srs/projections/impl/base_static.hpp>
44 #include <boost/geometry/srs/projections/impl/base_dynamic.hpp>
45 #include <boost/geometry/srs/projections/impl/projects.hpp>
46 #include <boost/geometry/srs/projections/impl/factory_entry.hpp>
47 #include <boost/geometry/srs/projections/impl/pj_mlfn.hpp>
48 #include <boost/geometry/srs/projections/impl/pj_msfn.hpp>
50 namespace boost { namespace geometry
55 #ifndef DOXYGEN_NO_DETAIL
56 namespace detail { namespace poly
59 static const double tolerance = 1e-10;
60 static const double conv_tolerance = 1e-10;
61 static const int n_iter = 10;
62 static const int i_iter = 20;
63 static const double i_tolerance = 1.e-12;
72 template <typename T, typename Parameters>
73 struct base_poly_ellipsoid
75 par_poly<T> m_proj_parm;
77 // FORWARD(e_forward) ellipsoid
78 // Project coordinates from geographic (lon, lat) to cartesian (x, y)
79 inline void fwd(Parameters const& par, T lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
83 if (fabs(lp_lat) <= tolerance) {
85 xy_y = -this->m_proj_parm.ml0;
88 ms = fabs(cp = cos(lp_lat)) > tolerance ? pj_msfn(sp, cp, par.es) / sp : 0.;
89 xy_x = ms * sin(lp_lon *= sp);
90 xy_y = (pj_mlfn(lp_lat, sp, cp, this->m_proj_parm.en) - this->m_proj_parm.ml0) + ms * (1. - cos(lp_lon));
94 // INVERSE(e_inverse) ellipsoid
95 // Project coordinates from cartesian (x, y) to geographic (lon, lat)
96 inline void inv(Parameters const& par, T const& xy_x, T xy_y, T& lp_lon, T& lp_lat) const
98 xy_y += this->m_proj_parm.ml0;
99 if (fabs(xy_y) <= tolerance) {
103 T r, c, sp, cp, s2ph, ml, mlb, mlp, dPhi;
106 r = xy_y * xy_y + xy_x * xy_x;
107 for (lp_lat = xy_y, i = i_iter; i ; --i) {
109 s2ph = sp * ( cp = cos(lp_lat));
110 if (fabs(cp) < i_tolerance) {
111 BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
113 c = sp * (mlp = sqrt(1. - par.es * sp * sp)) / cp;
114 ml = pj_mlfn(lp_lat, sp, cp, this->m_proj_parm.en);
116 mlp = par.one_es / (mlp * mlp * mlp);
118 ( ml + ml + c * mlb - 2. * xy_y * (c * ml + 1.) ) / (
119 par.es * s2ph * (mlb - 2. * xy_y * ml) / c +
120 2.* (xy_y - ml) * (c * mlp - 1. / s2ph) - mlp - mlp ));
121 if (fabs(dPhi) <= i_tolerance)
125 BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
128 lp_lon = asin(xy_x * tan(lp_lat) * sqrt(1. - par.es * c * c)) / sin(lp_lat);
132 static inline std::string get_name()
134 return "poly_ellipsoid";
139 template <typename T, typename Parameters>
140 struct base_poly_spheroid
142 par_poly<T> m_proj_parm;
144 // FORWARD(s_forward) spheroid
145 // Project coordinates from geographic (lon, lat) to cartesian (x, y)
146 inline void fwd(Parameters const& par, T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
150 if (fabs(lp_lat) <= tolerance) {
152 xy_y = this->m_proj_parm.ml0;
154 cot = 1. / tan(lp_lat);
155 xy_x = sin(E = lp_lon * sin(lp_lat)) * cot;
156 xy_y = lp_lat - par.phi0 + cot * (1. - cos(E));
160 // INVERSE(s_inverse) spheroid
161 // Project coordinates from cartesian (x, y) to geographic (lon, lat)
162 inline void inv(Parameters const& par, T const& xy_x, T xy_y, T& lp_lon, T& lp_lat) const
167 if (fabs(xy_y = par.phi0 + xy_y) <= tolerance) {
172 B = xy_x * xy_x + xy_y * xy_y;
176 lp_lat -= (dphi = (xy_y * (lp_lat * tp + 1.) - lp_lat -
177 .5 * ( lp_lat * lp_lat + B) * tp) /
178 ((lp_lat - xy_y) / tp - 1.));
179 } while (fabs(dphi) > conv_tolerance && --i);
181 BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
183 lp_lon = asin(xy_x * tan(lp_lat)) / sin(lp_lat);
187 static inline std::string get_name()
189 return "poly_spheroid";
194 // Polyconic (American)
195 template <typename Parameters, typename T>
196 inline void setup_poly(Parameters const& par, par_poly<T>& proj_parm)
199 proj_parm.en = pj_enfn<T>(par.es);
200 proj_parm.ml0 = pj_mlfn(par.phi0, sin(par.phi0), cos(par.phi0), proj_parm.en);
202 proj_parm.ml0 = -par.phi0;
206 }} // namespace detail::poly
210 \brief Polyconic (American) projection
212 \tparam Geographic latlong point type
213 \tparam Cartesian xy point type
214 \tparam Parameters parameter type
215 \par Projection characteristics
220 \image html ex_poly.gif
222 template <typename T, typename Parameters>
223 struct poly_ellipsoid : public detail::poly::base_poly_ellipsoid<T, Parameters>
225 template <typename Params>
226 inline poly_ellipsoid(Params const& , Parameters const& par)
228 detail::poly::setup_poly(par, this->m_proj_parm);
233 \brief Polyconic (American) projection
235 \tparam Geographic latlong point type
236 \tparam Cartesian xy point type
237 \tparam Parameters parameter type
238 \par Projection characteristics
243 \image html ex_poly.gif
245 template <typename T, typename Parameters>
246 struct poly_spheroid : public detail::poly::base_poly_spheroid<T, Parameters>
248 template <typename Params>
249 inline poly_spheroid(Params const& , Parameters const& par)
251 detail::poly::setup_poly(par, this->m_proj_parm);
255 #ifndef DOXYGEN_NO_DETAIL
260 BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_FI2(srs::spar::proj_poly, poly_spheroid, poly_ellipsoid)
263 BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI2(poly_entry, poly_spheroid, poly_ellipsoid)
265 BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(poly_init)
267 BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(poly, poly_entry)
270 } // namespace detail
273 } // namespace projections
275 }} // namespace boost::geometry
277 #endif // BOOST_GEOMETRY_PROJECTIONS_POLY_HPP