pj_init.hpp 20 KB

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