dangles.c 7.7 KB

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