dangles.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*!
  2. \file lib/vector/Vlib/dangles.c
  3. \brief Vector library - clean geometry (dangles)
  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 <stdlib.h>
  11. #include <grass/vector.h>
  12. #include <grass/glocale.h>
  13. #define REMOVE_DANGLE 0
  14. #define CHTYPE_DANGLE 1
  15. #define SELECT_DANGLE 2
  16. static void dangles(struct Map_info *, int, int, double,
  17. struct Map_info *, struct ilist *);
  18. /*!
  19. \brief Remove dangles from vector map.
  20. Remove dangles of given type shorter than maxlength from vector
  21. map.
  22. Line is considered to be a dangle if on at least one end node is no
  23. other line of given type(s). If a dangle is formed by more lines,
  24. such string of lines is taken as one dangle and either deleted are
  25. all parts or nothing.
  26. Optionally deleted dangles are written to error map.
  27. Input map must be opened on level 2 for update.
  28. \param Map input map where have to be deleted
  29. \param type type of dangles (GV_LINES, GV_LINE or GV_BOUNDARY)
  30. \param maxlength maxlength of dangles or -1 for all dangles
  31. \param[out] Err vector map where deleted dangles are written or NULL
  32. \return
  33. */
  34. void
  35. Vect_remove_dangles(struct Map_info *Map, int type, double maxlength,
  36. struct Map_info *Err)
  37. {
  38. dangles(Map, type, REMOVE_DANGLE, maxlength, Err, NULL);
  39. }
  40. /*!
  41. \brief Change boundary dangles to lines.
  42. Change boundary dangles to lines.
  43. Boundary is considered to be a dangle if on at least one end node
  44. is no other boundary. If a dangle is formed by more boundaries,
  45. such string of boundaries is taken as one dangle.
  46. Optionally deleted dangles are written to error map.
  47. Input map must be opened on level 2 for update at least on GV_BUILD_BASE.
  48. \param Map input map where have to be deleted
  49. \param maxlength maxlength of dangles or -1 for all dangles
  50. \param[out] Err vector map where deleted dangles are written or NULL
  51. \return
  52. */
  53. void
  54. Vect_chtype_dangles(struct Map_info *Map, double maxlength,
  55. struct Map_info *Err)
  56. {
  57. dangles(Map, 0, CHTYPE_DANGLE, maxlength, Err, NULL);
  58. }
  59. /*!
  60. \brief Select dangles from vector map.
  61. Remove dangles of given type shorter than maxlength from vector map.
  62. Line is considered to be a dangle if on at least one end node is no
  63. other line of given type(s). If a dangle is formed by more lines,
  64. such string of lines is taken as one dangle.
  65. Input map must be opened on level 2 for update.
  66. \param Map input map where have to be deleted
  67. \param type type of dangles (GV_LINES, GV_LINE or GV_BOUNDARY)
  68. \param maxlength maxlength of dangles or -1 for all dangles
  69. \param[out] List list of selected features
  70. \return
  71. */
  72. void
  73. Vect_select_dangles(struct Map_info *Map, int type, double maxlength,
  74. struct ilist *List)
  75. {
  76. dangles(Map, type, SELECT_DANGLE, maxlength, NULL, List);
  77. }
  78. /*
  79. Line is considered to be a dangle if on at least one end node is no
  80. other line of given type(s). If a dangle is formed by more lines,
  81. such string of lines is taken as one dangle and either deleted are
  82. all parts or nothing. Optionally, if chtype is set to 1, only
  83. GV_BOUNDARY are checked for dangles, and if dangle is found lines
  84. are not deleted but rewritten with type GVLINE. Optionally deleted
  85. dangles are written to error map. Input map must be opened on level
  86. 2 for update at least on GV_BUILD_BASE.
  87. Parameters:
  88. Map input map where dangles have to be deleted
  89. type type of dangles
  90. option dangle option (REMOVE_DANGLE, CHTYPE_DANGLE, SELECT_DANGLE)
  91. maxlength maxlength of dangles or -1 for all dangles
  92. Err vector map where deleted dangles are written or NULL
  93. List_dangle list of feature (selected dangles) ids
  94. */
  95. static void dangles(struct Map_info *Map, int type, int option,
  96. double maxlength, struct Map_info *Err,
  97. struct ilist *List_dangle)
  98. {
  99. struct line_pnts *Points;
  100. struct line_cats *Cats;
  101. int i, line, ltype, next_line = 0, nnodelines;
  102. int nnodes, node, node1, node2, next_node;
  103. int lcount, tmp_next_line = 0;
  104. double length;
  105. int dangles_removed; /* number of removed dangles */
  106. int lines_removed; /* number of lines removed */
  107. struct ilist *List; /* List of lines in chain */
  108. char *lmsg;
  109. next_line = tmp_next_line = 0;
  110. dangles_removed = 0;
  111. lines_removed = 0;
  112. type &= GV_LINES; /* to work only with lines and boundaries */
  113. if (option == CHTYPE_DANGLE) {
  114. type = GV_BOUNDARY; /* process boundaries only */
  115. lmsg = _("Changed");
  116. }
  117. else if (option == REMOVE_DANGLE) {
  118. lmsg = _("Removed");
  119. }
  120. else {
  121. lmsg = _("Selected");
  122. }
  123. if (List_dangle)
  124. Vect_reset_list(List_dangle);
  125. Points = Vect_new_line_struct();
  126. Cats = Vect_new_cats_struct();
  127. List = Vect_new_list();
  128. nnodes = Vect_get_num_nodes(Map);
  129. G_debug(2, "nnodes = %d", nnodes);
  130. for (node = 1; node <= nnodes; node++) {
  131. G_percent(node, nnodes, 1);
  132. G_debug(3, "node = %d", node);
  133. if (!Vect_node_alive(Map, node))
  134. continue;
  135. nnodelines = Vect_get_node_n_lines(Map, node);
  136. lcount = 0; /* number of lines of given type */
  137. for (i = 0; i < nnodelines; i++) {
  138. line = Vect_get_node_line(Map, node, i);
  139. G_debug(3, " node line %d = %d", i, line);
  140. ltype = Vect_read_line(Map, NULL, NULL, abs(line));
  141. if (ltype & type) {
  142. lcount++;
  143. next_line = line;
  144. }
  145. }
  146. Vect_reset_list(List);
  147. if (lcount == 1) {
  148. G_debug(3, " node %d is dangle -> follow the line %d", node,
  149. next_line);
  150. while (next_line != 0) {
  151. Vect_list_append(List, abs(next_line));
  152. /* Look at the next end of the line if just one another line of the type is connected */
  153. Vect_get_line_nodes(Map, abs(next_line), &node1, &node2);
  154. next_node = next_line > 0 ? node2 : node1;
  155. G_debug(3, " next_node = %d", next_node);
  156. nnodelines = Vect_get_node_n_lines(Map, next_node);
  157. lcount = 0; /* number of lines of given type (except current next_line) */
  158. for (i = 0; i < nnodelines; i++) {
  159. line = Vect_get_node_line(Map, next_node, i);
  160. G_debug(3, " node line %d = %d", i, line);
  161. ltype = Vect_read_line(Map, NULL, NULL, abs(line));
  162. if (ltype & type && abs(line) != abs(next_line)) {
  163. lcount++;
  164. tmp_next_line = line;
  165. }
  166. }
  167. if (lcount == 1)
  168. next_line = tmp_next_line;
  169. else
  170. next_line = 0;
  171. }
  172. /* Length of the chain */
  173. length = 0;
  174. for (i = 0; i < List->n_values; i++) {
  175. G_debug(3, " chain line %d = %d", i, List->value[i]);
  176. ltype = Vect_read_line(Map, Points, NULL, List->value[i]);
  177. length += Vect_line_length(Points);
  178. }
  179. if (maxlength < 0 || length < maxlength) { /* delete the chain */
  180. G_debug(3, " delete the chain (length=%g)", length);
  181. for (i = 0; i < List->n_values; i++) {
  182. ltype = Vect_read_line(Map, Points, Cats, List->value[i]);
  183. /* Write to Err deleted dangle */
  184. if (Err) {
  185. Vect_write_line(Err, ltype, Points, Cats);
  186. }
  187. if (option == REMOVE_DANGLE) {
  188. Vect_delete_line(Map, List->value[i]);
  189. }
  190. else if (option == CHTYPE_DANGLE) {
  191. G_debug(3, " rewrite line %d", List->value[i]);
  192. Vect_rewrite_line(Map, List->value[i], GV_LINE,
  193. Points, Cats);
  194. }
  195. else {
  196. if (List_dangle) {
  197. Vect_list_append(List_dangle, List->value[i]);
  198. }
  199. }
  200. lines_removed++;
  201. }
  202. } /* delete the chain */
  203. dangles_removed++;
  204. } /* lcount == 1 */
  205. } /* node <= nnodes */
  206. G_verbose_message(_("%s lines: %d"), lmsg, lines_removed);
  207. G_verbose_message(_("%s dangles: %d"), lmsg, dangles_removed);
  208. }