remove_duplicates.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. /*!
  14. \brief Remove duplicate features from vector map.
  15. Remove duplicate lines of given types from vector map. Duplicate
  16. lines may be optionally written to error map. Input map must be
  17. opened on level 2 for update. Categories are merged.
  18. GV_BUILD_BASE is sufficient.
  19. \param[in,out] Map vector map where duplicate lines will be deleted
  20. \param type type of line to be delete
  21. \param[out] Err vector map where duplicate lines will be written or NULL
  22. \return void
  23. */
  24. void Vect_remove_duplicates(struct Map_info *Map, int type, struct Map_info *Err)
  25. {
  26. struct line_pnts *APoints, *BPoints;
  27. struct line_cats *ACats, *BCats;
  28. int i, c, atype, btype, aline, bline;
  29. int nlines, nacats_orig, npoints;
  30. struct bound_box ABox;
  31. struct boxlist *List;
  32. int ndupl, is_dupl;
  33. APoints = Vect_new_line_struct();
  34. BPoints = Vect_new_line_struct();
  35. ACats = Vect_new_cats_struct();
  36. BCats = Vect_new_cats_struct();
  37. List = Vect_new_boxlist(0);
  38. nlines = Vect_get_num_lines(Map);
  39. G_debug(1, "nlines = %d", nlines);
  40. /* Go through all lines in vector, for each line select lines which
  41. * overlap with the first vertex of this line and check if a
  42. * selected line is identical. If yes, remove the selected line.
  43. * If the line vertices are identical with those of any other line,
  44. * merge categories and rewrite the current line.
  45. */
  46. ndupl = 0;
  47. for (aline = 1; aline <= nlines; aline++) {
  48. G_percent(aline, nlines, 1);
  49. if (!Vect_line_alive(Map, aline))
  50. continue;
  51. atype = Vect_read_line(Map, APoints, ACats, aline);
  52. if (!(atype & type))
  53. continue;
  54. npoints = APoints->n_points;
  55. Vect_line_prune(APoints);
  56. if (npoints != APoints->n_points) {
  57. Vect_rewrite_line(Map, aline, atype, APoints, ACats);
  58. nlines = Vect_get_num_lines(Map);
  59. continue;
  60. }
  61. /* select potential duplicates */
  62. ABox.E = ABox.W = APoints->x[0];
  63. ABox.N = ABox.S = APoints->y[0];
  64. ABox.T = ABox.B = APoints->z[0];
  65. Vect_select_lines_by_box(Map, &ABox, type, List);
  66. G_debug(3, " %d lines selected by box", List->n_values);
  67. is_dupl = 0;
  68. for (i = 0; i < List->n_values; i++) {
  69. bline = List->id[i];
  70. G_debug(3, " j = %d bline = %d", i, bline);
  71. /* compare aline and bline only once */
  72. if (aline <= bline)
  73. continue;
  74. btype = Vect_read_line(Map, BPoints, BCats, bline);
  75. Vect_line_prune(BPoints);
  76. /* check for duplicate */
  77. if (!Vect_line_check_duplicate(APoints, BPoints, Vect_is_3d(Map)))
  78. continue;
  79. /* bline is identical to aline */
  80. if (!is_dupl) {
  81. if (Err) {
  82. Vect_write_line(Err, atype, APoints, ACats);
  83. }
  84. is_dupl = 1;
  85. }
  86. Vect_delete_line(Map, bline);
  87. /* merge categories */
  88. nacats_orig = ACats->n_cats;
  89. for (c = 0; c < BCats->n_cats; c++)
  90. Vect_cat_set(ACats, BCats->field[c], BCats->cat[c]);
  91. if (ACats->n_cats > nacats_orig) {
  92. G_debug(4, "cats merged: n_cats %d -> %d", nacats_orig,
  93. ACats->n_cats);
  94. }
  95. ndupl++;
  96. }
  97. if (is_dupl) {
  98. Vect_rewrite_line(Map, aline, atype, APoints, ACats);
  99. nlines = Vect_get_num_lines(Map);
  100. G_debug(3, "nlines = %d\n", nlines);
  101. }
  102. }
  103. G_verbose_message("Removed duplicates: %d", ndupl);
  104. }
  105. /*!
  106. \brief Check for duplicate lines
  107. Note that lines must be pruned with Vect_line_prune() before passed
  108. to Vect_line_check_duplicate()
  109. \param APoints first line geometry
  110. \param BPoints second line geometry
  111. \return 1 duplicate
  112. \return 0 not duplicate
  113. */
  114. int Vect_line_check_duplicate(const struct line_pnts *APoints,
  115. const struct line_pnts *BPoints, int with_z)
  116. {
  117. int k;
  118. int npoints;
  119. int forw, backw;
  120. if (APoints->n_points != BPoints->n_points)
  121. return 0;
  122. npoints = APoints->n_points;
  123. /* Forward */
  124. forw = 1;
  125. for (k = 0; k < APoints->n_points; k++) {
  126. if (APoints->x[k] != BPoints->x[k] ||
  127. APoints->y[k] != BPoints->y[k] ||
  128. (with_z && APoints->z[k] != BPoints->z[k])) {
  129. forw = 0;
  130. break;
  131. }
  132. }
  133. /* Backward */
  134. backw = 1;
  135. for (k = 0; k < APoints->n_points; k++) {
  136. if (APoints->x[k] != BPoints->x[npoints - k - 1] ||
  137. APoints->y[k] != BPoints->y[npoints - k - 1] ||
  138. (with_z && APoints->z[k] != BPoints->z[npoints - k - 1])) {
  139. backw = 0;
  140. break;
  141. }
  142. }
  143. if (!forw && !backw)
  144. return 0;
  145. return 1;
  146. }