sindex.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*!
  2. \file sindex.c
  3. \brief Vector library - spatial index
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2001-2008 by the GRASS Development Team
  6. This program is free software under the
  7. GNU General Public License (>=v2).
  8. Read the file COPYING that comes with GRASS
  9. for details.
  10. \author Radim Blazek
  11. \date 2001
  12. */
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <grass/glocale.h>
  16. #include <grass/gis.h>
  17. #include <grass/Vect.h>
  18. #include <grass/glocale.h>
  19. /*!
  20. \brief Init spatial index
  21. \param si pointer to spatial index structure
  22. \return void
  23. */
  24. void Vect_spatial_index_init(SPATIAL_INDEX * si)
  25. {
  26. G_debug(1, "Vect_spatial_index_init()");
  27. si->root = RTreeNewIndex();
  28. }
  29. /*!
  30. \brief Destroy existing spatial index
  31. Vect_spatial_index_init() must be call before new use.
  32. \param si pointer to spatial index structure
  33. \return void
  34. */
  35. void Vect_spatial_index_destroy(SPATIAL_INDEX * si)
  36. {
  37. G_debug(1, "Vect_spatial_index_destroy()");
  38. RTreeDestroyNode(si->root);
  39. }
  40. /*!
  41. \brief Add a new item to spatial index
  42. \param si pointer to spatial index structure
  43. \param id item identifier
  44. \param box pointer to item bounding box
  45. \return void
  46. */
  47. void Vect_spatial_index_add_item(SPATIAL_INDEX * si, int id, BOUND_BOX * box)
  48. {
  49. struct Rect rect;
  50. G_debug(3, "Vect_spatial_index_add_item(): id = %d", id);
  51. rect.boundary[0] = box->W;
  52. rect.boundary[1] = box->S;
  53. rect.boundary[2] = box->B;
  54. rect.boundary[3] = box->E;
  55. rect.boundary[4] = box->N;
  56. rect.boundary[5] = box->T;
  57. RTreeInsertRect(&rect, id, &(si->root), 0);
  58. }
  59. /*!
  60. \brief Delete item from spatial index
  61. \param si pointer to spatial index structure
  62. \param id item identifier
  63. \return void
  64. */
  65. void Vect_spatial_index_del_item(SPATIAL_INDEX * si, int id)
  66. {
  67. int ret;
  68. struct Rect rect;
  69. G_debug(3, "Vect_spatial_index_del_item(): id = %d", id);
  70. /* TODO */
  71. G_fatal_error("Vect_spatial_index_del_item() %s", _("not implemented"));
  72. /* Bounding box of item would be needed, which is not stored in si. */
  73. /*
  74. rect.boundary[0] = ; rect.boundary[1] = ; rect.boundary[2] = ;
  75. rect.boundary[3] = ; rect.boundary[4] = ; rect.boundary[5] = ;
  76. */
  77. ret = RTreeDeleteRect(&rect, id, &(si->root));
  78. if (ret)
  79. G_fatal_error(_("Unable to delete item %d from spatial index"), id);
  80. }
  81. /*!
  82. \brief Create spatial index if necessary.
  83. To be used in modules.
  84. Map must be opened on level 2.
  85. \param Map pointer to vector map
  86. \param out print progress here
  87. \return 0 OK
  88. \return 1 error
  89. */
  90. int Vect_build_spatial_index(struct Map_info *Map)
  91. {
  92. if (Map->level < 2) {
  93. G_fatal_error(_("Unable to build spatial index from topology, "
  94. "vector map is not opened at topo level 2"));
  95. }
  96. if (!(Map->plus.Spidx_built)) {
  97. return (Vect_build_sidx_from_topo(Map));
  98. }
  99. return 0;
  100. }
  101. /*!
  102. \brief Create spatial index from topo if necessary
  103. \param Map pointer to vector map
  104. \return 0 OK
  105. \return 1 error
  106. */
  107. int Vect_build_sidx_from_topo(struct Map_info *Map)
  108. {
  109. int i, total, done;
  110. struct Plus_head *plus;
  111. BOUND_BOX box;
  112. P_LINE *Line;
  113. P_NODE *Node;
  114. P_AREA *Area;
  115. P_ISLE *Isle;
  116. G_debug(3, "Vect_build_sidx_from_topo()");
  117. plus = &(Map->plus);
  118. dig_spidx_init(plus);
  119. total = plus->n_nodes + plus->n_lines + plus->n_areas + plus->n_isles;
  120. /* Nodes */
  121. for (i = 1; i <= plus->n_nodes; i++) {
  122. Node = plus->Node[i];
  123. if (!Node)
  124. G_fatal_error(_("BUG (Vect_build_sidx_from_topo): node does not exist"));
  125. dig_spidx_add_node(plus, i, Node->x, Node->y, Node->z);
  126. }
  127. /* Lines */
  128. done = plus->n_nodes;
  129. for (i = 1; i <= plus->n_lines; i++) {
  130. Line = plus->Line[i];
  131. if (!Line)
  132. G_fatal_error(_("BUG (Vect_build_sidx_from_topo): line does not exist"));
  133. box.N = Line->N;
  134. box.S = Line->S;
  135. box.E = Line->E;
  136. box.W = Line->W;
  137. box.T = Line->T;
  138. box.B = Line->B;
  139. dig_spidx_add_line(plus, i, &box);
  140. }
  141. /* Areas */
  142. done += plus->n_lines;
  143. for (i = 1; i <= plus->n_areas; i++) {
  144. Area = plus->Area[i];
  145. if (!Area)
  146. G_fatal_error(_("BUG (Vect_build_sidx_from_topo): area does not exist"));
  147. box.N = Area->N;
  148. box.S = Area->S;
  149. box.E = Area->E;
  150. box.W = Area->W;
  151. box.T = Area->T;
  152. box.B = Area->B;
  153. dig_spidx_add_area(plus, i, &box);
  154. }
  155. /* Isles */
  156. done += plus->n_areas;
  157. for (i = 1; i <= plus->n_isles; i++) {
  158. Isle = plus->Isle[i];
  159. if (!Isle)
  160. G_fatal_error(_("BUG (Vect_build_sidx_from_topo): isle does not exist"));
  161. box.N = Isle->N;
  162. box.S = Isle->S;
  163. box.E = Isle->E;
  164. box.W = Isle->W;
  165. box.T = Isle->T;
  166. box.B = Isle->B;
  167. dig_spidx_add_isle(plus, i, &box);
  168. }
  169. Map->plus.Spidx_built = 1;
  170. G_debug(3, "Spatial index was built");
  171. return 0;
  172. }
  173. /************************* SELECT BY BOX *********************************/
  174. /* This function is called by RTreeSearch() to add selected item to the list */
  175. static int _add_item(int id, struct ilist *list)
  176. {
  177. dig_list_add(list, id);
  178. return 1;
  179. }
  180. /*!
  181. \brief Select items by bounding box to list
  182. \param si pointer to spatial index structure
  183. \param box bounding box
  184. \param list pointer to list where selected items are stored
  185. \return number of selected items
  186. */
  187. int
  188. Vect_spatial_index_select(SPATIAL_INDEX * si, BOUND_BOX * box,
  189. struct ilist *list)
  190. {
  191. struct Rect rect;
  192. G_debug(3, "Vect_spatial_index_select()");
  193. list->n_values = 0;
  194. rect.boundary[0] = box->W;
  195. rect.boundary[1] = box->S;
  196. rect.boundary[2] = box->B;
  197. rect.boundary[3] = box->E;
  198. rect.boundary[4] = box->N;
  199. rect.boundary[5] = box->T;
  200. RTreeSearch(si->root, &rect, (void *)_add_item, list);
  201. G_debug(3, " %d items selected", list->n_values);
  202. return (list->n_values);
  203. }