00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <stdlib.h>
00018 #include <grass/gis.h>
00019 #include <grass/Vect.h>
00020
00034 void
00035 Vect_remove_duplicates ( struct Map_info *Map, int type, struct Map_info *Err, FILE *msgout )
00036 {
00037 struct line_pnts *APoints, *BPoints;
00038 struct line_cats *ACats, *BCats, *Cats;
00039 int i, j, k, c, atype, btype, bline;
00040 int nlines, npoints, nbcats_orig;
00041 BOUND_BOX ABox;
00042 struct ilist *List;
00043 int ndupl;
00044 int forw, backw;
00045
00046
00047 APoints = Vect_new_line_struct ();
00048 BPoints = Vect_new_line_struct ();
00049 ACats = Vect_new_cats_struct ();
00050 BCats = Vect_new_cats_struct ();
00051 Cats = Vect_new_cats_struct ();
00052 List = Vect_new_list ();
00053
00054 nlines = Vect_get_num_lines (Map);
00055
00056 G_debug (1, "nlines = %d", nlines );
00057
00058
00059
00060
00061
00062 ndupl = 0;
00063 if ( msgout ) fprintf (msgout, "Duplicates: %5d", ndupl );
00064 for ( i = 1; i <= nlines; i++ ){
00065 if ( !Vect_line_alive ( Map, i ) ) continue;
00066
00067 atype = Vect_read_line (Map, APoints, ACats, i);
00068 if ( !(atype & type) ) continue;
00069
00070 Vect_line_box ( APoints, &ABox );
00071 Vect_select_lines_by_box ( Map, &ABox, type, List);
00072 G_debug (3, " %d lines selected by box", List->n_values);
00073
00074 for ( j = 0; j < List->n_values; j++ ){
00075 bline = List->value[j];
00076 G_debug (3, " j = %d bline = %d", j, bline);
00077 if ( i == bline ) continue;
00078
00079 btype = Vect_read_line (Map, BPoints, BCats, bline);
00080
00081
00082 if ( APoints->n_points != BPoints->n_points ) continue;
00083
00084 npoints = APoints->n_points;
00085
00086 forw = 1;
00087 for ( k = 0; k < APoints->n_points; k++ ){
00088 if ( APoints->x[k] != BPoints->x[k] || APoints->y[k] != BPoints->y[k] ) {
00089 forw = 0; break;
00090 }
00091 }
00092
00093
00094 backw = 1;
00095 for ( k = 0; k < APoints->n_points; k++ ){
00096 if ( APoints->x[k] != BPoints->x[npoints - k - 1] ||
00097 APoints->y[k] != BPoints->y[npoints - k - 1] ) {
00098 backw = 0; break;
00099 }
00100 }
00101
00102 if ( !forw && !backw ) continue;
00103
00104
00105 if ( Err ) {
00106 Vect_write_line ( Err, atype, APoints, ACats );
00107 }
00108
00109 Vect_delete_line (Map, i);
00110
00111
00112 nbcats_orig = BCats->n_cats;
00113
00114 for ( c = 0; c < ACats->n_cats; c++ )
00115 Vect_cat_set ( BCats, ACats->field[c], ACats->cat[c] );
00116
00117 if ( BCats->n_cats > nbcats_orig ) {
00118 G_debug ( 4, "cats merged: n_cats %d -> %d", nbcats_orig, BCats->n_cats );
00119 Vect_rewrite_line ( Map, bline, btype, BPoints, BCats );
00120 }
00121
00122 ndupl++;
00123
00124 if ( msgout ) {
00125 fprintf (stderr, "\rDuplicates: %5d", ndupl );
00126 fflush ( stderr );
00127 }
00128
00129 break;
00130 }
00131 nlines = Vect_get_num_lines (Map);
00132 G_debug (3, "nlines = %d\n", nlines );
00133 }
00134 if ( msgout ) fprintf (stderr, "\n" );
00135
00136 return;
00137 }
00138