Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / geometry / srs / projections / proj / moll.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_MOLL_HPP
41 #define BOOST_GEOMETRY_PROJECTIONS_MOLL_HPP
42
43 #include <boost/geometry/util/math.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/projects.hpp>
48 #include <boost/geometry/srs/projections/impl/factory_entry.hpp>
49 #include <boost/geometry/srs/projections/impl/aasincos.hpp>
50
51 namespace boost { namespace geometry
52 {
53
54 namespace projections
55 {
56     #ifndef DOXYGEN_NO_DETAIL
57     namespace detail { namespace moll
58     {
59
60             static const int max_iter = 10;
61             static const double loop_tol = 1e-7;
62
63             template <typename T>
64             struct par_moll
65             {
66                 T    C_x, C_y, C_p;
67             };
68
69             template <typename T, typename Parameters>
70             struct base_moll_spheroid
71             {
72                 par_moll<T> m_proj_parm;
73
74                 // FORWARD(s_forward)  spheroid
75                 // Project coordinates from geographic (lon, lat) to cartesian (x, y)
76                 inline void fwd(Parameters const& , T const& lp_lon, T lp_lat, T& xy_x, T& xy_y) const
77                 {
78                     static const T half_pi = detail::half_pi<T>();
79
80                     T k, V;
81                     int i;
82
83                     k = this->m_proj_parm.C_p * sin(lp_lat);
84                     for (i = max_iter; i ; --i) {
85                         lp_lat -= V = (lp_lat + sin(lp_lat) - k) /
86                             (1. + cos(lp_lat));
87                         if (fabs(V) < loop_tol)
88                             break;
89                     }
90                     if (!i)
91                         lp_lat = (lp_lat < 0.) ? -half_pi : half_pi;
92                     else
93                         lp_lat *= 0.5;
94                     xy_x = this->m_proj_parm.C_x * lp_lon * cos(lp_lat);
95                     xy_y = this->m_proj_parm.C_y * sin(lp_lat);
96                 }
97
98                 // INVERSE(s_inverse)  spheroid
99                 // Project coordinates from cartesian (x, y) to geographic (lon, lat)
100                 inline void inv(Parameters const& , T const& xy_x, T const& xy_y, T& lp_lon, T& lp_lat) const
101                 {
102                     static const T pi = detail::pi<T>();
103
104                     lp_lat = aasin(xy_y / this->m_proj_parm.C_y);
105                     lp_lon = xy_x / (this->m_proj_parm.C_x * cos(lp_lat));
106                     if (fabs(lp_lon) < pi) {
107                         lp_lat += lp_lat;
108                         lp_lat = aasin((lp_lat + sin(lp_lat)) / this->m_proj_parm.C_p);
109                     } else {
110                         lp_lon = lp_lat = HUGE_VAL;
111                     }
112                 }
113
114                 static inline std::string get_name()
115                 {
116                     return "moll_spheroid";
117                 }
118
119             };
120
121             template <typename Parameters, typename T>
122             inline void setup(Parameters& par, par_moll<T>& proj_parm, T const& p) 
123             {
124                 T r, sp, p2 = p + p;
125
126                 par.es = 0;
127                 sp = sin(p);
128                 r = sqrt(geometry::math::two_pi<T>() * sp / (p2 + sin(p2)));
129
130                 proj_parm.C_x = 2. * r / geometry::math::pi<T>();
131                 proj_parm.C_y = r / sp;
132                 proj_parm.C_p = p2 + sin(p2);
133             }
134
135
136             // Mollweide
137             template <typename Parameters, typename T>
138             inline void setup_moll(Parameters& par, par_moll<T>& proj_parm)
139             {
140                 setup(par, proj_parm, geometry::math::half_pi<T>());
141             }
142
143             // Wagner IV
144             template <typename Parameters, typename T>
145             inline void setup_wag4(Parameters& par, par_moll<T>& proj_parm)
146             {
147                 setup(par, proj_parm, geometry::math::pi<T>()/3.);
148             }
149
150             // Wagner V
151             template <typename Parameters, typename T>
152             inline void setup_wag5(Parameters& par, par_moll<T>& proj_parm)
153             {
154                 par.es = 0;
155                 proj_parm.C_x = 0.90977;
156                 proj_parm.C_y = 1.65014;
157                 proj_parm.C_p = 3.00896;
158             }
159
160     }} // namespace detail::moll
161     #endif // doxygen
162
163     /*!
164         \brief Mollweide projection
165         \ingroup projections
166         \tparam Geographic latlong point type
167         \tparam Cartesian xy point type
168         \tparam Parameters parameter type
169         \par Projection characteristics
170          - Pseudocylindrical
171          - Spheroid
172         \par Example
173         \image html ex_moll.gif
174     */
175     template <typename T, typename Parameters>
176     struct moll_spheroid : public detail::moll::base_moll_spheroid<T, Parameters>
177     {
178         template <typename Params>
179         inline moll_spheroid(Params const& , Parameters & par)
180         {
181             detail::moll::setup_moll(par, this->m_proj_parm);
182         }
183     };
184
185     /*!
186         \brief Wagner IV projection
187         \ingroup projections
188         \tparam Geographic latlong point type
189         \tparam Cartesian xy point type
190         \tparam Parameters parameter type
191         \par Projection characteristics
192          - Pseudocylindrical
193          - Spheroid
194         \par Example
195         \image html ex_wag4.gif
196     */
197     template <typename T, typename Parameters>
198     struct wag4_spheroid : public detail::moll::base_moll_spheroid<T, Parameters>
199     {
200         template <typename Params>
201         inline wag4_spheroid(Params const& , Parameters & par)
202         {
203             detail::moll::setup_wag4(par, this->m_proj_parm);
204         }
205     };
206
207     /*!
208         \brief Wagner V projection
209         \ingroup projections
210         \tparam Geographic latlong point type
211         \tparam Cartesian xy point type
212         \tparam Parameters parameter type
213         \par Projection characteristics
214          - Pseudocylindrical
215          - Spheroid
216         \par Example
217         \image html ex_wag5.gif
218     */
219     template <typename T, typename Parameters>
220     struct wag5_spheroid : public detail::moll::base_moll_spheroid<T, Parameters>
221     {
222         template <typename Params>
223         inline wag5_spheroid(Params const& , Parameters & par)
224         {
225             detail::moll::setup_wag5(par, this->m_proj_parm);
226         }
227     };
228
229     #ifndef DOXYGEN_NO_DETAIL
230     namespace detail
231     {
232
233         // Static projection
234         BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_FI(srs::spar::proj_moll, moll_spheroid)
235         BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_FI(srs::spar::proj_wag4, wag4_spheroid)
236         BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_FI(srs::spar::proj_wag5, wag5_spheroid)
237
238         // Factory entry(s)
239         BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI(moll_entry, moll_spheroid)
240         BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI(wag4_entry, wag4_spheroid)
241         BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI(wag5_entry, wag5_spheroid)
242         
243         BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(moll_init)
244         {
245             BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(moll, moll_entry);
246             BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(wag4, wag4_entry);
247             BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(wag5, wag5_entry);
248         }
249
250     } // namespace detail
251     #endif // doxygen
252
253 } // namespace projections
254
255 }} // namespace boost::geometry
256
257 #endif // BOOST_GEOMETRY_PROJECTIONS_MOLL_HPP
258