dangles.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*!
  2. \file dangles.c
  3. \brief Vector library - clean geometry (dangles)
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2001-2008 by the GRASS Development Team
  6. This program is free software under the
  7. GNU General Public License (>=v2).
  8. Read the file COPYING that comes with GRASS
  9. for details.
  10. \author Radim Blazek
  11. \date 2001-2008
  12. */
  13. #include <stdlib.h>
  14. #include <grass/gis.h>
  15. #include <grass/Vect.h>
  16. #include <grass/glocale.h>
  17. #define REMOVE_DANGLE 0
  18. #define CHTYPE_DANGLE 1
  19. #define SELECT_DANGLE 2
  20. static void dangles(struct Map_info *, int, int, double,
  21. struct Map_info *, struct ilist *);
  22. /*!
  23. \brief Remove dangles from vector map.
  24. Remove dangles of given type shorter than maxlength from vector map.
  25. Line is considered to be a dangle if on at least one end node is no
  26. other line of given type(s). If a dangle is formed by more lines,
  27. such string of lines is taken as one dangle and either deleted are
  28. all parts or nothing.
  29. Optionally deleted dangles are written to error map.
  30. Input map must be opened on level 2 for update.
  31. \param Map input map where have to be deleted
  32. \param type type of dangles (GV_LINES, GV_LINE or GV_BOUNDARY)
  33. \param maxlength maxlength of dangles or -1 for all dangles
  34. \param Err vector map where deleted dangles are written or NULL
  35. \return
  36. */
  37. void
  38. Vect_remove_dangles(struct Map_info *Map, int type, double maxlength,
  39. struct Map_info *Err)
  40. {
  41. dangles(Map, type, REMOVE_DANGLE, maxlength, Err, NULL);
  42. }
  43. /*!
  44. \brief Change boundary dangles to lines.
  45. Change boundary dangles to lines.
  46. Boundary is considered to be a dangle if on at least one end node is
  47. no other boundary. If a dangle is formed by more boundaries, such
  48. string of boundaries is taken as one dangle.
  49. Optionally deleted dangles are written to error map.
  50. Input map must be opened on level 2 for update at least on GV_BUILD_BASE.
  51. \param Map input map where have to be deleted
  52. \param maxlength maxlength of dangles or -1 for all dangles
  53. \param Err vector map where deleted dangles are written or NULL
  54. \return
  55. */
  56. void
  57. Vect_chtype_dangles(struct Map_info *Map, double maxlength,
  58. struct Map_info *Err)
  59. {
  60. dangles(Map, 0, CHTYPE_DANGLE, maxlength, Err, NULL);
  61. }
  62. /*!
  63. \brief Select dangles from vector map.
  64. Remove dangles of given type shorter than maxlength from vector map.
  65. Line is considered to be a dangle if on at least one end node is no
  66. other line of given type(s). If a dangle is formed by more lines,
  67. such string of lines is taken as one dangle.
  68. Input map must be opened on level 2 for update.
  69. \param Map input map where have to be deleted
  70. \param type type of dangles (GV_LINES, GV_LINE or GV_BOUNDARY)
  71. \param maxlength maxlength of dangles or -1 for all dangles
  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 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_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. }