Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / geometry / srs / projections / proj / poly.hpp
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_POLY_HPP
41 #define BOOST_GEOMETRY_PROJECTIONS_POLY_HPP
42
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>
49
50 namespace boost { namespace geometry
51 {
52
53 namespace projections
54 {
55     #ifndef DOXYGEN_NO_DETAIL
56     namespace detail { namespace poly
57     {
58
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;
64
65             template <typename T>
66             struct par_poly
67             {
68                 T ml0;
69                 detail::en<T> en;
70             };
71
72             template <typename T, typename Parameters>
73             struct base_poly_ellipsoid
74             {
75                 par_poly<T> m_proj_parm;
76
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
80                 {
81                     T  ms, sp, cp;
82
83                     if (fabs(lp_lat) <= tolerance) {
84                         xy_x = lp_lon;
85                         xy_y = -this->m_proj_parm.ml0;
86                     } else {
87                         sp = sin(lp_lat);
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));
91                     }
92                 }
93
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
97                 {
98                     xy_y += this->m_proj_parm.ml0;
99                     if (fabs(xy_y) <= tolerance) {
100                         lp_lon = xy_x;
101                         lp_lat = 0.;
102                     } else {
103                         T r, c, sp, cp, s2ph, ml, mlb, mlp, dPhi;
104                         int i;
105
106                         r = xy_y * xy_y + xy_x * xy_x;
107                         for (lp_lat = xy_y, i = i_iter; i ; --i) {
108                             sp = sin(lp_lat);
109                             s2ph = sp * ( cp = cos(lp_lat));
110                             if (fabs(cp) < i_tolerance) {
111                                 BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
112                             }
113                             c = sp * (mlp = sqrt(1. - par.es * sp * sp)) / cp;
114                             ml = pj_mlfn(lp_lat, sp, cp, this->m_proj_parm.en);
115                             mlb = ml * ml + r;
116                             mlp = par.one_es / (mlp * mlp * mlp);
117                             lp_lat += ( dPhi =
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)
122                                 break;
123                         }
124                         if (!i) {
125                             BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
126                         }
127                         c = sin(lp_lat);
128                         lp_lon = asin(xy_x * tan(lp_lat) * sqrt(1. - par.es * c * c)) / sin(lp_lat);
129                     }
130                 }
131
132                 static inline std::string get_name()
133                 {
134                     return "poly_ellipsoid";
135                 }
136
137             };
138
139             template <typename T, typename Parameters>
140             struct base_poly_spheroid
141             {
142                 par_poly<T> m_proj_parm;
143
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
147                 {
148                     T  cot, E;
149
150                     if (fabs(lp_lat) <= tolerance) {
151                         xy_x = lp_lon;
152                         xy_y = this->m_proj_parm.ml0;
153                     } else {
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));
157                     }
158                 }
159
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
163                 {
164                     T B, dphi, tp;
165                     int i;
166
167                     if (fabs(xy_y = par.phi0 + xy_y) <= tolerance) {
168                         lp_lon = xy_x;
169                         lp_lat = 0.;
170                     } else {
171                         lp_lat = xy_y;
172                         B = xy_x * xy_x + xy_y * xy_y;
173                         i = n_iter;
174                         do {
175                             tp = tan(lp_lat);
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);
180                         if (! i) {
181                             BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
182                         }
183                         lp_lon = asin(xy_x * tan(lp_lat)) / sin(lp_lat);
184                     }
185                 }
186
187                 static inline std::string get_name()
188                 {
189                     return "poly_spheroid";
190                 }
191
192             };
193
194             // Polyconic (American)
195             template <typename Parameters, typename T>
196             inline void setup_poly(Parameters const& par, par_poly<T>& proj_parm)
197             {
198                 if (par.es != 0.0) {
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);
201                 } else {
202                     proj_parm.ml0 = -par.phi0;
203                 }
204             }
205
206     }} // namespace detail::poly
207     #endif // doxygen
208
209     /*!
210         \brief Polyconic (American) projection
211         \ingroup projections
212         \tparam Geographic latlong point type
213         \tparam Cartesian xy point type
214         \tparam Parameters parameter type
215         \par Projection characteristics
216          - Conic
217          - Spheroid
218          - Ellipsoid
219         \par Example
220         \image html ex_poly.gif
221     */
222     template <typename T, typename Parameters>
223     struct poly_ellipsoid : public detail::poly::base_poly_ellipsoid<T, Parameters>
224     {
225         template <typename Params>
226         inline poly_ellipsoid(Params const& , Parameters const& par)
227         {
228             detail::poly::setup_poly(par, this->m_proj_parm);
229         }
230     };
231
232     /*!
233         \brief Polyconic (American) projection
234         \ingroup projections
235         \tparam Geographic latlong point type
236         \tparam Cartesian xy point type
237         \tparam Parameters parameter type
238         \par Projection characteristics
239          - Conic
240          - Spheroid
241          - Ellipsoid
242         \par Example
243         \image html ex_poly.gif
244     */
245     template <typename T, typename Parameters>
246     struct poly_spheroid : public detail::poly::base_poly_spheroid<T, Parameters>
247     {
248         template <typename Params>
249         inline poly_spheroid(Params const& , Parameters const& par)
250         {
251             detail::poly::setup_poly(par, this->m_proj_parm);
252         }
253     };
254
255     #ifndef DOXYGEN_NO_DETAIL
256     namespace detail
257     {
258
259         // Static projection
260         BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_FI2(srs::spar::proj_poly, poly_spheroid, poly_ellipsoid)
261
262         // Factory entry(s)
263         BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI2(poly_entry, poly_spheroid, poly_ellipsoid)
264
265         BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(poly_init)
266         {
267             BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(poly, poly_entry)
268         }
269
270     } // namespace detail
271     #endif // doxygen
272
273 } // namespace projections
274
275 }} // namespace boost::geometry
276
277 #endif // BOOST_GEOMETRY_PROJECTIONS_POLY_HPP
278