break_lines.c 13 KB

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