File: lines.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 (154 lines) | stat: -rw-r--r-- 4,431 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <stdio.h>
#include <stdlib.h>
#include "Vect.h"
#include "global.h"

/* Read area cats for one side 
 *  val - pointer where value is stored
 *  count - pointer to count of cats read 
 *  ACats - area categories
 */
void read_side_cats ( struct line_cats *ACats, int *val, int *count )
{
    int i, found;

    G_debug ( 4, "read_side_cats() n_cats = %d, val = %d, count = %d", ACats->n_cats, *val, *count );
    
    if ( *count > 1 ) return; /* it doesn't make sense to read/check more cats */

    found = 0;

    for ( i = 0; i < ACats->n_cats; i++ ) {
	if ( ACats->field[i] == options.qfield ) {
	    found = 1;
	    if ( *count == 0 ) { /* first */
		*val = ACats->cat[i]; /* set value to first found cat */
		(*count)++;
	    } else { /* *count == 1 */
		/* Check if it is the same category */
		if ( *val != ACats->cat[i] ) {
		    *count = 2;
		    break; /* no need to read more */
		}
	    }
	} 
    }
    if ( !found ) { /* i.e. area cat is NULL (-1) */
	if ( *count == 0 ) { /* first */
	    *val = -1;
	    (*count)++;
	} else { /* *count == 1 */
	    /* Check if it is the same category */
	    if ( *val != -1 ) {
		*count = 2;
	    }
	}
    }
}

/* 
 * Read: - points/centroids : cat,count,coor
 *       - lines/boundaries : cat,count,length
 */

int 
read_lines(struct Map_info *Map )
{
    int    i, idx, nlines, type, found;
    register int line_num;
    struct line_pnts *Points;
    struct line_cats *Cats, *LCats, *RCats;
    double len;

    /* Initialize the Point struct */
    Points = Vect_new_line_struct();
    Cats = Vect_new_cats_struct ();
    LCats = Vect_new_cats_struct ();
    RCats = Vect_new_cats_struct ();

    /* Cycle through all lines */
    nlines = Vect_get_num_lines ( Map );
    for (line_num = 1 ; line_num <= nlines; line_num++)
    {
	type = Vect_read_line ( Map, Points, Cats, line_num);
	if ( !(type & options.type ) ) continue;

	found = 0;

	/* Find left/right area cats if necessary */
	if ( options.option == O_SIDES && type == GV_BOUNDARY ) {
	    int area_left, area_right, centroid;

	    Vect_get_line_areas ( Map, line_num, &area_left, &area_right );

	    /* left */
	    Vect_reset_cats ( LCats );
	    if ( area_left < 0 ) 
		area_left = Vect_get_isle_area ( Map, abs(area_left) ); 

	    if ( area_left > 0 ) { 
		centroid = Vect_get_area_centroid ( Map, area_left );
		if ( centroid > 0 ) {
		    Vect_read_line ( Map, NULL, LCats, centroid);
		}
	    } 
	     
	    /* right */
	    Vect_reset_cats ( RCats );
	    if ( area_right < 0 ) 
		area_right = Vect_get_isle_area ( Map, abs(area_right) ); 

	    if ( area_right > 0 ) { 
		centroid = Vect_get_area_centroid ( Map, area_right );
		if ( centroid > 0 ) {
		    Vect_read_line ( Map, NULL, RCats, centroid);
		}
	    } 
	}

	for ( i = 0; i < Cats->n_cats; i++ ) {
	    if ( Cats->field[i] == options.field ) {
		idx = find_cat(Cats->cat[i]);
		if (  options.option == O_COUNT ) {
		    Values[idx].count1++;
		} else if ( options.option == O_LENGTH && (type & GV_LINES) ) {
		    /* Calculate line length */
		    len = length (Points->n_points, Points->x, Points->y);
		    Values[idx].d1 += len;
		} else if ( options.option == O_COOR && (type & GV_POINTS) ) {
		    /* overwrite by last one, count is used in update */ 
		    Values[idx].d1 = Points->x[0];
		    Values[idx].d2 = Points->y[0];
		    Values[idx].d3 = Points->z[0];
		    Values[idx].count1++;
		} else if ( options.option == O_SIDES && type == GV_BOUNDARY ) {
		    
                    read_side_cats ( LCats, &(Values[idx].i1), &(Values[idx].count1) );
                    read_side_cats ( RCats, &(Values[idx].i2), &(Values[idx].count2) );
		}
		found = 1;
	    }
	}

	if ( !found ) {  /* Values for no category (cat = -1) are reported at the end */
	    idx = find_cat(-1);
	    if (  options.option == O_COUNT ) {
		Values[idx].count1++;
	    } else if ( options.option == O_LENGTH && (type & GV_LINES) ) {
		len = length (Points->n_points, Points->x, Points->y);
		Values[idx].d1 += len;
	    } else if ( options.option == O_COOR && (type & GV_POINTS) ) {
		Values[idx].d1 = Points->x[0];
		Values[idx].d2 = Points->y[0];
		Values[idx].d3 = Points->z[0];
		Values[idx].count1++;
	    } else if ( options.option == O_SIDES && type == GV_BOUNDARY ) {
		read_side_cats ( LCats, &(Values[idx].i1), &(Values[idx].count1) );
		read_side_cats ( RCats, &(Values[idx].i2), &(Values[idx].count2) );
	    }
	}
    }

    return 0;
}