dangles.c 8.3 KB

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