dangles.c 7.6 KB

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