break_lines.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*!
  2. * \file lib/vector/Vlib/break_lines.c
  3. *
  4. * \brief Vector library - Clean vector map (break lines)
  5. *
  6. * (C) 2001-2009 by the GRASS Development Team
  7. *
  8. * This program is free software under the
  9. * GNU General Public License (>=v2).
  10. * Read the file COPYING that comes with GRASS
  11. * for details.
  12. *
  13. * \author Radim Blazek
  14. */
  15. #include <stdlib.h>
  16. #include <grass/vector.h>
  17. #include <grass/glocale.h>
  18. /*!
  19. \brief Break lines in vector map at each intersection.
  20. For details see Vect_break_lines_list().
  21. \param Map input vector map
  22. \param type feature type
  23. \param[out] Err vector map where points at intersections will be written or NULL
  24. \return
  25. */
  26. void
  27. Vect_break_lines(struct Map_info *Map, int type, struct Map_info *Err)
  28. {
  29. Vect_break_lines_list(Map, NULL, NULL, type, Err);
  30. return;
  31. }
  32. /*!
  33. \brief Break selected lines in vector map at each intersection.
  34. Breaks selected lines specified by type in vector map. Points at
  35. intersections may be optionally written to error map. Input vector map
  36. must be opened on level 2 for update at least on GV_BUILD_BASE.
  37. The function also breaks lines forming collapsed loop, for example
  38. 0,0;1,0;0,0 is broken at 1,0.
  39. If reference lines are given (<i>List_ref</i>) break only lines
  40. which intersect reference lines.
  41. \param Map input vector map
  42. \param List_break list of lines (NULL for all lines in vector map)
  43. \param List_ref list of reference lines or NULL
  44. \param type feature type
  45. \param[out] Err vector map where points at intersections will be written or NULL
  46. \return number of intersections
  47. */
  48. int
  49. Vect_break_lines_list(struct Map_info *Map, struct ilist *List_break,
  50. struct ilist *List_ref, int type, struct Map_info *Err)
  51. {
  52. struct line_pnts *APoints, *BPoints, *Points;
  53. struct line_pnts **AXLines, **BXLines;
  54. struct line_cats *ACats, *BCats, *Cats;
  55. int j, k, l, ret, atype, btype, aline, bline, found, iline, nlines;
  56. int naxlines, nbxlines, nx;
  57. double *xx = NULL, *yx = NULL, *zx = NULL;
  58. struct bound_box ABox, BBox;
  59. struct ilist *List;
  60. int nbreaks;
  61. int touch1_n = 0, touch1_s = 0, touch1_e = 0, touch1_w = 0; /* other vertices except node1 touching box */
  62. int touch2_n = 0, touch2_s = 0, touch2_e = 0, touch2_w = 0; /* other vertices except node2 touching box */
  63. int is3d;
  64. int node, anode1, anode2, bnode1, bnode2;
  65. double nodex, nodey;
  66. APoints = Vect_new_line_struct();
  67. BPoints = Vect_new_line_struct();
  68. Points = Vect_new_line_struct();
  69. ACats = Vect_new_cats_struct();
  70. BCats = Vect_new_cats_struct();
  71. Cats = Vect_new_cats_struct();
  72. List = Vect_new_list();
  73. is3d = Vect_is_3d(Map);
  74. if (List_break) {
  75. nlines = List_break->n_values;
  76. }
  77. else {
  78. nlines = Vect_get_num_lines(Map);
  79. }
  80. G_debug(3, "nlines = %d", nlines);
  81. /* TODO: It seems that lines/boundaries are not broken at intersections
  82. * with points/centroids. Check if true, if yes, skip GV_POINTS
  83. */
  84. /* To find intersection of two lines (Vect_line_intersection) is quite slow.
  85. * Fortunately usual lines/boundaries in GIS often forms a network where lines
  86. * are connected by end points, and touch by MBR. This function checks and occasionaly
  87. * skips such cases. This is currently done for 2D only
  88. */
  89. /* Go through all lines in vector, for each select lines which overlap MBR of
  90. * this line exclude those connected by one endpoint (see above)
  91. * and try to intersect, if lines intersect write new lines at the end of
  92. * the file, and process next line (remaining lines overlapping box are skipped)
  93. */
  94. nbreaks = 0;
  95. for (iline = 0; iline < nlines; iline++) {
  96. G_percent(iline, nlines, 1);
  97. if (List_break) {
  98. aline = List_break->value[iline];
  99. }
  100. else {
  101. aline = iline + 1;
  102. }
  103. if (List_ref && !Vect_val_in_list(List_ref, aline))
  104. continue;
  105. G_debug(3, "aline = %d", aline);
  106. if (!Vect_line_alive(Map, aline))
  107. continue;
  108. atype = Vect_read_line(Map, APoints, ACats, aline);
  109. if (!(atype & type))
  110. continue;
  111. Vect_get_line_box(Map, aline, &ABox);
  112. /* Find which sides of the box are touched by intermediate (non-end) points of line */
  113. if (!is3d) {
  114. touch1_n = touch1_s = touch1_e = touch1_w = 0;
  115. for (j = 1; j < APoints->n_points; j++) {
  116. if (APoints->y[j] == ABox.N)
  117. touch1_n = 1;
  118. if (APoints->y[j] == ABox.S)
  119. touch1_s = 1;
  120. if (APoints->x[j] == ABox.E)
  121. touch1_e = 1;
  122. if (APoints->x[j] == ABox.W)
  123. touch1_w = 1;
  124. }
  125. G_debug(3, "touch1: n = %d s = %d e = %d w = %d", touch1_n,
  126. touch1_s, touch1_e, touch1_w);
  127. touch2_n = touch2_s = touch2_e = touch2_w = 0;
  128. for (j = 0; j < APoints->n_points - 1; j++) {
  129. if (APoints->y[j] == ABox.N)
  130. touch2_n = 1;
  131. if (APoints->y[j] == ABox.S)
  132. touch2_s = 1;
  133. if (APoints->x[j] == ABox.E)
  134. touch2_e = 1;
  135. if (APoints->x[j] == ABox.W)
  136. touch2_w = 1;
  137. }
  138. G_debug(3, "touch2: n = %d s = %d e = %d w = %d", touch2_n,
  139. touch2_s, touch2_e, touch2_w);
  140. }
  141. Vect_select_lines_by_box(Map, &ABox, type, List);
  142. G_debug(3, " %d lines selected by box", List->n_values);
  143. for (j = 0; j < List->n_values; j++) {
  144. bline = List->value[j];
  145. if (List_break && !Vect_val_in_list(List_break, bline)) {
  146. continue;
  147. }
  148. G_debug(3, " j = %d bline = %d", j, bline);
  149. /* Check if thouch by end node only */
  150. if (!is3d) {
  151. Vect_get_line_nodes(Map, aline, &anode1, &anode2);
  152. Vect_get_line_nodes(Map, bline, &bnode1, &bnode2);
  153. Vect_get_line_box(Map, bline, &BBox);
  154. if (anode1 == bnode1 || anode1 == bnode2)
  155. node = anode1;
  156. else if (anode2 == bnode1 || anode2 == bnode2)
  157. node = anode2;
  158. else
  159. node = 0;
  160. if (node) {
  161. Vect_get_node_coor(Map, node, &nodex, &nodey, NULL);
  162. if ((node == anode1 && nodey == ABox.N && !touch1_n &&
  163. nodey == BBox.S) || (node == anode2 &&
  164. nodey == ABox.N && !touch2_n &&
  165. nodey == BBox.S) ||
  166. (node == anode1 && nodey == ABox.S && !touch1_s &&
  167. nodey == BBox.N) || (node == anode2 &&
  168. nodey == ABox.S && !touch2_s &&
  169. nodey == BBox.N) ||
  170. (node == anode1 && nodex == ABox.E && !touch1_e &&
  171. nodex == BBox.W) || (node == anode2 &&
  172. nodex == ABox.E && !touch2_e &&
  173. nodex == BBox.W) ||
  174. (node == anode1 && nodex == ABox.W && !touch1_w &&
  175. nodex == BBox.E) || (node == anode2 &&
  176. nodex == ABox.W && !touch2_w &&
  177. nodex == BBox.E)) {
  178. G_debug(3,
  179. "lines %d and %d touching by end nodes only -> no intersection",
  180. aline, bline);
  181. continue;
  182. }
  183. }
  184. }
  185. btype = Vect_read_line(Map, BPoints, BCats, bline);
  186. AXLines = NULL;
  187. BXLines = NULL;
  188. Vect_line_intersection(APoints, BPoints, &AXLines, &BXLines,
  189. &naxlines, &nbxlines, 0);
  190. G_debug(3, " naxlines = %d nbxlines = %d", naxlines, nbxlines);
  191. /* This part handles a special case when aline == bline, no other intersection was found
  192. * and the line is forming collapsed loop, for example 0,0;1,0;0,0 should be broken at 1,0.
  193. * ---> */
  194. if (aline == bline && naxlines == 0 && nbxlines == 0 &&
  195. APoints->n_points >= 3) {
  196. int centre;
  197. int i;
  198. G_debug(3, " Check collapsed loop");
  199. if (APoints->n_points % 2) { /* odd number of vertices */
  200. centre = APoints->n_points / 2; /* index of centre */
  201. if (APoints->x[centre - 1] == APoints->x[centre + 1] && APoints->y[centre - 1] == APoints->y[centre + 1] && APoints->z[centre - 1] == APoints->z[centre + 1]) { /* -> break */
  202. AXLines =
  203. (struct line_pnts **)G_malloc(2 *
  204. sizeof(struct
  205. line_pnts
  206. *));
  207. AXLines[0] = Vect_new_line_struct();
  208. AXLines[1] = Vect_new_line_struct();
  209. for (i = 0; i <= centre; i++)
  210. Vect_append_point(AXLines[0], APoints->x[i],
  211. APoints->y[i], APoints->z[i]);
  212. for (i = centre; i < APoints->n_points; i++)
  213. Vect_append_point(AXLines[1], APoints->x[i],
  214. APoints->y[i], APoints->z[i]);
  215. naxlines = 2;
  216. }
  217. }
  218. }
  219. /* <--- */
  220. if (Err) { /* array for intersections (more than needed */
  221. xx = (double *)G_malloc((naxlines + nbxlines) *
  222. sizeof(double));
  223. yx = (double *)G_malloc((naxlines + nbxlines) *
  224. sizeof(double));
  225. zx = (double *)G_malloc((naxlines + nbxlines) *
  226. sizeof(double));
  227. }
  228. nx = 0; /* number of intersections to be written to Err */
  229. if (naxlines > 0) { /* intersection -> write out */
  230. Vect_delete_line(Map, aline);
  231. for (k = 0; k < naxlines; k++) {
  232. /* Write new line segments */
  233. /* line may collapse, don't write zero length lines */
  234. Vect_line_prune(AXLines[k]);
  235. if ((atype & GV_POINTS) || AXLines[k]->n_points > 1) {
  236. ret = Vect_write_line(Map, atype, AXLines[k], ACats);
  237. if (List_ref) {
  238. Vect_list_append(List_ref, ret);
  239. }
  240. G_debug(3, "Line %d written, npoints = %d", ret,
  241. AXLines[k]->n_points);
  242. if (List_break) {
  243. Vect_list_append(List_break, ret);
  244. }
  245. }
  246. /* Write intersection points */
  247. if (Err) {
  248. if (k > 0) {
  249. xx[nx] = AXLines[k]->x[0];
  250. yx[nx] = AXLines[k]->y[0];
  251. zx[nx] = AXLines[k]->z[0];
  252. nx++;
  253. }
  254. }
  255. Vect_destroy_line_struct(AXLines[k]);
  256. }
  257. nbreaks += naxlines - 1;
  258. }
  259. if (AXLines)
  260. G_free(AXLines);
  261. if (nbxlines > 0) {
  262. if (aline != bline) { /* Self intersection, do not write twice, TODO: is it OK? */
  263. Vect_delete_line(Map, bline);
  264. for (k = 0; k < nbxlines; k++) {
  265. /* Write new line segments */
  266. /* line may collapse, don't write zero length lines */
  267. Vect_line_prune(BXLines[k]);
  268. if ((btype & GV_POINTS) || BXLines[k]->n_points > 1) {
  269. ret =
  270. Vect_write_line(Map, btype, BXLines[k],
  271. BCats);
  272. G_debug(5, "Line %d written", ret);
  273. if (List_break) {
  274. Vect_list_append(List_break, ret);
  275. }
  276. }
  277. /* Write intersection points */
  278. if (Err) {
  279. if (k > 0) {
  280. found = 0;
  281. for (l = 0; l < nx; l++) {
  282. if (xx[l] == BXLines[k]->x[0] &&
  283. yx[l] == BXLines[k]->y[0] &&
  284. zx[l] == BXLines[k]->z[0]) {
  285. found = 1;
  286. break;
  287. }
  288. }
  289. if (!found) {
  290. xx[nx] = BXLines[k]->x[0];
  291. yx[nx] = BXLines[k]->y[0];
  292. zx[nx] = BXLines[k]->z[0];
  293. nx++;
  294. }
  295. }
  296. }
  297. Vect_destroy_line_struct(BXLines[k]);
  298. }
  299. nbreaks += nbxlines - 1;
  300. }
  301. else {
  302. for (k = 0; k < nbxlines; k++)
  303. Vect_destroy_line_struct(BXLines[k]);
  304. }
  305. }
  306. if (BXLines)
  307. G_free(BXLines);
  308. if (Err) {
  309. for (l = 0; l < nx; l++) { /* Write out errors */
  310. Vect_reset_line(Points);
  311. Vect_append_point(Points, xx[l], yx[l], zx[l]);
  312. ret = Vect_write_line(Err, GV_POINT, Points, Cats);
  313. }
  314. G_free(xx);
  315. G_free(yx);
  316. G_free(zx);
  317. }
  318. if (naxlines > 0)
  319. break; /* first line was broken and deleted -> take the next one */
  320. }
  321. if (List_break) {
  322. nlines = List_break->n_values;
  323. }
  324. else {
  325. nlines = Vect_get_num_lines(Map);
  326. }
  327. G_debug(3, "nlines = %d", nlines);
  328. } /* for each line */
  329. G_percent(nlines, nlines, 1); /* finish it */
  330. G_verbose_message(_("Intersections: %d"), nbreaks);
  331. Vect_destroy_list(List);
  332. return nbreaks;
  333. }