File: remove_duplicates.c

package info (click to toggle)
grass 6.0.2-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 40,044 kB
  • ctags: 31,303
  • sloc: ansic: 321,125; tcl: 25,676; sh: 11,176; cpp: 10,098; makefile: 5,025; fortran: 1,846; yacc: 493; lex: 462; perl: 133; sed: 1
file content (138 lines) | stat: -rw-r--r-- 4,274 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* **************************************************************
 * 
 * MODULE:       vector library
 * 
 * AUTHOR(S):    Radim Blazek
 *               
 * PURPOSE:      Clean lines
 *               
 * COPYRIGHT:    (C) 2001 by the GRASS Development Team
 *
 *               This program is free software under the 
 *               GNU General Public License (>=v2). 
 *               Read the file COPYING that comes with GRASS
 *               for details.
 *
 **************************************************************/
#include <stdlib.h> 
#include "gis.h"
#include "Vect.h"

/*!
 \fn void Vect_remove_duplicates ( struct Map_info *Map, int type, struct Map_info *Err, FILE *msgout)
 \brief Remove duplicate lines from vector map.

 Remove duplicate lines of given types from vector map. Duplicate lines may be optionaly 
 written to error map. Input map must be opened on level 2 for update. Categories are merged.

 \param Map input map where duplicate lines will be deleted
 \param type type of line to be delete
 \param Err vector map where duplicate lines will be written or NULL
 \param msgout file pointer where messages will be written or NULL
 \return
*/
void 
Vect_remove_duplicates ( struct Map_info *Map, int type, struct Map_info *Err, FILE *msgout )
{
	struct line_pnts *APoints, *BPoints;
	struct line_cats *ACats, *BCats, *Cats;
        int    i, j, k, c, atype, btype, bline;
	int    nlines, npoints, nbcats_orig;
	BOUND_BOX  ABox; 
	struct ilist *List; 
	int ndupl;
	int forw, backw;

	
        APoints = Vect_new_line_struct ();
        BPoints = Vect_new_line_struct ();
	ACats = Vect_new_cats_struct ();
	BCats = Vect_new_cats_struct ();
	Cats = Vect_new_cats_struct ();
	List = Vect_new_list ();
	
	nlines = Vect_get_num_lines (Map);

        G_debug (1, "nlines =  %d", nlines );
	/* Go through all lines in vector, for each select lines which overlap MBR of
	*  this line and check if some of them is identical. If someone is identical
	*  remove current line. (In each step just one line is deleted)
	*/
        /* TODO: 3D */
	ndupl = 0;
	if ( msgout ) fprintf (msgout, "Duplicates: %5d", ndupl ); 
	for ( i = 1; i <= nlines; i++ ){ 
	    if ( !Vect_line_alive ( Map, i ) ) continue;

	    atype = Vect_read_line (Map, APoints, ACats, i);
	    if ( !(atype & type) ) continue;

	    Vect_line_box ( APoints, &ABox ); 
	    Vect_select_lines_by_box ( Map, &ABox, type, List);
            G_debug (3, "  %d lines selected by box", List->n_values);
	    
	    for ( j = 0; j <  List->n_values; j++ ){ 
		bline = List->value[j];
                G_debug (3, "  j = %d bline = %d", j, bline);
		if ( i == bline ) continue; 
		
	        btype = Vect_read_line (Map, BPoints, BCats, bline);
	
		/* Check if the lines are identical */
		if ( APoints->n_points != BPoints->n_points ) continue;

		npoints = APoints->n_points;
		/* Forward */
		forw = 1;
	        for ( k = 0; k <  APoints->n_points; k++ ){ 
                    if ( APoints->x[k] != BPoints->x[k] || APoints->y[k] != BPoints->y[k] ) {
                        forw = 0; break;
		    }	
		}
		
		/* Backward */
		backw = 1;
	        for ( k = 0; k <  APoints->n_points; k++ ){ 
                    if ( APoints->x[k] != BPoints->x[npoints - k - 1] || 
			 APoints->y[k] != BPoints->y[npoints - k - 1] ) {
                        backw = 0; break;
		    }	
		}
		
		if ( !forw && !backw ) continue;

		/* Lines area identical -> remove current */
		if ( Err ) {
		    Vect_write_line ( Err, atype, APoints, ACats );
		}

		Vect_delete_line (Map, i); 

		/* Merge categories */
		nbcats_orig = BCats->n_cats;
		
		for ( c = 0; c < ACats->n_cats; c++ ) 
		    Vect_cat_set ( BCats, ACats->field[c], ACats->cat[c] );
		
		if ( BCats->n_cats > nbcats_orig ) {
		    G_debug ( 4, "cats merged: n_cats %d -> %d", nbcats_orig, BCats->n_cats );
		    Vect_rewrite_line ( Map, bline, btype, BPoints, BCats );
		}
		
		ndupl++;
		
		if ( msgout ) {
		    fprintf (stderr, "\rDuplicates: %5d", ndupl ); 
		    fflush ( stderr );
		}
		
		break; /* line was deleted -> take the next one */
	    }
	    nlines = Vect_get_num_lines (Map); /* For future when lines with cats will be rewritten */
	    G_debug (3, "nlines =  %d\n", nlines );
	}
	if ( msgout ) fprintf (stderr, "\n" ); 

	return;
}