File: overlay.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 (216 lines) | stat: -rw-r--r-- 7,679 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
****************************************************************************
*
* MODULE:       Vector library 
*   	    	
* AUTHOR(S):    Radim Blazek
*
* PURPOSE:      Higher level functions for reading/writing/manipulating vectors.
*
* 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 <string.h>
#include "Vect.h"

/* This is file is just example and starting point for writing overlay functions!!! */
int Vect_overlay_and ( struct Map_info *, int, struct ilist *, struct ilist *,
	               struct Map_info *, int, struct ilist *, struct ilist *,
		       struct Map_info *);


/*!
 \fn int Vect_overlay_str_to_operator ( char *str )
 \brief Get operator code from string
 \return operator code, -1 on error
 \param operator code string
*/
int 
Vect_overlay_str_to_operator ( char *str )
{
    
    if ( strcmp ( str, GV_ON_AND ) == 0 )
	return GV_O_AND;
    else if ( strcmp ( str, GV_ON_OVERLAP ) == 0 )
	return GV_O_OVERLAP;
    
    return -1;
}
    

/*!
 \fn int Vect_overlay ( struct Map_info *AMap, int atype, struct ilist *AList, struct ilist *AAList,
               struct Map_info *BMap, int btype, struct ilist *BList, struct ilist *BAList,
	       int operator,  struct Map_info *OMap )

 \brief Overlay 2 maps and create new one
 \return 0 on success,?? on error
 \param AMap, atype, AList, AAList, BMap, btype, BList, BAList,
   operator, OMap
*/
int 
Vect_overlay ( struct Map_info *AMap, int atype, struct ilist *AList, struct ilist *AAList, /* map A */
               struct Map_info *BMap, int btype, struct ilist *BList, struct ilist *BAList, /* map B */
	       int operator, 
               struct Map_info *OMap ) /* output map */
{
    switch (operator) {
        case GV_O_AND:
            Vect_overlay_and ( AMap, atype, AList, AAList, BMap, btype, BList, BAList, OMap );
            break;
	default:
	    G_fatal_error (" Vect_overlay(): unknown operator" );
    }
    
    return 0;
}

/*!
 \fn int Vect_overlay_and ( struct Map_info *AMap, int atype, struct ilist *AList, struct ilist *AAList,
                   struct Map_info *BMap, int btype, struct ilist *BList, struct ilist *BAList,
                   struct Map_info *OMap )
 \brief overlay 2 vector maps with AND.  AND supports:       point line area
               point  +     -    +
               line   -     -    -
               area   +     -    -
 \return 1 on success, 0 on error
 \param AMap,  atype,  AList, AAList, BMap, btype, BList, BAList,
                   OMap )
*/
int 
Vect_overlay_and ( struct Map_info *AMap, int atype, struct ilist *AList, struct ilist *AAList,
                   struct Map_info *BMap, int btype, struct ilist *BList, struct ilist *BAList,
                   struct Map_info *OMap )
{
    int i, j, k, node, line, altype, bltype, oltype, area, centr;
    struct line_pnts *Points;
    struct line_cats *ACats, *BCats, *OCats;
    struct ilist *AOList, *BOList;
    
    /* TODO: support Lists */
    
    Points = Vect_new_line_struct ();
    ACats = Vect_new_cats_struct ();
    BCats = Vect_new_cats_struct ();
    OCats = Vect_new_cats_struct ();
    AOList = Vect_new_list ();
    BOList = Vect_new_list ();
    
    /* TODO: support all types; at present only point x point, area x point and point x area supported  */
    if ( ( atype & GV_LINES ) || (btype & GV_LINES) )
        G_warning ("overlay: line/boundary types not supported by AND operator"); 
    
    if ( ( atype & GV_AREA ) && (btype & GV_AREA) )
        G_warning ("overlay: area x area types not supported by AND operator"); 
	
    /* TODO: more points in one node in one map */
    
