spindex_rw.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503
  1. /*!
  2. \file diglib/spindex.c
  3. \brief Vector library - spatial index - read/write (lower level functions)
  4. Lower level functions for reading/writing/manipulating vectors.
  5. (C) 2001-2009 by the GRASS Development Team
  6. This program is free software under the GNU General Public License
  7. (>=v2). Read the file COPYING that comes with GRASS for details.
  8. \author Original author CERL, probably Dave Gerdes
  9. \author Update to GRASS 5.7 Radim Blazek
  10. \author Update to GRASS 7 Markus Metz
  11. */
  12. #include <sys/types.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <unistd.h>
  16. #include <assert.h>
  17. #include <grass/vector.h>
  18. #include <grass/glocale.h>
  19. #include <grass/version.h>
  20. /* TODO: only write out actually used sides */
  21. #ifndef NUMSIDES
  22. #define NUMSIDES 6
  23. #endif
  24. /* TODO: merge these two */
  25. struct spidxstack
  26. {
  27. off_t pos[MAXCARD]; /* file position of child node, object ID on level 0 */
  28. struct RTree_Node sn; /* stack node */
  29. int branch_id; /* branch no to follow down */
  30. };
  31. struct spidxpstack
  32. {
  33. off_t pos[MAXCARD]; /* file position of child node, object ID on level 0 */
  34. struct RTree_Node *sn; /* stack node pointer */
  35. int branch_id; /* branch no to follow down */
  36. };
  37. /*!
  38. \brief Write spatial index header to file
  39. \param[in,out] fp pointer to struct gvfile
  40. \param ptr pointer to Plus_head structure
  41. \return 0 on success
  42. \return -1 on error
  43. */
  44. int dig_Wr_spidx_head(struct gvfile * fp, struct Plus_head *ptr)
  45. {
  46. unsigned char buf[6];
  47. long length = 81; /* header length in bytes */
  48. struct RTree *t;
  49. size_t size;
  50. dig_rewind(fp);
  51. dig_set_cur_port(&(ptr->spidx_port));
  52. /* use ptr->off_t_size = 4 if possible */
  53. if (sizeof(off_t) > 4) {
  54. size = ptr->Node_spidx->n_nodes * ptr->Node_spidx->nodesize;
  55. size += ptr->Line_spidx->n_nodes * ptr->Line_spidx->nodesize;
  56. size += ptr->Area_spidx->n_nodes * ptr->Area_spidx->nodesize;
  57. size += ptr->Isle_spidx->n_nodes * ptr->Isle_spidx->nodesize;
  58. if (size < PORT_INT_MAX)
  59. ptr->spidx_port.off_t_size = 4;
  60. else
  61. ptr->spidx_port.off_t_size = 8;
  62. }
  63. else
  64. ptr->spidx_port.off_t_size = 4;
  65. /* bytes 1 - 6 */
  66. buf[0] = GV_SIDX_VER_MAJOR;
  67. buf[1] = GV_SIDX_VER_MINOR;
  68. buf[2] = GV_SIDX_EARLIEST_MAJOR;
  69. buf[3] = GV_SIDX_EARLIEST_MINOR;
  70. buf[4] = ptr->spidx_port.byte_order;
  71. buf[5] = (unsigned char)ptr->spidx_port.off_t_size;
  72. if (0 >= dig__fwrite_port_C((const char *)buf, 6, fp))
  73. return (-1);
  74. /* adjust header size for large files */
  75. if (ptr->spidx_port.off_t_size == 4) {
  76. if (ptr->off_t_size == 4)
  77. length = 113;
  78. else if (ptr->off_t_size == 8)
  79. length = 117;
  80. else
  81. G_fatal_error(_("Topology file must be written before spatial index file"));
  82. }
  83. else if (ptr->spidx_port.off_t_size == 8) {
  84. if (ptr->off_t_size == 4)
  85. length = 141;
  86. else if (ptr->off_t_size == 8)
  87. length = 145;
  88. else
  89. G_fatal_error(_("Topology file must be written before spatial index file"));
  90. }
  91. /* bytes 7 - 10 : header size */
  92. if (0 >= dig__fwrite_port_L(&length, 1, fp))
  93. return (0);
  94. ptr->spidx_head_size = length;
  95. /* byte 11 : dimension 2D or 3D */
  96. buf[0] = ptr->spidx_with_z;
  97. if (0 >= dig__fwrite_port_C((const char *)buf, 1, fp))
  98. return (-1);
  99. /* identical for all spatial indices: */
  100. t = ptr->Node_spidx;
  101. /* byte 12 : n dimensions */
  102. if (0 >= dig__fwrite_port_C((const char *)&(t->ndims), 1, fp))
  103. return (-1);
  104. /* byte 13 : n sides */
  105. if (0 >= dig__fwrite_port_C((const char *)&(t->nsides), 1, fp))
  106. return (-1);
  107. /* bytes 14 - 17 : nodesize */
  108. if (0 >= dig__fwrite_port_I(&(t->nodesize), 1, fp))
  109. return (-1);
  110. /* bytes 18 - 21 : nodecard */
  111. if (0 >= dig__fwrite_port_I(&(t->nodecard), 1, fp))
  112. return (-1);
  113. /* bytes 22 - 25 : leafcard */
  114. if (0 >= dig__fwrite_port_I(&(t->leafcard), 1, fp))
  115. return (-1);
  116. /* bytes 26 - 29 : min node fill */
  117. if (0 >= dig__fwrite_port_I(&(t->min_node_fill), 1, fp))
  118. return (-1);
  119. /* bytes 30 - 33 : min leaf fill */
  120. if (0 >= dig__fwrite_port_I(&(t->min_leaf_fill), 1, fp))
  121. return (-1);
  122. /* for each spatial index : */
  123. /* Node spatial index */
  124. /* bytes 34 - 37 : n nodes */
  125. if (0 >= dig__fwrite_port_I((const int *)&(t->n_nodes), 1, fp))
  126. return (-1);
  127. /* bytes 38 - 41 : n leafs */
  128. if (0 >= dig__fwrite_port_I((const int *)&(t->n_leafs), 1, fp))
  129. return (-1);
  130. /* bytes 42 - 45 : n levels */
  131. if (0 >= dig__fwrite_port_I(&(t->rootlevel), 1, fp))
  132. return (-1);
  133. /* bytes 46 - 49 (LFS 53) : root node offset */
  134. if (0 >=
  135. dig__fwrite_port_O(&(ptr->Node_spidx_offset), 1, fp,
  136. ptr->spidx_port.off_t_size))
  137. return (-1);
  138. /* Line spatial index */
  139. t = ptr->Line_spidx;
  140. /* bytes 50 - 53 (LFS 54 - 57) : n nodes */
  141. if (0 >= dig__fwrite_port_I((const int *)&(t->n_nodes), 1, fp))
  142. return (-1);
  143. /* bytes 54 - 57 (LFS 58 - 61) : n leafs */
  144. if (0 >= dig__fwrite_port_I((const int *)&(t->n_leafs), 1, fp))
  145. return (-1);
  146. /* bytes 58 - 61 (LFS 62 - 65) : n levels */
  147. if (0 >= dig__fwrite_port_I(&(t->rootlevel), 1, fp))
  148. return (-1);
  149. /* bytes 62 - 65 (LFS 66 - 73) : root node offset */
  150. if (0 >=
  151. dig__fwrite_port_O(&(ptr->Line_spidx_offset), 1, fp,
  152. ptr->spidx_port.off_t_size))
  153. return (-1);
  154. /* Area spatial index */
  155. t = ptr->Area_spidx;
  156. /* bytes 66 - 69 (LFS 74 - 77) : n nodes */
  157. if (0 >= dig__fwrite_port_I((const int *)&(t->n_nodes), 1, fp))
  158. return (-1);
  159. /* bytes 70 - 73 (LFS 78 - 81) : n leafs */
  160. if (0 >= dig__fwrite_port_I((const int *)&(t->n_leafs), 1, fp))
  161. return (-1);
  162. /* bytes 74 - 77 (LFS 82 - 85) : n levels */
  163. if (0 >= dig__fwrite_port_I(&(t->rootlevel), 1, fp))
  164. return (-1);
  165. /* bytes 78 - 81 (LFS 86 - 93) : root node offset */
  166. if (0 >=
  167. dig__fwrite_port_O(&(ptr->Area_spidx_offset), 1, fp,
  168. ptr->spidx_port.off_t_size))
  169. return (-1);
  170. /* Isle spatial index */
  171. t = ptr->Isle_spidx;
  172. /* bytes 82 - 85 (LFS 94 - 97) : n nodes */
  173. if (0 >= dig__fwrite_port_I((const int *)&(t->n_nodes), 1, fp))
  174. return (-1);
  175. /* bytes 86 - 89 (LFS 98 - 101) : n leafs */
  176. if (0 >= dig__fwrite_port_I((const int *)&(t->n_leafs), 1, fp))
  177. return (-1);
  178. /* bytes 90 - 93 (LFS 102 - 105) : n levels */
  179. if (0 >= dig__fwrite_port_I(&(t->rootlevel), 1, fp))
  180. return (-1);
  181. /* bytes 94 - 97 (LFS 106 - 113) : root node offset */
  182. if (0 >=
  183. dig__fwrite_port_O(&(ptr->Isle_spidx_offset), 1, fp,
  184. ptr->spidx_port.off_t_size))
  185. return (-1);
  186. /* 3D future : */
  187. /* Face spatial index */
  188. /* bytes 98 - 101 (LFS 114 - 121) : root node offset */
  189. if (0 >=
  190. dig__fwrite_port_O(&(ptr->Face_spidx_offset), 1, fp,
  191. ptr->spidx_port.off_t_size))
  192. return (-1);
  193. /* ptr->Face_spidx->rootpos = ptr->Face_spidx_offset; */
  194. /* Volume spatial index */
  195. /* bytes 102 - 105 (LFS 122 - 129) : root node offset */
  196. if (0 >=
  197. dig__fwrite_port_O(&(ptr->Volume_spidx_offset), 1, fp,
  198. ptr->spidx_port.off_t_size))
  199. return (-1);
  200. /* ptr->Volume_spidx->rootpos = ptr->Volume_spidx_offset; */
  201. /* Hole spatial index */
  202. /* bytes 106 - 109 (LFS 130 - 137) : root node offset */
  203. if (0 >=
  204. dig__fwrite_port_O(&(ptr->Hole_spidx_offset), 1, fp,
  205. ptr->spidx_port.off_t_size))
  206. return (-1);
  207. /* ptr->Hole_spidx->rootpos = ptr->Hole_spidx_offset; */
  208. G_debug(3, "spidx offset node = %lu line = %lu, area = %lu isle = %lu",
  209. (long unsigned)ptr->Node_spidx_offset,
  210. (long unsigned)ptr->Line_spidx_offset,
  211. (long unsigned)ptr->Area_spidx_offset,
  212. (long unsigned)ptr->Isle_spidx_offset);
  213. /* coor file size : bytes 110 - 113 (117) (LFS: 138 - 141 (145)) */
  214. if (0 >= dig__fwrite_port_O(&(ptr->coor_size), 1, fp, ptr->off_t_size))
  215. return (-1);
  216. length = (long unsigned)dig_ftell(fp);
  217. G_debug(1, "spidx body offset %lu", length);
  218. if (ptr->spidx_head_size != length)
  219. G_fatal_error("wrong sidx head length %ld", ptr->spidx_head_size);
  220. return (0);
  221. }
  222. /*!
  223. \brief Read spatial index header from sidx file
  224. \param fp pointer to struct gvfile
  225. \param[in,out] ptr pointer to Plus_head structure
  226. \return 0 on success
  227. \return -1 on error
  228. */
  229. int dig_Rd_spidx_head(struct gvfile * fp, struct Plus_head *ptr)
  230. {
  231. unsigned char buf[6];
  232. int byte_order;
  233. struct RTree *t;
  234. dig_rewind(fp);
  235. /* bytes 1 - 6 */
  236. if (0 >= dig__fread_port_C((char *)buf, 6, fp))
  237. return (-1);
  238. ptr->version.spidx.major = buf[0];
  239. ptr->version.spidx.minor = buf[1];
  240. ptr->version.spidx.back_major = buf[2];
  241. ptr->version.spidx.back_minor = buf[3];
  242. byte_order = buf[4];
  243. ptr->spidx_port.off_t_size = buf[5];
  244. G_debug(2,
  245. "Spidx header: file version %d.%d , supported from GRASS version %d.%d",
  246. ptr->version.spidx.major, ptr->version.spidx.minor,
  247. ptr->version.spidx.back_major, ptr->version.spidx.back_minor);
  248. G_debug(2, " byte order %d", byte_order);
  249. /* check version numbers */
  250. if (ptr->version.spidx.major > GV_SIDX_VER_MAJOR ||
  251. ptr->version.spidx.minor > GV_SIDX_VER_MINOR) {
  252. /* The file was created by GRASS library with higher version than this one */
  253. if (ptr->version.spidx.back_major > GV_SIDX_VER_MAJOR ||
  254. ptr->version.spidx.back_minor > GV_SIDX_VER_MINOR) {
  255. /* This version of GRASS lib is lower than the oldest which can read this format */
  256. G_debug(1, "Spatial index format version %d.%d",
  257. ptr->version.spidx.major, ptr->version.spidx.minor);
  258. G_fatal_error
  259. (_("This version of GRASS (%d.%d) is too old to read this spatial index format."
  260. " Try to rebuild topology or upgrade GRASS to at least version %d."),
  261. GRASS_VERSION_MAJOR, GRASS_VERSION_MINOR, GRASS_VERSION_MAJOR + 1);
  262. return (-1);
  263. }
  264. G_warning(_("Your GRASS version does not fully support "
  265. "spatial index format %d.%d of the vector."
  266. " Consider to rebuild topology or upgrade GRASS."),
  267. ptr->version.spidx.major, ptr->version.spidx.minor);
  268. }
  269. if (ptr->version.spidx.major < GV_SIDX_VER_MAJOR ||
  270. (ptr->version.spidx.major == GV_SIDX_VER_MAJOR &&
  271. ptr->version.spidx.minor < GV_SIDX_VER_MINOR)) {
  272. /* The file was created by GRASS library with lower version than this one */
  273. G_fatal_error(_("Spatial index format version %d.%d is not "
  274. "supported by this release."
  275. " Please rebuild topology."),
  276. ptr->version.spidx.major, ptr->version.spidx.minor);
  277. return (-1);
  278. }
  279. /* can this library read the sidx file ? */
  280. if (ptr->spidx_port.off_t_size > (int)sizeof(off_t)) {
  281. G_fatal_error("Spatial index was written with LFS but this "
  282. "GRASS version does not support LFS. "
  283. "Please get a GRASS version with LFS support.");
  284. }
  285. dig_init_portable(&(ptr->spidx_port), byte_order);
  286. dig_set_cur_port(&(ptr->spidx_port));
  287. /* bytes 7 - 10 : header size */
  288. if (0 >= dig__fread_port_L(&(ptr->spidx_head_size), 1, fp))
  289. return (-1);
  290. G_debug(2, " header size %ld", ptr->spidx_head_size);
  291. /* byte 11 : dimension 2D or 3D */
  292. if (0 >= dig__fread_port_C((char *)buf, 1, fp))
  293. return (-1);
  294. ptr->spidx_with_z = buf[0];
  295. G_debug(2, " with_z %d", ptr->spidx_with_z);
  296. /* identical for all spatial indices: */
  297. t = ptr->Node_spidx;
  298. /* byte 12 : n dimensions */
  299. if (0 >= dig__fread_port_C((char *)&(t->ndims), 1, fp))
  300. return (-1);
  301. ptr->Node_spidx->ndims = t->ndims;
  302. ptr->Line_spidx->ndims = t->ndims;
  303. ptr->Area_spidx->ndims = t->ndims;
  304. ptr->Isle_spidx->ndims = t->ndims;
  305. /* byte 13 : n sides */
  306. if (0 >= dig__fread_port_C((char *)&(t->nsides), 1, fp))
  307. return (-1);
  308. ptr->Node_spidx->nsides = t->nsides;
  309. ptr->Line_spidx->nsides = t->nsides;
  310. ptr->Area_spidx->nsides = t->nsides;
  311. ptr->Isle_spidx->nsides = t->nsides;
  312. /* bytes 14 - 17 : nodesize */
  313. if (0 >= dig__fread_port_I(&(t->nodesize), 1, fp))
  314. return (-1);
  315. ptr->Node_spidx->nodesize = t->nodesize;
  316. ptr->Line_spidx->nodesize = t->nodesize;
  317. ptr->Area_spidx->nodesize = t->nodesize;
  318. ptr->Isle_spidx->nodesize = t->nodesize;
  319. /* bytes 18 - 21 : nodecard */
  320. if (0 >= dig__fread_port_I(&(t->nodecard), 1, fp))
  321. return (-1);
  322. ptr->Node_spidx->nodecard = t->nodecard;
  323. ptr->Line_spidx->nodecard = t->nodecard;
  324. ptr->Area_spidx->nodecard = t->nodecard;
  325. ptr->Isle_spidx->nodecard = t->nodecard;
  326. /* bytes 22 - 25 : leafcard */
  327. if (0 >= dig__fread_port_I(&(t->leafcard), 1, fp))
  328. return (-1);
  329. ptr->Node_spidx->leafcard = t->leafcard;
  330. ptr->Line_spidx->leafcard = t->leafcard;
  331. ptr->Area_spidx->leafcard = t->leafcard;
  332. ptr->Isle_spidx->leafcard = t->leafcard;
  333. /* bytes 26 - 29 : min node fill */
  334. if (0 >= dig__fread_port_I(&(t->min_node_fill), 1, fp))
  335. return (-1);
  336. ptr->Node_spidx->min_node_fill = t->min_node_fill;
  337. ptr->Line_spidx->min_node_fill = t->min_node_fill;
  338. ptr->Area_spidx->min_node_fill = t->min_node_fill;
  339. ptr->Isle_spidx->min_node_fill = t->min_node_fill;
  340. /* bytes 30 - 33 : min leaf fill */
  341. if (0 >= dig__fread_port_I(&(t->min_leaf_fill), 1, fp))
  342. return (-1);
  343. ptr->Node_spidx->min_leaf_fill = t->min_leaf_fill;
  344. ptr->Line_spidx->min_leaf_fill = t->min_leaf_fill;
  345. ptr->Area_spidx->min_leaf_fill = t->min_leaf_fill;
  346. ptr->Isle_spidx->min_leaf_fill = t->min_leaf_fill;
  347. /* for each spatial index : */
  348. /* Node spatial index */
  349. /* bytes 34 - 37 : n nodes */
  350. if (0 >= dig__fread_port_I((int *)&(t->n_nodes), 1, fp))
  351. return (-1);
  352. /* bytes 38 - 41 : n leafs */
  353. if (0 >= dig__fread_port_I((int *)&(t->n_leafs), 1, fp))
  354. return (-1);
  355. /* bytes 42 - 45 : n levels */
  356. if (0 >= dig__fread_port_I(&(t->rootlevel), 1, fp))
  357. return (-1);
  358. /* bytes 46 - 49 (LFS 53) : root node offset */
  359. if (0 >=
  360. dig__fread_port_O(&(ptr->Node_spidx_offset), 1, fp,
  361. ptr->spidx_port.off_t_size))
  362. return (-1);
  363. t->rootpos = ptr->Node_spidx_offset;
  364. /* Line spatial index */
  365. t = ptr->Line_spidx;
  366. /* bytes 50 - 53 (LFS 54 - 57) : n nodes */
  367. if (0 >= dig__fread_port_I((int *)&(t->n_nodes), 1, fp))
  368. return (-1);
  369. /* bytes 54 - 57 (LFS 58 - 61) : n leafs */
  370. if (0 >= dig__fread_port_I((int *)&(t->n_leafs), 1, fp))
  371. return (-1);
  372. /* bytes 58 - 61 (LFS 62 - 65) : n levels */
  373. if (0 >= dig__fread_port_I(&(t->rootlevel), 1, fp))
  374. return (-1);
  375. /* bytes 62 - 65 (LFS 66 - 73) : root node offset */
  376. if (0 >=
  377. dig__fread_port_O(&(ptr->Line_spidx_offset), 1, fp,
  378. ptr->spidx_port.off_t_size))
  379. return (-1);
  380. ptr->Line_spidx->rootpos = ptr->Line_spidx_offset;
  381. /* Area spatial index */
  382. t = ptr->Area_spidx;
  383. /* bytes 66 - 69 (LFS 74 - 77) : n nodes */
  384. if (0 >= dig__fread_port_I((int *)&(t->n_nodes), 1, fp))
  385. return (-1);
  386. /* bytes 70 - 73 (LFS 78 - 81) : n leafs */
  387. if (0 >= dig__fread_port_I((int *)&(t->n_leafs), 1, fp))
  388. return (-1);
  389. /* bytes 74 - 77 (LFS 82 - 85) : n levels */
  390. if (0 >= dig__fread_port_I(&(t->rootlevel), 1, fp))
  391. return (-1);
  392. /* bytes 78 - 81 (LFS 86 - 93) : root node offset */
  393. if (0 >=
  394. dig__fread_port_O(&(ptr->Area_spidx_offset), 1, fp,
  395. ptr->spidx_port.off_t_size))
  396. return (-1);
  397. ptr->Area_spidx->rootpos = ptr->Area_spidx_offset;
  398. /* Isle spatial index */
  399. t = ptr->Isle_spidx;
  400. /* bytes 82 - 85 (LFS 94 - 97) : n nodes */
  401. if (0 >= dig__fread_port_I((int *)&(t->n_nodes), 1, fp))
  402. return (-1);
  403. /* bytes 86 - 89 (LFS 98 - 101) : n leafs */
  404. if (0 >= dig__fread_port_I((int *)&(t->n_leafs), 1, fp))
  405. return (-1);
  406. /* bytes 90 - 93 (LFS 102 - 105) : n levels */
  407. if (0 >= dig__fread_port_I(&(t->rootlevel), 1, fp))
  408. return (-1);
  409. /* bytes 94 - 97 (LFS 106 - 113) : root node offset */
  410. if (0 >=
  411. dig__fread_port_O(&(ptr->Isle_spidx_offset), 1, fp,
  412. ptr->spidx_port.off_t_size))
  413. return (-1);
  414. ptr->Isle_spidx->rootpos = ptr->Isle_spidx_offset;
  415. /* 3D future : */
  416. /* Face spatial index */
  417. /* bytes 98 - 101 (LFS 114 - 121) : root node offset */
  418. if (0 >=
  419. dig__fread_port_O(&(ptr->Face_spidx_offset), 1, fp,
  420. ptr->spidx_port.off_t_size))
  421. return (-1);
  422. /* ptr->Face_spidx->rootpos = ptr->Face_spidx_offset; */
  423. /* Volume spatial index */
  424. /* bytes 102 - 105 (LFS 122 - 129) : root node offset */
  425. if (0 >=
  426. dig__fread_port_O(&(ptr->Volume_spidx_offset), 1, fp,
  427. ptr->spidx_port.off_t_size))
  428. return (-1);
  429. /* ptr->Volume_spidx->rootpos = ptr->Volume_spidx_offset; */
  430. /* Hole spatial index */
  431. /* bytes 106 - 109 (LFS 130 - 137) : root node offset */
  432. if (0 >=
  433. dig__fread_port_O(&(ptr->Hole_spidx_offset), 1, fp,
  434. ptr->spidx_port.off_t_size))
  435. return (-1);
  436. /* ptr->Hole_spidx->rootpos = ptr->Hole_spidx_offset; */
  437. /* coor file size : bytes 110 - 113 (117) (LFS: 138 - 145) */
  438. if (ptr->off_t_size == -1)
  439. ptr->off_t_size = ptr->spidx_port.off_t_size;
  440. if (0 >= dig__fread_port_O(&(ptr->coor_size), 1, fp, ptr->off_t_size))
  441. return (-1);
  442. G_debug(2, " coor size %lu", (long unsigned)ptr->coor_size);
  443. dig_fseek(fp, ptr->spidx_head_size, SEEK_SET);
  444. return (0);
  445. }
  446. static int rtree_dump_node(FILE *, struct RTree_Node *n, int);
  447. /*!
  448. \brief Dump R-tree branch to the file
  449. \param fp pointer to FILE
  450. \param b pointer to Branch structure
  451. \param with_z non-zero value for 3D vector data
  452. \param level level value
  453. \return 0
  454. */
  455. static int rtree_dump_branch(FILE * fp, struct RTree_Branch *b, int with_z,
  456. int level)
  457. {
  458. const struct RTree_Rect *r;
  459. r = &(b->rect);
  460. if (level == 0)
  461. fprintf(fp, " id = %d ", b->child.id);
  462. fprintf(fp, " %f %f %f %f %f %f\n", r->boundary[0], r->boundary[1],
  463. r->boundary[2], r->boundary[3], r->boundary[4], r->boundary[5]);
  464. if (level > 0) {
  465. rtree_dump_node(fp, b->child.ptr, with_z);
  466. }
  467. return 0;
  468. }
  469. /*!
  470. \brief Dump R-tree node to the file
  471. \param fp pointer to FILE
  472. \param n pointer to Node structure
  473. \param with_z non-zero value for 3D vector data
  474. \return 0
  475. */
  476. int rtree_dump_node(FILE * fp, struct RTree_Node *n, int with_z)
  477. {
  478. int i;
  479. /* recursive nearly-but-a-bit-messy depth-first pre-order traversal
  480. * potentially filling up memory */
  481. /* TODO: change to non-recursive depth-first post-order traversal */
  482. /* left for comparison with GRASS6.x */
  483. fprintf(fp, "Node level=%d count=%d\n", n->level, n->count);
  484. if (n->level > 0)
  485. for (i = 0; i < NODECARD; i++) {
  486. if (n->branch[i].child.ptr) {
  487. fprintf(fp, " Branch %d", i);
  488. rtree_dump_branch(fp, &n->branch[i], with_z, n->level);
  489. }
  490. }
  491. else
  492. for (i = 0; i < LEAFCARD; i++) {
  493. if (n->branch[i].child.id) {
  494. fprintf(fp, " Branch %d", i);
  495. rtree_dump_branch(fp, &n->branch[i], with_z, n->level);
  496. }
  497. }
  498. return 0;
  499. }
  500. static int rtree_dump_node_file(FILE *, off_t, int, struct RTree *);
  501. /*!
  502. \brief Dump R-tree branch from temp file to the file
  503. \param fp pointer to FILE
  504. \param b pointer to Branch structure
  505. \param with_z non-zero value for 3D vector data
  506. \param level level value
  507. \return 0
  508. */
  509. static int rtree_dump_branch_file(FILE * fp, struct RTree_Branch *b, int with_z,
  510. int level, struct RTree *t)
  511. {
  512. const struct RTree_Rect *r;
  513. r = &(b->rect);
  514. if (level == 0)
  515. fprintf(fp, " id = %d ", b->child.id);
  516. fprintf(fp, " %f %f %f %f %f %f\n", r->boundary[0], r->boundary[1],
  517. r->boundary[2], r->boundary[3], r->boundary[4], r->boundary[5]);
  518. if (level > 0) {
  519. rtree_dump_node_file(fp, b->child.pos, with_z, t);
  520. }
  521. return 0;
  522. }
  523. /*!
  524. \brief Dump R-tree node from temp file to the file
  525. \param fp pointer to FILE
  526. \param pos position of Node in temp file
  527. \param with_z non-zero value for 3D vector data
  528. \param t RTree to dump
  529. \return 0
  530. */
  531. int rtree_dump_node_file(FILE * fp, off_t pos, int with_z, struct RTree *t)
  532. {
  533. int i;
  534. static struct RTree_Node *n = NULL;
  535. if (!n) {
  536. n = RTreeAllocNode(t, 1);
  537. }
  538. /* recursive nearly-but-a-bit-messy depth-first pre-order traversal
  539. * potentially filling up memory */
  540. /* TODO: change to non-recursive depth-first post-order traversal */
  541. /* left for comparison with GRASS6.x */
  542. RTreeReadNode(n, pos, t);
  543. fprintf(fp, "Node level=%d count=%d\n", n->level, n->count);
  544. if (n->level > 0)
  545. for (i = 0; i < NODECARD; i++) {
  546. if (n->branch[i].child.pos >= 0) {
  547. fprintf(fp, " Branch %d", i);
  548. rtree_dump_branch_file(fp, &(n->branch[i]), with_z, n->level, t);
  549. }
  550. }
  551. else
  552. for (i = 0; i < LEAFCARD; i++) {
  553. if (n->branch[i].child.id) {
  554. fprintf(fp, " Branch %d", i);
  555. rtree_dump_branch_file(fp, &(n->branch[i]), with_z, n->level, t);
  556. }
  557. }
  558. return 0;
  559. }
  560. /*
  561. * all following methods to transfer spatial indices (rtrees) are based
  562. * on the same idea
  563. * do a postorder depth-first non-recursive traversal of the rtree
  564. * a leaf node is transferred first
  565. * the root node is transferred last
  566. *
  567. * this applies to all four scenarios
  568. * - from intermediate file to sidx file
  569. * - from sidx file to intermediate file
  570. * - from memory to sidx file
  571. * - from sidx file to memory
  572. *
  573. * I could not think of one function that's good for all four scenarios,
  574. * but that doesn't mean there is none...
  575. *
  576. * maybe something like V2_read_line_array and Write_line_array
  577. * in Vlib/read.c and Vlib/write.c, at least for transferring from sidx
  578. * and transferrring to sidx?
  579. */
  580. /*!
  581. \brief Write RTree body from memory to sidx file
  582. Must be called when new or updated vector is closed
  583. \param[out] fp pointer to struct gvfile
  584. \param startpos offset to struct gvfile where to start writing out
  585. \param t pointer to RTree
  586. \param off_t_size size of off_t used to write struct gvfile
  587. \return -1 on error
  588. \return offset to root node on success
  589. */
  590. static off_t rtree_write_from_memory(struct gvfile *fp, off_t startpos,
  591. struct RTree *t, int off_t_size)
  592. {
  593. off_t nextfreepos = startpos;
  594. int sidx_nodesize, sidx_leafsize;
  595. struct RTree_Node *n;
  596. int i, j, writeout, maxcard;
  597. struct spidxpstack *s = G_malloc(MAXLEVEL * sizeof(struct spidxstack));
  598. int top = 0;
  599. /* should be foolproof */
  600. sidx_nodesize =
  601. (int)(2 * PORT_INT + t->nodecard * (off_t_size + NUMSIDES * PORT_DOUBLE));
  602. sidx_leafsize =
  603. (int)(2 * PORT_INT + t->leafcard * (off_t_size + NUMSIDES * PORT_DOUBLE));
  604. /* stack size of t->rootlevel + 1 would be enough because of
  605. * depth-first post-order traversal:
  606. * only one node per level on stack at any given time */
  607. /* add root node position to stack */
  608. s[top].branch_id = i = 0;
  609. s[top].sn = t->root;
  610. /* depth-first postorder traversal
  611. * all children of a node are visitied and written out first
  612. * when a child is written out, its position in file is stored in pos[] for
  613. * the parent node and written out with the parent node */
  614. /* root node is written out last and its position returned */
  615. while (top >= 0) {
  616. if (s[top].sn == NULL)
  617. G_fatal_error("NULL node ptr at top = %d", top);
  618. n = s[top].sn;
  619. writeout = 1;
  620. /* this is an internal node in the RTree
  621. * all its children are processed first,
  622. * before it is written out to the sidx file */
  623. if (s[top].sn->level > 0) {
  624. for (i = s[top].branch_id; i < t->nodecard; i++) {
  625. s[top].pos[i] = 0;
  626. if (n->branch[i].child.ptr != NULL) {
  627. s[top++].branch_id = i + 1;
  628. s[top].sn = n->branch[i].child.ptr;
  629. s[top].branch_id = 0;
  630. writeout = 0;
  631. break;
  632. }
  633. }
  634. if (writeout) {
  635. /* nothing else found, ready to write out */
  636. s[top].branch_id = t->nodecard;
  637. }
  638. }
  639. if (writeout) {
  640. /* write node to sidx file */
  641. if (G_ftell(fp->file) != nextfreepos)
  642. G_fatal_error("Unable to write spatial index. "
  643. "Wrong node position (%"PRI_OFF_T") in file (should be %"PRI_OFF_T").",
  644. G_ftell(fp->file), nextfreepos);
  645. /* write with dig__fwrite_port_* fns */
  646. dig__fwrite_port_I(&(s[top].sn->count), 1, fp);
  647. dig__fwrite_port_I(&(s[top].sn->level), 1, fp);
  648. maxcard = s[top].sn->level ? t->nodecard : t->leafcard;
  649. for (j = 0; j < maxcard; j++) {
  650. dig__fwrite_port_D(s[top].sn->branch[j].rect.boundary,
  651. NUMSIDES, fp);
  652. /* leaf node: vector object IDs are stored in child.id */
  653. if (s[top].sn->level == 0)
  654. s[top].pos[j] = (off_t) s[top].sn->branch[j].child.id;
  655. dig__fwrite_port_O(&(s[top].pos[j]), 1, fp, off_t_size);
  656. }
  657. top--;
  658. /* update corresponding child position of parent node
  659. * this node is only updated if its level is > 0, i.e.
  660. * this is an internal node
  661. * children of internal nodes do not have an ID, instead
  662. * they hold the position in file of the next nodes down the tree */
  663. if (top >= 0) {
  664. s[top].pos[s[top].branch_id - 1] = nextfreepos;
  665. nextfreepos += (s[top + 1].sn->level ? sidx_nodesize : sidx_leafsize);
  666. }
  667. }
  668. }
  669. G_free(s);
  670. return nextfreepos;
  671. }
  672. /*!
  673. \brief Write RTree body from temporary file to sidx file
  674. Must be called when new or updated vector is closed
  675. \param[out] fp pointer to struct gvfile
  676. \param startpos offset to struct gvfile where to start writing out
  677. \param t pointer to RTree
  678. \param off_t_size size of off_t used to write struct gvfile
  679. \return -1 on error
  680. \return offset to root node on success
  681. */
  682. static off_t rtree_write_from_file(struct gvfile *fp, off_t startpos,
  683. struct RTree *t, int off_t_size)
  684. {
  685. off_t nextfreepos = startpos;
  686. int sidx_nodesize, sidx_leafsize;
  687. struct RTree_Node *n;
  688. int i, j, writeout, maxcard;
  689. static struct spidxstack *s = NULL;
  690. int top = 0;
  691. if (!s) {
  692. s = G_malloc(MAXLEVEL * sizeof(struct spidxstack));
  693. for (i = 0; i < MAXLEVEL; i++) {
  694. s[i].sn.branch = G_malloc(MAXCARD * sizeof(struct RTree_Branch));
  695. for (j = 0; j < MAXCARD; j++) {
  696. s[i].sn.branch[j].rect.boundary = G_malloc(6 * sizeof(RectReal));
  697. }
  698. }
  699. }
  700. /* write pending changes to file */
  701. RTreeFlushBuffer(t);
  702. /* should be foolproof */
  703. sidx_nodesize =
  704. (int)(2 * PORT_INT + t->nodecard * (off_t_size + NUMSIDES * PORT_DOUBLE));
  705. sidx_leafsize =
  706. (int)(2 * PORT_INT + t->leafcard * (off_t_size + NUMSIDES * PORT_DOUBLE));
  707. /* stack size of t->rootlevel + 1 would be enough because of
  708. * depth-first post-order traversal:
  709. * only one node per level on stack at any given time */
  710. /* add root node position to stack */
  711. s[top].branch_id = i = 0;
  712. RTreeReadNode(&s[top].sn, t->rootpos, t);
  713. /* depth-first postorder traversal
  714. * all children of a node are visitied and written out first
  715. * when a child is written out, its position in file is stored in pos[] for
  716. * the parent node and written out with the parent node */
  717. /* root node is written out last and its position returned */
  718. while (top >= 0) {
  719. n = &(s[top].sn);
  720. writeout = 1;
  721. /* this is an internal node in the RTree
  722. * all its children are processed first,
  723. * before it is written out to the sidx file */
  724. if (s[top].sn.level > 0) {
  725. for (i = s[top].branch_id; i < t->nodecard; i++) {
  726. s[top].pos[i] = 0;
  727. if (n->branch[i].child.pos >= 0) {
  728. s[top++].branch_id = i + 1;
  729. RTreeReadNode(&s[top].sn, n->branch[i].child.pos, t);
  730. s[top].branch_id = 0;
  731. writeout = 0;
  732. break;
  733. }
  734. }
  735. if (writeout) {
  736. /* nothing else found, ready to write out */
  737. s[top].branch_id = t->nodecard;
  738. }
  739. }
  740. if (writeout) {
  741. /* write node to sidx file */
  742. if (G_ftell(fp->file) != nextfreepos)
  743. G_fatal_error("Unable to write spatial index. "
  744. "Wrong node position (%"PRI_OFF_T") in file (should be %"PRI_OFF_T").",
  745. G_ftell(fp->file), nextfreepos);
  746. /* write with dig__fwrite_port_* fns */
  747. dig__fwrite_port_I(&(s[top].sn.count), 1, fp);
  748. dig__fwrite_port_I(&(s[top].sn.level), 1, fp);
  749. maxcard = s[top].sn.level ? t->nodecard : t->leafcard;
  750. for (j = 0; j < maxcard; j++) {
  751. dig__fwrite_port_D(s[top].sn.branch[j].rect.boundary,
  752. NUMSIDES, fp);
  753. /* leaf node: vector object IDs are stored in child.id */
  754. if (s[top].sn.level == 0)
  755. s[top].pos[j] = (off_t) s[top].sn.branch[j].child.id;
  756. dig__fwrite_port_O(&(s[top].pos[j]), 1, fp, off_t_size);
  757. }
  758. top--;
  759. /* update corresponding child position of parent node
  760. * this node is only updated if its level is > 0, i.e.
  761. * this is an internal node
  762. * children of internal nodes do not have an ID, instead
  763. * they hold the position in file of the next nodes down the tree */
  764. if (top >= 0) {
  765. s[top].pos[s[top].branch_id - 1] = nextfreepos;
  766. nextfreepos += (s[top + 1].sn.level ? sidx_nodesize : sidx_leafsize);
  767. }
  768. }
  769. }
  770. close(t->fd);
  771. return nextfreepos;
  772. }
  773. /* write RTree body to sidx file */
  774. static off_t rtree_write_to_sidx(struct gvfile *fp, off_t startpos,
  775. struct RTree *t, int off_t_size)
  776. {
  777. if (t->fd > -1)
  778. return rtree_write_from_file(fp, startpos, t, off_t_size);
  779. else
  780. return rtree_write_from_memory(fp, startpos, t, off_t_size);
  781. }
  782. /*!
  783. \brief Load RTree body from sidx file to memory
  784. Must be called when old vector is opened in update mode
  785. \param fp pointer to struct gvfile
  786. \param rootpos position of root node in file
  787. \param t pointer to RTree
  788. \param off_t_size size of off_t used to read struct gvfile
  789. \return pointer to root node on success
  790. */
  791. static void rtree_load_to_memory(struct gvfile *fp, off_t rootpos,
  792. struct RTree *t, int off_t_size)
  793. {
  794. struct RTree_Node *newnode = NULL;
  795. int i, j, loadnode, maxcard;
  796. struct spidxstack *last;
  797. static struct spidxstack *s = NULL;
  798. int top = 0;
  799. if (!s) {
  800. s = G_malloc(MAXLEVEL * sizeof(struct spidxstack));
  801. for (i = 0; i < MAXLEVEL; i++) {
  802. s[i].sn.branch = G_malloc(MAXCARD * sizeof(struct RTree_Branch));
  803. for (j = 0; j < MAXCARD; j++) {
  804. s[i].sn.branch[j].rect.boundary = G_malloc(6 * sizeof(RectReal));
  805. }
  806. }
  807. }
  808. /* stack size of t->rootlevel + 1 would be enough because of
  809. * depth-first postorder traversal:
  810. * only one node per level on stack at any given time */
  811. /* add root node position to stack */
  812. last = &(s[top]);
  813. G_fseek(fp->file, rootpos, SEEK_SET);
  814. /* read with dig__fread_port_* fns */
  815. dig__fread_port_I(&(s[top].sn.count), 1, fp);
  816. dig__fread_port_I(&(s[top].sn.level), 1, fp);
  817. maxcard = s[top].sn.level ? t->nodecard : t->leafcard;
  818. for (j = 0; j < maxcard; j++) {
  819. dig__fread_port_D(s[top].sn.branch[j].rect.boundary, NUMSIDES, fp);
  820. dig__fread_port_O(&(s[top].pos[j]), 1, fp, off_t_size);
  821. /* leaf node: vector object IDs are stored in child.id */
  822. if (s[top].sn.level == 0) {
  823. s[top].sn.branch[j].child.id = (int)s[top].pos[j];
  824. }
  825. else {
  826. s[top].sn.branch[j].child.ptr = NULL;
  827. }
  828. }
  829. s[top].branch_id = i = 0;
  830. /* some sort of postorder traversal */
  831. /* root node is loaded last and returned */
  832. while (top >= 0) {
  833. last = &(s[top]);
  834. loadnode = 1;
  835. /* this is an internal node in the RTree
  836. * all its children are read first,
  837. * before it is transferred to the RTree in memory */
  838. if (s[top].sn.level > 0) {
  839. for (i = s[top].branch_id; i < t->nodecard; i++) {
  840. if (s[top].pos[i] > 0) {
  841. s[top++].branch_id = i + 1;
  842. G_fseek(fp->file, last->pos[i], SEEK_SET);
  843. /* read with dig__fread_port_* fns */
  844. dig__fread_port_I(&(s[top].sn.count), 1, fp);
  845. dig__fread_port_I(&(s[top].sn.level), 1, fp);
  846. maxcard = s[top].sn.level ? t->nodecard : t->leafcard;
  847. for (j = 0; j < maxcard; j++) {
  848. dig__fread_port_D(s[top].sn.branch[j].rect.boundary,
  849. NUMSIDES, fp);
  850. dig__fread_port_O(&(s[top].pos[j]), 1, fp,
  851. off_t_size);
  852. /* leaf node
  853. * vector object IDs are stored in file as
  854. * off_t but always fit into an int, see dig_structs.h
  855. * vector object IDs are transferred to child.id */
  856. if (s[top].sn.level == 0) {
  857. s[top].sn.branch[j].child.id =
  858. (int)s[top].pos[j];
  859. }
  860. else {
  861. s[top].sn.branch[j].child.ptr = NULL;
  862. }
  863. }
  864. s[top].branch_id = 0;
  865. loadnode = 0;
  866. break;
  867. }
  868. else if (last->pos[i] < 0)
  869. G_fatal_error("corrupt spatial index");
  870. }
  871. if (loadnode) {
  872. /* nothing else found, ready to load */
  873. s[top].branch_id = t->nodecard;
  874. }
  875. }
  876. if (loadnode) {
  877. /* ready to load node to memory */
  878. newnode = RTreeAllocNode(t, s[top].sn.level);
  879. /* copy from stack node */
  880. RTreeCopyNode(newnode, &(s[top].sn), t);
  881. top--;
  882. /* update child of parent node
  883. * this node is only updated if its level is > 0, i.e.
  884. * this is an internal node
  885. * children of internal nodes do not have an ID, instead
  886. * they point to the next nodes down the tree */
  887. if (top >= 0) {
  888. s[top].sn.branch[s[top].branch_id - 1].child.ptr = newnode;
  889. }
  890. }
  891. }
  892. t->root = newnode;
  893. }
  894. /*!
  895. \brief Load RTree body from sidx file to temporary file
  896. Must be called when old vector is opened in update mode
  897. \param fp pointer to struct gvfile
  898. \param rootpos position of root node in file
  899. \param t pointer to RTree
  900. \param off_t_size size of off_t used to read struct gvfile
  901. \return offset to root node
  902. */
  903. static void rtree_load_to_file(struct gvfile *fp, off_t rootpos,
  904. struct RTree *t, int off_t_size)
  905. {
  906. off_t newnode_pos = -1;
  907. int i, j, loadnode, maxcard;
  908. struct spidxstack *last;
  909. static struct spidxstack *s = NULL;
  910. int top = 0;
  911. if (!s) {
  912. s = G_malloc(MAXLEVEL * sizeof(struct spidxstack));
  913. for (i = 0; i < MAXLEVEL; i++) {
  914. s[i].sn.branch = G_malloc(MAXCARD * sizeof(struct RTree_Branch));
  915. for (j = 0; j < MAXCARD; j++) {
  916. s[i].sn.branch[j].rect.boundary = G_malloc(6 * sizeof(RectReal));
  917. }
  918. }
  919. }
  920. /* stack size of t->rootlevel + 1 would be enough because of
  921. * depth-first postorder traversal:
  922. * only one node per level on stack at any given time */
  923. /* add root node position to stack */
  924. last = &(s[top]);
  925. G_fseek(fp->file, rootpos, SEEK_SET);
  926. /* read with dig__fread_port_* fns */
  927. dig__fread_port_I(&(s[top].sn.count), 1, fp);
  928. dig__fread_port_I(&(s[top].sn.level), 1, fp);
  929. maxcard = t->rootlevel ? t->nodecard : t->leafcard;
  930. for (j = 0; j < maxcard; j++) {
  931. dig__fread_port_D(s[top].sn.branch[j].rect.boundary, NUMSIDES, fp);
  932. dig__fread_port_O(&(s[top].pos[j]), 1, fp, off_t_size);
  933. /* leaf node: vector object IDs are stored in child.id */
  934. if (s[top].sn.level == 0) {
  935. s[top].sn.branch[j].child.id = (int)s[top].pos[j];
  936. }
  937. else {
  938. s[top].sn.branch[j].child.pos = -1;
  939. }
  940. }
  941. s[top].branch_id = i = 0;
  942. /* depth-first postorder traversal */
  943. /* root node is loaded last and returned */
  944. while (top >= 0) {
  945. last = &(s[top]);
  946. loadnode = 1;
  947. /* this is an internal node in the RTree
  948. * all its children are read first,
  949. * before it is transferred to the RTree in memory */
  950. if (s[top].sn.level > 0) {
  951. for (i = s[top].branch_id; i < t->nodecard; i++) {
  952. if (s[top].pos[i] > 0) {
  953. s[top++].branch_id = i + 1;
  954. G_fseek(fp->file, last->pos[i], SEEK_SET);
  955. /* read with dig__fread_port_* fns */
  956. dig__fread_port_I(&(s[top].sn.count), 1, fp);
  957. dig__fread_port_I(&(s[top].sn.level), 1, fp);
  958. maxcard = s[top].sn.level ? t->nodecard : t->leafcard;
  959. for (j = 0; j < maxcard; j++) {
  960. dig__fread_port_D(s[top].sn.branch[j].rect.boundary,
  961. NUMSIDES, fp);
  962. dig__fread_port_O(&(s[top].pos[j]), 1, fp,
  963. off_t_size);
  964. /* leaf node
  965. * vector object IDs are stored in file as
  966. * off_t but always fit into an int, see dig_structs.h
  967. * vector object IDs are transferred to child.id */
  968. if (s[top].sn.level == 0) {
  969. s[top].sn.branch[j].child.id =
  970. (int)s[top].pos[j];
  971. }
  972. else {
  973. s[top].sn.branch[j].child.pos = -1;
  974. }
  975. }
  976. s[top].branch_id = 0;
  977. loadnode = 0;
  978. break;
  979. }
  980. else if (last->pos[i] < 0)
  981. G_fatal_error("corrupt spatial index");
  982. }
  983. if (loadnode) {
  984. /* nothing else found, ready to load */
  985. s[top].branch_id = t->nodecard;
  986. }
  987. }
  988. if (loadnode) {
  989. /* ready to load node and write to temp file */
  990. newnode_pos = RTreeGetNodePos(t);
  991. RTreeWriteNode(&(s[top].sn), t);
  992. top--;
  993. /* update child of parent node
  994. * this node is only updated if its level is > 0, i.e.
  995. * this is an internal node
  996. * children of internal nodes do not have an ID, instead
  997. * they point to the next nodes down the tree */
  998. if (top >= 0) {
  999. s[top].sn.branch[s[top].branch_id - 1].child.pos = newnode_pos;
  1000. }
  1001. }
  1002. }
  1003. t->rootpos = newnode_pos;
  1004. }
  1005. static void rtree_load_from_sidx(struct gvfile *fp, off_t rootpos,
  1006. struct RTree *t, int off_t_size)
  1007. {
  1008. if (t->fd > -1)
  1009. return rtree_load_to_file(fp, rootpos, t, off_t_size);
  1010. else
  1011. return rtree_load_to_memory(fp, rootpos, t, off_t_size);
  1012. }
  1013. /*!
  1014. \brief Write spatial index to file
  1015. \param[out] fp pointer to struct gvfile
  1016. \param Plus pointer to Plus_head structure
  1017. \return 0
  1018. */
  1019. int dig_Wr_spidx(struct gvfile *fp, struct Plus_head *Plus)
  1020. {
  1021. G_debug(1, "dig_Wr_spidx()");
  1022. dig_set_cur_port(&(Plus->spidx_port));
  1023. dig_rewind(fp);
  1024. dig_Wr_spidx_head(fp, Plus);
  1025. /* Nodes */
  1026. Plus->Node_spidx_offset =
  1027. rtree_write_to_sidx(fp, dig_ftell(fp), Plus->Node_spidx,
  1028. Plus->spidx_port.off_t_size);
  1029. /* Lines */
  1030. Plus->Line_spidx_offset =
  1031. rtree_write_to_sidx(fp, dig_ftell(fp), Plus->Line_spidx,
  1032. Plus->spidx_port.off_t_size);
  1033. /* Areas */
  1034. Plus->Area_spidx_offset =
  1035. rtree_write_to_sidx(fp, dig_ftell(fp), Plus->Area_spidx,
  1036. Plus->spidx_port.off_t_size);
  1037. /* Isles */
  1038. Plus->Isle_spidx_offset =
  1039. rtree_write_to_sidx(fp, dig_ftell(fp), Plus->Isle_spidx,
  1040. Plus->spidx_port.off_t_size);
  1041. /* 3D future : */
  1042. /* Faces */
  1043. /* Volumes */
  1044. /* Holes */
  1045. dig_rewind(fp);
  1046. dig_Wr_spidx_head(fp, Plus); /* rewrite with offsets */
  1047. dig_fflush(fp);
  1048. return 0;
  1049. }
  1050. /*!
  1051. \brief Read spatial index from sidx file
  1052. Only needed when old vector is opened in update mode
  1053. \param fp pointer to struct gvfile
  1054. \param[in,out] Plus pointer to Plus_head structure
  1055. \return 0
  1056. */
  1057. int dig_Rd_spidx(struct gvfile * fp, struct Plus_head *Plus)
  1058. {
  1059. G_debug(1, "dig_read_spindx()");
  1060. /* free old trees, init new trees */
  1061. dig_spidx_free(Plus);
  1062. dig_spidx_init(Plus);
  1063. dig_rewind(fp);
  1064. dig_Rd_spidx_head(fp, Plus);
  1065. dig_set_cur_port(&(Plus->spidx_port));
  1066. /* Nodes */
  1067. rtree_load_from_sidx(fp, Plus->Node_spidx_offset,
  1068. Plus->Node_spidx, Plus->spidx_port.off_t_size);
  1069. /* Lines */
  1070. rtree_load_from_sidx(fp, Plus->Line_spidx_offset,
  1071. Plus->Line_spidx, Plus->spidx_port.off_t_size);
  1072. /* Areas */
  1073. rtree_load_from_sidx(fp, Plus->Area_spidx_offset,
  1074. Plus->Area_spidx, Plus->spidx_port.off_t_size);
  1075. /* Isles */
  1076. rtree_load_from_sidx(fp, Plus->Isle_spidx_offset,
  1077. Plus->Isle_spidx, Plus->spidx_port.off_t_size);
  1078. /* 3D future : */
  1079. /* Faces */
  1080. /* Volumes */
  1081. /* Holes */
  1082. return 0;
  1083. }
  1084. /*!
  1085. \brief Dump spatial index
  1086. \param[out] fp pointer to FILE
  1087. \param Plus pointer to Plus_head structure
  1088. \return 0
  1089. */
  1090. int dig_dump_spidx(FILE * fp, const struct Plus_head *Plus)
  1091. {
  1092. fprintf(fp, "Nodes\n");
  1093. if (Plus->Node_spidx->fd < 0)
  1094. rtree_dump_node(fp, Plus->Node_spidx->root, Plus->with_z);
  1095. else {
  1096. RTreeFlushBuffer(Plus->Node_spidx);
  1097. rtree_dump_node_file(fp, Plus->Node_spidx->rootpos, Plus->with_z,
  1098. Plus->Node_spidx);
  1099. }
  1100. fprintf(fp, "Lines\n");
  1101. if (Plus->Line_spidx->fd < 0)
  1102. rtree_dump_node(fp, Plus->Line_spidx->root, Plus->with_z);
  1103. else {
  1104. RTreeFlushBuffer(Plus->Line_spidx);
  1105. rtree_dump_node_file(fp, Plus->Line_spidx->rootpos, Plus->with_z,
  1106. Plus->Line_spidx);
  1107. }
  1108. fprintf(fp, "Areas\n");
  1109. if (Plus->Area_spidx->fd < 0)
  1110. rtree_dump_node(fp, Plus->Area_spidx->root, Plus->with_z);
  1111. else {
  1112. RTreeFlushBuffer(Plus->Area_spidx);
  1113. rtree_dump_node_file(fp, Plus->Area_spidx->rootpos, Plus->with_z,
  1114. Plus->Area_spidx);
  1115. }
  1116. fprintf(fp, "Isles\n");
  1117. if (Plus->Isle_spidx->fd < 0)
  1118. rtree_dump_node(fp, Plus->Isle_spidx->root, Plus->with_z);
  1119. else {
  1120. RTreeFlushBuffer(Plus->Isle_spidx);
  1121. rtree_dump_node_file(fp, Plus->Isle_spidx->rootpos, Plus->with_z,
  1122. Plus->Isle_spidx);
  1123. }
  1124. return 0;
  1125. }
  1126. /* read node from file */
  1127. static void rtree_read_node(struct NodeBuffer *nb,
  1128. off_t nodepos, struct RTree *t, struct Plus_head *Plus)
  1129. {
  1130. int i, maxcard;
  1131. off_t pos;
  1132. struct gvfile *file = &(Plus->spidx_fp);
  1133. dig_fseek(file, nodepos, SEEK_SET);
  1134. /* read with dig__fread_port_* fns */
  1135. dig__fread_port_I(&(nb->n.count), 1, file);
  1136. dig__fread_port_I(&(nb->n.level), 1, file);
  1137. maxcard = nb->n.level ? t->nodecard : t->leafcard;
  1138. for (i = 0; i < maxcard; i++) {
  1139. dig__fread_port_D(nb->n.branch[i].rect.boundary, NUMSIDES,
  1140. file);
  1141. dig__fread_port_O(&pos, 1, file,
  1142. Plus->spidx_port.off_t_size);
  1143. /* leaf node: vector object IDs are stored in child.id */
  1144. if (nb->n.level == 0) {
  1145. nb->n.branch[i].child.id = (int)pos;
  1146. }
  1147. else {
  1148. nb->n.branch[i].child.pos = pos;
  1149. }
  1150. }
  1151. }
  1152. /* get node from buffer or file */
  1153. static struct RTree_Node *rtree_get_node(off_t nodepos, int level,
  1154. struct RTree *t,
  1155. struct Plus_head *Plus)
  1156. {
  1157. int which, i = 0;
  1158. /* check mru first */
  1159. /* t->used[level][i] */
  1160. while (t->nb[level][t->used[level][i]].pos != nodepos &&
  1161. t->nb[level][t->used[level][i]].pos >= 0 &&
  1162. i < NODE_BUFFER_SIZE - 1) {
  1163. i++;
  1164. }
  1165. which = t->used[level][i];
  1166. if (t->nb[level][which].pos != nodepos) {
  1167. rtree_read_node(&(t->nb[level][which]), nodepos, t, Plus);
  1168. t->nb[level][which].pos = nodepos;
  1169. }
  1170. assert(t->nb[level][which].n.level == level);
  1171. /* make it mru */
  1172. if (i) { /* t->used[level][0] != which */
  1173. #if 0
  1174. t->used[level][i] = t->used[level][0];
  1175. t->used[level][0] = which;
  1176. #else
  1177. while (i) {
  1178. t->used[level][i] = t->used[level][i - 1];
  1179. i--;
  1180. }
  1181. t->used[level][0] = which;
  1182. #endif
  1183. }
  1184. return &(t->nb[level][which].n);
  1185. }
  1186. /*!
  1187. \brief Search spatial index file
  1188. Can't use regular RTreeSearch() here because sidx must be read
  1189. with dig__fread_port_*() functions
  1190. \param t pointer to RTree
  1191. \param r search rectangle
  1192. \param shcb user-provided callback
  1193. \param cbarg argument for shcb
  1194. \param Plus pointer to Plus_head structure
  1195. \return number of qualifying rectangles
  1196. */
  1197. int rtree_search(struct RTree *t, struct RTree_Rect *r,
  1198. SearchHitCallback shcb, void *cbarg, struct Plus_head *Plus)
  1199. {
  1200. int hitCount = 0, found;
  1201. /* int j, maxcard; */
  1202. int i;
  1203. struct spidxpstack s[MAXLEVEL];
  1204. int top = 0, level;
  1205. off_t lastpos;
  1206. assert(r);
  1207. assert(t);
  1208. /* stack size of t->rootlevel + 1 is enough because of depth first search */
  1209. /* only one node per level on stack at any given time */
  1210. dig_set_cur_port(&(Plus->spidx_port));
  1211. /* add root node position to stack */
  1212. s[top].sn = rtree_get_node(t->rootpos, t->rootlevel, t, Plus);
  1213. #if 0
  1214. dig_fseek(&(Plus->spidx_fp), t->rootpos, SEEK_SET);
  1215. /* read with dig__fread_port_* fns */
  1216. dig__fread_port_I(&(s[top].sn.count), 1, &(Plus->spidx_fp));
  1217. dig__fread_port_I(&(s[top].sn.level), 1, &(Plus->spidx_fp));
  1218. maxcard = t->rootlevel ? t->nodecard : t->leafcard;
  1219. for (j = 0; j < maxcard; j++) {
  1220. dig__fread_port_D(s[top].sn.branch[j].rect.boundary, NUMSIDES,
  1221. &(Plus->spidx_fp));
  1222. dig__fread_port_O(&(s[top].pos[j]), 1, &(Plus->spidx_fp),
  1223. Plus->spidx_port.off_t_size);
  1224. /* leaf node: vector object IDs are stored in child.id */
  1225. if (s[top].sn.level == 0) {
  1226. s[top].sn.branch[j].child.id = (int)s[top].pos[j];
  1227. }
  1228. else {
  1229. s[top].sn.branch[j].child.pos = s[top].pos[j];
  1230. }
  1231. }
  1232. #endif
  1233. s[top].branch_id = i = 0;
  1234. while (top >= 0) {
  1235. level = s[top].sn->level;
  1236. if (level > 0) { /* this is an internal node in the tree */
  1237. found = 1;
  1238. for (i = s[top].branch_id; i < t->nodecard; i++) {
  1239. lastpos = s[top].sn->branch[i].child.pos;
  1240. if (lastpos > 0 &&
  1241. RTreeOverlap(r, &(s[top].sn->branch[i].rect), t)) {
  1242. s[top++].branch_id = i + 1;
  1243. s[top].sn = rtree_get_node(lastpos, level - 1, t, Plus);
  1244. #if 0
  1245. dig_fseek(&(Plus->spidx_fp), lastpos, SEEK_SET);
  1246. /* read with dig__fread_port_* fns */
  1247. dig__fread_port_I(&(s[top].sn.count), 1,
  1248. &(Plus->spidx_fp));
  1249. dig__fread_port_I(&(s[top].sn.level), 1,
  1250. &(Plus->spidx_fp));
  1251. maxcard = s[top].sn.level ? t->nodecard : t->leafcard;
  1252. for (j = 0; j < maxcard; j++) {
  1253. dig__fread_port_D(s[top].sn.branch[j].rect.boundary,
  1254. NUMSIDES, &(Plus->spidx_fp));
  1255. dig__fread_port_O(&(s[top].pos[j]), 1,
  1256. &(Plus->spidx_fp),
  1257. Plus->spidx_port.off_t_size);
  1258. if (s[top].sn.level == 0) {
  1259. s[top].sn.branch[j].child.id = (int)s[top].pos[j];
  1260. }
  1261. else {
  1262. s[top].sn.branch[j].child.pos = s[top].pos[j];
  1263. }
  1264. }
  1265. #endif
  1266. s[top].branch_id = 0;
  1267. found = 0;
  1268. break;
  1269. }
  1270. }
  1271. if (found) {
  1272. /* nothing else found, go back up */
  1273. s[top].branch_id = t->nodecard;
  1274. top--;
  1275. }
  1276. }
  1277. else { /* this is a leaf node */
  1278. for (i = 0; i < t->leafcard; i++) {
  1279. if (s[top].sn->branch[i].child.id &&
  1280. RTreeOverlap(r, &(s[top].sn->branch[i].rect), t)) {
  1281. hitCount++;
  1282. if (shcb) { /* call the user-provided callback */
  1283. if (!shcb((int)s[top].sn->branch[i].child.id,
  1284. &s[top].sn->branch[i].rect, cbarg)) {
  1285. /* callback wants to terminate search early */
  1286. return hitCount;
  1287. }
  1288. }
  1289. }
  1290. }
  1291. top--;
  1292. }
  1293. }
  1294. return hitCount;
  1295. }