pj_datum_set.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. // This file was modified by Oracle on 2017, 2018, 2019.
  5. // Modifications copyright (c) 2017-2019, Oracle and/or its affiliates.
  6. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. // This file is converted from PROJ4, http://trac.osgeo.org/proj
  11. // PROJ4 is originally written by Gerald Evenden (then of the USGS)
  12. // PROJ4 is maintained by Frank Warmerdam
  13. // PROJ4 is converted to Geometry Library by Barend Gehrels (Geodan, Amsterdam)
  14. // Original copyright notice:
  15. // Permission is hereby granted, free of charge, to any person obtaining a
  16. // copy of this software and associated documentation files (the "Software"),
  17. // to deal in the Software without restriction, including without limitation
  18. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  19. // and/or sell copies of the Software, and to permit persons to whom the
  20. // Software is furnished to do so, subject to the following conditions:
  21. // The above copyright notice and this permission notice shall be included
  22. // in all copies or substantial portions of the Software.
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  24. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  26. // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  28. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  29. // DEALINGS IN THE SOFTWARE.
  30. #ifndef BOOST_GEOMETRY_PROJECTIONS_IMPL_PJ_DATUM_SET_HPP
  31. #define BOOST_GEOMETRY_PROJECTIONS_IMPL_PJ_DATUM_SET_HPP
  32. #include <string>
  33. #include <vector>
  34. #include <boost/algorithm/string.hpp>
  35. #include <boost/geometry/srs/projections/dpar.hpp>
  36. #include <boost/geometry/srs/projections/exception.hpp>
  37. #include <boost/geometry/srs/projections/impl/projects.hpp>
  38. #include <boost/geometry/srs/projections/impl/pj_datums.hpp>
  39. #include <boost/geometry/srs/projections/impl/pj_param.hpp>
  40. #include <boost/geometry/srs/projections/proj4.hpp>
  41. #include <boost/geometry/srs/projections/spar.hpp>
  42. namespace boost { namespace geometry { namespace projections {
  43. namespace detail {
  44. /* SEC_TO_RAD = Pi/180/3600 */
  45. template <typename T>
  46. inline T sec_to_rad() { return 4.84813681109535993589914102357e-6; }
  47. /************************************************************************/
  48. /* pj_datum_find_datum() */
  49. /************************************************************************/
  50. template <typename T>
  51. inline const pj_datums_type<T>* pj_datum_find_datum(srs::detail::proj4_parameters const& params)
  52. {
  53. std::string name = pj_get_param_s(params, "datum");
  54. if(! name.empty())
  55. {
  56. /* find the datum definition */
  57. const pj_datums_type<T>* pj_datums = pj_get_datums<T>().first;
  58. const int n = pj_get_datums<T>().second;
  59. int index = -1;
  60. for (int i = 0; i < n && index == -1; i++)
  61. {
  62. if(pj_datums[i].id == name)
  63. {
  64. index = i;
  65. }
  66. }
  67. if (index != -1)
  68. {
  69. return pj_datums + index;
  70. }
  71. else
  72. {
  73. BOOST_THROW_EXCEPTION( projection_exception(error_unknown_ellp_param) );
  74. }
  75. }
  76. return NULL;
  77. }
  78. template <typename T>
  79. inline const pj_datums_type<T>* pj_datum_find_datum(srs::dpar::parameters<T> const& params)
  80. {
  81. typename srs::dpar::parameters<T>::const_iterator
  82. it = pj_param_find(params, srs::dpar::datum);
  83. if (it != params.end())
  84. {
  85. const pj_datums_type<T>* pj_datums = pj_get_datums<T>().first;
  86. const int n = pj_get_datums<T>().second;
  87. int i = it->template get_value<int>();
  88. if (i >= 0 && i < n)
  89. {
  90. return pj_datums + i;
  91. }
  92. else
  93. {
  94. BOOST_THROW_EXCEPTION( projection_exception(error_unknown_ellp_param) );
  95. }
  96. }
  97. return NULL;
  98. }
  99. template
  100. <
  101. typename Params,
  102. typename Param = typename srs::spar::detail::tuples_find_if
  103. <
  104. Params,
  105. srs::spar::detail::is_param_tr<srs::spar::detail::datum_traits>::pred
  106. >::type,
  107. bool IsFound = srs::spar::detail::tuples_is_found<Param>::value
  108. >
  109. struct pj_datum_find_datum_static
  110. {
  111. template <typename T>
  112. static const pj_datums_type<T>* apply(Params const& )
  113. {
  114. const pj_datums_type<T>* pj_datums = pj_get_datums<T>().first;
  115. const int n = pj_get_datums<T>().second;
  116. const int i = srs::spar::detail::datum_traits<Param>::id;
  117. if (i >= 0 && i < n)
  118. {
  119. return pj_datums + i;
  120. }
  121. else
  122. {
  123. // TODO: Implemnt as MPL_ASSERT instead
  124. BOOST_THROW_EXCEPTION( projection_exception(error_unknown_ellp_param) );
  125. }
  126. }
  127. };
  128. template <typename Params, typename Param>
  129. struct pj_datum_find_datum_static<Params, Param, false>
  130. {
  131. template <typename T>
  132. static const pj_datums_type<T>* apply(Params const& )
  133. {
  134. return NULL;
  135. }
  136. };
  137. template <typename T, BOOST_GEOMETRY_PROJECTIONS_DETAIL_TYPENAME_PX>
  138. inline const pj_datums_type<T>* pj_datum_find_datum(srs::spar::parameters<BOOST_GEOMETRY_PROJECTIONS_DETAIL_PX> const& params)
  139. {
  140. return pj_datum_find_datum_static
  141. <
  142. srs::spar::parameters<BOOST_GEOMETRY_PROJECTIONS_DETAIL_PX>
  143. >::template apply<T>(params);
  144. }
  145. /************************************************************************/
  146. /* pj_datum_find_nadgrids() */
  147. /************************************************************************/
  148. inline bool pj_datum_find_nadgrids(srs::detail::proj4_parameters const& params,
  149. srs::detail::nadgrids & out)
  150. {
  151. std::string snadgrids = pj_get_param_s(params, "nadgrids");
  152. if (! snadgrids.empty())
  153. {
  154. for (std::string::size_type i = 0 ; i < snadgrids.size() ; )
  155. {
  156. std::string::size_type end = snadgrids.find(',', i);
  157. std::string name = snadgrids.substr(i, end - i);
  158. i = end;
  159. if (end != std::string::npos)
  160. ++i;
  161. if (! name.empty())
  162. out.push_back(name);
  163. }
  164. }
  165. return ! out.empty();
  166. }
  167. template <typename T>
  168. inline bool pj_datum_find_nadgrids(srs::dpar::parameters<T> const& params,
  169. srs::detail::nadgrids & out)
  170. {
  171. typename srs::dpar::parameters<T>::const_iterator
  172. it = pj_param_find(params, srs::dpar::nadgrids);
  173. if (it != params.end())
  174. {
  175. out = it->template get_value<srs::detail::nadgrids>();
  176. }
  177. return ! out.empty();
  178. }
  179. template
  180. <
  181. typename Params,
  182. int I = srs::spar::detail::tuples_find_index_if
  183. <
  184. Params,
  185. srs::spar::detail::is_param<srs::spar::nadgrids>::pred
  186. >::value,
  187. int N = boost::tuples::length<Params>::value
  188. >
  189. struct pj_datum_find_nadgrids_static
  190. {
  191. static void apply(Params const& params, srs::detail::nadgrids & out)
  192. {
  193. out = boost::tuples::get<I>(params);
  194. }
  195. };
  196. template <typename Params, int N>
  197. struct pj_datum_find_nadgrids_static<Params, N, N>
  198. {
  199. static void apply(Params const& , srs::detail::nadgrids & )
  200. {}
  201. };
  202. template <BOOST_GEOMETRY_PROJECTIONS_DETAIL_TYPENAME_PX>
  203. inline bool pj_datum_find_nadgrids(srs::spar::parameters<BOOST_GEOMETRY_PROJECTIONS_DETAIL_PX> const& params,
  204. srs::detail::nadgrids & out)
  205. {
  206. pj_datum_find_nadgrids_static
  207. <
  208. srs::spar::parameters<BOOST_GEOMETRY_PROJECTIONS_DETAIL_PX>
  209. >::apply(params, out);
  210. return ! out.empty();
  211. }
  212. /************************************************************************/
  213. /* pj_datum_find_towgs84() */
  214. /************************************************************************/
  215. template <typename T>
  216. inline bool pj_datum_find_towgs84(srs::detail::proj4_parameters const& params,
  217. srs::detail::towgs84<T> & out)
  218. {
  219. std::string towgs84 = pj_get_param_s(params, "towgs84");
  220. if(! towgs84.empty())
  221. {
  222. std::vector<std::string> parm;
  223. boost::split(parm, towgs84, boost::is_any_of(" ,"));
  224. std::size_t n = (std::min<std::size_t>)(parm.size(), 7);
  225. std::size_t z = n <= 3 ? 3 : 7;
  226. /* parse out the pvalues */
  227. for (std::size_t i = 0 ; i < n; ++i)
  228. {
  229. out.push_back(geometry::str_cast<T>(parm[i]));
  230. }
  231. for (std::size_t i = out.size() ; i < z; ++i)
  232. {
  233. out.push_back(T(0));
  234. }
  235. }
  236. return ! out.empty();
  237. }
  238. template <typename T>
  239. inline bool pj_datum_find_towgs84(srs::dpar::parameters<T> const& params,
  240. srs::detail::towgs84<T> & out)
  241. {
  242. typename srs::dpar::parameters<T>::const_iterator
  243. it = pj_param_find(params, srs::dpar::towgs84);
  244. if (it != params.end())
  245. {
  246. srs::detail::towgs84<T> const&
  247. towgs84 = it->template get_value<srs::detail::towgs84<T> >();
  248. std::size_t n = (std::min<std::size_t>)(towgs84.size(), 7u);
  249. std::size_t z = n <= 3 ? 3 : 7;
  250. for (std::size_t i = 0 ; i < n; ++i)
  251. {
  252. out.push_back(towgs84[i]);
  253. }
  254. for (std::size_t i = out.size() ; i < z; ++i)
  255. {
  256. out.push_back(T(0));
  257. }
  258. }
  259. return ! out.empty();
  260. }
  261. template
  262. <
  263. typename Params,
  264. int I = srs::spar::detail::tuples_find_index_if
  265. <
  266. Params,
  267. srs::spar::detail::is_param_t<srs::spar::towgs84>::pred
  268. >::value,
  269. int N = boost::tuples::length<Params>::value
  270. >
  271. struct pj_datum_find_towgs84_static
  272. {
  273. template <typename T>
  274. static void apply(Params const& params, srs::detail::towgs84<T> & out)
  275. {
  276. typename boost::tuples::element<I, Params>::type const&
  277. towgs84 = boost::tuples::get<I>(params);
  278. std::size_t n = (std::min<std::size_t>)(towgs84.size(), 7u);
  279. std::size_t z = n <= 3 ? 3 : 7;
  280. for (std::size_t i = 0 ; i < n; ++i)
  281. {
  282. out.push_back(towgs84[i]);
  283. }
  284. for (std::size_t i = out.size() ; i < z; ++i)
  285. {
  286. out.push_back(T(0));
  287. }
  288. }
  289. };
  290. template <typename Params, int N>
  291. struct pj_datum_find_towgs84_static<Params, N, N>
  292. {
  293. template <typename T>
  294. static void apply(Params const& , srs::detail::towgs84<T> & )
  295. {}
  296. };
  297. template <typename T, BOOST_GEOMETRY_PROJECTIONS_DETAIL_TYPENAME_PX>
  298. inline bool pj_datum_find_towgs84(srs::spar::parameters<BOOST_GEOMETRY_PROJECTIONS_DETAIL_PX> const& params,
  299. srs::detail::towgs84<T> & out)
  300. {
  301. pj_datum_find_towgs84_static
  302. <
  303. srs::spar::parameters<BOOST_GEOMETRY_PROJECTIONS_DETAIL_PX>
  304. >::apply(params, out);
  305. return ! out.empty();
  306. }
  307. /************************************************************************/
  308. /* pj_datum_prepare_towgs84() */
  309. /************************************************************************/
  310. template <typename T>
  311. inline bool pj_datum_prepare_towgs84(srs::detail::towgs84<T> & towgs84)
  312. {
  313. if( towgs84.size() == 7
  314. && (towgs84[3] != 0.0
  315. || towgs84[4] != 0.0
  316. || towgs84[5] != 0.0
  317. || towgs84[6] != 0.0) )
  318. {
  319. static const T sec_to_rad = detail::sec_to_rad<T>();
  320. /* transform from arc seconds to radians */
  321. towgs84[3] *= sec_to_rad;
  322. towgs84[4] *= sec_to_rad;
  323. towgs84[5] *= sec_to_rad;
  324. /* transform from parts per million to scaling factor */
  325. towgs84[6] = (towgs84[6]/1000000.0) + 1;
  326. return true;
  327. }
  328. else
  329. {
  330. return false;
  331. }
  332. }
  333. /************************************************************************/
  334. /* pj_datum_init() */
  335. /************************************************************************/
  336. // This function works differently than the original pj_datum_set().
  337. // It doesn't push parameters defined in datum into params list.
  338. // Instead it tries to use nadgrids and towgs84 and only then
  339. // falls back to nadgrid or towgs84 defiend in datum parameter.
  340. template <typename Params, typename T>
  341. inline void pj_datum_init(Params const& params, parameters<T> & projdef)
  342. {
  343. projdef.datum_type = datum_unknown;
  344. // Check for nadgrids parameter.
  345. if(pj_datum_find_nadgrids(params, projdef.nadgrids))
  346. {
  347. // NOTE: It's different than in the original proj4.
  348. // Nadgrids names are stored in projection definition.
  349. projdef.datum_type = datum_gridshift;
  350. }
  351. // Check for towgs84 parameter.
  352. else if(pj_datum_find_towgs84(params, projdef.datum_params))
  353. {
  354. if (pj_datum_prepare_towgs84(projdef.datum_params))
  355. {
  356. projdef.datum_type = datum_7param;
  357. }
  358. else
  359. {
  360. projdef.datum_type = datum_3param;
  361. }
  362. /* Note that pj_init() will later switch datum_type to
  363. PJD_WGS84 if shifts are all zero, and ellipsoid is WGS84 or GRS80 */
  364. }
  365. // Check for datum parameter.
  366. else
  367. {
  368. const pj_datums_type<T>* datum = pj_datum_find_datum<T>(params);
  369. if (datum != NULL)
  370. {
  371. if (! datum->nadgrids.empty())
  372. {
  373. projdef.nadgrids = datum->nadgrids;
  374. projdef.datum_type = datum_gridshift;
  375. }
  376. else if ( ! datum->towgs84.empty() )
  377. {
  378. projdef.datum_params = datum->towgs84;
  379. if (pj_datum_prepare_towgs84(projdef.datum_params))
  380. {
  381. projdef.datum_type = datum_7param;
  382. }
  383. else
  384. {
  385. projdef.datum_type = datum_3param;
  386. }
  387. }
  388. }
  389. }
  390. }
  391. } // namespace detail
  392. }}} // namespace boost::geometry::projections
  393. #endif // BOOST_GEOMETRY_PROJECTIONS_IMPL_PJ_DATUM_SET_HPP