remove_duplicates.c

Go to the documentation of this file.
00001 /* **************************************************************
00002  * 
00003  * MODULE:       vector library
00004  * 
00005  * AUTHOR(S):    Radim Blazek
00006  *               
00007  * PURPOSE:      Clean lines
00008  *               
00009  * COPYRIGHT:    (C) 2001 by the GRASS Development Team
00010  *
00011  *               This program is free software under the 
00012  *               GNU General Public License (>=v2). 
00013  *               Read the file COPYING that comes with GRASS
00014  *               for details.
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         /* Go through all lines in vector, for each select lines which overlap MBR of
00058         *  this line and check if some of them is identical. If someone is identical
00059         *  remove current line. (In each step just one line is deleted)
00060         */
00061         /* TODO: 3D */
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                 /* Check if the lines are identical */
00082                 if ( APoints->n_points != BPoints->n_points ) continue;
00083 
00084                 npoints = APoints->n_points;
00085                 /* Forward */
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                 /* Backward */
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                 /* Lines area identical -> remove current */
00105                 if ( Err ) {
00106                     Vect_write_line ( Err, atype, APoints, ACats );
00107                 }
00108 
00109                 Vect_delete_line (Map, i); 
00110 
00111                 /* Merge categories */
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; /* line was deleted -> take the next one */
00130             }
00131             nlines = Vect_get_num_lines (Map); /* For future when lines with cats will be rewritten */
00132             G_debug (3, "nlines =  %d\n", nlines );
00133         }
00134         if ( msgout ) fprintf (stderr, "\n" ); 
00135 
00136         return;
00137 }
00138 

Generated on Sun Apr 6 17:32:44 2008 for GRASS by  doxygen 1.5.5