remove_duplicates.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 <grass/config.h>
  11. #include <stdlib.h>
  12. #include <grass/gis.h>
  13. #include <grass/vector.h>
  14. #include <grass/glocale.h>
  15. /*!
  16. \brief Remove duplicate features from vector map.
  17. Remove duplicate lines of given types from vector map. Duplicate
  18. lines may be optionally written to error map. Input map must be
  19. opened on level 2 for update. Categories are merged.
  20. \param[in,out] Map vector map where duplicate lines will be deleted
  21. \param type type of line to be delete
  22. \param[out] Err vector map where duplicate lines will be written or NULL
  23. \return void
  24. */
  25. void
  26. Vect_remove_duplicates(struct Map_info *Map, int type, struct Map_info *Err)
  27. {
  28. struct line_pnts *APoints, *BPoints;
  29. struct line_cats *ACats, *BCats, *Cats;
  30. int i, j, c, atype, btype, bline;
  31. int nlines, nbcats_orig;
  32. struct bound_box ABox;
  33. struct ilist *List;
  34. int ndupl;
  35. APoints = Vect_new_line_struct();
  36. BPoints = Vect_new_line_struct();
  37. ACats = Vect_new_cats_struct();
  38. BCats = Vect_new_cats_struct();
  39. Cats = Vect_new_cats_struct();
  40. List = Vect_new_list();
  41. nlines = Vect_get_num_lines(Map);
  42. G_debug(1, "nlines = %d", nlines);
  43. /* Go through all lines in vector, for each select lines which overlap MBR of
  44. * this line and check if some of them is identical. If someone is identical
  45. * remove current line. (In each step just one line is deleted)
  46. */
  47. ndupl = 0;
  48. for (i = 1; i <= nlines; i++) {
  49. G_percent(i, nlines, 1);
  50. if (!Vect_line_alive(Map, i))
  51. continue;
  52. atype = Vect_read_line(Map, APoints, ACats, i);
  53. if (!(atype & type))
  54. continue;
  55. Vect_line_box(APoints, &ABox);
  56. Vect_select_lines_by_box(Map, &ABox, type, List);
  57. G_debug(3, " %d lines selected by box", List->n_values);
  58. for (j = 0; j < List->n_values; j++) {
  59. bline = List->value[j];
  60. G_debug(3, " j = %d bline = %d", j, bline);
  61. if (i == bline)
  62. continue;
  63. btype = Vect_read_line(Map, BPoints, BCats, bline);
  64. /* check for duplicates */
  65. if (!Vect_line_check_duplicate(APoints, BPoints, Vect_is_3d(Map)))
  66. continue;
  67. /* Lines area identical -> remove current */
  68. if (Err) {
  69. Vect_write_line(Err, atype, APoints, ACats);
  70. }
  71. Vect_delete_line(Map, i);
  72. /* Merge categories */
  73. nbcats_orig = BCats->n_cats;
  74. for (c = 0; c < ACats->n_cats; c++)
  75. Vect_cat_set(BCats, ACats->field[c], ACats->cat[c]);
  76. if (BCats->n_cats > nbcats_orig) {
  77. G_debug(4, "cats merged: n_cats %d -> %d", nbcats_orig,
  78. BCats->n_cats);
  79. Vect_rewrite_line(Map, bline, btype, BPoints, BCats);
  80. }
  81. ndupl++;
  82. break; /* line was deleted -> take the next one */
  83. }
  84. nlines = Vect_get_num_lines(Map); /* For future when lines with cats will be rewritten */
  85. G_debug(3, "nlines = %d\n", nlines);
  86. }
  87. G_verbose_message("Removed duplicates: %d", ndupl);
  88. }
  89. /*!
  90. \brief Check for duplicate lines
  91. \param APoints first line geometry
  92. \param BPoints second line geometry
  93. \return 1 duplicate
  94. \return 0 not duplicate
  95. */
  96. int Vect_line_check_duplicate(const struct line_pnts *APoints,
  97. const struct line_pnts *BPoints, int with_z)
  98. {
  99. int k;
  100. int npoints;
  101. int forw, backw;
  102. if (APoints->n_points != BPoints->n_points)
  103. return 0;
  104. npoints = APoints->n_points;
  105. /* Forward */
  106. forw = 1;
  107. for (k = 0; k < APoints->n_points; k++) {
  108. if (APoints->x[k] != BPoints->x[k] ||
  109. APoints->y[k] != BPoints->y[k] ||
  110. (with_z && APoints->z[k] != BPoints->z[k])) {
  111. forw = 0;
  112. break;
  113. }
  114. }
  115. /* Backward */
  116. backw = 1;
  117. for (k = 0; k < APoints->n_points; k++) {
  118. if (APoints->x[k] != BPoints->x[npoints - k - 1] ||
  119. APoints->y[k] != BPoints->y[npoints - k - 1] ||
  120. (with_z && APoints->z[k] != BPoints->z[npoints - k - 1])) {
  121. backw = 0;
  122. break;
  123. }
  124. }
  125. if (!forw && !backw)
  126. return 0;
  127. return 1;
  128. }