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 }
dbDriver * db_start_driver_open_database(const char *drvname, const char *dbname)
Open driver/database connection.
Definition: db.c:28
int db_select_CatValArray(dbDriver *driver, const char *tab, const char *key, const char *col, const char *where, dbCatValArray *cvarr)
Select pairs key/value to array, values are sorted by key (must be integer)
struct driver * driver
Definition: driver/init.c:26
struct field_info * Vect_get_field(struct Map_info *Map, int field)
Get information about link to database.
Definition: field.c:404
int Vect_set_varray_from_db(struct Map_info *Map, int field, const char *where, int type, int value, VARRAY *varray)
Set values in &#39;varray&#39; to &#39;value&#39;.
Definition: array.c:251
int Vect_get_line_nodes(struct Map_info *Map, int line, int *n1, int *n2)
Get line nodes.
Definition: level_two.c:230
int db_close_database_shutdown_driver(dbDriver *driver)
Close driver/database connection.
Definition: db.c:62
struct line_pnts * Vect_new_line_struct()
Creates and initializes a struct line_pnts.
Definition: line.c:57
void db_CatValArray_free(dbCatValArray *arr)
Definition: value.c:348
int Vect_reset_line(struct line_pnts *Points)
Reset line.
Definition: line.c:148
int y
Definition: plot.c:34
int Vect_append_point(struct line_pnts *Points, double x, double y, double z)
Appends one point to the end of a line.
Definition: line.c:168
int db_CatValArray_get_value_double(dbCatValArray *arr, int key, double *val)
Find value (double) by key.
void NetA_varray_to_nodes(struct Map_info *map, struct varray *varray, struct ilist *nodes, int *nodes_to_features)
Get list of nodes from varray.
Definition: utils.c:166
int Vect_set_varray_from_cat_string(struct Map_info *Map, int field, const char *cstring, int type, int value, VARRAY *varray)
Set values in &#39;varray&#39; to &#39;value&#39;.
Definition: array.c:85
int NetA_initialise_varray(struct Map_info *In, int layer, int mask_type, char *where, char *cat, struct varray **varray)
Initialize varray.
Definition: utils.c:213
int NetA_get_node_costs(struct Map_info *In, int layer, char *column, int *node_costs)
Get node cost.
Definition: utils.c:102
VARRAY * Vect_new_varray(int size)
Create new VARRAY and allocate space for given number of items.
Definition: array.c:44
int Vect_destroy_cats_struct(struct line_cats *p)
Frees all memory associated with line_cats structure, including the struct itself.
int Vect_list_append(struct ilist *list, int val)
Append new item to the end of list if not yet present.
char * value
Definition: env.c:30
void db_CatValArray_init(dbCatValArray *arr)
Definition: value.c:335
struct line_cats * Vect_new_cats_struct()
Creates and initializes line_cats structure.
return NULL
Definition: dbfopen.c:1394
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
int Vect_get_num_lines(struct Map_info *map)
Fetch number of features (points, lines, boundaries, centroids) in vector map.
Definition: level_two.c:69
void NetA_points_to_nodes(struct Map_info *In, struct ilist *point_list)
Finds node.
Definition: utils.c:73
void NetA_add_point_on_node(struct Map_info *In, struct Map_info *Out, int node, struct line_cats *Cats)
Writes point.
Definition: utils.c:35
long Vect_write_line(struct Map_info *Map, int type, struct line_pnts *points, struct line_cats *cats)
Writes new feature to the end of file (table)
CELL cat
Definition: g3dcats.c:90
int Vect_get_node_coor(struct Map_info *map, int num, double *x, double *y, double *z)
Get node coordinates.
Definition: level_two.c:206
int Vect_get_num_nodes(struct Map_info *map)
Get number of nodes in vector map.
Definition: level_two.c:29
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
int Vect_destroy_line_struct(struct line_pnts *p)
Frees all memory associated with a struct line_pnts, including the struct itself. ...
Definition: line.c:90
int Vect_read_line(struct Map_info *Map, struct line_pnts *line_p, struct line_cats *line_c, int line)
Read vector feature.
int Vect_cat_get(struct line_cats *Cats, int field, int *cat)
Get first found category of given field.