indexm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /****************************************************************************
  2. * MODULE: R-Tree library
  3. *
  4. * AUTHOR(S): Antonin Guttman - original code
  5. * Daniel Green (green@superliminal.com) - major clean-up
  6. * and implementation of bounding spheres
  7. * Markus Metz - file-based and memory-based R*-tree
  8. *
  9. * PURPOSE: Multidimensional index
  10. *
  11. * COPYRIGHT: (C) 2010 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General Public
  14. * License (>=v2). Read the file COPYING that comes with GRASS
  15. * for details.
  16. *****************************************************************************/
  17. #include <stdlib.h>
  18. #include <assert.h>
  19. #include <grass/gis.h>
  20. #include "index.h"
  21. int RTreeValidChildM(union RTree_Child *child)
  22. {
  23. return (child->ptr != NULL);
  24. }
  25. /*
  26. * Search in an index tree for all data retangles that
  27. * overlap the argument rectangle.
  28. * Return the number of qualifying data rects.
  29. */
  30. int RTreeSearchM(struct RTree *t, struct RTree_Rect *r,
  31. SearchHitCallback *shcb, void *cbarg)
  32. {
  33. struct RTree_Node *n;
  34. int hitCount = 0, notfound;
  35. int i;
  36. int top = 0;
  37. struct nstack *s = t->ns;
  38. /* stack size of t->rootlevel + 1 is enough because of depth first search */
  39. /* only one node per level on stack at any given time */
  40. /* add root node position to stack */
  41. s[top].sn = t->root;
  42. s[top].branch_id = i = 0;
  43. n = s[top].sn;
  44. while (top >= 0) {
  45. n = s[top].sn;
  46. if (s[top].sn->level > 0) { /* this is an internal node in the tree */
  47. notfound = 1;
  48. for (i = s[top].branch_id; i < t->nodecard; i++) {
  49. if (s[top].sn->branch[i].child.ptr &&
  50. RTreeOverlap(r, &(s[top].sn->branch[i].rect), t)) {
  51. s[top++].branch_id = i + 1;
  52. /* add next node to stack */
  53. s[top].sn = n->branch[i].child.ptr;
  54. s[top].branch_id = 0;
  55. notfound = 0;
  56. break;
  57. }
  58. }
  59. if (notfound) {
  60. /* nothing else found, go back up */
  61. s[top].branch_id = t->nodecard;
  62. top--;
  63. }
  64. }
  65. else { /* this is a leaf node */
  66. for (i = 0; i < t->leafcard; i++) {
  67. if (s[top].sn->branch[i].child.id &&
  68. RTreeOverlap(r, &(s[top].sn->branch[i].rect), t)) {
  69. hitCount++;
  70. if (shcb) { /* call the user-provided callback */
  71. if (!shcb(s[top].sn->branch[i].child.id,
  72. &s[top].sn->branch[i].rect, cbarg)) {
  73. /* callback wants to terminate search early */
  74. return hitCount;
  75. }
  76. }
  77. }
  78. }
  79. top--;
  80. }
  81. }
  82. return hitCount;
  83. }
  84. /*
  85. * Inserts a new data rectangle into the index structure.
  86. * Non-recursively descends tree.
  87. * Returns 0 if node was not split and nothing was removed.
  88. * Returns 1 if root node was split. Old node updated to become one of two.
  89. * Returns 2 if branches need to be reinserted.
  90. * The level argument specifies the number of steps up from the leaf
  91. * level to insert; e.g. a data rectangle goes in at level = 0.
  92. */
  93. static int RTreeInsertRect2M(struct RTree_Rect *r, union RTree_Child child, int level,
  94. struct RTree_Node **newnode,
  95. struct RTree *t,
  96. struct RTree_ListBranch **ee, char *overflow)
  97. {
  98. int i;
  99. struct RTree_Node *n, *n2;
  100. struct RTree_Rect *cover;
  101. int top = 0, down = 0;
  102. int result;
  103. struct RTree_Branch *b = &(t->tmpb2);
  104. struct nstack *s = t->ns;
  105. /* add root node to stack */
  106. s[top].sn = t->root;
  107. /* go down to level of insertion */
  108. while (s[top].sn->level > level) {
  109. n = s[top].sn;
  110. i = RTreePickBranch(r, n, t);
  111. s[top++].branch_id = i;
  112. /* add next node to stack */
  113. s[top].sn = n->branch[i].child.ptr;
  114. }
  115. /* Have reached level for insertion. Remove p rectangles or split */
  116. RTreeCopyRect(&(b->rect), r, t);
  117. /* child field of leaves contains tid of data record */
  118. b->child = child;
  119. /* add branch, may split node or remove branches */
  120. cover = NULL;
  121. if (top)
  122. cover = &(s[top - 1].sn->branch[s[top - 1].branch_id].rect);
  123. result = RTreeAddBranch(b, s[top].sn, &n2, ee, cover, overflow, t);
  124. /* update node count */
  125. if (result == 1) {
  126. t->n_nodes++;
  127. }
  128. /* go back up */
  129. while (top) {
  130. down = top--;
  131. i = s[top].branch_id;
  132. if (result == 0) { /* branch was added */
  133. RTreeExpandRect(&(s[top].sn->branch[i].rect), r, t);
  134. }
  135. else if (result == 2) { /* branches were removed */
  136. /* get node cover of previous node */
  137. RTreeNodeCover(s[down].sn, &(s[top].sn->branch[i].rect), t);
  138. }
  139. else if (result == 1) { /* node was split */
  140. /* get node cover of previous node */
  141. RTreeNodeCover(s[down].sn, &(s[top].sn->branch[i].rect), t);
  142. /* add new branch for new node previously added by RTreeAddBranch() */
  143. b->child.ptr = n2;
  144. RTreeNodeCover(b->child.ptr, &(b->rect), t);
  145. /* add branch, may split node or remove branches */
  146. cover = NULL;
  147. if (top)
  148. cover = &(s[top - 1].sn->branch[s[top - 1].branch_id].rect);
  149. result =
  150. RTreeAddBranch(b, s[top].sn, &n2, ee, cover, overflow, t);
  151. /* update node count */
  152. if (result == 1) {
  153. t->n_nodes++;
  154. }
  155. }
  156. }
  157. *newnode = n2;
  158. return result;
  159. }
  160. /*
  161. * Insert a data rectangle into an index structure.
  162. * RTreeInsertRect provides for splitting the root;
  163. * returns 1 if root was split, 0 if it was not.
  164. * The level argument specifies the number of steps up from the leaf
  165. * level to insert; e.g. a data rectangle goes in at level = 0.
  166. * RTreeInsertRect2 does the actual insertion.
  167. */
  168. int RTreeInsertRectM(struct RTree_Rect *r, union RTree_Child child, int level,
  169. struct RTree *t)
  170. {
  171. struct RTree_Node *newnode, *newroot;
  172. struct RTree_ListBranch *reInsertList = NULL;
  173. struct RTree_ListBranch *e;
  174. int result;
  175. char overflow[MAXLEVEL];
  176. struct RTree_Branch *b = &(t->tmpb1);
  177. /* R*-tree forced reinsertion: for each level only once */
  178. memset(overflow, t->overflow, MAXLEVEL);
  179. result = RTreeInsertRect2M(r, child, level, &newnode, t,
  180. &reInsertList, overflow);
  181. if (result == 1) { /* root split */
  182. /* grow a new root, & tree taller */
  183. t->rootlevel++;
  184. newroot = RTreeAllocNode(t, t->rootlevel);
  185. newroot->level = t->rootlevel;
  186. /* branch for old root */
  187. RTreeNodeCover(t->root, &(b->rect), t);
  188. b->child.ptr = t->root;
  189. RTreeAddBranch(b, newroot, NULL, NULL, NULL, NULL, t);
  190. /* branch for new node created by RTreeInsertRect2() */
  191. RTreeNodeCover(newnode, &(b->rect), t);
  192. b->child.ptr = newnode;
  193. RTreeAddBranch(b, newroot, NULL, NULL, NULL, NULL, t);
  194. /* set new root node */
  195. t->root = newroot;
  196. t->n_nodes++;
  197. return result;
  198. }
  199. if (result == 2) { /* branches were removed */
  200. while (reInsertList) {
  201. /* get next branch in list */
  202. RTreeCopyBranch(b, &(reInsertList->b), t);
  203. level = reInsertList->level;
  204. e = reInsertList;
  205. reInsertList = reInsertList->next;
  206. RTreeFreeListBranch(e);
  207. /* reinsert branches */
  208. result =
  209. RTreeInsertRect2M(&(b->rect), b->child, level, &newnode, t,
  210. &reInsertList, overflow);
  211. if (result == 1) { /* root split */
  212. /* grow a new root, & tree taller */
  213. t->rootlevel++;
  214. newroot = RTreeAllocNode(t, t->rootlevel);
  215. newroot->level = t->rootlevel;
  216. /* branch for old root */
  217. RTreeNodeCover(t->root, &(b->rect), t);
  218. b->child.ptr = t->root;
  219. RTreeAddBranch(b, newroot, NULL, NULL, NULL, NULL, t);
  220. /* branch for new node created by RTreeInsertRect2() */
  221. RTreeNodeCover(newnode, &(b->rect), t);
  222. b->child.ptr = newnode;
  223. RTreeAddBranch(b, newroot, NULL, NULL, NULL, NULL, t);
  224. /* set new root node */
  225. t->root = newroot;
  226. t->n_nodes++;
  227. }
  228. }
  229. }
  230. return result;
  231. }
  232. /*
  233. * Delete a rectangle from non-root part of an index structure.
  234. * Called by RTreeDeleteRect. Descends tree non-recursively,
  235. * merges branches on the way back up.
  236. * Returns 1 if record not found, 0 if success.
  237. */
  238. static int
  239. RTreeDeleteRect2M(struct RTree_Rect *r, union RTree_Child child, struct RTree *t,
  240. struct RTree_ListNode **ee)
  241. {
  242. int i, notfound = 1;
  243. struct RTree_Node *n;
  244. int top = 0, down = 0;
  245. int minfill;
  246. struct nstack *s = t->ns;
  247. assert(ee);
  248. /* add root node position to stack */
  249. s[top].sn = t->root;
  250. s[top].branch_id = 0;
  251. n = s[top].sn;
  252. while (notfound && top >= 0) {
  253. /* go down to level 0, remember path */
  254. if (s[top].sn->level > 0) {
  255. n = s[top].sn;
  256. for (i = s[top].branch_id; i < t->nodecard; i++) {
  257. if (n->branch[i].child.ptr &&
  258. RTreeOverlap(r, &(n->branch[i].rect), t)) {
  259. s[top++].branch_id = i + 1;
  260. /* add next node to stack */
  261. s[top].sn = n->branch[i].child.ptr;
  262. s[top].branch_id = 0;
  263. notfound = 0;
  264. break;
  265. }
  266. }
  267. if (notfound) {
  268. /* nothing else found, go back up */
  269. s[top].branch_id = t->nodecard;
  270. top--;
  271. }
  272. else /* found a way down but not yet the item */
  273. notfound = 1;
  274. }
  275. else {
  276. for (i = 0; i < t->leafcard; i++) {
  277. if (s[top].sn->branch[i].child.id &&
  278. s[top].sn->branch[i].child.id == child.id) { /* found item */
  279. RTreeDisconnectBranch(s[top].sn, i, t);
  280. t->n_leafs--;
  281. notfound = 0;
  282. break;
  283. }
  284. }
  285. if (notfound) /* continue searching */
  286. top--;
  287. }
  288. }
  289. if (notfound) {
  290. return notfound;
  291. }
  292. /* go back up */
  293. while (top) {
  294. down = top;
  295. top--;
  296. i = s[top].branch_id - 1;
  297. assert(s[down].sn->level == s[top].sn->level - 1);
  298. minfill = (s[down].sn->level ? t->min_node_fill : t->min_leaf_fill);
  299. if (s[down].sn->count >= minfill) {
  300. /* just update node cover */
  301. RTreeNodeCover(s[down].sn, &(s[top].sn->branch[i].rect), t);
  302. }
  303. else {
  304. /* not enough entries in child, eliminate child node */
  305. RTreeReInsertNode(s[top].sn->branch[i].child.ptr, ee);
  306. RTreeDisconnectBranch(s[top].sn, i, t);
  307. }
  308. }
  309. return notfound;
  310. }
  311. /*
  312. * should be called by RTreeDeleteRect() only
  313. *
  314. * Delete a data rectangle from an index structure.
  315. * Pass in a pointer to a Rect, the tid of the record, ptr RTree.
  316. * Returns 1 if record not found, 0 if success.
  317. * RTreeDeleteRect1 provides for eliminating the root.
  318. */
  319. int RTreeDeleteRectM(struct RTree_Rect *r, union RTree_Child child, struct RTree *t)
  320. {
  321. int i;
  322. struct RTree_Node *n;
  323. struct RTree_ListNode *reInsertList = NULL;
  324. struct RTree_ListNode *e;
  325. if (!RTreeDeleteRect2M(r, child, t, &reInsertList)) {
  326. /* found and deleted a data item */
  327. /* reinsert any branches from eliminated nodes */
  328. while (reInsertList) {
  329. t->n_nodes--;
  330. n = reInsertList->node;
  331. if (n->level > 0) { /* reinsert node branches */
  332. for (i = 0; i < t->nodecard; i++) {
  333. if (n->branch[i].child.ptr) {
  334. RTreeInsertRectM(&(n->branch[i].rect),
  335. n->branch[i].child, n->level, t);
  336. }
  337. }
  338. }
  339. else { /* reinsert leaf branches */
  340. for (i = 0; i < t->leafcard; i++) {
  341. if (n->branch[i].child.id) {
  342. RTreeInsertRectM(&(n->branch[i].rect),
  343. n->branch[i].child, n->level, t);
  344. }
  345. }
  346. }
  347. e = reInsertList;
  348. reInsertList = reInsertList->next;
  349. RTreeFreeNode(e->node);
  350. RTreeFreeListNode(e);
  351. }
  352. /* check for redundant root (not leaf, 1 child) and eliminate */
  353. n = t->root;
  354. if (n->count == 1 && n->level > 0) {
  355. for (i = 0; i < t->nodecard; i++) {
  356. if (n->branch[i].child.ptr)
  357. break;
  358. }
  359. t->root = n->branch[i].child.ptr;
  360. RTreeFreeNode(n);
  361. t->rootlevel--;
  362. }
  363. return 0;
  364. }
  365. return 1;
  366. }