utils.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*!
  2. \file vector/neta/utils.c
  3. \brief Network Analysis library - utils
  4. Utils subroutines.
  5. (C) 2009-2010 by Daniel Bundala, and the GRASS Development Team
  6. This program is free software under the GNU General Public License
  7. (>=v2). Read the file COPYING that comes with GRASS for details.
  8. \author Daniel Bundala (Google Summer of Code 2009)
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <grass/gis.h>
  13. #include <grass/vector.h>
  14. #include <grass/glocale.h>
  15. #include <grass/dbmi.h>
  16. #include <grass/neta.h>
  17. /*!
  18. \brief Writes point
  19. Writes GV_POINT to Out at the position of the node in <em>In</em>.
  20. \param In pointer to Map_info structure (input vector map)
  21. \param[in,out] Out pointer to Map_info structure (output vector map)
  22. \param node node id
  23. \param Cats pointer to line_cats structures
  24. */
  25. void NetA_add_point_on_node(struct Map_info *In, struct Map_info *Out,
  26. int node, struct line_cats *Cats)
  27. {
  28. static struct line_pnts *Points;
  29. double x, y, z;
  30. Points = Vect_new_line_struct();
  31. Vect_get_node_coor(In, node, &x, &y, &z);
  32. Vect_reset_line(Points);
  33. Vect_append_point(Points, x, y, z);
  34. Vect_write_line(Out, GV_POINT, Points, Cats);
  35. Vect_destroy_line_struct(Points);
  36. }
  37. /* Returns the list of all points with the given category and field */
  38. /*void NetA_get_points_by_category(struct Map_info *In, int field, int cat, struct ilist *point_list)
  39. * {
  40. * int i, nlines;
  41. * struct line_cats *Cats;
  42. * Cats = Vect_new_cats_struct();
  43. * Vect_get_num_lines(In);
  44. * for(i=1;i<=nlines;i++){
  45. * int type = Vect_read_line(In, NULL, Cats, i);
  46. * if(type!=GV_POINT)continue;
  47. * }
  48. *
  49. * Vect_destroy_cats_struct(Cats);
  50. * }
  51. */
  52. /*!
  53. \brief Finds node
  54. Find the node corresponding to each point in the point_list
  55. \param In pointer to Map_info structure
  56. \param point_list list of points (their ids)
  57. */
  58. void NetA_points_to_nodes(struct Map_info *In, struct ilist *point_list)
  59. {
  60. int i, node;
  61. struct line_pnts *Points = Vect_new_line_struct();
  62. for (i = 0; i < point_list->n_values; i++) {
  63. /* Vect_get_line_nodes(In, point_list->value[i], &node, NULL); */
  64. node = Vect_find_node(In, Points->x[0], Points->y[0], Points->z[0], 0, 0);
  65. point_list->value[i] = node;
  66. }
  67. Vect_destroy_line_struct(Points);
  68. }
  69. /*!
  70. \brief Get node cost
  71. For each node in the map, finds the category of the point on it (if
  72. there is any) and stores the value associated with this category in
  73. the array node_costs. If there is no point with a category,
  74. node_costs=0.
  75. node_costs are multiplied by the graph's cost multiplier and
  76. truncated to integers (as is done in Vect_net_build_graph)
  77. \param In pointer to Map_info structure
  78. \param layer layer number
  79. \param column name of column
  80. \param[out] node_costs list of node costs
  81. \returns 1 on success
  82. \return 0 on failure
  83. */
  84. int NetA_get_node_costs(struct Map_info *In, int layer, char *column,
  85. int *node_costs)
  86. {
  87. int i, nlines, nnodes;
  88. dbCatValArray vals;
  89. struct line_cats *Cats;
  90. struct line_pnts *Points;
  91. dbDriver *driver;
  92. struct field_info *Fi;
  93. Fi = Vect_get_field(In, layer);
  94. driver = db_start_driver_open_database(Fi->driver, Fi->database);
  95. if (driver == NULL)
  96. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  97. Fi->database, Fi->driver);
  98. nlines = Vect_get_num_lines(In);
  99. nnodes = Vect_get_num_nodes(In);
  100. Cats = Vect_new_cats_struct();
  101. Points = Vect_new_line_struct();
  102. for (i = 1; i <= nnodes; i++)
  103. node_costs[i] = 0;
  104. db_CatValArray_init(&vals);
  105. if (db_select_CatValArray(driver, Fi->table, Fi->key, column, NULL, &vals)
  106. == -1)
  107. return 0;
  108. for (i = 1; i <= nlines; i++) {
  109. int type = Vect_read_line(In, Points, Cats, i);
  110. if (type == GV_POINT) {
  111. int node, cat;
  112. double value;
  113. if (!Vect_cat_get(Cats, layer, &cat))
  114. continue;
  115. Vect_get_line_nodes(In, i, &node, NULL);
  116. if (db_CatValArray_get_value_double(&vals, cat, &value) == DB_OK) {
  117. if (value < 0)
  118. node_costs[node] = -1;
  119. else
  120. node_costs[node] = value * In->dgraph.cost_multip;
  121. }
  122. }
  123. }
  124. Vect_destroy_cats_struct(Cats);
  125. db_CatValArray_free(&vals);
  126. db_close_database_shutdown_driver(driver);
  127. return 1;
  128. }
  129. /*!
  130. \brief Get list of nodes from varray
  131. Returns the list of all nodes on features selected by varray.
  132. nodes_to_features contains the index of a feature adjacent to each
  133. node or -1 if no such feature specified by varray
  134. exists. Nodes_to_features might be NULL, in which case it is left
  135. unitialised. Nodes_to_features will be wrong if several lines connect
  136. to the same node.
  137. \param map pointer to Map_info structure
  138. \param varray pointer to varray structure
  139. \param[out] nodes list of node ids
  140. \param[out] nodes_to_features maps nodes to features
  141. */
  142. void NetA_varray_to_nodes(struct Map_info *map, struct varray *varray,
  143. struct ilist *nodes, int *nodes_to_features)
  144. {
  145. int nlines, nnodes, i;
  146. struct line_pnts *Points = Vect_new_line_struct();
  147. nlines = Vect_get_num_lines(map);
  148. nnodes = Vect_get_num_nodes(map);
  149. if (nodes_to_features)
  150. for (i = 1; i <= nnodes; i++)
  151. nodes_to_features[i] = -1;
  152. for (i = 1; i <= nlines; i++) {
  153. if (varray->c[i]) {
  154. int type = Vect_read_line(map, Points, NULL, i);
  155. if (type == GV_POINT) {
  156. int node;
  157. node = Vect_find_node(map, Points->x[0], Points->y[0], Points->z[0], 0, 0);
  158. if (node) {
  159. Vect_list_append(nodes, node);
  160. if (nodes_to_features)
  161. nodes_to_features[node] = i;
  162. }
  163. else
  164. G_warning(_("Point %d is not connected!"), i);
  165. }
  166. else {
  167. int node1, node2;
  168. Vect_get_line_nodes(map, i, &node1, &node2);
  169. Vect_list_append(nodes, node1);
  170. Vect_list_append(nodes, node2);
  171. if (nodes_to_features)
  172. nodes_to_features[node1] = nodes_to_features[node2] = i;
  173. }
  174. }
  175. }
  176. Vect_destroy_line_struct(Points);
  177. }
  178. /*!
  179. \brief Initialize varray
  180. \param In pointer to Map_info structure
  181. \param layer layer number
  182. \param mask_type ?
  183. \param where where statement
  184. \param cat ?
  185. \param[out] pointer to varray structure
  186. \return number of items set
  187. \return -1 on error
  188. */
  189. int NetA_initialise_varray(struct Map_info *In, int layer, int mask_type,
  190. char *where, char *cat, struct varray **varray)
  191. {
  192. int n, ni;
  193. if (layer < 1)
  194. G_fatal_error(_("'%s' must be > 0"), "layer");
  195. n = Vect_get_num_lines(In);
  196. *varray = Vect_new_varray(n);
  197. ni = 0;
  198. /* parse filter option and select appropriate lines */
  199. if (where) {
  200. if (cat)
  201. G_warning(_("'where' and 'cats' parameters were supplied, cat will be ignored"));
  202. ni = Vect_set_varray_from_db(In, layer, where, mask_type, 1, *varray);
  203. if (ni == -1) {
  204. G_warning(_("Unable to load data from database"));
  205. }
  206. return ni;
  207. }
  208. else if (cat) {
  209. ni = Vect_set_varray_from_cat_string(In, layer, cat, mask_type, 1, *varray);
  210. if (ni == -1) {
  211. G_warning(_("Problem loading category values"));
  212. }
  213. return ni;
  214. }
  215. else { /* all features of given layer */
  216. int i, cat;
  217. int ltype; /* line type */
  218. struct line_cats *Cats;
  219. Cats = Vect_new_cats_struct();
  220. for (i = 1; i <= n; i++) {
  221. ltype = Vect_read_line(In, NULL, Cats, i);
  222. if (!(ltype & mask_type))
  223. continue; /* is not specified type */
  224. if (Vect_cat_get(Cats, layer, &cat)) {
  225. (*varray)->c[i] = 1;
  226. ni++;
  227. }
  228. }
  229. Vect_destroy_cats_struct(Cats);
  230. return ni;
  231. }
  232. }