1 // Boost.Geometry - gis-projections (based on PROJ4)
3 // Copyright (c) 2008-2015 Barend Gehrels, Amsterdam, the Netherlands.
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.
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)
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
18 // Last updated version of proj: 5.0.0
20 // Original copyright notice:
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:
29 // The above copyright notice and this permission notice shall be included
30 // in all copies or substantial portions of the Software.
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.
40 #ifndef BOOST_GEOMETRY_PROJECTIONS_LAEA_HPP
41 #define BOOST_GEOMETRY_PROJECTIONS_LAEA_HPP
43 #include <boost/config.hpp>
44 #include <boost/geometry/util/math.hpp>
45 #include <boost/math/special_functions/hypot.hpp>
47 #include <boost/geometry/srs/projections/impl/base_static.hpp>
48 #include <boost/geometry/srs/projections/impl/base_dynamic.hpp>
49 #include <boost/geometry/srs/projections/impl/projects.hpp>
50 #include <boost/geometry/srs/projections/impl/factory_entry.hpp>
51 #include <boost/geometry/srs/projections/impl/pj_auth.hpp>
52 #include <boost/geometry/srs/projections/impl/pj_qsfn.hpp>
54 namespace boost { namespace geometry
59 #ifndef DOXYGEN_NO_DETAIL
60 namespace detail { namespace laea
62 static const double epsilon10 = 1.e-10;
86 template <typename T, typename Parameters>
87 struct base_laea_ellipsoid
89 par_laea<T> m_proj_parm;
91 // FORWARD(e_forward) ellipsoid
92 // Project coordinates from geographic (lon, lat) to cartesian (x, y)
93 inline void fwd(Parameters const& par, T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
95 static const T half_pi = detail::half_pi<T>();
97 T coslam, sinlam, sinphi, q, sinb=0.0, cosb=0.0, b=0.0;
100 sinlam = sin(lp_lon);
101 sinphi = sin(lp_lat);
102 q = pj_qsfn(sinphi, par.e, par.one_es);
104 if (this->m_proj_parm.mode == obliq || this->m_proj_parm.mode == equit) {
105 sinb = q / this->m_proj_parm.qp;
106 cosb = sqrt(1. - sinb * sinb);
109 switch (this->m_proj_parm.mode) {
111 b = 1. + this->m_proj_parm.sinb1 * sinb + this->m_proj_parm.cosb1 * cosb * coslam;
114 b = 1. + cosb * coslam;
117 b = half_pi + lp_lat;
118 q = this->m_proj_parm.qp - q;
121 b = lp_lat - half_pi;
122 q = this->m_proj_parm.qp + q;
125 if (fabs(b) < epsilon10) {
126 BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
129 switch (this->m_proj_parm.mode) {
132 xy_y = this->m_proj_parm.ymf * b * (this->m_proj_parm.cosb1 * sinb - this->m_proj_parm.sinb1 * cosb * coslam);
136 b = sqrt(2. / (1. + cosb * coslam));
137 xy_y = b * sinb * this->m_proj_parm.ymf;
139 xy_x = this->m_proj_parm.xmf * b * cosb * sinlam;
146 xy_y = coslam * (this->m_proj_parm.mode == s_pole ? b : -b);
153 // INVERSE(e_inverse) ellipsoid
154 // Project coordinates from cartesian (x, y) to geographic (lon, lat)
155 inline void inv(Parameters const& par, T xy_x, T xy_y, T& lp_lon, T& lp_lat) const
157 T cCe, sCe, q, rho, ab=0.0;
159 switch (this->m_proj_parm.mode) {
162 xy_x /= this->m_proj_parm.dd;
163 xy_y *= this->m_proj_parm.dd;
164 rho = boost::math::hypot(xy_x, xy_y);
165 if (rho < epsilon10) {
170 sCe = 2. * asin(.5 * rho / this->m_proj_parm.rq);
174 if (this->m_proj_parm.mode == obliq) {
175 ab = cCe * this->m_proj_parm.sinb1 + xy_y * sCe * this->m_proj_parm.cosb1 / rho;
176 xy_y = rho * this->m_proj_parm.cosb1 * cCe - xy_y * this->m_proj_parm.sinb1 * sCe;
178 ab = xy_y * sCe / rho;
186 q = (xy_x * xy_x + xy_y * xy_y);
192 ab = 1. - q / this->m_proj_parm.qp;
193 if (this->m_proj_parm.mode == s_pole)
197 lp_lon = atan2(xy_x, xy_y);
198 lp_lat = pj_authlat(asin(ab), this->m_proj_parm.apa);
201 static inline std::string get_name()
203 return "laea_ellipsoid";
208 template <typename T, typename Parameters>
209 struct base_laea_spheroid
211 par_laea<T> m_proj_parm;
213 // FORWARD(s_forward) spheroid
214 // Project coordinates from geographic (lon, lat) to cartesian (x, y)
215 inline void fwd(Parameters const& par, T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
217 static const T fourth_pi = detail::fourth_pi<T>();
219 T coslam, cosphi, sinphi;
221 sinphi = sin(lp_lat);
222 cosphi = cos(lp_lat);
223 coslam = cos(lp_lon);
224 switch (this->m_proj_parm.mode) {
226 xy_y = 1. + cosphi * coslam;
229 xy_y = 1. + this->m_proj_parm.sinb1 * sinphi + this->m_proj_parm.cosb1 * cosphi * coslam;
231 if (xy_y <= epsilon10) {
232 BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
234 xy_y = sqrt(2. / xy_y);
235 xy_x = xy_y * cosphi * sin(lp_lon);
236 xy_y *= this->m_proj_parm.mode == equit ? sinphi :
237 this->m_proj_parm.cosb1 * sinphi - this->m_proj_parm.sinb1 * cosphi * coslam;
243 if (fabs(lp_lat + par.phi0) < epsilon10) {
244 BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
246 xy_y = fourth_pi - lp_lat * .5;
247 xy_y = 2. * (this->m_proj_parm.mode == s_pole ? cos(xy_y) : sin(xy_y));
248 xy_x = xy_y * sin(lp_lon);
254 // INVERSE(s_inverse) spheroid
255 // Project coordinates from cartesian (x, y) to geographic (lon, lat)
256 inline void inv(Parameters const& par, T xy_x, T xy_y, T& lp_lon, T& lp_lat) const
258 static const T half_pi = detail::half_pi<T>();
260 T cosz=0.0, rh, sinz=0.0;
262 rh = boost::math::hypot(xy_x, xy_y);
263 if ((lp_lat = rh * .5 ) > 1.) {
264 BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
266 lp_lat = 2. * asin(lp_lat);
267 if (this->m_proj_parm.mode == obliq || this->m_proj_parm.mode == equit) {
271 switch (this->m_proj_parm.mode) {
273 lp_lat = fabs(rh) <= epsilon10 ? 0. : asin(xy_y * sinz / rh);
278 lp_lat = fabs(rh) <= epsilon10 ? par.phi0 :
279 asin(cosz * this->m_proj_parm.sinb1 + xy_y * sinz * this->m_proj_parm.cosb1 / rh);
280 xy_x *= sinz * this->m_proj_parm.cosb1;
281 xy_y = (cosz - sin(lp_lat) * this->m_proj_parm.sinb1) * rh;
285 lp_lat = half_pi - lp_lat;
291 lp_lon = (xy_y == 0. && (this->m_proj_parm.mode == equit || this->m_proj_parm.mode == obliq)) ?
292 0. : atan2(xy_x, xy_y);
295 static inline std::string get_name()
297 return "laea_spheroid";
302 // Lambert Azimuthal Equal Area
303 template <typename Parameters, typename T>
304 inline void setup_laea(Parameters& par, par_laea<T>& proj_parm)
306 static const T half_pi = detail::half_pi<T>();
311 if (fabs(t - half_pi) < epsilon10)
312 proj_parm.mode = par.phi0 < 0. ? s_pole : n_pole;
313 else if (fabs(t) < epsilon10)
314 proj_parm.mode = equit;
316 proj_parm.mode = obliq;
320 par.e = sqrt(par.es); // TODO : Isn't it already set?
321 proj_parm.qp = pj_qsfn(1., par.e, par.one_es);
322 proj_parm.mmf = .5 / (1. - par.es);
323 proj_parm.apa = pj_authset<T>(par.es);
324 switch (proj_parm.mode) {
330 proj_parm.dd = 1. / (proj_parm.rq = sqrt(.5 * proj_parm.qp));
332 proj_parm.ymf = .5 * proj_parm.qp;
335 proj_parm.rq = sqrt(.5 * proj_parm.qp);
336 sinphi = sin(par.phi0);
337 proj_parm.sinb1 = pj_qsfn(sinphi, par.e, par.one_es) / proj_parm.qp;
338 proj_parm.cosb1 = sqrt(1. - proj_parm.sinb1 * proj_parm.sinb1);
339 proj_parm.dd = cos(par.phi0) / (sqrt(1. - par.es * sinphi * sinphi) *
340 proj_parm.rq * proj_parm.cosb1);
341 proj_parm.ymf = (proj_parm.xmf = proj_parm.rq) / proj_parm.dd;
342 proj_parm.xmf *= proj_parm.dd;
346 if (proj_parm.mode == obliq) {
347 proj_parm.sinb1 = sin(par.phi0);
348 proj_parm.cosb1 = cos(par.phi0);
357 \brief Lambert Azimuthal Equal Area projection
359 \tparam Geographic latlong point type
360 \tparam Cartesian xy point type
361 \tparam Parameters parameter type
362 \par Projection characteristics
367 \image html ex_laea.gif
369 template <typename T, typename Parameters>
370 struct laea_ellipsoid : public detail::laea::base_laea_ellipsoid<T, Parameters>
372 template <typename Params>
373 inline laea_ellipsoid(Params const& , Parameters & par)
375 detail::laea::setup_laea(par, this->m_proj_parm);
380 \brief Lambert Azimuthal Equal Area projection
382 \tparam Geographic latlong point type
383 \tparam Cartesian xy point type
384 \tparam Parameters parameter type
385 \par Projection characteristics
390 \image html ex_laea.gif
392 template <typename T, typename Parameters>
393 struct laea_spheroid : public detail::laea::base_laea_spheroid<T, Parameters>
395 template <typename Params>
396 inline laea_spheroid(Params const& , Parameters & par)
398 detail::laea::setup_laea(par, this->m_proj_parm);
402 #ifndef DOXYGEN_NO_DETAIL
407 BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_FI2(srs::spar::proj_laea, laea_spheroid, laea_ellipsoid)
410 BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI2(laea_entry, laea_spheroid, laea_ellipsoid)
412 BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(laea_init)
414 BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(laea, laea_entry)
417 } // namespace detail
420 } // namespace projections
422 }} // namespace boost::geometry
424 #endif // BOOST_GEOMETRY_PROJECTIONS_LAEA_HPP