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("topo must be written before sidx");
  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("topo must be written before sidx");
  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 (0 >= dig__fread_port_O(&(ptr->coor_size), 1, fp, ptr->off_t_size))
  428. return (-1);
  429. G_debug(2, " coor size %lu", (long unsigned)ptr->coor_size);
  430. dig_fseek(fp, ptr->spidx_head_size, SEEK_SET);
  431. return (0);
  432. }
  433. static int rtree_dump_node(FILE *, struct RTree_Node *n, int);
  434. /*!
  435. \brief Dump R-tree branch to the file
  436. \param fp pointer to FILE
  437. \param b pointer to Branch structure
  438. \param with_z non-zero value for 3D vector data
  439. \param level level value
  440. \return 0
  441. */
  442. static int rtree_dump_branch(FILE * fp, struct RTree_Branch *b, int with_z,
  443. int level)
  444. {
  445. const struct RTree_Rect *r;
  446. r = &(b->rect);
  447. if (level == 0)
  448. fprintf(fp, " id = %d ", b->child.id);
  449. fprintf(fp, " %f %f %f %f %f %f\n", r->boundary[0], r->boundary[1],
  450. r->boundary[2], r->boundary[3], r->boundary[4], r->boundary[5]);
  451. if (level > 0) {
  452. rtree_dump_node(fp, b->child.ptr, with_z);
  453. }
  454. return 0;
  455. }
  456. /*!
  457. \brief Dump R-tree node to the file
  458. \param fp pointer to FILE
  459. \param n pointer to Node structure
  460. \param with_z non-zero value for 3D vector data
  461. \return 0
  462. */
  463. int rtree_dump_node(FILE * fp, struct RTree_Node *n, int with_z)
  464. {
  465. int i;
  466. /* recursive nearly-but-a-bit-messy depth-first pre-order traversal
  467. * potentially filling up memory */
  468. /* TODO: change to non-recursive depth-first post-order traversal */
  469. /* left for comparison with GRASS6.x */
  470. fprintf(fp, "Node level=%d count=%d\n", n->level, n->count);
  471. if (n->level > 0)
  472. for (i = 0; i < NODECARD; i++) {
  473. if (n->branch[i].child.ptr) {
  474. fprintf(fp, " Branch %d", i);
  475. rtree_dump_branch(fp, &n->branch[i], with_z, n->level);
  476. }
  477. }
  478. else
  479. for (i = 0; i < LEAFCARD; i++) {
  480. if (n->branch[i].child.id) {
  481. fprintf(fp, " Branch %d", i);
  482. rtree_dump_branch(fp, &n->branch[i], with_z, n->level);
  483. }
  484. }
  485. return 0;
  486. }
  487. static int rtree_dump_node_file(FILE *, off_t, int, struct RTree *);
  488. /*!
  489. \brief Dump R-tree branch from temp file to the file
  490. \param fp pointer to FILE
  491. \param b pointer to Branch structure
  492. \param with_z non-zero value for 3D vector data
  493. \param level level value
  494. \return 0
  495. */
  496. static int rtree_dump_branch_file(FILE * fp, struct RTree_Branch *b, int with_z,
  497. int level, struct RTree *t)
  498. {
  499. const struct RTree_Rect *r;
  500. r = &(b->rect);
  501. if (level == 0)
  502. fprintf(fp, " id = %d ", b->child.id);
  503. fprintf(fp, " %f %f %f %f %f %f\n", r->boundary[0], r->boundary[1],
  504. r->boundary[2], r->boundary[3], r->boundary[4], r->boundary[5]);
  505. if (level > 0) {
  506. rtree_dump_node_file(fp, b->child.pos, with_z, t);
  507. }
  508. return 0;
  509. }
  510. /*!
  511. \brief Dump R-tree node from temp file to the file
  512. \param fp pointer to FILE
  513. \param pos position of Node in temp file
  514. \param with_z non-zero value for 3D vector data
  515. \param t RTree to dump
  516. \return 0
  517. */
  518. int rtree_dump_node_file(FILE * fp, off_t pos, int with_z, struct RTree *t)
  519. {
  520. int i;
  521. static struct RTree_Node n;
  522. static int node_init = 0;
  523. if (!node_init) {
  524. for (i = 0; i < MAXCARD; i++)
  525. RTreeNewRect(&(n.branch[i].rect), t);
  526. }
  527. /* recursive nearly-but-a-bit-messy depth-first pre-order traversal
  528. * potentially filling up memory */
  529. /* TODO: change to non-recursive depth-first post-order traversal */
  530. /* left for comparison with GRASS6.x */
  531. RTreeReadNode(&n, pos, t);
  532. fprintf(fp, "Node level=%d count=%d\n", n.level, n.count);
  533. if (n.level > 0)
  534. for (i = 0; i < NODECARD; i++) {
  535. if (n.branch[i].child.pos >= 0) {
  536. fprintf(fp, " Branch %d", i);
  537. rtree_dump_branch_file(fp, &n.branch[i], with_z, n.level, t);
  538. }
  539. }
  540. else
  541. for (i = 0; i < LEAFCARD; i++) {
  542. if (n.branch[i].child.id) {
  543. fprintf(fp, " Branch %d", i);
  544. rtree_dump_branch_file(fp, &n.branch[i], with_z, n.level, t);
  545. }
  546. }
  547. return 0;
  548. }
  549. /*
  550. * all following methods to transfer spatial indices (rtrees) are based
  551. * on the same idea
  552. * do a postorder depth-first non-recursive traversal of the rtree
  553. * a leaf node is transfered first
  554. * the root node is transfered last
  555. *
  556. * this applies to all four scenarios
  557. * - from intermediate file to sidx file
  558. * - from sidx file to intermediate file
  559. * - from memory to sidx file
  560. * - from sidx file to memory
  561. *
  562. * I could not think of one function that's good for all four scenarios,
  563. * but that doesn't mean there is none...
  564. *
  565. * maybe something like V2_read_line_array and Write_line_array
  566. * in Vlib/read.c and Vlib/write.c, at least for transferring from sidx
  567. * and transferrring to sidx?
  568. */
  569. /*!
  570. \brief Write RTree body from memory to sidx file
  571. Must be called when new or updated vector is closed
  572. \param[out] fp pointer to struct gvfile
  573. \param startpos offset to struct gvfile where to start writing out
  574. \param t pointer to RTree
  575. \param off_t_size size of off_t used to write struct gvfile
  576. \return -1 on error
  577. \return offset to root node on success
  578. */
  579. static off_t rtree_write_from_memory(struct gvfile *fp, off_t startpos,
  580. struct RTree *t, int off_t_size)
  581. {
  582. off_t nextfreepos = startpos;
  583. int sidx_nodesize, sidx_leafsize;
  584. struct RTree_Node *n;
  585. int i, j, writeout, maxcard;
  586. struct spidxpstack s[MAXLEVEL];
  587. int top = 0;
  588. /* should be foolproof */
  589. sidx_nodesize =
  590. (int)(2 * PORT_INT + t->nodecard * (off_t_size + NUMSIDES * PORT_DOUBLE));
  591. sidx_leafsize =
  592. (int)(2 * PORT_INT + t->leafcard * (off_t_size + NUMSIDES * PORT_DOUBLE));
  593. /* stack size of t->rootlevel + 1 would be enough because of
  594. * depth-first post-order traversal:
  595. * only one node per level on stack at any given time */
  596. /* add root node position to stack */
  597. s[top].branch_id = i = 0;
  598. s[top].sn = t->root;
  599. /* depth-first postorder traversal
  600. * all children of a node are visitied and written out first
  601. * when a child is written out, its position in file is stored in pos[] for
  602. * the parent node and written out with the parent node */
  603. /* root node is written out last and its position returned */
  604. while (top >= 0) {
  605. n = s[top].sn;
  606. writeout = 1;
  607. /* this is an internal node in the RTree
  608. * all its children are processed first,
  609. * before it is written out to the sidx file */
  610. if (s[top].sn->level > 0) {
  611. for (i = s[top].branch_id; i < t->nodecard; i++) {
  612. s[top].pos[i] = 0;
  613. if (n->branch[i].child.ptr != NULL) {
  614. s[top++].branch_id = i + 1;
  615. s[top].sn = n->branch[i].child.ptr;
  616. s[top].branch_id = 0;
  617. writeout = 0;
  618. break;
  619. }
  620. }
  621. if (writeout) {
  622. /* nothing else found, ready to write out */
  623. s[top].branch_id = t->nodecard;
  624. }
  625. }
  626. if (writeout) {
  627. /* write node to sidx file */
  628. if (G_ftell(fp->file) != nextfreepos)
  629. G_fatal_error("write sidx: wrong node position in file");
  630. /* write with dig__fwrite_port_* fns */
  631. dig__fwrite_port_I(&(s[top].sn->count), 1, fp);
  632. dig__fwrite_port_I(&(s[top].sn->level), 1, fp);
  633. maxcard = s[top].sn->level ? t->nodecard : t->leafcard;
  634. for (j = 0; j < maxcard; j++) {
  635. dig__fwrite_port_D(s[top].sn->branch[j].rect.boundary,
  636. NUMSIDES, fp);
  637. /* leaf node: vector object IDs are stored in child.id */
  638. if (s[top].sn->level == 0)
  639. s[top].pos[j] = (off_t) s[top].sn->branch[j].child.id;
  640. dig__fwrite_port_O(&(s[top].pos[j]), 1, fp, off_t_size);
  641. }
  642. top--;
  643. /* update corresponding child position of parent node
  644. * this node is only updated if its level is > 0, i.e.
  645. * this is an internal node
  646. * children of internal nodes do not have an ID, instead
  647. * they hold the position in file of the next nodes down the tree */
  648. if (top >= 0) {
  649. s[top].pos[s[top].branch_id - 1] = nextfreepos;
  650. nextfreepos += (s[top + 1].sn->level ? sidx_nodesize : sidx_leafsize);
  651. }
  652. }
  653. }
  654. return nextfreepos;
  655. }
  656. /*!
  657. \brief Write RTree body from temporary file to sidx file
  658. Must be called when new or updated vector is closed
  659. \param[out] fp pointer to struct gvfile
  660. \param startpos offset to struct gvfile where to start writing out
  661. \param t pointer to RTree
  662. \param off_t_size size of off_t used to write struct gvfile
  663. \return -1 on error
  664. \return offset to root node on success
  665. */
  666. static off_t rtree_write_from_file(struct gvfile *fp, off_t startpos,
  667. struct RTree *t, int off_t_size)
  668. {
  669. off_t nextfreepos = startpos;
  670. int sidx_nodesize, sidx_leafsize;
  671. struct RTree_Node *n;
  672. int i, j, writeout, maxcard;
  673. static struct spidxstack s[MAXLEVEL];
  674. static int stack_init = 0;
  675. int top = 0;
  676. if (!stack_init) {
  677. for (i = 0; i < MAXLEVEL; i++) {
  678. for (j = 0; j < MAXCARD; j++) {
  679. s[i].sn.branch[j].rect.boundary = G_malloc(6 * sizeof(RectReal));
  680. }
  681. }
  682. stack_init = 1;
  683. }
  684. /* write pending changes to file */
  685. RTreeFlushBuffer(t);
  686. /* should be foolproof */
  687. sidx_nodesize =
  688. (int)(2 * PORT_INT + t->nodecard * (off_t_size + NUMSIDES * PORT_DOUBLE));
  689. sidx_leafsize =
  690. (int)(2 * PORT_INT + t->leafcard * (off_t_size + NUMSIDES * PORT_DOUBLE));
  691. /* stack size of t->rootlevel + 1 would be enough because of
  692. * depth-first post-order traversal:
  693. * only one node per level on stack at any given time */
  694. /* add root node position to stack */
  695. s[top].branch_id = i = 0;
  696. RTreeReadNode(&s[top].sn, t->rootpos, t);
  697. /* depth-first postorder traversal
  698. * all children of a node are visitied and written out first
  699. * when a child is written out, its position in file is stored in pos[] for
  700. * the parent node and written out with the parent node */
  701. /* root node is written out last and its position returned */
  702. while (top >= 0) {
  703. n = &(s[top].sn);
  704. writeout = 1;
  705. /* this is an internal node in the RTree
  706. * all its children are processed first,
  707. * before it is written out to the sidx file */
  708. if (s[top].sn.level > 0) {
  709. for (i = s[top].branch_id; i < t->nodecard; i++) {
  710. s[top].pos[i] = -1;
  711. if (n->branch[i].child.pos >= 0) {
  712. s[top++].branch_id = i + 1;
  713. RTreeReadNode(&s[top].sn, n->branch[i].child.pos, t);
  714. s[top].branch_id = 0;
  715. writeout = 0;
  716. break;
  717. }
  718. }
  719. if (writeout) {
  720. /* nothing else found, ready to write out */
  721. s[top].branch_id = t->nodecard;
  722. }
  723. }
  724. if (writeout) {
  725. /* write node to sidx file */
  726. if (G_ftell(fp->file) != nextfreepos)
  727. G_fatal_error(_("Writing sidx: wrong node position in file"));
  728. /* write with dig__fwrite_port_* fns */
  729. dig__fwrite_port_I(&(s[top].sn.count), 1, fp);
  730. dig__fwrite_port_I(&(s[top].sn.level), 1, fp);
  731. maxcard = s[top].sn.level ? t->nodecard : t->leafcard;
  732. for (j = 0; j < maxcard; j++) {
  733. dig__fwrite_port_D(s[top].sn.branch[j].rect.boundary,
  734. NUMSIDES, fp);
  735. /* leaf node: vector object IDs are stored in child.id */
  736. if (s[top].sn.level == 0)
  737. s[top].pos[j] = (off_t) s[top].sn.branch[j].child.id;
  738. dig__fwrite_port_O(&(s[top].pos[j]), 1, fp, off_t_size);
  739. }
  740. top--;
  741. /* update corresponding child position of parent node
  742. * this node is only updated if its level is > 0, i.e.
  743. * this is an internal node
  744. * children of internal nodes do not have an ID, instead
  745. * they hold the position in file of the next nodes down the tree */
  746. if (top >= 0) {
  747. s[top].pos[s[top].branch_id - 1] = nextfreepos;
  748. nextfreepos += (s[top + 1].sn.level ? sidx_nodesize : sidx_leafsize);
  749. }
  750. }
  751. }
  752. return nextfreepos;
  753. }
  754. /* write RTree body to sidx file */
  755. static off_t rtree_write_to_sidx(struct gvfile *fp, off_t startpos,
  756. struct RTree *t, int off_t_size)
  757. {
  758. if (t->fd > -1)
  759. return rtree_write_from_file(fp, startpos, t, off_t_size);
  760. else
  761. return rtree_write_from_memory(fp, startpos, t, off_t_size);
  762. }
  763. /*!
  764. \brief Load RTree body from sidx file to memory
  765. Must be called when old vector is opened in update mode
  766. \param fp pointer to struct gvfile
  767. \param rootpos position of root node in file
  768. \param t pointer to RTree
  769. \param off_t_size size of off_t used to read struct gvfile
  770. \return pointer to root node on success
  771. */
  772. static void rtree_load_to_memory(struct gvfile *fp, off_t rootpos,
  773. struct RTree *t, int off_t_size)
  774. {
  775. struct RTree_Node *newnode = NULL;
  776. int i, j, loadnode, maxcard;
  777. struct spidxstack *last;
  778. static struct spidxstack s[MAXLEVEL];
  779. static int stack_init = 0;
  780. int top = 0;
  781. if (!stack_init) {
  782. for (i = 0; i < MAXLEVEL; i++) {
  783. for (j = 0; j < MAXCARD; j++) {
  784. s[i].sn.branch[j].rect.boundary = G_malloc(6 * sizeof(RectReal));
  785. }
  786. }
  787. stack_init = 1;
  788. }
  789. /* stack size of t->rootlevel + 1 would be enough because of
  790. * depth-first postorder traversal:
  791. * only one node per level on stack at any given time */
  792. /* add root node position to stack */
  793. last = &(s[top]);
  794. G_fseek(fp->file, rootpos, SEEK_SET);
  795. /* read with dig__fread_port_* fns */
  796. dig__fread_port_I(&(s[top].sn.count), 1, fp);
  797. dig__fread_port_I(&(s[top].sn.level), 1, fp);
  798. maxcard = s[top].sn.level ? t->nodecard : t->leafcard;
  799. for (j = 0; j < maxcard; j++) {
  800. dig__fread_port_D(s[top].sn.branch[j].rect.boundary, NUMSIDES, fp);
  801. dig__fread_port_O(&(s[top].pos[j]), 1, fp, off_t_size);
  802. /* leaf node: vector object IDs are stored in child.id */
  803. if (s[top].sn.level == 0) {
  804. s[top].sn.branch[j].child.id = (int)s[top].pos[j];
  805. }
  806. else {
  807. s[top].sn.branch[j].child.ptr = NULL;
  808. }
  809. }
  810. s[top].branch_id = i = 0;
  811. /* some sort of postorder traversal */
  812. /* root node is loaded last and returned */
  813. while (top >= 0) {
  814. last = &(s[top]);
  815. loadnode = 1;
  816. /* this is an internal node in the RTree
  817. * all its children are read first,
  818. * before it is transfered to the RTree in memory */
  819. if (s[top].sn.level > 0) {
  820. for (i = s[top].branch_id; i < t->nodecard; i++) {
  821. if (s[top].pos[i] > 0) {
  822. s[top++].branch_id = i + 1;
  823. G_fseek(fp->file, last->pos[i], SEEK_SET);
  824. /* read with dig__fread_port_* fns */
  825. dig__fread_port_I(&(s[top].sn.count), 1, fp);
  826. dig__fread_port_I(&(s[top].sn.level), 1, fp);
  827. maxcard = s[top].sn.level ? t->nodecard : t->leafcard;
  828. for (j = 0; j < maxcard; j++) {
  829. dig__fread_port_D(s[top].sn.branch[j].rect.boundary,
  830. NUMSIDES, fp);
  831. dig__fread_port_O(&(s[top].pos[j]), 1, fp,
  832. off_t_size);
  833. /* leaf node
  834. * vector object IDs are stored in file as
  835. * off_t but always fit into an int, see dig_structs.h
  836. * vector object IDs are transfered to child.id */
  837. if (s[top].sn.level == 0) {
  838. s[top].sn.branch[j].child.id =
  839. (int)s[top].pos[j];
  840. }
  841. else {
  842. s[top].sn.branch[j].child.ptr = NULL;
  843. }
  844. }
  845. s[top].branch_id = 0;
  846. loadnode = 0;
  847. break;
  848. }
  849. else if (last->pos[i] < 0)
  850. G_fatal_error("corrupt spatial index");
  851. }
  852. if (loadnode) {
  853. /* nothing else found, ready to load */
  854. s[top].branch_id = t->nodecard;
  855. }
  856. }
  857. if (loadnode) {
  858. /* ready to load node to memory */
  859. newnode = RTreeNewNode(t, s[top].sn.level);
  860. /* copy from stack node */
  861. newnode->level = s[top].sn.level;
  862. newnode->count = s[top].sn.count;
  863. maxcard = s[top].sn.level ? t->nodecard : t->leafcard;
  864. for (j = 0; j < maxcard; j++) {
  865. newnode->branch[j].rect = s[top].sn.branch[j].rect;
  866. newnode->branch[j].child = s[top].sn.branch[j].child;
  867. }
  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[MAXLEVEL];
  898. static int stack_init = 0;
  899. int top = 0;
  900. if (!stack_init) {
  901. for (i = 0; i < MAXLEVEL; i++) {
  902. for (j = 0; j < MAXCARD; j++) {
  903. s[i].sn.branch[j].rect.boundary = G_malloc(6 * sizeof(RectReal));
  904. }
  905. }
  906. stack_init = 1;
  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. /*!
  1123. \brief Search spatial index file
  1124. Can't use regular RTreeSearch() here because sidx must be read
  1125. with dig__fread_port_*() functions
  1126. \param t pointer to RTree
  1127. \param r search rectangle
  1128. \param shcb user-provided callback
  1129. \param cbarg argument for shcb
  1130. \param Plus pointer to Plus_head structure
  1131. \return number of qualifying rectangles
  1132. */
  1133. int rtree_search(struct RTree *t, struct RTree_Rect *r, SearchHitCallback shcb,
  1134. void *cbarg, struct Plus_head *Plus)
  1135. {
  1136. int hitCount = 0, found, maxcard;
  1137. int i, j;
  1138. struct spidxstack *last;
  1139. static struct spidxstack s[MAXLEVEL];
  1140. static int stack_init = 0;
  1141. int top = 0;
  1142. assert(r);
  1143. assert(t);
  1144. if (!stack_init) {
  1145. struct Rect_Real *r;
  1146. for (i = 0; i < MAXLEVEL; i++) {
  1147. for (j = 0; j < MAXCARD; j++) {
  1148. s[i].sn.branch[j].rect.boundary = G_malloc(6 * sizeof(RectReal));
  1149. }
  1150. }
  1151. stack_init = 1;
  1152. }
  1153. /* stack size of t->rootlevel + 1 is enough because of depth first search */
  1154. /* only one node per level on stack at any given time */
  1155. dig_set_cur_port(&(Plus->spidx_port));
  1156. /* add root node position to stack */
  1157. dig_fseek(&(Plus->spidx_fp), t->rootpos, SEEK_SET);
  1158. /* read with dig__fread_port_* fns */
  1159. dig__fread_port_I(&(s[top].sn.count), 1, &(Plus->spidx_fp));
  1160. dig__fread_port_I(&(s[top].sn.level), 1, &(Plus->spidx_fp));
  1161. maxcard = t->rootlevel ? t->nodecard : t->leafcard;
  1162. for (j = 0; j < maxcard; j++) {
  1163. dig__fread_port_D(s[top].sn.branch[j].rect.boundary, NUMSIDES,
  1164. &(Plus->spidx_fp));
  1165. dig__fread_port_O(&(s[top].pos[j]), 1, &(Plus->spidx_fp),
  1166. Plus->spidx_port.off_t_size);
  1167. /* leaf node: vector object IDs are stored in child.id */
  1168. if (s[top].sn.level == 0) {
  1169. s[top].sn.branch[j].child.id = (int)s[top].pos[j];
  1170. }
  1171. else {
  1172. s[top].sn.branch[j].child.pos = s[top].pos[j];
  1173. }
  1174. }
  1175. s[top].branch_id = i = 0;
  1176. while (top >= 0) {
  1177. last = &(s[top]);
  1178. if (s[top].sn.level > 0) { /* this is an internal node in the tree */
  1179. found = 1;
  1180. for (i = s[top].branch_id; i < t->nodecard; i++) {
  1181. if (s[top].pos[i] > 0 &&
  1182. RTreeOverlap(r, &(s[top].sn.branch[i].rect), t)) {
  1183. s[top++].branch_id = i + 1;
  1184. dig_fseek(&(Plus->spidx_fp), last->pos[i], SEEK_SET);
  1185. /* read with dig__fread_port_* fns */
  1186. dig__fread_port_I(&(s[top].sn.count), 1,
  1187. &(Plus->spidx_fp));
  1188. dig__fread_port_I(&(s[top].sn.level), 1,
  1189. &(Plus->spidx_fp));
  1190. maxcard = s[top].sn.level ? t->nodecard : t->leafcard;
  1191. for (j = 0; j < maxcard; j++) {
  1192. dig__fread_port_D(s[top].sn.branch[j].rect.boundary,
  1193. NUMSIDES, &(Plus->spidx_fp));
  1194. dig__fread_port_O(&(s[top].pos[j]), 1,
  1195. &(Plus->spidx_fp),
  1196. Plus->spidx_port.off_t_size);
  1197. if (s[top].sn.level == 0) {
  1198. s[top].sn.branch[j].child.id = (int)s[top].pos[j];
  1199. }
  1200. else {
  1201. s[top].sn.branch[j].child.pos = s[top].pos[j];
  1202. }
  1203. }
  1204. s[top].branch_id = 0;
  1205. found = 0;
  1206. break;
  1207. }
  1208. }
  1209. if (found) {
  1210. /* nothing else found, go back up */
  1211. s[top].branch_id = t->nodecard;
  1212. top--;
  1213. }
  1214. }
  1215. else { /* this is a leaf node */
  1216. for (i = 0; i < t->leafcard; i++) {
  1217. if (s[top].sn.branch[i].child.id &&
  1218. RTreeOverlap(r, &(s[top].sn.branch[i].rect), t)) {
  1219. hitCount++;
  1220. if (shcb) { /* call the user-provided callback */
  1221. if (!shcb((int)s[top].sn.branch[i].child.id,
  1222. s[top].sn.branch[i].rect, cbarg)) {
  1223. /* callback wants to terminate search early */
  1224. return hitCount;
  1225. }
  1226. }
  1227. }
  1228. }
  1229. top--;
  1230. }
  1231. }
  1232. return hitCount;
  1233. }