00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <string.h>
00018 #include <grass/Vect.h>
00019
00020
00021 int Vect_overlay_and ( struct Map_info *, int, struct ilist *, struct ilist *,
00022 struct Map_info *, int, struct ilist *, struct ilist *,
00023 struct Map_info *);
00024
00025
00032 int
00033 Vect_overlay_str_to_operator ( char *str )
00034 {
00035
00036 if ( strcmp ( str, GV_ON_AND ) == 0 )
00037 return GV_O_AND;
00038 else if ( strcmp ( str, GV_ON_OVERLAP ) == 0 )
00039 return GV_O_OVERLAP;
00040
00041 return -1;
00042 }
00043
00044
00055 int
00056 Vect_overlay ( struct Map_info *AMap, int atype, struct ilist *AList, struct ilist *AAList,
00057 struct Map_info *BMap, int btype, struct ilist *BList, struct ilist *BAList,
00058 int operator,
00059 struct Map_info *OMap )
00060 {
00061 switch (operator) {
00062 case GV_O_AND:
00063 Vect_overlay_and ( AMap, atype, AList, AAList, BMap, btype, BList, BAList, OMap );
00064 break;
00065 default:
00066 G_fatal_error (" Vect_overlay(): unknown operator" );
00067 }
00068
00069 return 0;
00070 }
00071
00084 int
00085 Vect_overlay_and ( struct Map_info *AMap, int atype, struct ilist *AList, struct ilist *AAList,
00086 struct Map_info *BMap, int btype, struct ilist *BList, struct ilist *BAList,
00087 struct Map_info *OMap )
00088 {
00089 int i, j, k, node, line, altype, bltype, oltype, area, centr;
00090 struct line_pnts *Points;
00091 struct line_cats *ACats, *BCats, *OCats;
00092 struct ilist *AOList, *BOList;
00093
00094
00095
00096 Points = Vect_new_line_struct ();
00097 ACats = Vect_new_cats_struct ();
00098 BCats = Vect_new_cats_struct ();
00099 OCats = Vect_new_cats_struct ();
00100 AOList = Vect_new_list ();
00101 BOList = Vect_new_list ();
00102
00103
00104 if ( ( atype & GV_LINES ) || (btype & GV_LINES) )
00105 G_warning ("overlay: line/boundary types not supported by AND operator");
00106
00107 if ( ( atype & GV_AREA ) && (btype & GV_AREA) )
00108 G_warning ("overlay: area x area types not supported by AND operator");
00109
00110
00111
00112
00113 if ( ( atype & GV_POINTS ) && (btype & GV_POINTS) ) {
00114 G_debug ( 3, "overlay: AND: point x point");
00115 for ( i = 1; i <= Vect_get_num_lines ( AMap ); i++ ) {
00116 altype = Vect_read_line ( AMap, Points, ACats, i);
00117 if ( !(altype & GV_POINTS ) ) continue;
00118
00119 node = Vect_find_node ( BMap, Points->x[0], Points->y[0], Points->z[0], 0, 1);
00120 G_debug ( 3, "overlay: node = %d", node);
00121 if ( node == 0 ) continue;
00122
00123 Vect_reset_cats ( OCats );
00124
00125 for ( j = 0; j < Vect_get_node_n_lines ( BMap, node); j++ ) {
00126 line = Vect_get_node_line ( BMap, node, j );
00127 bltype = Vect_read_line ( BMap, NULL, BCats, line);
00128 if ( !( bltype & GV_POINTS ) ) continue;
00129
00130
00131
00132 for ( k = 0; k < ACats->n_cats; k++ )
00133 Vect_cat_set (OCats, ACats->field[k], ACats->cat[k]);
00134
00135 for ( k = 0; k < BCats->n_cats; k++ )
00136 Vect_cat_set (OCats, BCats->field[k], BCats->cat[k]);
00137
00138
00139 oltype = altype;
00140 Vect_write_line ( OMap, oltype, Points, OCats );
00141 Vect_list_append ( AOList, i);
00142 Vect_list_append ( BOList, line);
00143 break;
00144 }
00145 }
00146 }
00147
00148
00149
00150 if ( ( atype & GV_POINTS ) && (btype & GV_AREA) ) {
00151 G_debug ( 3, "overlay: AND: point x area");
00152
00153 for ( i = 1; i <= Vect_get_num_lines ( AMap ); i++ ) {
00154 altype = Vect_read_line ( AMap, Points, ACats, i);
00155 if ( !(altype & GV_POINTS ) ) continue;
00156
00157 area = Vect_find_area ( BMap, Points->x[0], Points->y[0] );
00158 if ( area == 0 ) continue;
00159
00160 Vect_reset_cats ( OCats );
00161
00162
00163 for ( k = 0; k < ACats->n_cats; k++ )
00164 Vect_cat_set (OCats, ACats->field[k], ACats->cat[k]);
00165
00166 centr = Vect_get_area_centroid ( BMap, area );
00167 if ( centr > 0 ) {
00168 bltype = Vect_read_line ( BMap, NULL, BCats, centr);
00169 for ( k = 0; k < BCats->n_cats; k++ )
00170 Vect_cat_set (OCats, BCats->field[k], BCats->cat[k]);
00171 }
00172
00173
00174 if ( !(Vect_val_in_list ( AOList, i ) ) ) {
00175 Vect_write_line ( OMap, altype, Points, OCats );
00176 Vect_list_append ( AOList, i);
00177 }
00178
00179 }
00180 }
00181
00182 if ( ( btype & GV_POINTS ) && (atype & GV_AREA) ) {
00183 G_debug ( 3, "overlay: AND: area x point");
00184
00185 for ( i = 1; i <= Vect_get_num_lines ( BMap ); i++ ) {
00186 bltype = Vect_read_line ( BMap, Points, BCats, i);
00187 if ( !(bltype & GV_POINTS ) ) continue;
00188
00189 area = Vect_find_area ( AMap, Points->x[0], Points->y[0] );
00190 if ( area == 0 ) continue;
00191
00192 Vect_reset_cats ( OCats );
00193
00194
00195 for ( k = 0; k < BCats->n_cats; k++ )
00196 Vect_cat_set (OCats, BCats->field[k], BCats->cat[k]);
00197
00198 centr = Vect_get_area_centroid ( AMap, area );
00199 if ( centr > 0 ) {
00200 altype = Vect_read_line ( AMap, NULL, ACats, centr);
00201 for ( k = 0; k < ACats->n_cats; k++ )
00202 Vect_cat_set (OCats, ACats->field[k], ACats->cat[k]);
00203 }
00204
00205
00206 if ( !(Vect_val_in_list ( BOList, i ) ) ) {
00207 Vect_write_line ( OMap, bltype, Points, OCats );
00208 Vect_list_append ( BOList, i);
00209 }
00210
00211 }
00212 }
00213
00214 return 0;
00215 }
00216