    /* point x point: select all points with identical coordinates in both maps */
    if ( ( atype & GV_POINTS ) && (btype & GV_POINTS) ) { /* both points and centroids */
        G_debug ( 3, "overlay: AND: point x point"); 
        for ( i = 1; i <= Vect_get_num_lines ( AMap ); i++ ) {
	    altype = Vect_read_line ( AMap, Points, ACats, i);
	    if ( !(altype & GV_POINTS ) ) continue;
		    
	    node = Vect_find_node ( BMap, Points->x[0], Points->y[0], Points->z[0], 0, 1);
            G_debug ( 3, "overlay: node = %d", node); 
	    if ( node == 0 ) continue;
	    
	    Vect_reset_cats ( OCats );
	    
	    for ( j = 0; j < Vect_get_node_n_lines ( BMap, node); j++ ) {
	        line = Vect_get_node_line ( BMap, node, j );
	        bltype = Vect_read_line ( BMap, NULL, BCats, line);
	        if ( !( bltype & GV_POINTS ) ) continue; 
	       
		/* Identical points found -> write out */ 
		/* TODO: do something if fields in ACats and BCats are identical */
		for ( k = 0; k < ACats->n_cats; k++ )    
                    Vect_cat_set (OCats, ACats->field[k], ACats->cat[k]);
		
		for ( k = 0; k < BCats->n_cats; k++ )    
                    Vect_cat_set (OCats, BCats->field[k], BCats->cat[k]);

		/* TODO: what to do if one type is GV_POINT and second GV_CENTROID */
		oltype = altype;
		Vect_write_line ( OMap, oltype, Points, OCats );
	        Vect_list_append ( AOList, i);    /* add to list of written lines */	
	        Vect_list_append ( BOList, line);	
		break;
	    }
        }
    }

    /* TODO: check only labeled areas */
    /* point x area: select points from A in areas in B */
    if ( ( atype & GV_POINTS ) && (btype & GV_AREA) ) { /* both points and centroids */
	G_debug ( 3, "overlay: AND: point x area"); 
	
	for ( i = 1; i <= Vect_get_num_lines ( AMap ); i++ ) {
	    altype = Vect_read_line ( AMap, Points, ACats, i);
	    if ( !(altype & GV_POINTS ) ) continue;
		    
	    area = Vect_find_area ( BMap, Points->x[0], Points->y[0] );
	    if ( area == 0 ) continue;
	    
	    Vect_reset_cats ( OCats );
	    
	    /* TODO: do something if fields in ACats and BCats are identical */
	    for ( k = 0; k < ACats->n_cats; k++ )    
		Vect_cat_set (OCats, ACats->field[k], ACats->cat[k]);
	    
	    centr = Vect_get_area_centroid ( BMap, area );  
	    if ( centr > 0 ) {
		bltype = Vect_read_line ( BMap, NULL, BCats, centr);
		for ( k = 0; k < BCats->n_cats; k++ )    
		    Vect_cat_set (OCats, BCats->field[k], BCats->cat[k]);
	    }

	    /* Check if not yet written */
	    if ( !(Vect_val_in_list ( AOList, i ) )  ) { 
		Vect_write_line ( OMap, altype, Points, OCats );
		Vect_list_append ( AOList, i);    	
	    }
	    
	}
    }
    /* area x point: select points from B in areas in A */
    if ( ( btype & GV_POINTS ) && (atype & GV_AREA) ) { /* both points and centroids */
	G_debug ( 3, "overlay: AND: area x point"); 
	
	for ( i = 1; i <= Vect_get_num_lines ( BMap ); i++ ) {
	    bltype = Vect_read_line ( BMap, Points, BCats, i);
	    if ( !(bltype & GV_POINTS ) ) continue;
		    
	    area = Vect_find_area ( AMap, Points->x[0], Points->y[0] );
	    if ( area == 0 ) continue;
	    
	    Vect_reset_cats ( OCats );
	    
	    /* TODO: do something if fields in ACats and BCats are identical */
	    for ( k = 0; k < BCats->n_cats; k++ )    
		Vect_cat_set (OCats, BCats->field[k], BCats->cat[k]);
	    
	    centr = Vect_get_area_centroid ( AMap, area );  
	    if ( centr > 0 ) {
		altype = Vect_read_line ( AMap, NULL, ACats, centr);
		for ( k = 0; k < ACats->n_cats; k++ )    
		    Vect_cat_set (OCats, ACats->field[k], ACats->cat[k]);
	    }

	    /* Check if not yet written */
	    if ( !(Vect_val_in_list ( BOList, i ) )  ) { 
		Vect_write_line ( OMap, bltype, Points, OCats );
		Vect_list_append ( BOList, i);    	
	    }
	    
	}
    }

    return 0;
}