]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/geometry/srs/projections/impl/pj_init.hpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / boost / geometry / srs / projections / impl / pj_init.hpp
1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 // This file is manually converted from PROJ4
3
4 // Copyright (c) 2008-2012 Barend Gehrels, Amsterdam, the Netherlands.
5
6 // This file was modified by Oracle on 2017, 2018.
7 // Modifications copyright (c) 2017-2018, Oracle and/or its affiliates.
8 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
9
10 // Use, modification and distribution is subject to the Boost Software License,
11 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
12 // http://www.boost.org/LICENSE_1_0.txt)
13
14 // This file is converted from PROJ4, http://trac.osgeo.org/proj
15 // PROJ4 is originally written by Gerald Evenden (then of the USGS)
16 // PROJ4 is maintained by Frank Warmerdam
17 // PROJ4 is converted to Geometry Library by Barend Gehrels (Geodan, Amsterdam)
18
19 // Original copyright notice:
20
21 // Permission is hereby granted, free of charge, to any person obtaining a
22 // copy of this software and associated documentation files (the "Software"),
23 // to deal in the Software without restriction, including without limitation
24 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
25 // and/or sell copies of the Software, and to permit persons to whom the
26 // Software is furnished to do so, subject to the following conditions:
27
28 // The above copyright notice and this permission notice shall be included
29 // in all copies or substantial portions of the Software.
30
31 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
32 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
34 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
36 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
37 // DEALINGS IN THE SOFTWARE.
38
39 #ifndef BOOST_GEOMETRY_PROJECTIONS_IMPL_PJ_INIT_HPP
40 #define BOOST_GEOMETRY_PROJECTIONS_IMPL_PJ_INIT_HPP
41
42 #include <cstdlib>
43 #include <string>
44 #include <vector>
45
46 #include <boost/algorithm/string.hpp>
47 #include <boost/mpl/find.hpp>
48 #include <boost/mpl/if.hpp>
49 #include <boost/range.hpp>
50 #include <boost/type_traits/is_same.hpp>
51
52 #include <boost/geometry/util/math.hpp>
53 #include <boost/geometry/util/condition.hpp>
54
55 #include <boost/geometry/srs/projections/dpar.hpp>
56 #include <boost/geometry/srs/projections/impl/dms_parser.hpp>
57 #include <boost/geometry/srs/projections/impl/pj_datum_set.hpp>
58 #include <boost/geometry/srs/projections/impl/pj_datums.hpp>
59 #include <boost/geometry/srs/projections/impl/pj_ell_set.hpp>
60 #include <boost/geometry/srs/projections/impl/pj_param.hpp>
61 #include <boost/geometry/srs/projections/impl/pj_units.hpp>
62 #include <boost/geometry/srs/projections/impl/projects.hpp>
63 #include <boost/geometry/srs/projections/proj4.hpp>
64 #include <boost/geometry/srs/projections/spar.hpp>
65
66
67 namespace boost { namespace geometry { namespace projections
68 {
69
70
71 namespace detail
72 {
73
74 /************************************************************************/
75 /* pj_init_proj() */
76 /************************************************************************/
77
78 template <typename T>
79 inline void pj_init_proj(srs::detail::proj4_parameters const& params,
80 parameters<T> & par)
81 {
82 par.id = pj_get_param_s(params, "proj");
83 }
84
85 template <typename T>
86 inline void pj_init_proj(srs::dpar::parameters<T> const& params,
87 parameters<T> & par)
88 {
89 typename srs::dpar::parameters<T>::const_iterator it = pj_param_find(params, srs::dpar::proj);
90 if (it != params.end())
91 {
92 par.id = static_cast<srs::dpar::value_proj>(it->template get_value<int>());
93 }
94 }
95
96 template <typename T, BOOST_GEOMETRY_PROJECTIONS_DETAIL_TYPENAME_PX>
97 inline void pj_init_proj(srs::spar::parameters<BOOST_GEOMETRY_PROJECTIONS_DETAIL_PX> const& ,
98 parameters<T> & par)
99 {
100 typedef srs::spar::parameters<BOOST_GEOMETRY_PROJECTIONS_DETAIL_PX> params_type;
101 typedef typename srs::spar::detail::tuples_find_if
102 <
103 params_type,
104 srs::spar::detail::is_param_tr<srs::spar::detail::proj_traits>::pred
105 >::type proj_type;
106
107 static const bool is_found = srs::spar::detail::tuples_is_found<proj_type>::value;
108
109 BOOST_MPL_ASSERT_MSG((is_found), PROJECTION_NOT_NAMED, (params_type));
110
111 par.id = srs::spar::detail::proj_traits<proj_type>::id;
112 }
113
114 /************************************************************************/
115 /* pj_init_units() */
116 /************************************************************************/
117
118 template <typename T, bool Vertical>
119 inline void pj_init_units(srs::detail::proj4_parameters const& params,
120 T & to_meter,
121 T & fr_meter,
122 T const& default_to_meter,
123 T const& default_fr_meter)
124 {
125 std::string s;
126 std::string units = pj_get_param_s(params, Vertical ? "vunits" : "units");
127 if (! units.empty())
128 {
129 static const int n = sizeof(pj_units) / sizeof(pj_units[0]);
130 int index = -1;
131 for (int i = 0; i < n && index == -1; i++)
132 {
133 if(pj_units[i].id == units)
134 {
135 index = i;
136 }
137 }
138
139 if (index == -1) {
140 BOOST_THROW_EXCEPTION( projection_exception(error_unknow_unit_id) );
141 }
142 s = pj_units[index].to_meter;
143 }
144
145 if (s.empty())
146 {
147 s = pj_get_param_s(params, Vertical ? "vto_meter" : "to_meter");
148 }
149
150 // TODO: numerator and denominator could be taken from pj_units
151 if (! s.empty())
152 {
153 std::size_t const pos = s.find('/');
154 if (pos == std::string::npos)
155 {
156 to_meter = geometry::str_cast<T>(s);
157 }
158 else
159 {
160 T const numerator = geometry::str_cast<T>(s.substr(0, pos));
161 T const denominator = geometry::str_cast<T>(s.substr(pos + 1));
162 if (numerator == 0.0 || denominator == 0.0)
163 {
164 BOOST_THROW_EXCEPTION( projection_exception(error_unit_factor_less_than_0) );
165 }
166 to_meter = numerator / denominator;
167 }
168 if (to_meter == 0.0)
169 {
170 BOOST_THROW_EXCEPTION( projection_exception(error_unit_factor_less_than_0) );
171 }
172 fr_meter = 1. / to_meter;
173 }
174 else
175 {
176 to_meter = default_to_meter;
177 fr_meter = default_fr_meter;
178 }
179 }
180
181 template <typename T, bool Vertical>
182 inline void pj_init_units(srs::dpar::parameters<T> const& params,
183 T & to_meter,
184 T & fr_meter,
185 T const& default_to_meter,
186 T const& default_fr_meter)
187 {
188 typename srs::dpar::parameters<T>::const_iterator
189 it = pj_param_find(params, Vertical ? srs::dpar::vunits : srs::dpar::units);
190 if (it != params.end())
191 {
192 static const int n = sizeof(pj_units) / sizeof(pj_units[0]);
193 const int i = it->template get_value<int>();
194 if (i >= 0 && i < n)
195 {
196 T const numerator = pj_units[i].numerator;
197 T const denominator = pj_units[i].denominator;
198 if (numerator == 0.0 || denominator == 0.0)
199 {
200 BOOST_THROW_EXCEPTION( projection_exception(error_unit_factor_less_than_0) );
201 }
202 to_meter = numerator / denominator;
203 fr_meter = 1. / to_meter;
204 }
205 else
206 {
207 BOOST_THROW_EXCEPTION( projection_exception(error_unknow_unit_id) );
208 }
209 }
210 else
211 {
212 it = pj_param_find(params, Vertical ? srs::dpar::vto_meter : srs::dpar::to_meter);
213 if (it != params.end())
214 {
215 to_meter = it->template get_value<T>();
216 fr_meter = 1. / to_meter;
217 }
218 else
219 {
220 to_meter = default_to_meter;
221 fr_meter = default_fr_meter;
222 }
223 }
224 }
225
226 template
227 <
228 typename Params,
229 bool Vertical,
230 int UnitsI = srs::spar::detail::tuples_find_index_if
231 <
232 Params,
233 boost::mpl::if_c
234 <
235 Vertical,
236 srs::spar::detail::is_param_t<srs::spar::vunits>,
237 srs::spar::detail::is_param_tr<srs::spar::detail::units_traits>
238 >::type::template pred
239 >::value,
240 int ToMeterI = srs::spar::detail::tuples_find_index_if
241 <
242 Params,
243 boost::mpl::if_c
244 <
245 Vertical,
246 srs::spar::detail::is_param_t<srs::spar::vto_meter>,
247 srs::spar::detail::is_param_t<srs::spar::to_meter>
248 >::type::template pred
249 >::value,
250 int N = boost::tuples::length<Params>::value
251 >
252 struct pj_init_units_static
253 : pj_init_units_static<Params, Vertical, UnitsI, N, N>
254 {};
255
256 template <typename Params, bool Vertical, int UnitsI, int N>
257 struct pj_init_units_static<Params, Vertical, UnitsI, N, N>
258 {
259 static const int n = sizeof(pj_units) / sizeof(pj_units[0]);
260 static const int i = srs::spar::detail::units_traits
261 <
262 typename boost::tuples::element<UnitsI, Params>::type
263 >::id;
264 static const bool is_valid = i >= 0 && i < n;
265
266 BOOST_MPL_ASSERT_MSG((is_valid), UNKNOWN_UNIT_ID, (Params));
267
268 template <typename T>
269 static void apply(Params const& ,
270 T & to_meter, T & fr_meter,
271 T const& , T const& )
272 {
273 T const numerator = pj_units[i].numerator;
274 T const denominator = pj_units[i].denominator;
275 if (numerator == 0.0 || denominator == 0.0)
276 {
277 BOOST_THROW_EXCEPTION( projection_exception(error_unit_factor_less_than_0) );
278 }
279 to_meter = numerator / denominator;
280 fr_meter = 1. / to_meter;
281 }
282 };
283
284 template <typename Params, bool Vertical, int ToMeterI, int N>
285 struct pj_init_units_static<Params, Vertical, N, ToMeterI, N>
286 {
287 template <typename T>
288 static void apply(Params const& params,
289 T & to_meter, T & fr_meter,
290 T const& , T const& )
291 {
292 to_meter = boost::tuples::get<ToMeterI>(params).value;
293 fr_meter = 1. / to_meter;
294 }
295 };
296
297 template <typename Params, bool Vertical, int N>
298 struct pj_init_units_static<Params, Vertical, N, N, N>
299 {
300 template <typename T>
301 static void apply(Params const& ,
302 T & to_meter, T & fr_meter,
303 T const& default_to_meter, T const& default_fr_meter)
304 {
305 to_meter = default_to_meter;
306 fr_meter = default_fr_meter;
307 }
308 };
309
310 template <typename T, bool Vertical, BOOST_GEOMETRY_PROJECTIONS_DETAIL_TYPENAME_PX>
311 inline void pj_init_units(srs::spar::parameters<BOOST_GEOMETRY_PROJECTIONS_DETAIL_PX> const& params,
312 T & to_meter,
313 T & fr_meter,
314 T const& default_to_meter,
315 T const& default_fr_meter)
316 {
317 pj_init_units_static
318 <
319 srs::spar::parameters<BOOST_GEOMETRY_PROJECTIONS_DETAIL_PX>,
320 Vertical
321 >::apply(params, to_meter, fr_meter, default_to_meter, default_fr_meter);
322 }
323
324 /************************************************************************/
325 /* pj_init_pm() */
326 /************************************************************************/
327
328 template <typename T>
329 inline void pj_init_pm(srs::detail::proj4_parameters const& params, T& val)
330 {
331 std::string pm = pj_get_param_s(params, "pm");
332 if (! pm.empty())
333 {
334 int n = sizeof(pj_prime_meridians) / sizeof(pj_prime_meridians[0]);
335 for (int i = 0; i < n ; i++)
336 {
337 if(pj_prime_meridians[i].id == pm)
338 {
339 val = pj_prime_meridians[i].deg * math::d2r<T>();
340 return;
341 }
342 }
343
344 // TODO: Is this try-catch needed?
345 // In other cases the bad_str_cast exception is simply thrown
346 BOOST_TRY
347 {
348 val = dms_parser<T, true>::apply(pm).angle();
349 return;
350 }
351 BOOST_CATCH(geometry::bad_str_cast const&)
352 {
353 BOOST_THROW_EXCEPTION( projection_exception(error_unknown_prime_meridian) );
354 }
355 BOOST_CATCH_END
356 }
357
358 val = 0.0;
359 }
360
361 template <typename T>
362 inline void pj_init_pm(srs::dpar::parameters<T> const& params, T& val)
363 {
364 typename srs::dpar::parameters<T>::const_iterator it = pj_param_find(params, srs::dpar::pm);
365 if (it != params.end())
366 {
367 if (it->template is_value_set<int>())
368 {
369 int n = sizeof(pj_prime_meridians) / sizeof(pj_prime_meridians[0]);
370 int i = it->template get_value<int>();
371 if (i >= 0 && i < n)
372 {
373 val = pj_prime_meridians[i].deg * math::d2r<T>();
374 return;
375 }
376 else
377 {
378 BOOST_THROW_EXCEPTION( projection_exception(error_unknown_prime_meridian) );
379 }
380 }
381 else if (it->template is_value_set<T>())
382 {
383 val = it->template get_value<T>() * math::d2r<T>();
384 return;
385 }
386 }
387
388 val = 0.0;
389 }
390
391 template
392 <
393 typename Params,
394 int I = srs::spar::detail::tuples_find_index_if
395 <
396 Params,
397 srs::spar::detail::is_param_tr<srs::spar::detail::pm_traits>::pred
398 >::value,
399 int N = boost::tuples::length<Params>::value
400 >
401 struct pj_init_pm_static
402 {
403 template <typename T>
404 static void apply(Params const& params, T & val)
405 {
406 typedef typename boost::tuples::element<I, Params>::type param_type;
407
408 val = srs::spar::detail::pm_traits<param_type>::value(boost::tuples::get<I>(params));
409 }
410 };
411 template <typename Params, int N>
412 struct pj_init_pm_static<Params, N, N>
413 {
414 template <typename T>
415 static void apply(Params const& , T & val)
416 {
417 val = 0;
418 }
419 };
420
421 template <typename T, BOOST_GEOMETRY_PROJECTIONS_DETAIL_TYPENAME_PX>
422 inline void pj_init_pm(srs::spar::parameters<BOOST_GEOMETRY_PROJECTIONS_DETAIL_PX> const& params, T& val)
423 {
424 pj_init_pm_static
425 <
426 srs::spar::parameters<BOOST_GEOMETRY_PROJECTIONS_DETAIL_PX>
427 >::apply(params, val);
428 }
429
430 /************************************************************************/
431 /* pj_init() */
432 /* */
433 /* Main entry point for initialing a PJ projections */
434 /* definition. */
435 /************************************************************************/
436 template <typename T, typename Params>
437 inline parameters<T> pj_init(Params const& params)
438 {
439 parameters<T> pin;
440
441 // find projection -> implemented in projection factory
442 pj_init_proj(params, pin);
443 // exception thrown in projection<>
444 // TODO: consider throwing here both projection_unknown_id_exception and
445 // projection_not_named_exception in order to throw before other exceptions
446 //if (pin.name.empty())
447 //{ BOOST_THROW_EXCEPTION( projection_not_named_exception() ); }
448
449 // NOTE: proj4 gets defaults from "proj_def.dat".
450 // In Boost.Geometry this is emulated by manually setting them in
451 // pj_ell_init and projections aea, lcc and lagrng
452
453 /* set datum parameters */
454 pj_datum_init(params, pin);
455
456 /* set ellipsoid/sphere parameters */
457 pj_ell_init(params, pin.a, pin.es);
458
459 pin.a_orig = pin.a;
460 pin.es_orig = pin.es;
461
462 pin.e = sqrt(pin.es);
463 pin.ra = 1. / pin.a;
464 pin.one_es = 1. - pin.es;
465 if (pin.one_es == 0.) {
466 BOOST_THROW_EXCEPTION( projection_exception(error_eccentricity_is_one) );
467 }
468 pin.rone_es = 1./pin.one_es;
469
470 /* Now that we have ellipse information check for WGS84 datum */
471 if( pin.datum_type == datum_3param
472 && pin.datum_params[0] == 0.0
473 && pin.datum_params[1] == 0.0
474 && pin.datum_params[2] == 0.0
475 && pin.a == 6378137.0
476 && geometry::math::abs(pin.es - 0.006694379990) < 0.000000000050 )/*WGS84/GRS80*/
477 {
478 pin.datum_type = datum_wgs84;
479 }
480
481 /* set pin.geoc coordinate system */
482 pin.geoc = (pin.es && pj_get_param_b<srs::spar::geoc>(params, "geoc", srs::dpar::geoc));
483
484 /* over-ranging flag */
485 pin.over = pj_get_param_b<srs::spar::over>(params, "over", srs::dpar::over);
486
487 /* longitude center for wrapping */
488 pin.is_long_wrap_set = pj_param_r<srs::spar::lon_wrap>(params, "lon_wrap", srs::dpar::lon_wrap, pin.long_wrap_center);
489
490 /* central meridian */
491 pin.lam0 = pj_get_param_r<T, srs::spar::lon_0>(params, "lon_0", srs::dpar::lon_0);
492
493 /* central latitude */
494 pin.phi0 = pj_get_param_r<T, srs::spar::lat_0>(params, "lat_0", srs::dpar::lat_0);
495
496 /* false easting and northing */
497 pin.x0 = pj_get_param_f<T, srs::spar::x_0>(params, "x_0", srs::dpar::x_0);
498 pin.y0 = pj_get_param_f<T, srs::spar::y_0>(params, "y_0", srs::dpar::y_0);
499
500 /* general scaling factor */
501 if (pj_param_f<srs::spar::k_0>(params, "k_0", srs::dpar::k_0, pin.k0)) {
502 /* empty */
503 } else if (pj_param_f<srs::spar::k>(params, "k", srs::dpar::k, pin.k0)) {
504 /* empty */
505 } else
506 pin.k0 = 1.;
507 if (pin.k0 <= 0.) {
508 BOOST_THROW_EXCEPTION( projection_exception(error_k_less_than_zero) );
509 }
510
511 /* set units */
512 pj_init_units<T, false>(params, pin.to_meter, pin.fr_meter, 1., 1.);
513 pj_init_units<T, true>(params, pin.vto_meter, pin.vfr_meter, pin.to_meter, pin.fr_meter);
514
515 /* prime meridian */
516 pj_init_pm(params, pin.from_greenwich);
517
518 return pin;
519 }
520
521
522 } // namespace detail
523 }}} // namespace boost::geometry::projections
524
525 #endif // BOOST_GEOMETRY_PROJECTIONS_IMPL_PJ_INIT_HPP