remove_duplicates.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 optionaly
  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. \param msgout file pointer where messages will be written or NULL
  25. \return void
  26. */
  27. void
  28. Vect_remove_duplicates ( struct Map_info *Map, int type, struct Map_info *Err, FILE *msgout )
  29. {
  30. struct line_pnts *APoints, *BPoints;
  31. struct line_cats *ACats, *BCats, *Cats;
  32. int i, j, c, atype, btype, bline;
  33. int nlines, nbcats_orig;
  34. BOUND_BOX ABox;
  35. struct ilist *List;
  36. int ndupl;
  37. APoints = Vect_new_line_struct ();
  38. BPoints = Vect_new_line_struct ();
  39. ACats = Vect_new_cats_struct ();
  40. BCats = Vect_new_cats_struct ();
  41. Cats = Vect_new_cats_struct ();
  42. List = Vect_new_list ();
  43. nlines = Vect_get_num_lines (Map);
  44. G_debug (1, "nlines = %d", nlines );
  45. /* Go through all lines in vector, for each select lines which overlap MBR of
  46. * this line and check if some of them is identical. If someone is identical
  47. * remove current line. (In each step just one line is deleted)
  48. */
  49. ndupl = 0;
  50. if ( msgout )
  51. fprintf (msgout, "%s: %5d", _("Duplicates"), ndupl );
  52. for ( i = 1; i <= nlines; i++ ){
  53. if ( !Vect_line_alive ( Map, i ) ) continue;
  54. atype = Vect_read_line (Map, APoints, ACats, i);
  55. if ( !(atype & type) ) continue;
  56. Vect_line_box ( APoints, &ABox );
  57. Vect_select_lines_by_box ( Map, &ABox, type, List);
  58. G_debug (3, " %d lines selected by box", List->n_values);
  59. for ( j = 0; j < List->n_values; j++ ){
  60. bline = List->value[j];
  61. G_debug (3, " j = %d bline = %d", j, bline);
  62. if ( i == bline ) 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, BCats->n_cats );
  78. Vect_rewrite_line ( Map, bline, btype, BPoints, BCats );
  79. }
  80. ndupl++;
  81. if ( msgout ) {
  82. fprintf (msgout, "\r%s: %5d", _("Duplicates"), ndupl );
  83. fflush (msgout);
  84. }
  85. break; /* line was deleted -> take the next one */
  86. }
  87. nlines = Vect_get_num_lines (Map); /* For future when lines with cats will be rewritten */
  88. G_debug (3, "nlines = %d\n", nlines );
  89. }
  90. if ( msgout )
  91. fprintf (msgout, "\n");
  92. return;
  93. }
  94. /*!
  95. \brief Check for duplicate lines
  96. \param APoints first line geometry
  97. \param BPoints second line geometry
  98. \return 1 duplicate
  99. \return 0 not duplicate
  100. */
  101. int Vect_line_check_duplicate(const struct line_pnts *APoints,
  102. const struct line_pnts *BPoints, int with_z)
  103. {
  104. int k;
  105. int npoints;
  106. int forw, backw;
  107. if ( APoints->n_points != BPoints->n_points )
  108. return 0;
  109. npoints = APoints->n_points;
  110. /* Forward */
  111. forw = 1;
  112. for (k = 0; k < APoints->n_points; k++) {
  113. if (APoints->x[k] != BPoints->x[k] ||
  114. APoints->y[k] != BPoints->y[k] ||
  115. (with_z && APoints->z[k] != BPoints->z[k])) {
  116. forw = 0;
  117. break;
  118. }
  119. }
  120. /* Backward */
  121. backw = 1;
  122. for (k = 0; k < APoints->n_points; k++) {
  123. if (APoints->x[k] != BPoints->x[npoints - k - 1] ||
  124. APoints->y[k] != BPoints->y[npoints - k - 1] ||
  125. (with_z && APoints->z[k] != BPoints->z[npoints - k - 1])) {
  126. backw = 0;
  127. break;
  128. }
  129. }
  130. if (!forw && !backw)
  131. return 0;
  132. return 1;
  133. }