utils.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*!
  2. \file vector/neta/timetables.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 1000000 and truncated to integers (as
  76. 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. node_costs[node] = value * 1000000.0;
  118. }
  119. }
  120. Vect_destroy_cats_struct(Cats);
  121. db_CatValArray_free(&vals);
  122. db_close_database_shutdown_driver(driver);
  123. return 1;
  124. }
  125. /*!
  126. \brief Get list of nodes from varray
  127. Returns the list of all nodes on features selected by varray.
  128. nodes_to_features conains the index of a feature adjecent to each
  129. node or -1 if no such feature specified by varray
  130. exists. Nodes_to_features might be NULL, in which case it is left
  131. unitialised.
  132. \param map pointer to Map_info structure
  133. \param varray pointer to varray structure
  134. \param[out] nodes list of node ids
  135. \param node_to_features ?
  136. */
  137. void NetA_varray_to_nodes(struct Map_info *map, struct varray *varray,
  138. struct ilist *nodes, int *nodes_to_features)
  139. {
  140. int nlines, nnodes, i;
  141. nlines = Vect_get_num_lines(map);
  142. nnodes = Vect_get_num_nodes(map);
  143. if (nodes_to_features)
  144. for (i = 1; i <= nnodes; i++)
  145. nodes_to_features[i] = -1;
  146. for (i = 1; i <= nlines; i++)
  147. if (varray->c[i]) {
  148. int type = Vect_read_line(map, NULL, NULL, i);
  149. if (type == GV_POINT) {
  150. int node;
  151. Vect_get_line_nodes(map, i, &node, NULL);
  152. Vect_list_append(nodes, node);
  153. if (nodes_to_features)
  154. nodes_to_features[node] = i;
  155. }
  156. else {
  157. int node1, node2;
  158. Vect_get_line_nodes(map, i, &node1, &node2);
  159. Vect_list_append(nodes, node1);
  160. Vect_list_append(nodes, node2);
  161. if (nodes_to_features)
  162. nodes_to_features[node1] = nodes_to_features[node2] = i;
  163. }
  164. }
  165. }
  166. /*!
  167. \brief Initialize varray
  168. \param In pointer to Map_info structure
  169. \param layer layer number
  170. \param mask_type ?
  171. \param where where statement
  172. \param cat ?
  173. \param[out] pointer to varray structure
  174. \return ?
  175. */
  176. int NetA_initialise_varray(struct Map_info *In, int layer, int mask_type,
  177. char *where, char *cat, struct varray **varray)
  178. {
  179. /* parse filter option and select appropriate lines */
  180. if (where) {
  181. if (layer < 1)
  182. G_fatal_error(_("'%s' must be > 0 for '%s'"), "layer", "where");
  183. if (cat)
  184. G_warning(_("'where' and 'cats' parameters were supplied, cat will be ignored"));
  185. *varray = Vect_new_varray(Vect_get_num_lines(In));
  186. if (Vect_set_varray_from_db
  187. (In, layer, where, mask_type, 1, *varray) == -1) {
  188. G_warning(_("Unable to load data from database"));
  189. return 0;
  190. }
  191. return 1;
  192. }
  193. else if (cat) {
  194. if (layer < 1)
  195. G_fatal_error(_("'%s' must be > 0 for '%s'"), "layer", GV_KEY_COLUMN);
  196. *varray = Vect_new_varray(Vect_get_num_lines(In));
  197. if (Vect_set_varray_from_cat_string
  198. (In, layer, cat, mask_type, 1, *varray) == -1) {
  199. G_warning(_("Problem loading category values"));
  200. return 0;
  201. }
  202. return 1;
  203. }
  204. else {
  205. return 2;
  206. }
  207. }