remove_duplicates.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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, j, c, atype, btype, bline;
  29. int nlines, nbcats_orig;
  30. struct bound_box ABox;
  31. struct boxlist *List;
  32. int ndupl;
  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 select lines which overlap MBR of
  41. * this line and check if some of them is identical. If someone is identical
  42. * remove current line. (In each step just one line is deleted)
  43. */
  44. ndupl = 0;
  45. for (i = 1; i <= nlines; i++) {
  46. G_percent(i, nlines, 1);
  47. if (!Vect_line_alive(Map, i))
  48. continue;
  49. atype = Vect_read_line(Map, APoints, ACats, i);
  50. if (!(atype & type))
  51. continue;
  52. Vect_line_box(APoints, &ABox);
  53. Vect_select_lines_by_box(Map, &ABox, type, List);
  54. G_debug(3, " %d lines selected by box", List->n_values);
  55. for (j = 0; j < List->n_values; j++) {
  56. bline = List->id[j];
  57. G_debug(3, " j = %d bline = %d", j, bline);
  58. if (i == bline)
  59. continue;
  60. btype = Vect_read_line(Map, BPoints, BCats, bline);
  61. /* check for duplicates */
  62. if (!Vect_line_check_duplicate(APoints, BPoints, Vect_is_3d(Map)))
  63. continue;
  64. /* Lines area identical -> remove current */
  65. if (Err) {
  66. Vect_write_line(Err, atype, APoints, ACats);
  67. }
  68. Vect_delete_line(Map, i);
  69. /* Merge categories */
  70. nbcats_orig = BCats->n_cats;
  71. for (c = 0; c < ACats->n_cats; c++)
  72. Vect_cat_set(BCats, ACats->field[c], ACats->cat[c]);
  73. if (BCats->n_cats > nbcats_orig) {
  74. G_debug(4, "cats merged: n_cats %d -> %d", nbcats_orig,
  75. BCats->n_cats);
  76. Vect_rewrite_line(Map, bline, btype, BPoints, BCats);
  77. }
  78. ndupl++;
  79. break; /* line was deleted -> take the next one */
  80. }
  81. nlines = Vect_get_num_lines(Map); /* For future when lines with cats will be rewritten */
  82. G_debug(3, "nlines = %d\n", nlines);
  83. }
  84. G_verbose_message("Removed duplicates: %d", ndupl);
  85. }
  86. /*!
  87. \brief Check for duplicate lines
  88. \param APoints first line geometry
  89. \param BPoints second line geometry
  90. \return 1 duplicate
  91. \return 0 not duplicate
  92. */
  93. int Vect_line_check_duplicate(const struct line_pnts *APoints,
  94. const struct line_pnts *BPoints, int with_z)
  95. {
  96. int k;
  97. int npoints;
  98. int forw, backw;
  99. if (APoints->n_points != BPoints->n_points)
  100. return 0;
  101. npoints = APoints->n_points;
  102. /* Forward */
  103. forw = 1;
  104. for (k = 0; k < APoints->n_points; k++) {
  105. if (APoints->x[k] != BPoints->x[k] ||
  106. APoints->y[k] != BPoints->y[k] ||
  107. (with_z && APoints->z[k] != BPoints->z[k])) {
  108. forw = 0;
  109. break;
  110. }
  111. }
  112. /* Backward */
  113. backw = 1;
  114. for (k = 0; k < APoints->n_points; k++) {
  115. if (APoints->x[k] != BPoints->x[npoints - k - 1] ||
  116. APoints->y[k] != BPoints->y[npoints - k - 1] ||
  117. (with_z && APoints->z[k] != BPoints->z[npoints - k - 1])) {
  118. backw = 0;
  119. break;
  120. }
  121. }
  122. if (!forw && !backw)
  123. return 0;
  124. return 1;
  125. }