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_VANDG_HPP 41 #define BOOST_GEOMETRY_PROJECTIONS_VANDG_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 50 namespace boost { namespace geometry 51 { 52 53 namespace projections 54 { 55 #ifndef DOXYGEN_NO_DETAIL 56 namespace detail { namespace vandg 57 { 58 59 static const double tolerance = 1.e-10; 60 61 template <typename T> C2_27()62 inline T C2_27() { return .07407407407407407407407407407407; } 63 template <typename T> PI4_3()64 inline T PI4_3() { return boost::math::constants::four_thirds_pi<T>(); } 65 template <typename T> TPISQ()66 inline T TPISQ() { return 19.739208802178717237668981999752; } 67 template <typename T> HPISQ()68 inline T HPISQ() { return 4.9348022005446793094172454999381; } 69 70 template <typename T, typename Parameters> 71 struct base_vandg_spheroid 72 { 73 // FORWARD(s_forward) spheroid 74 // Project coordinates from geographic (lon, lat) to cartesian (x, y) fwdboost::geometry::projections::detail::vandg::base_vandg_spheroid75 inline void fwd(Parameters const& , T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const 76 { 77 static const T half_pi = detail::half_pi<T>(); 78 static const T pi = detail::pi<T>(); 79 80 T al, al2, g, g2, p2; 81 82 p2 = fabs(lp_lat / half_pi); 83 if ((p2 - tolerance) > 1.) { 84 BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) ); 85 } 86 if (p2 > 1.) 87 p2 = 1.; 88 if (fabs(lp_lat) <= tolerance) { 89 xy_x = lp_lon; 90 xy_y = 0.; 91 } else if (fabs(lp_lon) <= tolerance || fabs(p2 - 1.) < tolerance) { 92 xy_x = 0.; 93 xy_y = pi * tan(.5 * asin(p2)); 94 if (lp_lat < 0.) xy_y = -xy_y; 95 } else { 96 al = .5 * fabs(pi / lp_lon - lp_lon / pi); 97 al2 = al * al; 98 g = sqrt(1. - p2 * p2); 99 g = g / (p2 + g - 1.); 100 g2 = g * g; 101 p2 = g * (2. / p2 - 1.); 102 p2 = p2 * p2; 103 xy_x = g - p2; g = p2 + al2; 104 xy_x = pi * (al * xy_x + sqrt(al2 * xy_x * xy_x - g * (g2 - p2))) / g; 105 if (lp_lon < 0.) xy_x = -xy_x; 106 xy_y = fabs(xy_x / pi); 107 xy_y = 1. - xy_y * (xy_y + 2. * al); 108 if (xy_y < -tolerance) { 109 BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) ); 110 } 111 if (xy_y < 0.) 112 xy_y = 0.; 113 else 114 xy_y = sqrt(xy_y) * (lp_lat < 0. ? -pi : pi); 115 } 116 } 117 118 // INVERSE(s_inverse) spheroid 119 // Project coordinates from cartesian (x, y) to geographic (lon, lat) invboost::geometry::projections::detail::vandg::base_vandg_spheroid120 inline void inv(Parameters const& , T const& xy_x, T const& xy_y, T& lp_lon, T& lp_lat) const 121 { 122 static const T half_pi = detail::half_pi<T>(); 123 static const T pi = detail::pi<T>(); 124 static const T pi_sqr = detail::pi_sqr<T>(); 125 static const T third = detail::third<T>(); 126 static const T two_pi = detail::two_pi<T>(); 127 128 static const T C2_27 = vandg::C2_27<T>(); 129 static const T PI4_3 = vandg::PI4_3<T>(); 130 static const T TPISQ = vandg::TPISQ<T>(); 131 static const T HPISQ = vandg::HPISQ<T>(); 132 133 T t, c0, c1, c2, c3, al, r2, r, m, d, ay, x2, y2; 134 135 x2 = xy_x * xy_x; 136 if ((ay = fabs(xy_y)) < tolerance) { 137 lp_lat = 0.; 138 t = x2 * x2 + TPISQ * (x2 + HPISQ); 139 lp_lon = fabs(xy_x) <= tolerance ? 0. : 140 .5 * (x2 - pi_sqr + sqrt(t)) / xy_x; 141 return; 142 } 143 y2 = xy_y * xy_y; 144 r = x2 + y2; r2 = r * r; 145 c1 = - pi * ay * (r + pi_sqr); 146 c3 = r2 + two_pi * (ay * r + pi * (y2 + pi * (ay + half_pi))); 147 c2 = c1 + pi_sqr * (r - 3. * y2); 148 c0 = pi * ay; 149 c2 /= c3; 150 al = c1 / c3 - third * c2 * c2; 151 m = 2. * sqrt(-third * al); 152 d = C2_27 * c2 * c2 * c2 + (c0 * c0 - third * c2 * c1) / c3; 153 if (((t = fabs(d = 3. * d / (al * m))) - tolerance) <= 1.) { 154 d = t > 1. ? (d > 0. ? 0. : pi) : acos(d); 155 lp_lat = pi * (m * cos(d * third + PI4_3) - third * c2); 156 if (xy_y < 0.) lp_lat = -lp_lat; 157 t = r2 + TPISQ * (x2 - y2 + HPISQ); 158 lp_lon = fabs(xy_x) <= tolerance ? 0. : 159 .5 * (r - pi_sqr + (t <= 0. ? 0. : sqrt(t))) / xy_x; 160 } else { 161 BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) ); 162 } 163 } 164 get_nameboost::geometry::projections::detail::vandg::base_vandg_spheroid165 static inline std::string get_name() 166 { 167 return "vandg_spheroid"; 168 } 169 170 }; 171 172 // van der Grinten (I) 173 template <typename Parameters> setup_vandg(Parameters & par)174 inline void setup_vandg(Parameters& par) 175 { 176 par.es = 0.; 177 } 178 179 }} // namespace detail::vandg 180 #endif // doxygen 181 182 /*! 183 \brief van der Grinten (I) projection 184 \ingroup projections 185 \tparam Geographic latlong point type 186 \tparam Cartesian xy point type 187 \tparam Parameters parameter type 188 \par Projection characteristics 189 - Miscellaneous 190 - Spheroid 191 \par Example 192 \image html ex_vandg.gif 193 */ 194 template <typename T, typename Parameters> 195 struct vandg_spheroid : public detail::vandg::base_vandg_spheroid<T, Parameters> 196 { 197 template <typename Params> vandg_spheroidboost::geometry::projections::vandg_spheroid198 inline vandg_spheroid(Params const& , Parameters & par) 199 { 200 detail::vandg::setup_vandg(par); 201 } 202 }; 203 204 #ifndef DOXYGEN_NO_DETAIL 205 namespace detail 206 { 207 208 // Static projection BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_FI(srs::spar::proj_vandg,vandg_spheroid)209 BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_FI(srs::spar::proj_vandg, vandg_spheroid) 210 211 // Factory entry(s) 212 BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI(vandg_entry, vandg_spheroid) 213 214 BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(vandg_init) 215 { 216 BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(vandg, vandg_entry) 217 } 218 219 } // namespace detail 220 #endif // doxygen 221 222 } // namespace projections 223 224 }} // namespace boost::geometry 225 226 #endif // BOOST_GEOMETRY_PROJECTIONS_VANDG_HPP 227 228