Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / geometry / srs / projections / proj / bonne.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_BONNE_HPP
41 #define BOOST_GEOMETRY_PROJECTIONS_BONNE_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/factory_entry.hpp>
46 #include <boost/geometry/srs/projections/impl/pj_mlfn.hpp>
47 #include <boost/geometry/srs/projections/impl/pj_param.hpp>
48 #include <boost/geometry/srs/projections/impl/projects.hpp>
49
50 #include <boost/geometry/util/math.hpp>
51
52 #include <boost/math/special_functions/hypot.hpp>
53
54 namespace boost { namespace geometry
55 {
56
57 namespace projections
58 {
59     #ifndef DOXYGEN_NO_DETAIL
60     namespace detail { namespace bonne
61     {
62
63             static const double epsilon10 = 1e-10;
64
65             template <typename T>
66             struct par_bonne
67             {
68                 T phi1;
69                 T cphi1;
70                 T am1;
71                 T m1;
72                 detail::en<T> en;
73             };
74
75             template <typename T, typename Parameters>
76             struct base_bonne_ellipsoid
77             {
78                 par_bonne<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, T& xy_y) const
83                 {
84                     T rh, E, c;
85
86                     rh = this->m_proj_parm.am1 + this->m_proj_parm.m1 - pj_mlfn(lp_lat, E = sin(lp_lat), c = cos(lp_lat), this->m_proj_parm.en);
87                     E = c * lp_lon / (rh * sqrt(1. - par.es * E * E));
88                     xy_x = rh * sin(E);
89                     xy_y = this->m_proj_parm.am1 - rh * cos(E);
90                 }
91
92                 // INVERSE(e_inverse)  ellipsoid
93                 // Project coordinates from cartesian (x, y) to geographic (lon, lat)
94                 inline void inv(Parameters const& par, T const& xy_x, T xy_y, T& lp_lon, T& lp_lat) const
95                 {
96                     static const T half_pi = detail::half_pi<T>();
97
98                     T s, rh;
99
100                     rh = boost::math::hypot(xy_x, xy_y = this->m_proj_parm.am1 - xy_y);
101                     lp_lat = pj_inv_mlfn(this->m_proj_parm.am1 + this->m_proj_parm.m1 - rh, par.es, this->m_proj_parm.en);
102                     if ((s = fabs(lp_lat)) < half_pi) {
103                         s = sin(lp_lat);
104                         lp_lon = rh * atan2(xy_x, xy_y) *
105                            sqrt(1. - par.es * s * s) / cos(lp_lat);
106                     } else if (fabs(s - half_pi) <= epsilon10)
107                         lp_lon = 0.;
108                     else
109                         BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
110                 }
111
112                 static inline std::string get_name()
113                 {
114                     return "bonne_ellipsoid";
115                 }
116
117             };
118
119             template <typename T, typename Parameters>
120             struct base_bonne_spheroid
121             {
122                 par_bonne<T> m_proj_parm;
123
124                 // FORWARD(s_forward)  spheroid
125                 // Project coordinates from geographic (lon, lat) to cartesian (x, y)
126                 inline void fwd(Parameters const& , T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
127                 {
128                     T E, rh;
129
130                     rh = this->m_proj_parm.cphi1 + this->m_proj_parm.phi1 - lp_lat;
131                     if (fabs(rh) > epsilon10) {
132                         xy_x = rh * sin(E = lp_lon * cos(lp_lat) / rh);
133                         xy_y = this->m_proj_parm.cphi1 - rh * cos(E);
134                     } else
135                         xy_x = xy_y = 0.;
136                 }
137
138                 // INVERSE(s_inverse)  spheroid
139                 // Project coordinates from cartesian (x, y) to geographic (lon, lat)
140                 inline void inv(Parameters const& , T const& xy_x, T xy_y, T& lp_lon, T& lp_lat) const
141                 {
142                     static const T half_pi = detail::half_pi<T>();
143
144                     T rh;
145
146                     rh = boost::math::hypot(xy_x, xy_y = this->m_proj_parm.cphi1 - xy_y);
147                     lp_lat = this->m_proj_parm.cphi1 + this->m_proj_parm.phi1 - rh;
148                     if (fabs(lp_lat) > half_pi) {
149                         BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
150                     }
151                     if (fabs(fabs(lp_lat) - half_pi) <= epsilon10)
152                         lp_lon = 0.;
153                     else
154                         lp_lon = rh * atan2(xy_x, xy_y) / cos(lp_lat);
155                 }
156
157                 static inline std::string get_name()
158                 {
159                     return "bonne_spheroid";
160                 }
161
162             };
163
164             // Bonne (Werner lat_1=90)
165             template <typename Params, typename Parameters, typename T>
166             inline void setup_bonne(Params const& params, Parameters const& par, par_bonne<T>& proj_parm)
167             {
168                 static const T half_pi = detail::half_pi<T>();
169
170                 T c;
171
172                 proj_parm.phi1 = pj_get_param_r<T, srs::spar::lat_1>(params, "lat_1", srs::dpar::lat_1);
173                 if (fabs(proj_parm.phi1) < epsilon10)
174                     BOOST_THROW_EXCEPTION( projection_exception(error_lat1_is_zero) );
175
176                 if (par.es != 0.0) {
177                     proj_parm.en = pj_enfn<T>(par.es);
178                     proj_parm.m1 = pj_mlfn(proj_parm.phi1, proj_parm.am1 = sin(proj_parm.phi1),
179                         c = cos(proj_parm.phi1), proj_parm.en);
180                     proj_parm.am1 = c / (sqrt(1. - par.es * proj_parm.am1 * proj_parm.am1) * proj_parm.am1);
181                 } else {
182                     if (fabs(proj_parm.phi1) + epsilon10 >= half_pi)
183                         proj_parm.cphi1 = 0.;
184                     else
185                         proj_parm.cphi1 = 1. / tan(proj_parm.phi1);
186                 }
187             }
188
189     }} // namespace detail::bonne
190     #endif // doxygen
191
192     /*!
193         \brief Bonne (Werner lat_1=90) projection
194         \ingroup projections
195         \tparam Geographic latlong point type
196         \tparam Cartesian xy point type
197         \tparam Parameters parameter type
198         \par Projection characteristics
199          - Conic
200          - Spheroid
201          - Ellipsoid
202         \par Projection parameters
203          - lat_1: Latitude of first standard parallel (degrees)
204         \par Example
205         \image html ex_bonne.gif
206     */
207     template <typename T, typename Parameters>
208     struct bonne_ellipsoid : public detail::bonne::base_bonne_ellipsoid<T, Parameters>
209     {
210         template <typename Params>
211         inline bonne_ellipsoid(Params const& params, Parameters const& par)
212         {
213             detail::bonne::setup_bonne(params, par, this->m_proj_parm);
214         }
215     };
216
217     /*!
218         \brief Bonne (Werner lat_1=90) projection
219         \ingroup projections
220         \tparam Geographic latlong point type
221         \tparam Cartesian xy point type
222         \tparam Parameters parameter type
223         \par Projection characteristics
224          - Conic
225          - Spheroid
226          - Ellipsoid
227         \par Projection parameters
228          - lat_1: Latitude of first standard parallel (degrees)
229         \par Example
230         \image html ex_bonne.gif
231     */
232     template <typename T, typename Parameters>
233     struct bonne_spheroid : public detail::bonne::base_bonne_spheroid<T, Parameters>
234     {
235         template <typename Params>
236         inline bonne_spheroid(Params const& params, Parameters const& par)
237         {
238             detail::bonne::setup_bonne(params, par, this->m_proj_parm);
239         }
240     };
241
242     #ifndef DOXYGEN_NO_DETAIL
243     namespace detail
244     {
245
246         // Static projection
247         BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_FI2(srs::spar::proj_bonne, bonne_spheroid, bonne_ellipsoid)
248
249         // Factory entry(s)
250         BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI2(bonne_entry, bonne_spheroid, bonne_ellipsoid)
251
252         BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(bonne_init)
253         {
254             BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(bonne, bonne_entry);
255         }
256
257     } // namespace detail
258     #endif // doxygen
259
260 } // namespace projections
261
262 }} // namespace boost::geometry
263
264 #endif // BOOST_GEOMETRY_PROJECTIONS_BONNE_HPP
265