Debian Codegraph @

include/boost_1_77_0/patches/boost/geometry/srs/projections/proj/stere.hpp

raw
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, 2026, 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_STERE_HPP
41 #define BOOST_GEOMETRY_PROJECTIONS_STERE_HPP
42
43 #include <boost/config.hpp>
44 #include <boost/geometry/util/math.hpp>
45 #include <boost/math/special_functions/hypot.hpp>
46
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/factory_entry.hpp>
50 #include <boost/geometry/srs/projections/impl/pj_param.hpp>
51 #include <boost/geometry/srs/projections/impl/pj_tsfn.hpp>
52 #include <boost/geometry/srs/projections/impl/projects.hpp>
53
54 namespace boost { namespace geometry
55 {
56
57 namespace projections
58 {
59 #ifndef DOXYGEN_NO_DETAIL
60 namespace detail { namespace stere
61 {
62 static const double epsilon10 = 1.e-10;
63 static const double tolerance = 1.e-8;
64 static const int n_iter = 8;
65 static const double conv_tolerance = 1.e-10;
66
67 enum mode_type {
68 s_pole = 0,
69 n_pole = 1,
70 obliq = 2,
71 equit = 3
72 };
73
74 template <typename T>
75 struct par_stere
76 {
77 T phits;
78 T sinX1;
79 T cosX1;
80 T akm1;
81 mode_type mode;
82 bool variant_c;
83 };
84
85 template <typename T>
86 inline T ssfn_(T const& phit, T sinphi, T const& eccen)
87 {
88 static const T half_pi = detail::half_pi<T>();
89
90 sinphi *= eccen;
91 return (tan (.5 * (half_pi + phit)) *
92 math::pow((T(1) - sinphi) / (T(1) + sinphi), T(0.5) * eccen));
93 }
94
95 template <typename T, typename Parameters>
96 struct base_stere_ellipsoid
97 {
98 par_stere<T> m_proj_parm;
99
100 // FORWARD(e_forward) ellipsoid
101 // Project coordinates from geographic (lon, lat) to cartesian (x, y)
102 inline void fwd(Parameters const& par, T const& lp_lon, T lp_lat, T& xy_x, T& xy_y) const
103 {
104 static const T half_pi = detail::half_pi<T>();
105
106 T coslam, sinlam, sinX=0.0, cosX=0.0, X, A = 0.0, sinphi;
107
108 coslam = cos(lp_lon);
109 sinlam = sin(lp_lon);
110 sinphi = sin(lp_lat);
111 if (this->m_proj_parm.mode == obliq || this->m_proj_parm.mode == equit) {
112 sinX = sin(X = 2. * atan(ssfn_(lp_lat, sinphi, par.e)) - half_pi);
113 cosX = cos(X);
114 }
115 switch (this->m_proj_parm.mode) {
116 case obliq:
117 A = this->m_proj_parm.akm1 / (this->m_proj_parm.cosX1 * (1. + this->m_proj_parm.sinX1 * sinX +
118 this->m_proj_parm.cosX1 * cosX * coslam));
119 xy_y = A * (this->m_proj_parm.cosX1 * sinX - this->m_proj_parm.sinX1 * cosX * coslam);
120 goto xmul; /* but why not just xy.x = A * cosX; break; ? */
121
122 case equit:
123 // TODO: calculate denominator once
124 /* avoid zero division */
125 if (1. + cosX * coslam == 0.0) {
126 xy_y = HUGE_VAL;
127 } else {
128 A = this->m_proj_parm.akm1 / (1. + cosX * coslam);
129 xy_y = A * sinX;
130 }
131 xmul:
132 xy_x = A * cosX;
133 break;
134
135 case s_pole:
136 lp_lat = -lp_lat;
137 coslam = - coslam;
138 sinphi = -sinphi;
139 BOOST_FALLTHROUGH;
140 case n_pole:
141 // see IOGP Publication 373-7-2 – Geomatics Guidance Note number 7, part 2
142 // December 2021 pg. 82
143 if( m_proj_parm.variant_c )
144 {
145 auto t = pj_tsfn(lp_lat, sinphi, par.e);
146 auto tf = pj_tsfn(this->m_proj_parm.phits,
147 sin(this->m_proj_parm.phits),
148 par.e);
149 xy_x = this->m_proj_parm.akm1 * t;
150 auto mf = this->m_proj_parm.akm1 * tf;
151 xy_y = - xy_x * coslam - mf;
152 } else {
153 xy_x = this->m_proj_parm.akm1 * pj_tsfn(lp_lat, sinphi, par.e);
154 xy_y = - xy_x * coslam;
155 }
156 break;
157 }
158
159 xy_x = xy_x * sinlam;
160 }
161
162 // INVERSE(e_inverse) ellipsoid
163 // Project coordinates from cartesian (x, y) to geographic (lon, lat)
164 inline void inv(Parameters const& par, T xy_x, T xy_y, T& lp_lon, T& lp_lat) const
165 {
166 static const T half_pi = detail::half_pi<T>();
167
168 T cosphi, sinphi, tp=0.0, phi_l=0.0, rho, halfe=0.0, halfpi=0.0;
169 T mf = 0;
170 int i;
171
172 rho = boost::math::hypot(xy_x, xy_y);
173 switch (this->m_proj_parm.mode) {
174 case obliq:
175 case equit:
176 cosphi = cos( tp = 2. * atan2(rho * this->m_proj_parm.cosX1 , this->m_proj_parm.akm1) );
177 sinphi = sin(tp);
178 if( rho == 0.0 )
179 phi_l = asin(cosphi * this->m_proj_parm.sinX1);
180 else
181 phi_l = asin(cosphi * this->m_proj_parm.sinX1 + (xy_y * sinphi * this->m_proj_parm.cosX1 / rho));
182
183 tp = tan(.5 * (half_pi + phi_l));
184 xy_x *= sinphi;
185 xy_y = rho * this->m_proj_parm.cosX1 * cosphi - xy_y * this->m_proj_parm.sinX1* sinphi;
186 halfpi = half_pi;
187 halfe = .5 * par.e;
188 break;
189 case n_pole:
190 xy_y = -xy_y;
191 BOOST_FALLTHROUGH;
192 case s_pole:
193 // see IOGP Publication 373-7-2 – Geomatics Guidance Note number 7, part 2
194 // December 2021 pg. 82
195 if( m_proj_parm.variant_c )
196 {
197 auto tf = pj_tsfn(this->m_proj_parm.phits,
198 sin(this->m_proj_parm.phits),
199 par.e);
200 mf = this->m_proj_parm.akm1 * tf;
201 rho = boost::math::hypot(xy_x, xy_y + mf);
202 }
203 phi_l = half_pi - 2. * atan(tp = - rho / this->m_proj_parm.akm1);
204 halfpi = -half_pi;
205 halfe = -.5 * par.e;
206 break;
207 }
208 for (i = n_iter; i--; phi_l = lp_lat) {
209 sinphi = par.e * sin(phi_l);
210 lp_lat = T(2) * atan(tp * math::pow((T(1)+sinphi)/(T(1)-sinphi), halfe)) - halfpi;
211 if (fabs(phi_l - lp_lat) < conv_tolerance) {
212 if (this->m_proj_parm.mode == s_pole)
213 lp_lat = -lp_lat;
214 lp_lon = (xy_x == 0. && xy_y == 0.) ? 0. : atan2(xy_x, xy_y + mf);
215 return;
216 }
217 }
218 BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
219 }
220
221 static inline std::string get_name()
222 {
223 return "stere_ellipsoid";
224 }
225
226 };
227
228 template <typename T, typename Parameters>
229 struct base_stere_spheroid
230 {
231 par_stere<T> m_proj_parm;
232
233 // FORWARD(s_forward) spheroid
234 // Project coordinates from geographic (lon, lat) to cartesian (x, y)
235 inline void fwd(Parameters const& , T const& lp_lon, T lp_lat, T& xy_x, T& xy_y) const
236 {
237 static const T fourth_pi = detail::fourth_pi<T>();
238 static const T half_pi = detail::half_pi<T>();
239
240 T sinphi, cosphi, coslam, sinlam;
241
242 sinphi = sin(lp_lat);
243 cosphi = cos(lp_lat);
244 coslam = cos(lp_lon);
245 sinlam = sin(lp_lon);
246 switch (this->m_proj_parm.mode) {
247 case equit:
248 xy_y = 1. + cosphi * coslam;
249 goto oblcon;
250 case obliq:
251 xy_y = 1. + this->m_proj_parm.sinX1 * sinphi + this->m_proj_parm.cosX1 * cosphi * coslam;
252 oblcon:
253 if (xy_y <= epsilon10) {
254 BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
255 }
256 xy_x = (xy_y = this->m_proj_parm.akm1 / xy_y) * cosphi * sinlam;
257 xy_y *= (this->m_proj_parm.mode == equit) ? sinphi :
258 this->m_proj_parm.cosX1 * sinphi - this->m_proj_parm.sinX1 * cosphi * coslam;
259 break;
260 case n_pole:
261 coslam = - coslam;
262 lp_lat = - lp_lat;
263 BOOST_FALLTHROUGH;
264 case s_pole:
265 if (fabs(lp_lat - half_pi) < tolerance) {
266 BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
267 }
268 xy_x = sinlam * ( xy_y = this->m_proj_parm.akm1 * tan(fourth_pi + .5 * lp_lat) );
269 xy_y *= coslam;
270 break;
271 }
272 }
273
274 // INVERSE(s_inverse) spheroid
275 // Project coordinates from cartesian (x, y) to geographic (lon, lat)
276 inline void inv(Parameters const& par, T const& xy_x, T xy_y, T& lp_lon, T& lp_lat) const
277 {
278 T c, rh, sinc, cosc;
279
280 sinc = sin(c = 2. * atan((rh = boost::math::hypot(xy_x, xy_y)) / this->m_proj_parm.akm1));
281 cosc = cos(c);
282 lp_lon = 0.;
283
284 switch (this->m_proj_parm.mode) {
285 case equit:
286 if (fabs(rh) <= epsilon10)
287 lp_lat = 0.;
288 else
289 lp_lat = asin(xy_y * sinc / rh);
290 if (cosc != 0. || xy_x != 0.)
291 lp_lon = atan2(xy_x * sinc, cosc * rh);
292 break;
293 case obliq:
294 if (fabs(rh) <= epsilon10)
295 lp_lat = par.phi0;
296 else
297 lp_lat = asin(cosc * this->m_proj_parm.sinX1 + xy_y * sinc * this->m_proj_parm.cosX1 / rh);
298 if ((c = cosc - this->m_proj_parm.sinX1 * sin(lp_lat)) != 0. || xy_x != 0.)
299 lp_lon = atan2(xy_x * sinc * this->m_proj_parm.cosX1, c * rh);
300 break;
301 case n_pole:
302 xy_y = -xy_y;
303 BOOST_FALLTHROUGH;
304 case s_pole:
305 if (fabs(rh) <= epsilon10)
306 lp_lat = par.phi0;
307 else
308 lp_lat = asin(this->m_proj_parm.mode == s_pole ? - cosc : cosc);
309 lp_lon = (xy_x == 0. && xy_y == 0.) ? 0. : atan2(xy_x, xy_y);
310 break;
311 }
312 }
313
314 static inline std::string get_name()
315 {
316 return "stere_spheroid";
317 }
318
319 };
320
321 template <typename Parameters, typename T>
322 inline void setup(Parameters const& par, par_stere<T>& proj_parm) /* general initialization */
323 {
324 static const T fourth_pi = detail::fourth_pi<T>();
325 static const T half_pi = detail::half_pi<T>();
326
327 T t;
328
329 if (fabs((t = fabs(par.phi0)) - half_pi) < epsilon10)
330 proj_parm.mode = par.phi0 < 0. ? s_pole : n_pole;
331 else
332 proj_parm.mode = t > epsilon10 ? obliq : equit;
333 proj_parm.phits = fabs(proj_parm.phits);
334
335 if (par.es != 0.0) {
336 T X;
337
338 switch (proj_parm.mode) {
339 case n_pole:
340 case s_pole:
341 if (fabs(proj_parm.phits - half_pi) < epsilon10)
342 proj_parm.akm1 = 2. * par.k0 /
343 sqrt(math::pow(T(1)+par.e,T(1)+par.e)*math::pow(T(1)-par.e,T(1)-par.e));
344 else {
345 proj_parm.akm1 = cos(proj_parm.phits) /
346 pj_tsfn(proj_parm.phits, t = sin(proj_parm.phits), par.e);
347 t *= par.e;
348 proj_parm.akm1 /= sqrt(1. - t * t);
349 }
350 break;
351 case equit:
352 case obliq:
353 t = sin(par.phi0);
354 X = 2. * atan(ssfn_(par.phi0, t, par.e)) - half_pi;
355 t *= par.e;
356 proj_parm.akm1 = 2. * par.k0 * cos(par.phi0) / sqrt(1. - t * t);
357 proj_parm.sinX1 = sin(X);
358 proj_parm.cosX1 = cos(X);
359 break;
360 }
361 } else {
362 switch (proj_parm.mode) {
363 case obliq:
364 proj_parm.sinX1 = sin(par.phi0);
365 proj_parm.cosX1 = cos(par.phi0);
366 BOOST_FALLTHROUGH;
367 case equit:
368 proj_parm.akm1 = 2. * par.k0;
369 break;
370 case s_pole:
371 case n_pole:
372 proj_parm.akm1 = fabs(proj_parm.phits - half_pi) >= epsilon10 ?
373 cos(proj_parm.phits) / tan(fourth_pi - .5 * proj_parm.phits) :
374 2. * par.k0 ;
375 break;
376 }
377 }
378 }
379
380
381 // Stereographic
382 template <typename Params, typename Parameters, typename T>
383 inline void setup_stere(Params const& params, Parameters const& par, par_stere<T>& proj_parm)
384 {
385 static const T half_pi = detail::half_pi<T>();
386
387 if (! pj_param_r<srs::spar::lat_ts>(params, "lat_ts", srs::dpar::lat_ts, proj_parm.phits))
388 proj_parm.phits = half_pi;
389
390 proj_parm.variant_c = false;
391 if (pj_param_exists<srs::spar::variant_c>(params, "variant_c", srs::dpar::variant_c))
392 proj_parm.variant_c = true;
393
394 setup(par, proj_parm);
395 }
396
397 // Universal Polar Stereographic
398 template <typename Params, typename Parameters, typename T>
399 inline void setup_ups(Params const& params, Parameters& par, par_stere<T>& proj_parm)
400 {
401 static const T half_pi = detail::half_pi<T>();
402
403 /* International Ellipsoid */
404 par.phi0 = pj_get_param_b<srs::spar::south>(params, "south", srs::dpar::south) ? -half_pi: half_pi;
405 if (par.es == 0.0) {
406 BOOST_THROW_EXCEPTION( projection_exception(error_ellipsoid_use_required) );
407 }
408 par.k0 = .994;
409 par.x0 = 2000000.;
410 par.y0 = 2000000.;
411 proj_parm.phits = half_pi;
412 par.lam0 = 0.;
413
414 setup(par, proj_parm);
415 }
416
417 }} // namespace detail::stere
418 #endif // doxygen
419
420 /*!
421 \brief Stereographic projection
422 \ingroup projections
423 \tparam Geographic latlong point type
424 \tparam Cartesian xy point type
425 \tparam Parameters parameter type
426 \par Projection characteristics
427 - Azimuthal
428 - Spheroid
429 - Ellipsoid
430 \par Projection parameters
431 - lat_ts: Latitude of true scale (degrees)
432 \par Example
433 \image html ex_stere.gif
434 */
435 template <typename T, typename Parameters>
436 struct stere_ellipsoid : public detail::stere::base_stere_ellipsoid<T, Parameters>
437 {
438 template <typename Params>
439 inline stere_ellipsoid(Params const& params, Parameters const& par)
440 {
441 detail::stere::setup_stere(params, par, this->m_proj_parm);
442 }
443 };
444
445 /*!
446 \brief Stereographic projection
447 \ingroup projections
448 \tparam Geographic latlong point type
449 \tparam Cartesian xy point type
450 \tparam Parameters parameter type
451 \par Projection characteristics
452 - Azimuthal
453 - Spheroid
454 - Ellipsoid
455 \par Projection parameters
456 - lat_ts: Latitude of true scale (degrees)
457 \par Example
458 \image html ex_stere.gif
459 */
460 template <typename T, typename Parameters>
461 struct stere_spheroid : public detail::stere::base_stere_spheroid<T, Parameters>
462 {
463 template <typename Params>
464 inline stere_spheroid(Params const& params, Parameters const& par)
465 {
466 detail::stere::setup_stere(params, par, this->m_proj_parm);
467 }
468 };
469
470 /*!
471 \brief Universal Polar Stereographic projection
472 \ingroup projections
473 \tparam Geographic latlong point type
474 \tparam Cartesian xy point type
475 \tparam Parameters parameter type
476 \par Projection characteristics
477 - Azimuthal
478 - Spheroid
479 - Ellipsoid
480 \par Projection parameters
481 - south: Denotes southern hemisphere UTM zone (boolean)
482 \par Example
483 \image html ex_ups.gif
484 */
485 template <typename T, typename Parameters>
486 struct ups_ellipsoid : public detail::stere::base_stere_ellipsoid<T, Parameters>
487 {
488 template <typename Params>
489 inline ups_ellipsoid(Params const& params, Parameters & par)
490 {
491 detail::stere::setup_ups(params, par, this->m_proj_parm);
492 }
493 };
494
495 /*!
496 \brief Universal Polar Stereographic projection
497 \ingroup projections
498 \tparam Geographic latlong point type
499 \tparam Cartesian xy point type
500 \tparam Parameters parameter type
501 \par Projection characteristics
502 - Azimuthal
503 - Spheroid
504 - Ellipsoid
505 \par Projection parameters
506 - south: Denotes southern hemisphere UTM zone (boolean)
507 \par Example
508 \image html ex_ups.gif
509 */
510 template <typename T, typename Parameters>
511 struct ups_spheroid : public detail::stere::base_stere_spheroid<T, Parameters>
512 {
513 template <typename Params>
514 inline ups_spheroid(Params const& params, Parameters & par)
515 {
516 detail::stere::setup_ups(params, par, this->m_proj_parm);
517 }
518 };
519
520 #ifndef DOXYGEN_NO_DETAIL
521 namespace detail
522 {
523
524 // Static projection
525 BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_FI2(srs::spar::proj_stere, stere_spheroid, stere_ellipsoid)
526 BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_FI2(srs::spar::proj_ups, ups_spheroid, ups_ellipsoid)
527
528 // Factory entry(s)
529 BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI2(stere_entry, stere_spheroid, stere_ellipsoid)
530 BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI2(ups_entry, ups_spheroid, ups_ellipsoid)
531
532 BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(stere_init)
533 {
534 BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(stere, stere_entry)
535 BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(ups, ups_entry)
536 }
537
538 } // namespace detail
539 #endif // doxygen
540
541 } // namespace projections
542
543 }} // namespace boost::geometry
544
545 #endif // BOOST_GEOMETRY_PROJECTIONS_STERE_HPP
546