sindex.c 9.4 KB

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