sindex.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*!
  2. \file lib/vector/Vlib/sindex.c
  3. \brief Vector library - select vector features
  4. Higher 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 Radim Blazek
  9. */
  10. #include <stdlib.h>
  11. #include <grass/vector.h>
  12. /*!
  13. \brief Select lines by box.
  14. Select lines whose boxes overlap specified box!!! It means that
  15. selected line may or may not overlap the box.
  16. \param Map vector map
  17. \param Box bounding box
  18. \param type line type
  19. \param[out] list output list, must be initialized
  20. \return number of lines
  21. */
  22. int
  23. Vect_select_lines_by_box(struct Map_info *Map, const struct bound_box * Box,
  24. int type, struct ilist *list)
  25. {
  26. int i, line, nlines;
  27. struct Plus_head *plus;
  28. struct P_line *Line;
  29. static struct ilist *LocList = NULL;
  30. G_debug(3, "Vect_select_lines_by_box()");
  31. G_debug(3, " Box(N,S,E,W,T,B): %e, %e, %e, %e, %e, %e", Box->N, Box->S,
  32. Box->E, Box->W, Box->T, Box->B);
  33. plus = &(Map->plus);
  34. if (!(plus->Spidx_built)) {
  35. G_debug(3, "Building spatial index.");
  36. Vect_build_sidx_from_topo(Map);
  37. }
  38. list->n_values = 0;
  39. if (!LocList)
  40. LocList = Vect_new_list();
  41. nlines = dig_select_lines(plus, Box, LocList);
  42. G_debug(3, " %d lines selected (all types)", nlines);
  43. /* Remove lines of not requested types */
  44. for (i = 0; i < nlines; i++) {
  45. line = LocList->value[i];
  46. if (plus->Line[line] == NULL)
  47. continue; /* Should not happen */
  48. Line = plus->Line[line];
  49. if (!(Line->type & type))
  50. continue;
  51. dig_list_add(list, line);
  52. }
  53. G_debug(3, " %d lines of requested type", list->n_values);
  54. return list->n_values;
  55. }
  56. /*!
  57. \brief Select areas by box.
  58. Select areas whose boxes overlap specified box!!!
  59. It means that selected area may or may not overlap the box.
  60. \param Map vector map
  61. \param Box bounding box
  62. \param[out] output list, must be initialized
  63. \return number of areas
  64. */
  65. int
  66. Vect_select_areas_by_box(struct Map_info *Map, const struct bound_box * Box,
  67. struct ilist *list)
  68. {
  69. int i;
  70. static int debug_level = -1;
  71. if (debug_level == -1) {
  72. const char *dstr = G__getenv("DEBUG");
  73. if (dstr != NULL)
  74. debug_level = atoi(dstr);
  75. else
  76. debug_level = 0;
  77. }
  78. G_debug(3, "Vect_select_areas_by_box()");
  79. G_debug(3, "Box(N,S,E,W,T,B): %e, %e, %e, %e, %e, %e", Box->N, Box->S,
  80. Box->E, Box->W, Box->T, Box->B);
  81. if (!(Map->plus.Spidx_built)) {
  82. G_debug(3, "Building spatial index.");
  83. Vect_build_sidx_from_topo(Map);
  84. }
  85. dig_select_areas(&(Map->plus), Box, list);
  86. G_debug(3, " %d areas selected", list->n_values);
  87. /* avoid loop when not debugging */
  88. if (debug_level > 2) {
  89. for (i = 0; i < list->n_values; i++) {
  90. G_debug(3, " area = %d pointer to area structure = %lx",
  91. list->value[i],
  92. (unsigned long)Map->plus.Area[list->value[i]]);
  93. }
  94. }
  95. return list->n_values;
  96. }
  97. /*!
  98. \brief Select isles by box.
  99. Select isles whose boxes overlap specified box!!!
  100. It means that selected isle may or may not overlap the box.
  101. \param Map vector map
  102. \param Box bounding box
  103. \param[out] list output list, must be initialized
  104. \return number of isles
  105. */
  106. int
  107. Vect_select_isles_by_box(struct Map_info *Map, const struct bound_box * Box,
  108. struct ilist *list)
  109. {
  110. G_debug(3, "Vect_select_isles_by_box()");
  111. G_debug(3, "Box(N,S,E,W,T,B): %e, %e, %e, %e, %e, %e", Box->N, Box->S,
  112. Box->E, Box->W, Box->T, Box->B);
  113. if (!(Map->plus.Spidx_built)) {
  114. G_debug(3, "Building spatial index.");
  115. Vect_build_sidx_from_topo(Map);
  116. }
  117. dig_select_isles(&(Map->plus), Box, list);
  118. G_debug(3, " %d isles selected", list->n_values);
  119. return list->n_values;
  120. }
  121. /*!
  122. \brief Select nodes by box.
  123. \param Map vector map
  124. \param Box bounding box
  125. \param[out] list output list, must be initialized
  126. \return number of nodes
  127. */
  128. int
  129. Vect_select_nodes_by_box(struct Map_info *Map, const struct bound_box * Box,
  130. struct ilist *list)
  131. {
  132. struct Plus_head *plus;
  133. G_debug(3, "Vect_select_nodes_by_box()");
  134. G_debug(3, "Box(N,S,E,W,T,B): %e, %e, %e, %e, %e, %e", Box->N, Box->S,
  135. Box->E, Box->W, Box->T, Box->B);
  136. plus = &(Map->plus);
  137. if (!(plus->Spidx_built)) {
  138. G_debug(3, "Building spatial index.");
  139. Vect_build_sidx_from_topo(Map);
  140. }
  141. list->n_values = 0;
  142. dig_select_nodes(plus, Box, list);
  143. G_debug(3, " %d nodes selected", list->n_values);
  144. return list->n_values;
  145. }
  146. /*!
  147. \brief Select lines by Polygon with optional isles.
  148. Polygons should be closed, i.e. first and last points must be identical.
  149. \param Map vector map
  150. \param Polygon outer ring
  151. \param nisles number of islands or 0
  152. \param Isles array of islands or NULL
  153. \param type line type
  154. \param[out] list output list, must be initialised
  155. \return number of lines
  156. */
  157. int
  158. Vect_select_lines_by_polygon(struct Map_info *Map, struct line_pnts *Polygon,
  159. int nisles, struct line_pnts **Isles, int type,
  160. struct ilist *List)
  161. {
  162. int i;
  163. struct bound_box box;
  164. static struct line_pnts *LPoints = NULL;
  165. static struct ilist *LocList = NULL;
  166. /* TODO: this function was not tested with isles */
  167. G_debug(3, "Vect_select_lines_by_polygon() nisles = %d", nisles);
  168. List->n_values = 0;
  169. if (!LPoints)
  170. LPoints = Vect_new_line_struct();
  171. if (!LocList)
  172. LocList = Vect_new_list();
  173. /* Select first all lines by box */
  174. dig_line_box(Polygon, &box);
  175. box.T = PORT_DOUBLE_MAX;
  176. box.B = -PORT_DOUBLE_MAX;
  177. Vect_select_lines_by_box(Map, &box, type, LocList);
  178. G_debug(3, " %d lines selected by box", LocList->n_values);
  179. /* Check all lines if intersect the polygon */
  180. for (i = 0; i < LocList->n_values; i++) {
  181. int j, line, intersect = 0;
  182. line = LocList->value[i];
  183. /* Read line points */
  184. Vect_read_line(Map, LPoints, NULL, line);
  185. /* Check if any of line vertices is within polygon */
  186. for (j = 0; j < LPoints->n_points; j++) {
  187. if (Vect_point_in_poly(LPoints->x[j], LPoints->y[j], Polygon) >= 1) { /* inside polygon */
  188. int k, inisle = 0;
  189. for (k = 0; k < nisles; k++) {
  190. if (Vect_point_in_poly(LPoints->x[j], LPoints->y[j], Isles[k]) >= 1) { /* in isle */
  191. inisle = 1;
  192. break;
  193. }
  194. }
  195. if (!inisle) { /* inside polygon, outside isles -> select */
  196. intersect = 1;
  197. break;
  198. }
  199. }
  200. }
  201. if (intersect) {
  202. dig_list_add(List, line);
  203. continue;
  204. }
  205. /* Check intersections of the line with area/isles boundary */
  206. /* Outer boundary */
  207. if (Vect_line_check_intersection(LPoints, Polygon, 0)) {
  208. dig_list_add(List, line);
  209. continue;
  210. }
  211. /* Islands */
  212. for (j = 0; j < nisles; j++) {
  213. if (Vect_line_check_intersection(LPoints, Isles[j], 0)) {
  214. intersect = 1;
  215. break;
  216. }
  217. }
  218. if (intersect) {
  219. dig_list_add(List, line);
  220. }
  221. }
  222. G_debug(4, " %d lines selected by polygon", List->n_values);
  223. return List->n_values;
  224. }
  225. /*!
  226. \brief Select areas by Polygon with optional isles.
  227. Polygons should be closed, i.e. first and last points must be identical.
  228. Warning : values in list may be duplicate!
  229. \param Map vector map
  230. \param Polygon outer ring
  231. \param nisles number of islands or 0
  232. \param Isles array of islands or NULL
  233. \param[out] list output list, must be initialised
  234. \return number of areas
  235. */
  236. int
  237. Vect_select_areas_by_polygon(struct Map_info *Map, struct line_pnts *Polygon,
  238. int nisles, struct line_pnts **Isles,
  239. struct ilist *List)
  240. {
  241. int i, area;
  242. static struct ilist *BoundList = NULL;
  243. /* TODO: this function was not tested with isles */
  244. G_debug(3, "Vect_select_areas_by_polygon() nisles = %d", nisles);
  245. List->n_values = 0;
  246. if (!BoundList)
  247. BoundList = Vect_new_list();
  248. /* Select boundaries by polygon */
  249. Vect_select_lines_by_polygon(Map, Polygon, nisles, Isles, GV_BOUNDARY,
  250. BoundList);
  251. /* Add areas on left/right side of selected boundaries */
  252. for (i = 0; i < BoundList->n_values; i++) {
  253. int line, left, right;
  254. line = BoundList->value[i];
  255. Vect_get_line_areas(Map, line, &left, &right);
  256. G_debug(4, "boundary = %d left = %d right = %d", line, left, right);
  257. if (left > 0) {
  258. dig_list_add(List, left);
  259. }
  260. else if (left < 0) { /* island */
  261. area = Vect_get_isle_area(Map, abs(left));
  262. G_debug(4, " left island -> area = %d", area);
  263. if (area > 0)
  264. dig_list_add(List, area);
  265. }
  266. if (right > 0) {
  267. dig_list_add(List, right);
  268. }
  269. else if (right < 0) { /* island */
  270. area = Vect_get_isle_area(Map, abs(right));
  271. G_debug(4, " right island -> area = %d", area);
  272. if (area > 0)
  273. dig_list_add(List, area);
  274. }
  275. }
  276. /* But the Polygon may be completely inside the area (only one), in that case
  277. * we find the area by one polygon point and add it to the list */
  278. area = Vect_find_area(Map, Polygon->x[0], Polygon->y[0]);
  279. if (area > 0)
  280. dig_list_add(List, area);
  281. G_debug(3, " %d areas selected by polygon", List->n_values);
  282. return List->n_values;
  283. }