pj_gridinfo.hpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. // Boost.Geometry
  2. // This file is manually converted from PROJ4
  3. // This file was modified by Oracle on 2018, 2019.
  4. // Modifications copyright (c) 2018-2019, Oracle and/or its affiliates.
  5. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  6. // Use, modification and distribution is subject to the Boost Software License,
  7. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. // This file is converted from PROJ4, http://trac.osgeo.org/proj
  10. // PROJ4 is originally written by Gerald Evenden (then of the USGS)
  11. // PROJ4 is maintained by Frank Warmerdam
  12. // This file was converted to Geometry Library by Adam Wulkiewicz
  13. // Original copyright notice:
  14. // Author: Frank Warmerdam, warmerdam@pobox.com
  15. // Copyright (c) 2000, Frank Warmerdam
  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_SRS_PROJECTIONS_IMPL_PJ_GRIDINFO_HPP
  32. #define BOOST_GEOMETRY_SRS_PROJECTIONS_IMPL_PJ_GRIDINFO_HPP
  33. #include <boost/algorithm/string.hpp>
  34. #include <boost/geometry/core/assert.hpp>
  35. #include <boost/geometry/util/math.hpp>
  36. #include <boost/cstdint.hpp>
  37. #include <algorithm>
  38. #include <string>
  39. #include <vector>
  40. namespace boost { namespace geometry { namespace projections
  41. {
  42. namespace detail
  43. {
  44. /************************************************************************/
  45. /* swap_words() */
  46. /* */
  47. /* Convert the byte order of the given word(s) in place. */
  48. /************************************************************************/
  49. inline bool is_lsb()
  50. {
  51. static const int byte_order_test = 1;
  52. static bool result = (1 == ((const unsigned char *) (&byte_order_test))[0]);
  53. return result;
  54. }
  55. inline void swap_words( char *data, int word_size, int word_count )
  56. {
  57. for (int word = 0; word < word_count; word++)
  58. {
  59. for (int i = 0; i < word_size/2; i++)
  60. {
  61. std::swap(data[i], data[word_size-i-1]);
  62. }
  63. data += word_size;
  64. }
  65. }
  66. inline bool cstr_equal(const char * s1, const char * s2, std::size_t n)
  67. {
  68. return std::equal(s1, s1 + n, s2);
  69. }
  70. struct is_trimmable_char
  71. {
  72. inline bool operator()(char ch)
  73. {
  74. return ch == '\n' || ch == ' ';
  75. }
  76. };
  77. // structs originally defined in projects.h
  78. struct pj_ctable
  79. {
  80. struct lp_t { double lam, phi; };
  81. struct flp_t { float lam, phi; };
  82. struct ilp_t { boost::int32_t lam, phi; };
  83. std::string id; // ascii info
  84. lp_t ll; // lower left corner coordinates
  85. lp_t del; // size of cells
  86. ilp_t lim; // limits of conversion matrix
  87. std::vector<flp_t> cvs; // conversion matrix
  88. inline void swap(pj_ctable & r)
  89. {
  90. id.swap(r.id);
  91. std::swap(ll, r.ll);
  92. std::swap(del, r.del);
  93. std::swap(lim, r.lim);
  94. cvs.swap(r.cvs);
  95. }
  96. };
  97. struct pj_gi_load
  98. {
  99. enum format_t { missing = 0, ntv1, ntv2, gtx, ctable, ctable2 };
  100. typedef boost::long_long_type offset_t;
  101. explicit pj_gi_load(std::string const& gname = "",
  102. format_t f = missing,
  103. offset_t off = 0,
  104. bool swap = false)
  105. : gridname(gname)
  106. , format(f)
  107. , grid_offset(off)
  108. , must_swap(swap)
  109. {}
  110. std::string gridname; // identifying name of grid, eg "conus" or ntv2_0.gsb
  111. format_t format; // format of this grid, ie "ctable", "ntv1", "ntv2" or "missing".
  112. offset_t grid_offset; // offset in file, for delayed loading
  113. bool must_swap; // only for NTv2
  114. pj_ctable ct;
  115. inline void swap(pj_gi_load & r)
  116. {
  117. gridname.swap(r.gridname);
  118. std::swap(format, r.format);
  119. std::swap(grid_offset, r.grid_offset);
  120. std::swap(must_swap, r.must_swap);
  121. ct.swap(r.ct);
  122. }
  123. };
  124. struct pj_gi
  125. : pj_gi_load
  126. {
  127. explicit pj_gi(std::string const& gname = "",
  128. pj_gi_load::format_t f = missing,
  129. pj_gi_load::offset_t off = 0,
  130. bool swap = false)
  131. : pj_gi_load(gname, f, off, swap)
  132. {}
  133. std::vector<pj_gi> children;
  134. inline void swap(pj_gi & r)
  135. {
  136. pj_gi_load::swap(r);
  137. children.swap(r.children);
  138. }
  139. };
  140. typedef std::vector<pj_gi> pj_gridinfo;
  141. /************************************************************************/
  142. /* pj_gridinfo_load_ctable() */
  143. /* */
  144. /* Load the data portion of a ctable formatted grid. */
  145. /************************************************************************/
  146. // Originally nad_ctable_load() defined in nad_init.c
  147. template <typename IStream>
  148. bool pj_gridinfo_load_ctable(IStream & is, pj_gi_load & gi)
  149. {
  150. pj_ctable & ct = gi.ct;
  151. // Move the input stream by the size of the proj4 original CTABLE
  152. std::size_t header_size = 80
  153. + 2 * sizeof(pj_ctable::lp_t)
  154. + sizeof(pj_ctable::ilp_t)
  155. + sizeof(pj_ctable::flp_t*);
  156. is.seekg(header_size);
  157. // read all the actual shift values
  158. std::size_t a_size = ct.lim.lam * ct.lim.phi;
  159. ct.cvs.resize(a_size);
  160. std::size_t ch_size = sizeof(pj_ctable::flp_t) * a_size;
  161. is.read(reinterpret_cast<char*>(&ct.cvs[0]), ch_size);
  162. if (is.fail() || std::size_t(is.gcount()) != ch_size)
  163. {
  164. ct.cvs.clear();
  165. //ctable loading failed on fread() - binary incompatible?
  166. return false;
  167. }
  168. return true;
  169. }
  170. /************************************************************************/
  171. /* pj_gridinfo_load_ctable2() */
  172. /* */
  173. /* Load the data portion of a ctable2 formatted grid. */
  174. /************************************************************************/
  175. // Originally nad_ctable2_load() defined in nad_init.c
  176. template <typename IStream>
  177. bool pj_gridinfo_load_ctable2(IStream & is, pj_gi_load & gi)
  178. {
  179. pj_ctable & ct = gi.ct;
  180. is.seekg(160);
  181. // read all the actual shift values
  182. std::size_t a_size = ct.lim.lam * ct.lim.phi;
  183. ct.cvs.resize(a_size);
  184. std::size_t ch_size = sizeof(pj_ctable::flp_t) * a_size;
  185. is.read(reinterpret_cast<char*>(&ct.cvs[0]), ch_size);
  186. if (is.fail() || std::size_t(is.gcount()) != ch_size)
  187. {
  188. //ctable2 loading failed on fread() - binary incompatible?
  189. ct.cvs.clear();
  190. return false;
  191. }
  192. if (! is_lsb())
  193. {
  194. swap_words(reinterpret_cast<char*>(&ct.cvs[0]), 4, (int)a_size * 2);
  195. }
  196. return true;
  197. }
  198. /************************************************************************/
  199. /* pj_gridinfo_load_ntv1() */
  200. /* */
  201. /* NTv1 format. */
  202. /* We process one line at a time. Note that the array storage */
  203. /* direction (e-w) is different in the NTv1 file and what */
  204. /* the CTABLE is supposed to have. The phi/lam are also */
  205. /* reversed, and we have to be aware of byte swapping. */
  206. /************************************************************************/
  207. // originally in pj_gridinfo_load() function
  208. template <typename IStream>
  209. inline bool pj_gridinfo_load_ntv1(IStream & is, pj_gi_load & gi)
  210. {
  211. static const double s2r = math::d2r<double>() / 3600.0;
  212. std::size_t const r_size = gi.ct.lim.lam * 2;
  213. std::size_t const ch_size = sizeof(double) * r_size;
  214. is.seekg(gi.grid_offset);
  215. std::vector<double> row_buf(r_size);
  216. gi.ct.cvs.resize(gi.ct.lim.lam * gi.ct.lim.phi);
  217. for (boost::int32_t row = 0; row < gi.ct.lim.phi; row++ )
  218. {
  219. is.read(reinterpret_cast<char*>(&row_buf[0]), ch_size);
  220. if (is.fail() || std::size_t(is.gcount()) != ch_size)
  221. {
  222. gi.ct.cvs.clear();
  223. return false;
  224. }
  225. if (is_lsb())
  226. swap_words(reinterpret_cast<char*>(&row_buf[0]), 8, (int)r_size);
  227. // convert seconds to radians
  228. for (boost::int32_t i = 0; i < gi.ct.lim.lam; i++ )
  229. {
  230. pj_ctable::flp_t & cvs = gi.ct.cvs[row * gi.ct.lim.lam + (gi.ct.lim.lam - i - 1)];
  231. cvs.phi = (float) (row_buf[i*2] * s2r);
  232. cvs.lam = (float) (row_buf[i*2+1] * s2r);
  233. }
  234. }
  235. return true;
  236. }
  237. /* -------------------------------------------------------------------- */
  238. /* pj_gridinfo_load_ntv2() */
  239. /* */
  240. /* NTv2 format. */
  241. /* We process one line at a time. Note that the array storage */
  242. /* direction (e-w) is different in the NTv2 file and what */
  243. /* the CTABLE is supposed to have. The phi/lam are also */
  244. /* reversed, and we have to be aware of byte swapping. */
  245. /* -------------------------------------------------------------------- */
  246. // originally in pj_gridinfo_load() function
  247. template <typename IStream>
  248. inline bool pj_gridinfo_load_ntv2(IStream & is, pj_gi_load & gi)
  249. {
  250. static const double s2r = math::d2r<double>() / 3600.0;
  251. std::size_t const r_size = gi.ct.lim.lam * 4;
  252. std::size_t const ch_size = sizeof(float) * r_size;
  253. is.seekg(gi.grid_offset);
  254. std::vector<float> row_buf(r_size);
  255. gi.ct.cvs.resize(gi.ct.lim.lam * gi.ct.lim.phi);
  256. for (boost::int32_t row = 0; row < gi.ct.lim.phi; row++ )
  257. {
  258. is.read(reinterpret_cast<char*>(&row_buf[0]), ch_size);
  259. if (is.fail() || std::size_t(is.gcount()) != ch_size)
  260. {
  261. gi.ct.cvs.clear();
  262. return false;
  263. }
  264. if (gi.must_swap)
  265. {
  266. swap_words(reinterpret_cast<char*>(&row_buf[0]), 4, (int)r_size);
  267. }
  268. // convert seconds to radians
  269. for (boost::int32_t i = 0; i < gi.ct.lim.lam; i++ )
  270. {
  271. pj_ctable::flp_t & cvs = gi.ct.cvs[row * gi.ct.lim.lam + (gi.ct.lim.lam - i - 1)];
  272. // skip accuracy values
  273. cvs.phi = (float) (row_buf[i*4] * s2r);
  274. cvs.lam = (float) (row_buf[i*4+1] * s2r);
  275. }
  276. }
  277. return true;
  278. }
  279. /************************************************************************/
  280. /* pj_gridinfo_load_gtx() */
  281. /* */
  282. /* GTX format. */
  283. /************************************************************************/
  284. // originally in pj_gridinfo_load() function
  285. template <typename IStream>
  286. inline bool pj_gridinfo_load_gtx(IStream & is, pj_gi_load & gi)
  287. {
  288. boost::int32_t words = gi.ct.lim.lam * gi.ct.lim.phi;
  289. std::size_t const ch_size = sizeof(float) * words;
  290. is.seekg(gi.grid_offset);
  291. // TODO: Consider changing this unintuitive code
  292. // NOTE: Vertical shift data (one float per point) is stored in a container
  293. // holding horizontal shift data (two floats per point).
  294. gi.ct.cvs.resize((words + 1) / 2);
  295. is.read(reinterpret_cast<char*>(&gi.ct.cvs[0]), ch_size);
  296. if (is.fail() || std::size_t(is.gcount()) != ch_size)
  297. {
  298. gi.ct.cvs.clear();
  299. return false;
  300. }
  301. if (is_lsb())
  302. {
  303. swap_words(reinterpret_cast<char*>(&gi.ct.cvs[0]), 4, words);
  304. }
  305. return true;
  306. }
  307. /************************************************************************/
  308. /* pj_gridinfo_load() */
  309. /* */
  310. /* This function is intended to implement delayed loading of */
  311. /* the data contents of a grid file. The header and related */
  312. /* stuff are loaded by pj_gridinfo_init(). */
  313. /************************************************************************/
  314. template <typename IStream>
  315. inline bool pj_gridinfo_load(IStream & is, pj_gi_load & gi)
  316. {
  317. if (! gi.ct.cvs.empty())
  318. {
  319. return true;
  320. }
  321. if (! is.is_open())
  322. {
  323. return false;
  324. }
  325. // Original platform specific CTable format.
  326. if (gi.format == pj_gi::ctable)
  327. {
  328. return pj_gridinfo_load_ctable(is, gi);
  329. }
  330. // CTable2 format.
  331. else if (gi.format == pj_gi::ctable2)
  332. {
  333. return pj_gridinfo_load_ctable2(is, gi);
  334. }
  335. // NTv1 format.
  336. else if (gi.format == pj_gi::ntv1)
  337. {
  338. return pj_gridinfo_load_ntv1(is, gi);
  339. }
  340. // NTv2 format.
  341. else if (gi.format == pj_gi::ntv2)
  342. {
  343. return pj_gridinfo_load_ntv2(is, gi);
  344. }
  345. // GTX format.
  346. else if (gi.format == pj_gi::gtx)
  347. {
  348. return pj_gridinfo_load_gtx(is, gi);
  349. }
  350. else
  351. {
  352. return false;
  353. }
  354. }
  355. /************************************************************************/
  356. /* pj_gridinfo_parent() */
  357. /* */
  358. /* Seek a parent grid file by name from a grid list */
  359. /************************************************************************/
  360. template <typename It>
  361. inline It pj_gridinfo_parent(It first, It last, std::string const& name)
  362. {
  363. for ( ; first != last ; ++first)
  364. {
  365. if (first->ct.id == name)
  366. return first;
  367. It parent = pj_gridinfo_parent(first->children.begin(), first->children.end(), name);
  368. if( parent != first->children.end() )
  369. return parent;
  370. }
  371. return last;
  372. }
  373. /************************************************************************/
  374. /* pj_gridinfo_init_ntv2() */
  375. /* */
  376. /* Load a ntv2 (.gsb) file. */
  377. /************************************************************************/
  378. template <typename IStream>
  379. inline bool pj_gridinfo_init_ntv2(std::string const& gridname,
  380. IStream & is,
  381. pj_gridinfo & gridinfo)
  382. {
  383. BOOST_STATIC_ASSERT( sizeof(boost::int32_t) == 4 );
  384. BOOST_STATIC_ASSERT( sizeof(double) == 8 );
  385. static const double s2r = math::d2r<double>() / 3600.0;
  386. std::size_t gridinfo_orig_size = gridinfo.size();
  387. // Read the overview header.
  388. char header[11*16];
  389. is.read(header, sizeof(header));
  390. if( is.fail() )
  391. {
  392. return false;
  393. }
  394. bool must_swap = (header[8] == 11)
  395. ? !is_lsb()
  396. : is_lsb();
  397. // NOTE: This check is not implemented in proj4
  398. if (! cstr_equal(header + 56, "SECONDS", 7))
  399. {
  400. return false;
  401. }
  402. // Byte swap interesting fields if needed.
  403. if( must_swap )
  404. {
  405. swap_words( header+8, 4, 1 );
  406. swap_words( header+8+16, 4, 1 );
  407. swap_words( header+8+32, 4, 1 );
  408. swap_words( header+8+7*16, 8, 1 );
  409. swap_words( header+8+8*16, 8, 1 );
  410. swap_words( header+8+9*16, 8, 1 );
  411. swap_words( header+8+10*16, 8, 1 );
  412. }
  413. // Get the subfile count out ... all we really use for now.
  414. boost::int32_t num_subfiles;
  415. memcpy( &num_subfiles, header+8+32, 4 );
  416. // Step through the subfiles, creating a PJ_GRIDINFO for each.
  417. for( boost::int32_t subfile = 0; subfile < num_subfiles; subfile++ )
  418. {
  419. // Read header.
  420. is.read(header, sizeof(header));
  421. if( is.fail() )
  422. {
  423. return false;
  424. }
  425. if(! cstr_equal(header, "SUB_NAME", 8))
  426. {
  427. return false;
  428. }
  429. // Byte swap interesting fields if needed.
  430. if( must_swap )
  431. {
  432. swap_words( header+8+16*4, 8, 1 );
  433. swap_words( header+8+16*5, 8, 1 );
  434. swap_words( header+8+16*6, 8, 1 );
  435. swap_words( header+8+16*7, 8, 1 );
  436. swap_words( header+8+16*8, 8, 1 );
  437. swap_words( header+8+16*9, 8, 1 );
  438. swap_words( header+8+16*10, 4, 1 );
  439. }
  440. // Initialize a corresponding "ct" structure.
  441. pj_ctable ct;
  442. pj_ctable::lp_t ur;
  443. ct.id = std::string(header + 8, 8);
  444. ct.ll.lam = - *((double *) (header+7*16+8)); /* W_LONG */
  445. ct.ll.phi = *((double *) (header+4*16+8)); /* S_LAT */
  446. ur.lam = - *((double *) (header+6*16+8)); /* E_LONG */
  447. ur.phi = *((double *) (header+5*16+8)); /* N_LAT */
  448. ct.del.lam = *((double *) (header+9*16+8));
  449. ct.del.phi = *((double *) (header+8*16+8));
  450. ct.lim.lam = (boost::int32_t) (fabs(ur.lam-ct.ll.lam)/ct.del.lam + 0.5) + 1;
  451. ct.lim.phi = (boost::int32_t) (fabs(ur.phi-ct.ll.phi)/ct.del.phi + 0.5) + 1;
  452. ct.ll.lam *= s2r;
  453. ct.ll.phi *= s2r;
  454. ct.del.lam *= s2r;
  455. ct.del.phi *= s2r;
  456. boost::int32_t gs_count;
  457. memcpy( &gs_count, header + 8 + 16*10, 4 );
  458. if( gs_count != ct.lim.lam * ct.lim.phi )
  459. {
  460. return false;
  461. }
  462. //ct.cvs.clear();
  463. // Create a new gridinfo for this if we aren't processing the
  464. // 1st subfile, and initialize our grid info.
  465. // Attach to the correct list or sublist.
  466. // TODO is offset needed?
  467. pj_gi gi(gridname, pj_gi::ntv2, is.tellg(), must_swap);
  468. gi.ct = ct;
  469. if( subfile == 0 )
  470. {
  471. gridinfo.push_back(gi);
  472. }
  473. else if( cstr_equal(header+24, "NONE", 4) )
  474. {
  475. gridinfo.push_back(gi);
  476. }
  477. else
  478. {
  479. pj_gridinfo::iterator git = pj_gridinfo_parent(gridinfo.begin() + gridinfo_orig_size,
  480. gridinfo.end(),
  481. std::string((const char*)header+24, 8));
  482. if( git == gridinfo.end() )
  483. {
  484. gridinfo.push_back(gi);
  485. }
  486. else
  487. {
  488. git->children.push_back(gi);
  489. }
  490. }
  491. // Seek past the data.
  492. is.seekg(gs_count * 16, std::ios::cur);
  493. }
  494. return true;
  495. }
  496. /************************************************************************/
  497. /* pj_gridinfo_init_ntv1() */
  498. /* */
  499. /* Load an NTv1 style Canadian grid shift file. */
  500. /************************************************************************/
  501. template <typename IStream>
  502. inline bool pj_gridinfo_init_ntv1(std::string const& gridname,
  503. IStream & is,
  504. pj_gridinfo & gridinfo)
  505. {
  506. BOOST_STATIC_ASSERT( sizeof(boost::int32_t) == 4 );
  507. BOOST_STATIC_ASSERT( sizeof(double) == 8 );
  508. static const double d2r = math::d2r<double>();
  509. // Read the header.
  510. char header[176];
  511. is.read(header, sizeof(header));
  512. if( is.fail() )
  513. {
  514. return false;
  515. }
  516. // Regularize fields of interest.
  517. if( is_lsb() )
  518. {
  519. swap_words( header+8, 4, 1 );
  520. swap_words( header+24, 8, 1 );
  521. swap_words( header+40, 8, 1 );
  522. swap_words( header+56, 8, 1 );
  523. swap_words( header+72, 8, 1 );
  524. swap_words( header+88, 8, 1 );
  525. swap_words( header+104, 8, 1 );
  526. }
  527. // NTv1 grid shift file has wrong record count, corrupt?
  528. if( *((boost::int32_t *) (header+8)) != 12 )
  529. {
  530. return false;
  531. }
  532. // NOTE: This check is not implemented in proj4
  533. if (! cstr_equal(header + 120, "SECONDS", 7))
  534. {
  535. return false;
  536. }
  537. // Fill in CTABLE structure.
  538. pj_ctable ct;
  539. pj_ctable::lp_t ur;
  540. ct.id = "NTv1 Grid Shift File";
  541. ct.ll.lam = - *((double *) (header+72));
  542. ct.ll.phi = *((double *) (header+24));
  543. ur.lam = - *((double *) (header+56));
  544. ur.phi = *((double *) (header+40));
  545. ct.del.lam = *((double *) (header+104));
  546. ct.del.phi = *((double *) (header+88));
  547. ct.lim.lam = (boost::int32_t) (fabs(ur.lam-ct.ll.lam)/ct.del.lam + 0.5) + 1;
  548. ct.lim.phi = (boost::int32_t) (fabs(ur.phi-ct.ll.phi)/ct.del.phi + 0.5) + 1;
  549. ct.ll.lam *= d2r;
  550. ct.ll.phi *= d2r;
  551. ct.del.lam *= d2r;
  552. ct.del.phi *= d2r;
  553. //ct.cvs.clear();
  554. // is offset needed?
  555. gridinfo.push_back(pj_gi(gridname, pj_gi::ntv1, is.tellg()));
  556. gridinfo.back().ct = ct;
  557. return true;
  558. }
  559. /************************************************************************/
  560. /* pj_gridinfo_init_gtx() */
  561. /* */
  562. /* Load a NOAA .gtx vertical datum shift file. */
  563. /************************************************************************/
  564. template <typename IStream>
  565. inline bool pj_gridinfo_init_gtx(std::string const& gridname,
  566. IStream & is,
  567. pj_gridinfo & gridinfo)
  568. {
  569. BOOST_STATIC_ASSERT( sizeof(boost::int32_t) == 4 );
  570. BOOST_STATIC_ASSERT( sizeof(double) == 8 );
  571. static const double d2r = math::d2r<double>();
  572. // Read the header.
  573. char header[40];
  574. is.read(header, sizeof(header));
  575. if( is.fail() )
  576. {
  577. return false;
  578. }
  579. // Regularize fields of interest and extract.
  580. double xorigin, yorigin, xstep, ystep;
  581. boost::int32_t rows, columns;
  582. if( is_lsb() )
  583. {
  584. swap_words( header+0, 8, 4 );
  585. swap_words( header+32, 4, 2 );
  586. }
  587. memcpy( &yorigin, header+0, 8 );
  588. memcpy( &xorigin, header+8, 8 );
  589. memcpy( &ystep, header+16, 8 );
  590. memcpy( &xstep, header+24, 8 );
  591. memcpy( &rows, header+32, 4 );
  592. memcpy( &columns, header+36, 4 );
  593. // gtx file header has invalid extents, corrupt?
  594. if( xorigin < -360 || xorigin > 360
  595. || yorigin < -90 || yorigin > 90 )
  596. {
  597. return false;
  598. }
  599. // Fill in CTABLE structure.
  600. pj_ctable ct;
  601. ct.id = "GTX Vertical Grid Shift File";
  602. ct.ll.lam = xorigin;
  603. ct.ll.phi = yorigin;
  604. ct.del.lam = xstep;
  605. ct.del.phi = ystep;
  606. ct.lim.lam = columns;
  607. ct.lim.phi = rows;
  608. // some GTX files come in 0-360 and we shift them back into the
  609. // expected -180 to 180 range if possible. This does not solve
  610. // problems with grids spanning the dateline.
  611. if( ct.ll.lam >= 180.0 )
  612. ct.ll.lam -= 360.0;
  613. if( ct.ll.lam >= 0.0 && ct.ll.lam + ct.del.lam * ct.lim.lam > 180.0 )
  614. {
  615. //"This GTX spans the dateline! This will cause problems." );
  616. }
  617. ct.ll.lam *= d2r;
  618. ct.ll.phi *= d2r;
  619. ct.del.lam *= d2r;
  620. ct.del.phi *= d2r;
  621. //ct.cvs.clear();
  622. // is offset needed?
  623. gridinfo.push_back(pj_gi(gridname, pj_gi::gtx, 40));
  624. gridinfo.back().ct = ct;
  625. return true;
  626. }
  627. /************************************************************************/
  628. /* pj_gridinfo_init_ctable2() */
  629. /* */
  630. /* Read the header portion of a "ctable2" format grid. */
  631. /************************************************************************/
  632. // Originally nad_ctable2_init() defined in nad_init.c
  633. template <typename IStream>
  634. inline bool pj_gridinfo_init_ctable2(std::string const& gridname,
  635. IStream & is,
  636. pj_gridinfo & gridinfo)
  637. {
  638. BOOST_STATIC_ASSERT( sizeof(boost::int32_t) == 4 );
  639. BOOST_STATIC_ASSERT( sizeof(double) == 8 );
  640. char header[160];
  641. is.read(header, sizeof(header));
  642. if( is.fail() )
  643. {
  644. return false;
  645. }
  646. if( !is_lsb() )
  647. {
  648. swap_words( header + 96, 8, 4 );
  649. swap_words( header + 128, 4, 2 );
  650. }
  651. // ctable2 - wrong header!
  652. if (! cstr_equal(header, "CTABLE V2", 9))
  653. {
  654. return false;
  655. }
  656. // read the table header
  657. pj_ctable ct;
  658. ct.id = std::string(header + 16, std::find(header + 16, header + 16 + 80, '\0'));
  659. //memcpy( &ct.ll.lam, header + 96, 8 );
  660. //memcpy( &ct.ll.phi, header + 104, 8 );
  661. //memcpy( &ct.del.lam, header + 112, 8 );
  662. //memcpy( &ct.del.phi, header + 120, 8 );
  663. //memcpy( &ct.lim.lam, header + 128, 4 );
  664. //memcpy( &ct.lim.phi, header + 132, 4 );
  665. memcpy( &ct.ll, header + 96, 40 );
  666. // do some minimal validation to ensure the structure isn't corrupt
  667. if ( (ct.lim.lam < 1) || (ct.lim.lam > 100000)
  668. || (ct.lim.phi < 1) || (ct.lim.phi > 100000) )
  669. {
  670. return false;
  671. }
  672. // trim white space and newlines off id
  673. boost::trim_right_if(ct.id, is_trimmable_char());
  674. //ct.cvs.clear();
  675. gridinfo.push_back(pj_gi(gridname, pj_gi::ctable2));
  676. gridinfo.back().ct = ct;
  677. return true;
  678. }
  679. /************************************************************************/
  680. /* pj_gridinfo_init_ctable() */
  681. /* */
  682. /* Read the header portion of a "ctable" format grid. */
  683. /************************************************************************/
  684. // Originally nad_ctable_init() defined in nad_init.c
  685. template <typename IStream>
  686. inline bool pj_gridinfo_init_ctable(std::string const& gridname,
  687. IStream & is,
  688. pj_gridinfo & gridinfo)
  689. {
  690. BOOST_STATIC_ASSERT( sizeof(boost::int32_t) == 4 );
  691. BOOST_STATIC_ASSERT( sizeof(double) == 8 );
  692. // 80 + 2*8 + 2*8 + 2*4
  693. char header[120];
  694. // NOTE: in proj4 data is loaded directly into CTABLE
  695. is.read(header, sizeof(header));
  696. if( is.fail() )
  697. {
  698. return false;
  699. }
  700. // NOTE: in proj4 LSB is not checked here
  701. // read the table header
  702. pj_ctable ct;
  703. ct.id = std::string(header, std::find(header, header + 80, '\0'));
  704. memcpy( &ct.ll, header + 80, 40 );
  705. // do some minimal validation to ensure the structure isn't corrupt
  706. if ( (ct.lim.lam < 1) || (ct.lim.lam > 100000)
  707. || (ct.lim.phi < 1) || (ct.lim.phi > 100000) )
  708. {
  709. return false;
  710. }
  711. // trim white space and newlines off id
  712. boost::trim_right_if(ct.id, is_trimmable_char());
  713. //ct.cvs.clear();
  714. gridinfo.push_back(pj_gi(gridname, pj_gi::ctable));
  715. gridinfo.back().ct = ct;
  716. return true;
  717. }
  718. /************************************************************************/
  719. /* pj_gridinfo_init() */
  720. /* */
  721. /* Open and parse header details from a datum gridshift file */
  722. /* returning a list of PJ_GRIDINFOs for the grids in that */
  723. /* file. This superceeds use of nad_init() for modern */
  724. /* applications. */
  725. /************************************************************************/
  726. template <typename IStream>
  727. inline bool pj_gridinfo_init(std::string const& gridname,
  728. IStream & is,
  729. pj_gridinfo & gridinfo)
  730. {
  731. char header[160];
  732. // Check if the stream is opened.
  733. if (! is.is_open()) {
  734. return false;
  735. }
  736. // Load a header, to determine the file type.
  737. is.read(header, sizeof(header));
  738. if ( is.fail() ) {
  739. return false;
  740. }
  741. is.seekg(0);
  742. // Determine file type.
  743. if ( cstr_equal(header + 0, "HEADER", 6)
  744. && cstr_equal(header + 96, "W GRID", 6)
  745. && cstr_equal(header + 144, "TO NAD83 ", 16) )
  746. {
  747. return pj_gridinfo_init_ntv1(gridname, is, gridinfo);
  748. }
  749. else if( cstr_equal(header + 0, "NUM_OREC", 8)
  750. && cstr_equal(header + 48, "GS_TYPE", 7) )
  751. {
  752. return pj_gridinfo_init_ntv2(gridname, is, gridinfo);
  753. }
  754. else if( boost::algorithm::ends_with(gridname, "gtx")
  755. || boost::algorithm::ends_with(gridname, "GTX") )
  756. {
  757. return pj_gridinfo_init_gtx(gridname, is, gridinfo);
  758. }
  759. else if( cstr_equal(header + 0, "CTABLE V2", 9) )
  760. {
  761. return pj_gridinfo_init_ctable2(gridname, is, gridinfo);
  762. }
  763. else
  764. {
  765. return pj_gridinfo_init_ctable(gridname, is, gridinfo);
  766. }
  767. }
  768. } // namespace detail
  769. }}} // namespace boost::geometry::projections
  770. #endif // BOOST_GEOMETRY_SRS_PROJECTIONS_IMPL_PJ_GRIDINFO_HPP