select.c 8.9 KB

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