GRASS Programmer's Manual  6.4.2(2012)
utils.c
Go to the documentation of this file.
00001 
00016 #include <stdio.h>
00017 #include <stdlib.h>
00018 #include <grass/gis.h>
00019 #include <grass/Vect.h>
00020 #include <grass/glocale.h>
00021 #include <grass/dbmi.h>
00022 #include <grass/neta.h>
00023 
00024 
00035 void NetA_add_point_on_node(struct Map_info *In, struct Map_info *Out,
00036                             int node, struct line_cats *Cats)
00037 {
00038     static struct line_pnts *Points;
00039     double x, y, z;
00040 
00041     Points = Vect_new_line_struct();
00042     Vect_get_node_coor(In, node, &x, &y, &z);
00043     Vect_reset_line(Points);
00044     Vect_append_point(Points, x, y, z);
00045     Vect_write_line(Out, GV_POINT, Points, Cats);
00046     Vect_destroy_line_struct(Points);
00047 }
00048 
00049 /* Returns the list of all points with the given category and field */
00050 /*void NetA_get_points_by_category(struct Map_info *In, int field, int cat, struct ilist *point_list)
00051  * {
00052  * int i, nlines;
00053  * struct line_cats *Cats;
00054  * Cats = Vect_new_cats_struct();
00055  * Vect_get_num_lines(In);
00056  * for(i=1;i<=nlines;i++){
00057  * int type = Vect_read_line(In, NULL, Cats, i);
00058  * if(type!=GV_POINT)continue;
00059  * }
00060  * 
00061  * Vect_destroy_cats_struct(Cats);
00062  * }
00063  */
00064 
00073 void NetA_points_to_nodes(struct Map_info *In, struct ilist *point_list)
00074 {
00075     int i, node;
00076 
00077     for (i = 0; i < point_list->n_values; i++) {
00078         Vect_get_line_nodes(In, point_list->value[i], &node, NULL);
00079         point_list->value[i] = node;
00080     }
00081 }
00082 
00102 int NetA_get_node_costs(struct Map_info *In, int layer, char *column,
00103                         int *node_costs)
00104 {
00105     int i, nlines, nnodes;
00106     dbCatValArray vals;
00107     struct line_cats *Cats;
00108     struct line_pnts *Points;
00109 
00110     dbDriver *driver;
00111     struct field_info *Fi;
00112 
00113     Fi = Vect_get_field(In, layer);
00114     driver = db_start_driver_open_database(Fi->driver, Fi->database);
00115     if (driver == NULL)
00116         G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
00117                       Fi->database, Fi->driver);
00118 
00119     nlines = Vect_get_num_lines(In);
00120     nnodes = Vect_get_num_nodes(In);
00121     Cats = Vect_new_cats_struct();
00122     Points = Vect_new_line_struct();
00123     for (i = 1; i <= nnodes; i++)
00124         node_costs[i] = 0;
00125 
00126     db_CatValArray_init(&vals);
00127 
00128     if (db_select_CatValArray(driver, Fi->table, Fi->key, column, NULL, &vals)
00129         == -1)
00130         return 0;
00131     for (i = 1; i <= nlines; i++) {
00132         int type = Vect_read_line(In, Points, Cats, i);
00133 
00134         if (type == GV_POINT) {
00135             int node, cat;
00136             double value;
00137 
00138             if (!Vect_cat_get(Cats, layer, &cat))
00139                 continue;
00140             Vect_get_line_nodes(In, i, &node, NULL);
00141             if (db_CatValArray_get_value_double(&vals, cat, &value) == DB_OK)
00142                 node_costs[node] = value * 1000000.0;
00143         }
00144     }
00145 
00146     Vect_destroy_cats_struct(Cats);
00147     db_CatValArray_free(&vals);
00148     db_close_database_shutdown_driver(driver);
00149     return 1;
00150 }
00151 
00166 void NetA_varray_to_nodes(struct Map_info *map, struct varray *varray,
00167                           struct ilist *nodes, int *nodes_to_features)
00168 {
00169     int nlines, nnodes, i;
00170 
00171     nlines = Vect_get_num_lines(map);
00172     nnodes = Vect_get_num_nodes(map);
00173     if (nodes_to_features)
00174         for (i = 1; i <= nnodes; i++)
00175             nodes_to_features[i] = -1;
00176 
00177     for (i = 1; i <= nlines; i++)
00178         if (varray->c[i]) {
00179             int type = Vect_read_line(map, NULL, NULL, i);
00180 
00181             if (type == GV_POINT) {
00182                 int node;
00183 
00184                 Vect_get_line_nodes(map, i, &node, NULL);
00185                 Vect_list_append(nodes, node);
00186                 if (nodes_to_features)
00187                     nodes_to_features[node] = i;
00188             }
00189             else {
00190                 int node1, node2;
00191 
00192                 Vect_get_line_nodes(map, i, &node1, &node2);
00193                 Vect_list_append(nodes, node1);
00194                 Vect_list_append(nodes, node2);
00195                 if (nodes_to_features)
00196                     nodes_to_features[node1] = nodes_to_features[node2] = i;
00197             }
00198         }
00199 }
00200 
00213 int NetA_initialise_varray(struct Map_info *In, int layer, int mask_type,
00214                            char *where, char *cat, struct varray **varray)
00215 {
00216     /* parse filter option and select appropriate lines */
00217     if (where) {
00218         if (layer < 1)
00219             G_fatal_error(_("'%s' must be > 0 for '%s'"), "layer", "where");
00220         if (cat)
00221             G_warning(_("'where' and 'cats' parameters were supplied, cat will be ignored"));
00222         *varray = Vect_new_varray(Vect_get_num_lines(In));
00223         if (Vect_set_varray_from_db
00224             (In, layer, where, mask_type, 1, *varray) == -1) {
00225             G_warning(_("Unable to load data from database"));
00226             return 0;
00227         }
00228         return 1;
00229     }
00230     else if (cat) {
00231         if (layer < 1)
00232             G_fatal_error(_("'%s' must be > 0 for '%s'"), "layer", "cat");
00233         *varray = Vect_new_varray(Vect_get_num_lines(In));
00234         if (Vect_set_varray_from_cat_string
00235             (In, layer, cat, mask_type, 1, *varray) == -1) {
00236             G_warning(_("Problem loading category values"));
00237             return 0;
00238         }
00239         return 1;
00240     }
00241     else {
00242         return 2;
00243     }
00244 
00245 
00246 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines