utils.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. for (i = 0; i < point_list->n_values; i++) {
  62. Vect_get_line_nodes(In, point_list->value[i], &node, NULL);
  63. point_list->value[i] = node;
  64. }
  65. }
  66. /*!
  67. \brief Get node cost
  68. For each node in the map, finds the category of the point on it (if
  69. there is any) and stores the value associated with this category in
  70. the array node_costs. If there is no point with a category,
  71. node_costs=0.
  72. node_costs are multiplied by 1000000 and truncated to integers (as
  73. is done in Vect_net_build_graph)
  74. \param In pointer to Map_info structure
  75. \param layer layer number
  76. \param column name of column
  77. \param[out] node_costs list of node costs
  78. \returns 1 on success
  79. \return 0 on failure
  80. */
  81. int NetA_get_node_costs(struct Map_info *In, int layer, char *column,
  82. int *node_costs)
  83. {
  84. int i, nlines, nnodes;
  85. dbCatValArray vals;
  86. struct line_cats *Cats;
  87. struct line_pnts *Points;
  88. dbDriver *driver;
  89. struct field_info *Fi;
  90. Fi = Vect_get_field(In, layer);
  91. driver = db_start_driver_open_database(Fi->driver, Fi->database);
  92. if (driver == NULL)
  93. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  94. Fi->database, Fi->driver);
  95. nlines = Vect_get_num_lines(In);
  96. nnodes = Vect_get_num_nodes(In);
  97. Cats = Vect_new_cats_struct();
  98. Points = Vect_new_line_struct();
  99. for (i = 1; i <= nnodes; i++)
  100. node_costs[i] = 0;
  101. db_CatValArray_init(&vals);
  102. if (db_select_CatValArray(driver, Fi->table, Fi->key, column, NULL, &vals)
  103. == -1)
  104. return 0;
  105. for (i = 1; i <= nlines; i++) {
  106. int type = Vect_read_line(In, Points, Cats, i);
  107. if (type == GV_POINT) {
  108. int node, cat;
  109. double value;
  110. if (!Vect_cat_get(Cats, layer, &cat))
  111. continue;
  112. Vect_get_line_nodes(In, i, &node, NULL);
  113. if (db_CatValArray_get_value_double(&vals, cat, &value) == DB_OK)
  114. node_costs[node] = value * 1000000.0;
  115. }
  116. }
  117. Vect_destroy_cats_struct(Cats);
  118. db_CatValArray_free(&vals);
  119. db_close_database_shutdown_driver(driver);
  120. return 1;
  121. }
  122. /*!
  123. \brief Get list of nodes from varray
  124. Returns the list of all nodes on features selected by varray.
  125. nodes_to_features conains the index of a feature adjecent to each
  126. node or -1 if no such feature specified by varray
  127. exists. Nodes_to_features might be NULL, in which case it is left
  128. unitialised.
  129. \param map pointer to Map_info structure
  130. \param varray pointer to varray structure
  131. \param[out] nodes list of node ids
  132. \param node_to_features ?
  133. */
  134. void NetA_varray_to_nodes(struct Map_info *map, struct varray *varray,
  135. struct ilist *nodes, int *nodes_to_features)
  136. {
  137. int nlines, nnodes, i;
  138. nlines = Vect_get_num_lines(map);
  139. nnodes = Vect_get_num_nodes(map);
  140. if (nodes_to_features)
  141. for (i = 1; i <= nnodes; i++)
  142. nodes_to_features[i] = -1;
  143. for (i = 1; i <= nlines; i++)
  144. if (varray->c[i]) {
  145. int type = Vect_read_line(map, NULL, NULL, i);
  146. if (type == GV_POINT) {
  147. int node;
  148. Vect_get_line_nodes(map, i, &node, NULL);
  149. Vect_list_append(nodes, node);
  150. if (nodes_to_features)
  151. nodes_to_features[node] = i;
  152. }
  153. else {
  154. int node1, node2;
  155. Vect_get_line_nodes(map, i, &node1, &node2);
  156. Vect_list_append(nodes, node1);
  157. Vect_list_append(nodes, node2);
  158. if (nodes_to_features)
  159. nodes_to_features[node1] = nodes_to_features[node2] = i;
  160. }
  161. }
  162. }
  163. /*!
  164. \brief Initialize varray
  165. \param In pointer to Map_info structure
  166. \param layer layer number
  167. \param mask_type ?
  168. \param where where statement
  169. \param cat ?
  170. \param[out] pointer to varray structure
  171. \return ?
  172. */
  173. int NetA_initialise_varray(struct Map_info *In, int layer, int mask_type,
  174. char *where, char *cat, struct varray **varray)
  175. {
  176. /* parse filter option and select appropriate lines */
  177. if (where) {
  178. if (layer < 1)
  179. G_fatal_error(_("'%s' must be > 0 for '%s'"), "layer", "where");
  180. if (cat)
  181. G_warning(_("'where' and 'cats' parameters were supplied, cat will be ignored"));
  182. *varray = Vect_new_varray(Vect_get_num_lines(In));
  183. if (Vect_set_varray_from_db
  184. (In, layer, where, mask_type, 1, *varray) == -1) {
  185. G_warning(_("Unable to load data from database"));
  186. return 0;
  187. }
  188. return 1;
  189. }
  190. else if (cat) {
  191. if (layer < 1)
  192. G_fatal_error(_("'%s' must be > 0 for '%s'"), "layer", "cat");
  193. *varray = Vect_new_varray(Vect_get_num_lines(In));
  194. if (Vect_set_varray_from_cat_string
  195. (In, layer, cat, mask_type, 1, *varray) == -1) {
  196. G_warning(_("Problem loading category values"));
  197. return 0;
  198. }
  199. return 1;
  200. }
  201. else {
  202. return 2;
  203. }
  204. }