GRASS Programmer's Manual  6.4.3(2013)-r
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Pages
utils.c
Go to the documentation of this file.
1 
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <grass/gis.h>
19 #include <grass/Vect.h>
20 #include <grass/glocale.h>
21 #include <grass/dbmi.h>
22 #include <grass/neta.h>
23 
24 
35 void NetA_add_point_on_node(struct Map_info *In, struct Map_info *Out,
36  int node, struct line_cats *Cats)
37 {
38  static struct line_pnts *Points;
39  double x, y, z;
40 
41  Points = Vect_new_line_struct();
42  Vect_get_node_coor(In, node, &x, &y, &z);
43  Vect_reset_line(Points);
44  Vect_append_point(Points, x, y, z);
45  Vect_write_line(Out, GV_POINT, Points, Cats);
47 }
48 
49 /* Returns the list of all points with the given category and field */
50 /*void NetA_get_points_by_category(struct Map_info *In, int field, int cat, struct ilist *point_list)
51  * {
52  * int i, nlines;
53  * struct line_cats *Cats;
54  * Cats = Vect_new_cats_struct();
55  * Vect_get_num_lines(In);
56  * for(i=1;i<=nlines;i++){
57  * int type = Vect_read_line(In, NULL, Cats, i);
58  * if(type!=GV_POINT)continue;
59  * }
60  *
61  * Vect_destroy_cats_struct(Cats);
62  * }
63  */
64 
73 void NetA_points_to_nodes(struct Map_info *In, struct ilist *point_list)
74 {
75  int i, node;
76 
77  for (i = 0; i < point_list->n_values; i++) {
78  Vect_get_line_nodes(In, point_list->value[i], &node, NULL);
79  point_list->value[i] = node;
80  }
81 }
82 
102 int NetA_get_node_costs(struct Map_info *In, int layer, char *column,
103  int *node_costs)
104 {
105  int i, nlines, nnodes;
106  dbCatValArray vals;
107  struct line_cats *Cats;
108  struct line_pnts *Points;
109 
110  dbDriver *driver;
111  struct field_info *Fi;
112 
113  Fi = Vect_get_field(In, layer);
114  driver = db_start_driver_open_database(Fi->driver, Fi->database);
115  if (driver == NULL)
116  G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
117  Fi->database, Fi->driver);
118 
119  nlines = Vect_get_num_lines(In);
120  nnodes = Vect_get_num_nodes(In);
121  Cats = Vect_new_cats_struct();
122  Points = Vect_new_line_struct();
123  for (i = 1; i <= nnodes; i++)
124  node_costs[i] = 0;
125 
126  db_CatValArray_init(&vals);
127 
128  if (db_select_CatValArray(driver, Fi->table, Fi->key, column, NULL, &vals)
129  == -1)
130  return 0;
131  for (i = 1; i <= nlines; i++) {
132  int type = Vect_read_line(In, Points, Cats, i);
133 
134  if (type == GV_POINT) {
135  int node, cat;
136  double value;
137 
138  if (!Vect_cat_get(Cats, layer, &cat))
139  continue;
140  Vect_get_line_nodes(In, i, &node, NULL);
141  if (db_CatValArray_get_value_double(&vals, cat, &value) == DB_OK)
142  node_costs[node] = value * 1000000.0;
143  }
144  }
145 
147  db_CatValArray_free(&vals);
149  return 1;
150 }
151 
166 void NetA_varray_to_nodes(struct Map_info *map, struct varray *varray,
167  struct ilist *nodes, int *nodes_to_features)
168 {
169  int nlines, nnodes, i;
170 
171  nlines = Vect_get_num_lines(map);
172  nnodes = Vect_get_num_nodes(map);
173  if (nodes_to_features)
174  for (i = 1; i <= nnodes; i++)
175  nodes_to_features[i] = -1;
176 
177  for (i = 1; i <= nlines; i++)
178  if (varray->c[i]) {
179  int type = Vect_read_line(map, NULL, NULL, i);
180 
181  if (type == GV_POINT) {
182  int node;
183 
184  Vect_get_line_nodes(map, i, &node, NULL);
185  Vect_list_append(nodes, node);
186  if (nodes_to_features)
187  nodes_to_features[node] = i;
188  }
189  else {
190  int node1, node2;
191 
192  Vect_get_line_nodes(map, i, &node1, &node2);
193  Vect_list_append(nodes, node1);
194  Vect_list_append(nodes, node2);
195  if (nodes_to_features)
196  nodes_to_features[node1] = nodes_to_features[node2] = i;
197  }
198  }
199 }
200 
213 int NetA_initialise_varray(struct Map_info *In, int layer, int mask_type,
214  char *where, char *cat, struct varray **varray)
215 {
216  /* parse filter option and select appropriate lines */
217  if (where) {
218  if (layer < 1)
219  G_fatal_error(_("'%s' must be > 0 for '%s'"), "layer", "where");
220  if (cat)
221  G_warning(_("'where' and 'cats' parameters were supplied, cat will be ignored"));
222  *varray = Vect_new_varray(Vect_get_num_lines(In));
224  (In, layer, where, mask_type, 1, *varray) == -1) {
225  G_warning(_("Unable to load data from database"));
226  return 0;
227  }
228  return 1;
229  }
230  else if (cat) {
231  if (layer < 1)
232  G_fatal_error(_("'%s' must be > 0 for '%s'"), "layer", "cat");
233  *varray = Vect_new_varray(Vect_get_num_lines(In));
235  (In, layer, cat, mask_type, 1, *varray) == -1) {
236  G_warning(_("Problem loading category values"));
237  return 0;
238  }
239  return 1;
240  }
241  else {
242  return 2;
243  }
244 
245 
246 }