remove_duplicates.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*!
  2. \file lib/vector/Vlib/remove_duplicates.c
  3. \brief Vector library - clean geometry (remove duplicates)
  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. static int cmp_int(const void *a, const void *b)
  14. {
  15. return (*(int *)a - *(int *)b);
  16. }
  17. static int boxlist_add_sorted(struct boxlist *list, int id)
  18. {
  19. int i;
  20. if (list->n_values > 0) {
  21. if (bsearch(&id, list->id, list->n_values, sizeof(int), cmp_int))
  22. return 0;
  23. }
  24. if (list->n_values == list->alloc_values) {
  25. size_t size = (list->n_values + 100) * sizeof(int);
  26. list->id = (int *)G_realloc((void *)list->id, size);
  27. list->alloc_values = list->n_values + 100;
  28. }
  29. i = 0;
  30. if (list->n_values > 0) {
  31. for (i = list->n_values; i > 0; i--) {
  32. if (list->id[i - 1] < id)
  33. break;
  34. list->id[i] = list->id[i - 1];
  35. }
  36. }
  37. list->id[i] = id;
  38. list->n_values++;
  39. return 1;
  40. }
  41. /*!
  42. \brief Remove duplicate features from vector map.
  43. Remove duplicate lines of given types from vector map. Duplicate
  44. lines may be optionally written to error map. Input map must be
  45. opened on level 2 for update. Categories are merged.
  46. GV_BUILD_BASE is sufficient.
  47. \param[in,out] Map vector map where duplicate lines will be deleted
  48. \param type type of line to be delete
  49. \param[out] Err vector map where duplicate lines will be written or NULL
  50. \return void
  51. */
  52. void Vect_remove_duplicates(struct Map_info *Map, int type, struct Map_info *Err)
  53. {
  54. struct line_pnts *APoints, *BPoints;
  55. struct line_cats *ACats, *BCats;
  56. int i, c, atype, btype, aline, bline;
  57. int nlines, nacats_orig, npoints;
  58. int na1, na2, nb1, nb2, nodelines, nline;
  59. struct bound_box ABox;
  60. struct boxlist *List;
  61. int ndupl, is_dupl;
  62. APoints = Vect_new_line_struct();
  63. BPoints = Vect_new_line_struct();
  64. ACats = Vect_new_cats_struct();
  65. BCats = Vect_new_cats_struct();
  66. List = Vect_new_boxlist(0);
  67. nlines = Vect_get_num_lines(Map);
  68. G_debug(1, "nlines = %d", nlines);
  69. /* Go through all lines in vector, for each line select lines which
  70. * overlap with the first vertex of this line and check if a
  71. * selected line is identical. If yes, remove the selected line.
  72. * If the line vertices are identical with those of any other line,
  73. * merge categories and rewrite the current line.
  74. */
  75. ndupl = 0;
  76. for (aline = 1; aline <= nlines; aline++) {
  77. G_percent(aline, nlines, 1);
  78. if (!Vect_line_alive(Map, aline))
  79. continue;
  80. atype = Vect_read_line(Map, APoints, ACats, aline);
  81. if (!(atype & type))
  82. continue;
  83. npoints = APoints->n_points;
  84. Vect_line_prune(APoints);
  85. if (npoints != APoints->n_points) {
  86. G_debug(3, "Line %d pruned, %d vertices removed", aline, npoints - APoints->n_points);
  87. Vect_rewrite_line(Map, aline, atype, APoints, ACats);
  88. nlines = Vect_get_num_lines(Map);
  89. continue;
  90. }
  91. na1 = na2 = -1;
  92. if (atype & GV_LINES) {
  93. /* faster than Vect_select_lines_by_box() */
  94. Vect_reset_boxlist(List);
  95. Vect_get_line_nodes(Map, aline, &na1, &na2);
  96. nodelines = Vect_get_node_n_lines(Map, na1);
  97. for (i = 0; i < nodelines; i++) {
  98. nline = abs(Vect_get_node_line(Map, na1, i));
  99. if (nline == aline)
  100. continue;
  101. if (Vect_get_line_type(Map, nline) != atype)
  102. continue;
  103. boxlist_add_sorted(List, nline);
  104. }
  105. }
  106. else {
  107. /* select potential duplicates */
  108. ABox.E = ABox.W = APoints->x[0];
  109. ABox.N = ABox.S = APoints->y[0];
  110. ABox.T = ABox.B = APoints->z[0];
  111. Vect_select_lines_by_box(Map, &ABox, atype, List);
  112. G_debug(3, " %d lines selected by box", List->n_values);
  113. }
  114. is_dupl = 0;
  115. for (i = 0; i < List->n_values; i++) {
  116. bline = List->id[i];
  117. G_debug(3, " j = %d bline = %d", i, bline);
  118. /* compare aline and bline only once */
  119. if (aline <= bline)
  120. continue;
  121. nb1 = nb2 = -1;
  122. if (atype & GV_LINES) {
  123. Vect_get_line_nodes(Map, bline, &nb1, &nb2);
  124. if ((na1 == nb1 && na2 != nb2) ||
  125. (na1 == nb2 && na2 != nb1))
  126. continue;
  127. }
  128. btype = Vect_read_line(Map, BPoints, BCats, bline);
  129. Vect_line_prune(BPoints);
  130. /* check for duplicate */
  131. if (!Vect_line_check_duplicate(APoints, BPoints, Vect_is_3d(Map)))
  132. continue;
  133. /* bline is identical to aline */
  134. if (!is_dupl) {
  135. if (Err) {
  136. Vect_write_line(Err, atype, APoints, ACats);
  137. }
  138. is_dupl = 1;
  139. }
  140. Vect_delete_line(Map, bline);
  141. /* merge categories */
  142. nacats_orig = ACats->n_cats;
  143. for (c = 0; c < BCats->n_cats; c++)
  144. Vect_cat_set(ACats, BCats->field[c], BCats->cat[c]);
  145. if (ACats->n_cats > nacats_orig) {
  146. G_debug(4, "cats merged: n_cats %d -> %d", nacats_orig,
  147. ACats->n_cats);
  148. }
  149. ndupl++;
  150. }
  151. if (is_dupl) {
  152. Vect_rewrite_line(Map, aline, atype, APoints, ACats);
  153. nlines = Vect_get_num_lines(Map);
  154. G_debug(3, "nlines = %d\n", nlines);
  155. }
  156. }
  157. G_verbose_message("Removed duplicates: %d", ndupl);
  158. }
  159. /*!
  160. \brief Check for duplicate lines
  161. Note that lines must be pruned with Vect_line_prune() before passed
  162. to Vect_line_check_duplicate(), as done by Vect_remove_duplicates()
  163. \param APoints first line geometry
  164. \param BPoints second line geometry
  165. \return 1 duplicate
  166. \return 0 not duplicate
  167. */
  168. int Vect_line_check_duplicate(const struct line_pnts *APoints,
  169. const struct line_pnts *BPoints, int with_z)
  170. {
  171. int k;
  172. int npoints;
  173. int forw, backw;
  174. if (APoints->n_points != BPoints->n_points)
  175. return 0;
  176. npoints = APoints->n_points;
  177. /* Forward */
  178. forw = 1;
  179. for (k = 0; k < APoints->n_points; k++) {
  180. if (APoints->x[k] != BPoints->x[k] ||
  181. APoints->y[k] != BPoints->y[k] ||
  182. (with_z && APoints->z[k] != BPoints->z[k])) {
  183. forw = 0;
  184. break;
  185. }
  186. }
  187. /* Backward */
  188. backw = 1;
  189. for (k = 0; k < APoints->n_points; k++) {
  190. if (APoints->x[k] != BPoints->x[npoints - k - 1] ||
  191. APoints->y[k] != BPoints->y[npoints - k - 1] ||
  192. (with_z && APoints->z[k] != BPoints->z[npoints - k - 1])) {
  193. backw = 0;
  194. break;
  195. }
  196. }
  197. if (!forw && !backw)
  198. return 0;
  199. return 1;
  200. }