select.c 8.9 KB

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