select.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*!
  2. \file Vlib/select.c
  3. \brief Vector library - spatial index
  4. Higher level functions for a custom spatial index.
  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 Radim Blazek
  9. */
  10. #include <grass/config.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <sys/stat.h>
  14. #include <string.h>
  15. #include <grass/glocale.h>
  16. #include <grass/gis.h>
  17. #include <grass/vector.h>
  18. #include <grass/glocale.h>
  19. /*!
  20. \brief Initialize spatial index structure
  21. \param si pointer to spatial index structure
  22. \return void
  23. */
  24. void Vect_spatial_index_init(struct spatial_index * si, int with_z)
  25. {
  26. G_debug(1, "Vect_spatial_index_init()");
  27. si->si_tree = RTreeNewIndex(2 + with_z);
  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(struct spatial_index * si)
  36. {
  37. G_debug(1, "Vect_spatial_index_destroy()");
  38. RTreeFreeIndex(si->si_tree);
  39. }
  40. /*!
  41. \brief Add a new item to spatial index structure
  42. \param[in,out] 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(struct spatial_index * si, int id,
  48. const struct bound_box * box)
  49. {
  50. struct Rect rect;
  51. G_debug(3, "Vect_spatial_index_add_item(): id = %d", id);
  52. rect.boundary[0] = box->W;
  53. rect.boundary[1] = box->S;
  54. rect.boundary[2] = box->B;
  55. rect.boundary[3] = box->E;
  56. rect.boundary[4] = box->N;
  57. rect.boundary[5] = box->T;
  58. RTreeInsertRect(&rect, id, si->si_tree);
  59. }
  60. /*!
  61. \brief Delete item from spatial index structure
  62. \param[in,out] si pointer to spatial index structure
  63. \param id item identifier
  64. \return void
  65. */
  66. void Vect_spatial_index_del_item(struct spatial_index * si, int id,
  67. const struct bound_box * box)
  68. {
  69. int ret;
  70. struct Rect rect;
  71. G_debug(3, "Vect_spatial_index_del_item(): id = %d", id);
  72. rect.boundary[0] = box->W;
  73. rect.boundary[1] = box->S;
  74. rect.boundary[2] = box->B;
  75. rect.boundary[3] = box->E;
  76. rect.boundary[4] = box->N;
  77. rect.boundary[5] = box->T;
  78. ret = RTreeDeleteRect(&rect, id, si->si_tree);
  79. if (ret)
  80. G_fatal_error(_("Unable to delete item %d from spatial index"), id);
  81. }
  82. /************************* SELECT BY BOX *********************************/
  83. /* This function is called by RTreeSearch() to add selected item to the list */
  84. static int _add_item(int id, struct ilist *list)
  85. {
  86. dig_list_add(list, id);
  87. return 1;
  88. }
  89. /*!
  90. \brief Select items by bounding box to list
  91. \param si pointer to spatial index structure
  92. \param box bounding box
  93. \param[out] list pointer to list where selected items are stored
  94. \return number of selected items
  95. */
  96. int
  97. Vect_spatial_index_select(const struct spatial_index * si, const struct bound_box * box,
  98. struct ilist *list)
  99. {
  100. struct Rect rect;
  101. G_debug(3, "Vect_spatial_index_select()");
  102. Vect_reset_list(list);
  103. rect.boundary[0] = box->W;
  104. rect.boundary[1] = box->S;
  105. rect.boundary[2] = box->B;
  106. rect.boundary[3] = box->E;
  107. rect.boundary[4] = box->N;
  108. rect.boundary[5] = box->T;
  109. RTreeSearch(si->si_tree, &rect, (void *)_add_item, list);
  110. G_debug(3, " %d items selected", list->n_values);
  111. return (list->n_values);
  112. }