spindex_rw.c 44 KB

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