spindex_rw.c 41 KB

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