File: nodes.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 (100 lines) | stat: -rw-r--r-- 2,781 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
/***************************************************************
 *
 * MODULE:       v.net
 * 
 * AUTHOR(S):    Radim Blazek
 *               
 * PURPOSE:      Network maintenance
 *               
 * 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"
#include "proto.h"

int nodes ( char *in, char *out)
{
    int    i, node, nnodes, line, nlines, count, type, found;
    double x, y, z;
    struct Map_info In, Out;
    char   *mapset;
    struct line_pnts *Points, *Pout;
    struct line_cats *Cats;

    mapset = G_find_vector2 (in, "");
    if (mapset == NULL) 
        G_fatal_error( "Vector file [%s] not available", in );

    Vect_set_open_level (2);
    Vect_open_old (&In, in, mapset);

    Vect_set_fatal_error (GV_FATAL_PRINT);
    if (1 > Vect_open_new (&Out, out, In.head.with_z)){
        Vect_close (&In);
	G_fatal_error ("Failed opening output vector file");
    }

    Vect_copy_head_data (&In, &Out);
    Vect_hist_copy (&In, &Out);
    Vect_hist_command ( &Out );

    Points = Vect_new_line_struct ();
    Pout = Vect_new_line_struct ();
    
    Cats = Vect_new_cats_struct ();
    
    /* Rewrite all primitives to output file */
    while ( (type = Vect_read_next_line (&In, Points, Cats)) >= 0) {
        Vect_write_line (&Out, type, Points, Cats);      
    }
    
    /* Go thorough all nodes in old map and write a new point if missing */    
    nnodes = Vect_get_num_nodes ( &In );
    Vect_reset_cats (Cats);
    count = 0;
    for ( node = 1; node <= nnodes; node++ ) {
	int has_lines = 0;
	
	nlines = Vect_get_node_n_lines ( &In, node );
	found = 0;
	for ( i = 0; i < nlines; i++ ) {
	    line = abs ( Vect_get_node_line ( &In, node, i ) );
            type = Vect_read_line (&In, NULL, NULL, line);
	    if ( type == GV_POINT ) {
		found = 1;
	    }
	    if ( type & GV_LINES ) {
		has_lines = 1;
	    }
	}
	if ( has_lines && !found ) { /* Write new point */
	    Vect_reset_line ( Pout );    
	    Vect_get_node_coor ( &In, node, &x, &y, &z);
	    Vect_append_point ( Pout, x, y, z);
	    Vect_write_line ( &Out, GV_POINT, Pout, Cats);
	    count++;
	}
    }
    fprintf (stderr, "%d new points written to output.\n", count );
    
    Vect_destroy_line_struct (Points);
    Vect_destroy_line_struct (Pout);
    Vect_destroy_cats_struct (Cats);

    Vect_copy_tables ( &In, &Out, 0 );
    
    /* Support */    
    Vect_build (&Out, stdout); 
    
    Vect_close (&In);
    Vect_close (&Out);
    
    return 0;
}