remove_duplicates.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*!
  2. \file remove_duplicates.c
  3. \brief Vector library - clean geometry (remove duplicates)
  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
  12. */
  13. #include <stdlib.h>
  14. #include <grass/gis.h>
  15. #include <grass/Vect.h>
  16. #include <grass/glocale.h>
  17. /*!
  18. \brief Remove duplicate lines from vector map.
  19. Remove duplicate lines of given types from vector map. Duplicate lines may be optionally
  20. written to error map. Input map must be opened on level 2 for update. Categories are merged.
  21. \param Map vector map where duplicate lines will be deleted
  22. \param type type of line to be delete
  23. \param Err vector map where duplicate lines will be written or NULL
  24. \return void
  25. */
  26. void
  27. Vect_remove_duplicates(struct Map_info *Map, int type, struct Map_info *Err)
  28. {
  29. struct line_pnts *APoints, *BPoints;
  30. struct line_cats *ACats, *BCats, *Cats;
  31. int i, j, c, atype, btype, bline;
  32. int nlines, nbcats_orig;
  33. BOUND_BOX ABox;
  34. struct ilist *List;
  35. int ndupl;
  36. APoints = Vect_new_line_struct();
  37. BPoints = Vect_new_line_struct();
  38. ACats = Vect_new_cats_struct();
  39. BCats = Vect_new_cats_struct();
  40. Cats = Vect_new_cats_struct();
  41. List = Vect_new_list();
  42. nlines = Vect_get_num_lines(Map);
  43. G_debug(1, "nlines = %d", nlines);
  44. /* Go through all lines in vector, for each select lines which overlap MBR of
  45. * this line and check if some of them is identical. If someone is identical
  46. * remove current line. (In each step just one line is deleted)
  47. */
  48. ndupl = 0;
  49. for (i = 1; i <= nlines; i++) {
  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. }
  88. /*!
  89. \brief Check for duplicate lines
  90. \param APoints first line geometry
  91. \param BPoints second line geometry
  92. \return 1 duplicate
  93. \return 0 not duplicate
  94. */
  95. int Vect_line_check_duplicate(const struct line_pnts *APoints,
  96. const struct line_pnts *BPoints, int with_z)
  97. {
  98. int k;
  99. int npoints;
  100. int forw, backw;
  101. if (APoints->n_points != BPoints->n_points)
  102. return 0;
  103. npoints = APoints->n_points;
  104. /* Forward */
  105. forw = 1;
  106. for (k = 0; k < APoints->n_points; k++) {
  107. if (APoints->x[k] != BPoints->x[k] ||
  108. APoints->y[k] != BPoints->y[k] ||
  109. (with_z && APoints->z[k] != BPoints->z[k])) {
  110. forw = 0;
  111. break;
  112. }
  113. }
  114. /* Backward */
  115. backw = 1;
  116. for (k = 0; k < APoints->n_points; k++) {
  117. if (APoints->x[k] != BPoints->x[npoints - k - 1] ||
  118. APoints->y[k] != BPoints->y[npoints - k - 1] ||
  119. (with_z && APoints->z[k] != BPoints->z[npoints - k - 1])) {
  120. backw = 0;
  121. break;
  122. }
  123. }
  124. if (!forw && !backw)
  125. return 0;
  126. return 1;
  127. }