spindex_rw.c 42 KB

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