overlay.c

Go to the documentation of this file.
00001 /*
00002 ****************************************************************************
00003 *
00004 * MODULE:       Vector library 
00005 *               
00006 * AUTHOR(S):    Radim Blazek
00007 *
00008 * PURPOSE:      Higher level functions for reading/writing/manipulating vectors.
00009 *
00010 * COPYRIGHT:    (C) 2001 by the GRASS Development Team
00011 *
00012 *               This program is free software under the GNU General Public
00013 *               License (>=v2). Read the file COPYING that comes with GRASS
00014 *               for details.
00015 *
00016 *****************************************************************************/
00017 #include <string.h>
00018 #include <grass/Vect.h>
00019 
00020 /* This is file is just example and starting point for writing overlay functions!!! */
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, /* map A */
00057                struct Map_info *BMap, int btype, struct ilist *BList, struct ilist *BAList, /* map B */
00058                int operator, 
00059                struct Map_info *OMap ) /* output map */
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     /* TODO: support Lists */
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     /* TODO: support all types; at present only point x point, area x point and point x area supported  */
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     /* TODO: more points in one node in one map */
00111     
00112     /* point x point: select all points with identical coordinates in both maps */
00113     if ( ( atype & GV_POINTS ) && (btype & GV_POINTS) ) { /* both points and centroids */
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                 /* Identical points found -> write out */ 
00131                 /* TODO: do something if fields in ACats and BCats are identical */
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                 /* TODO: what to do if one type is GV_POINT and second GV_CENTROID */
00139                 oltype = altype;
00140                 Vect_write_line ( OMap, oltype, Points, OCats );
00141                 Vect_list_append ( AOList, i);    /* add to list of written lines */    
00142                 Vect_list_append ( BOList, line);       
00143                 break;
00144             }
00145         }
00146     }
00147 
00148     /* TODO: check only labeled areas */
00149     /* point x area: select points from A in areas in B */
00150     if ( ( atype & GV_POINTS ) && (btype & GV_AREA) ) { /* both points and centroids */
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             /* TODO: do something if fields in ACats and BCats are identical */
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             /* Check if not yet written */
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     /* area x point: select points from B in areas in A */
00182     if ( ( btype & GV_POINTS ) && (atype & GV_AREA) ) { /* both points and centroids */
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             /* TODO: do something if fields in ACats and BCats are identical */
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             /* Check if not yet written */
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 

Generated on Sun Apr 6 17:32:44 2008 for GRASS by  doxygen 1.5.5