Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / geometry / srs / projections / proj / somerc.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_SOMERC_HPP
41 #define BOOST_GEOMETRY_PROJECTIONS_SOMERC_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 somerc
58     {
59             static const double epsilon = 1.e-10;
60             static const int n_iter = 6;
61
62             template <typename T>
63             struct par_somerc
64             {
65                 T K, c, hlf_e, kR, cosp0, sinp0;
66             };
67
68             template <typename T, typename Parameters>
69             struct base_somerc_ellipsoid
70             {
71                 par_somerc<T> m_proj_parm;
72
73                 // FORWARD(e_forward)
74                 // Project coordinates from geographic (lon, lat) to cartesian (x, y)
75                 inline void fwd(Parameters const& par, T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
76                 {
77                     static const T fourth_pi = detail::fourth_pi<T>();
78                     static const T half_pi = detail::half_pi<T>();
79
80                     T phip, lamp, phipp, lampp, sp, cp;
81
82                     sp = par.e * sin(lp_lat);
83                     phip = 2.* atan( exp( this->m_proj_parm.c * (
84                         log(tan(fourth_pi + 0.5 * lp_lat)) - this->m_proj_parm.hlf_e * log((1. + sp)/(1. - sp)))
85                         + this->m_proj_parm.K)) - half_pi;
86                     lamp = this->m_proj_parm.c * lp_lon;
87                     cp = cos(phip);
88                     phipp = aasin(this->m_proj_parm.cosp0 * sin(phip) - this->m_proj_parm.sinp0 * cp * cos(lamp));
89                     lampp = aasin(cp * sin(lamp) / cos(phipp));
90                     xy_x = this->m_proj_parm.kR * lampp;
91                     xy_y = this->m_proj_parm.kR * log(tan(fourth_pi + 0.5 * phipp));
92                 }
93
94                 // INVERSE(e_inverse)  ellipsoid & spheroid
95                 // Project coordinates from cartesian (x, y) to geographic (lon, lat)
96                 inline void inv(Parameters const& par, T const& xy_x, T const& xy_y, T& lp_lon, T& lp_lat) const
97                 {
98                     static const T fourth_pi = detail::fourth_pi<T>();
99
100                     T phip, lamp, phipp, lampp, cp, esp, con, delp;
101                     int i;
102
103                     phipp = 2. * (atan(exp(xy_y / this->m_proj_parm.kR)) - fourth_pi);
104                     lampp = xy_x / this->m_proj_parm.kR;
105                     cp = cos(phipp);
106                     phip = aasin(this->m_proj_parm.cosp0 * sin(phipp) + this->m_proj_parm.sinp0 * cp * cos(lampp));
107                     lamp = aasin(cp * sin(lampp) / cos(phip));
108                     con = (this->m_proj_parm.K - log(tan(fourth_pi + 0.5 * phip)))/this->m_proj_parm.c;
109                     for (i = n_iter; i ; --i) {
110                         esp = par.e * sin(phip);
111                         delp = (con + log(tan(fourth_pi + 0.5 * phip)) - this->m_proj_parm.hlf_e *
112                             log((1. + esp)/(1. - esp)) ) *
113                             (1. - esp * esp) * cos(phip) * par.rone_es;
114                         phip -= delp;
115                         if (fabs(delp) < epsilon)
116                             break;
117                     }
118                     if (i) {
119                         lp_lat = phip;
120                         lp_lon = lamp / this->m_proj_parm.c;
121                     } else {
122                         BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
123                     }
124                 }
125
126                 static inline std::string get_name()
127                 {
128                     return "somerc_ellipsoid";
129                 }
130
131             };
132
133             // Swiss. Obl. Mercator
134             template <typename Parameters, typename T>
135             inline void setup_somerc(Parameters const& par, par_somerc<T>& proj_parm)
136             {
137                 static const T fourth_pi = detail::fourth_pi<T>();
138
139                 T cp, phip0, sp;
140
141                 proj_parm.hlf_e = 0.5 * par.e;
142                 cp = cos(par.phi0);
143                 cp *= cp;
144                 proj_parm.c = sqrt(1 + par.es * cp * cp * par.rone_es);
145                 sp = sin(par.phi0);
146                 proj_parm.cosp0 = cos( phip0 = aasin(proj_parm.sinp0 = sp / proj_parm.c) );
147                 sp *= par.e;
148                 proj_parm.K = log(tan(fourth_pi + 0.5 * phip0)) - proj_parm.c * (
149                     log(tan(fourth_pi + 0.5 * par.phi0)) - proj_parm.hlf_e *
150                     log((1. + sp) / (1. - sp)));
151                 proj_parm.kR = par.k0 * sqrt(par.one_es) / (1. - sp * sp);
152             }
153
154     }} // namespace detail::somerc
155     #endif // doxygen
156
157     /*!
158         \brief Swiss. Obl. Mercator projection
159         \ingroup projections
160         \tparam Geographic latlong point type
161         \tparam Cartesian xy point type
162         \tparam Parameters parameter type
163         \par Projection characteristics
164          - Cylindrical
165          - Ellipsoid
166          - For CH1903
167         \par Example
168         \image html ex_somerc.gif
169     */
170     template <typename T, typename Parameters>
171     struct somerc_ellipsoid : public detail::somerc::base_somerc_ellipsoid<T, Parameters>
172     {
173         template <typename Params>
174         inline somerc_ellipsoid(Params const& , Parameters const& par)
175         {
176             detail::somerc::setup_somerc(par, this->m_proj_parm);
177         }
178     };
179
180     #ifndef DOXYGEN_NO_DETAIL
181     namespace detail
182     {
183
184         // Static projection
185         BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_FI(srs::spar::proj_somerc, somerc_ellipsoid)
186     
187         // Factory entry(s)
188         BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI(somerc_entry, somerc_ellipsoid)
189         
190         BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(somerc_init)
191         {
192             BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(somerc, somerc_entry)
193         }
194
195     } // namespace detail
196     #endif // doxygen
197
198 } // namespace projections
199
200 }} // namespace boost::geometry
201
202 #endif // BOOST_GEOMETRY_PROJECTIONS_SOMERC_HPP
203