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_CHAMB_HPP
41 #define BOOST_GEOMETRY_PROJECTIONS_CHAMB_HPP
45 #include <boost/geometry/srs/projections/impl/aasincos.hpp>
46 #include <boost/geometry/srs/projections/impl/base_static.hpp>
47 #include <boost/geometry/srs/projections/impl/base_dynamic.hpp>
48 #include <boost/geometry/srs/projections/impl/factory_entry.hpp>
49 #include <boost/geometry/srs/projections/impl/pj_param.hpp>
50 #include <boost/geometry/srs/projections/impl/projects.hpp>
52 #include <boost/geometry/util/math.hpp>
54 namespace boost { namespace geometry
59 #ifndef DOXYGEN_NO_DETAIL
60 namespace detail { namespace chamb
63 //static const double third = 0.333333333333333333;
64 static const double tolerance = 1e-9;
66 // specific for 'chamb'
68 struct vect_ra { T r, Az; };
70 struct point_xy { T x, y; };
75 struct { /* control point data */
83 T beta_0, beta_1, beta_2;
86 /* distance and azimuth from point 1 to point 2 */
88 inline vect_ra<T> vect(T const& dphi, T const& c1, T const& s1, T const& c2, T const& s2, T const& dlam)
94 if (fabs(dphi) > 1. || fabs(dlam) > 1.)
95 v.r = aacos(s1 * s2 + c1 * c2 * cdl);
96 else { /* more accurate for smaller distances */
99 v.r = 2. * aasin(sqrt(dp * dp + c1 * c2 * dl * dl));
101 if (fabs(v.r) > tolerance)
102 v.Az = atan2(c2 * sin(dlam), c1 * s2 - s1 * c2 * cdl);
109 template <typename T>
110 inline T lc(T const& b, T const& c, T const& a)
112 return aacos(.5 * (b * b + c * c - a * a) / (b * c));
115 template <typename T, typename Parameters>
116 struct base_chamb_spheroid
118 par_chamb<T> m_proj_parm;
120 // FORWARD(s_forward) spheroid
121 // Project coordinates from geographic (lon, lat) to cartesian (x, y)
122 inline void fwd(Parameters const& , T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
124 static const T third = detail::third<T>();
130 sinphi = sin(lp_lat);
131 cosphi = cos(lp_lat);
132 for (i = 0; i < 3; ++i) { /* dist/azimiths from control */
133 v[i] = vect(lp_lat - this->m_proj_parm.c[i].phi, this->m_proj_parm.c[i].cosphi, this->m_proj_parm.c[i].sinphi,
134 cosphi, sinphi, lp_lon - this->m_proj_parm.c[i].lam);
137 v[i].Az = adjlon(v[i].Az - this->m_proj_parm.c[i].v.Az);
139 if (i < 3) /* current point at control point */
140 { xy_x = this->m_proj_parm.c[i].p.x; xy_y = this->m_proj_parm.c[i].p.y; }
141 else { /* point mean of intersepts */
142 { xy_x = this->m_proj_parm.p.x; xy_y = this->m_proj_parm.p.y; }
143 for (i = 0; i < 3; ++i) {
144 j = i == 2 ? 0 : i + 1;
145 a = lc(this->m_proj_parm.c[i].v.r, v[i].r, v[j].r);
148 if (! i) { /* coord comp unique to each arc */
149 xy_x += v[i].r * cos(a);
150 xy_y -= v[i].r * sin(a);
152 a = this->m_proj_parm.beta_1 - a;
153 xy_x -= v[i].r * cos(a);
154 xy_y -= v[i].r * sin(a);
156 a = this->m_proj_parm.beta_2 - a;
157 xy_x += v[i].r * cos(a);
158 xy_y += v[i].r * sin(a);
161 xy_x *= third; /* mean of arc intercepts */
166 static inline std::string get_name()
168 return "chamb_spheroid";
173 template <typename T>
174 inline T chamb_init_lat(srs::detail::proj4_parameters const& params, int i)
176 static const std::string lat[3] = {"lat_1", "lat_2", "lat_3"};
177 return _pj_get_param_r<T>(params, lat[i]);
179 template <typename T>
180 inline T chamb_init_lat(srs::dpar::parameters<T> const& params, int i)
182 static const srs::dpar::name_r lat[3] = {srs::dpar::lat_1, srs::dpar::lat_2, srs::dpar::lat_3};
183 return _pj_get_param_r<T>(params, lat[i]);
186 template <typename T>
187 inline T chamb_init_lon(srs::detail::proj4_parameters const& params, int i)
189 static const std::string lon[3] = {"lon_1", "lon_2", "lon_3"};
190 return _pj_get_param_r<T>(params, lon[i]);
192 template <typename T>
193 inline T chamb_init_lon(srs::dpar::parameters<T> const& params, int i)
195 static const srs::dpar::name_r lon[3] = {srs::dpar::lon_1, srs::dpar::lon_2, srs::dpar::lon_3};
196 return _pj_get_param_r<T>(params, lon[i]);
199 // Chamberlin Trimetric
200 template <typename Params, typename Parameters, typename T>
201 inline void setup_chamb(Params const& params, Parameters& par, par_chamb<T>& proj_parm)
203 static const T pi = detail::pi<T>();
207 for (i = 0; i < 3; ++i) { /* get control point locations */
208 proj_parm.c[i].phi = chamb_init_lat<T>(params, i);
209 proj_parm.c[i].lam = chamb_init_lon<T>(params, i);
210 proj_parm.c[i].lam = adjlon(proj_parm.c[i].lam - par.lam0);
211 proj_parm.c[i].cosphi = cos(proj_parm.c[i].phi);
212 proj_parm.c[i].sinphi = sin(proj_parm.c[i].phi);
214 for (i = 0; i < 3; ++i) { /* inter ctl pt. distances and azimuths */
215 j = i == 2 ? 0 : i + 1;
216 proj_parm.c[i].v = vect(proj_parm.c[j].phi - proj_parm.c[i].phi, proj_parm.c[i].cosphi, proj_parm.c[i].sinphi,
217 proj_parm.c[j].cosphi, proj_parm.c[j].sinphi, proj_parm.c[j].lam - proj_parm.c[i].lam);
218 if (proj_parm.c[i].v.r == 0.0)
219 BOOST_THROW_EXCEPTION( projection_exception(error_control_point_no_dist) );
220 /* co-linearity problem ignored for now */
222 proj_parm.beta_0 = lc(proj_parm.c[0].v.r, proj_parm.c[2].v.r, proj_parm.c[1].v.r);
223 proj_parm.beta_1 = lc(proj_parm.c[0].v.r, proj_parm.c[1].v.r, proj_parm.c[2].v.r);
224 proj_parm.beta_2 = pi - proj_parm.beta_0;
225 proj_parm.p.y = 2. * (proj_parm.c[0].p.y = proj_parm.c[1].p.y = proj_parm.c[2].v.r * sin(proj_parm.beta_0));
226 proj_parm.c[2].p.y = 0.;
227 proj_parm.c[0].p.x = - (proj_parm.c[1].p.x = 0.5 * proj_parm.c[0].v.r);
228 proj_parm.p.x = proj_parm.c[2].p.x = proj_parm.c[0].p.x + proj_parm.c[2].v.r * cos(proj_parm.beta_0);
233 }} // namespace detail::chamb
237 \brief Chamberlin Trimetric projection
239 \tparam Geographic latlong point type
240 \tparam Cartesian xy point type
241 \tparam Parameters parameter type
242 \par Projection characteristics
246 \par Projection parameters
247 - lat_1: Latitude of control point 1 (degrees)
248 - lon_1: Longitude of control point 1 (degrees)
249 - lat_2: Latitude of control point 2 (degrees)
250 - lon_2: Longitude of control point 2 (degrees)
251 - lat_3: Latitude of control point 3 (degrees)
252 - lon_3: Longitude of control point 3 (degrees)
254 \image html ex_chamb.gif
256 template <typename T, typename Parameters>
257 struct chamb_spheroid : public detail::chamb::base_chamb_spheroid<T, Parameters>
259 template <typename Params>
260 inline chamb_spheroid(Params const& params, Parameters & par)
262 detail::chamb::setup_chamb(params, par, this->m_proj_parm);
266 #ifndef DOXYGEN_NO_DETAIL
271 BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_F(srs::spar::proj_chamb, chamb_spheroid)
274 BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_F(chamb_entry, chamb_spheroid)
276 BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(chamb_init)
278 BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(chamb, chamb_entry);
281 } // namespace detail
284 } // namespace projections
286 }} // namespace boost::geometry
288 #endif // BOOST_GEOMETRY_PROJECTIONS_CHAMB_HPP