path.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <math.h>
  5. #include <grass/gis.h>
  6. #include <grass/vector.h>
  7. #include <grass/dbmi.h>
  8. #include <grass/glocale.h>
  9. /* Result code */
  10. #define SP_OK 0 /* Path found */
  11. #define SP_UNREACHABLE 1 /* Node is not reachable */
  12. #define SP_NOPOINT 2 /* Missing point of given category */
  13. #define INPUT_MODE_NODE 1
  14. #define INPUT_MODE_COOR 2
  15. typedef struct
  16. { /* category index */
  17. int cat; /* line category */
  18. int line; /* line number */
  19. } CIDX;
  20. int cmp(const void *, const void *);
  21. int path(struct Map_info *In, struct Map_info *Out, char *filename,
  22. int nfield, double maxdist, int segments)
  23. {
  24. FILE *in_file = NULL;
  25. int i, nlines, line, npoints, type, cat, id, fcat, tcat, fline, tline,
  26. fnode, tnode, count;
  27. int ret, sp, input_mode, unreachable, nopoint, formaterr;
  28. struct ilist *AList;
  29. double cost;
  30. struct line_pnts *Points, *OPoints, *FPoints, *TPoints;
  31. struct line_cats *Cats;
  32. CIDX *Cidx, *Citem;
  33. char buf[2000], dummy[2000];
  34. double fx, fy, tx, ty;
  35. /* Attribute table */
  36. dbString sql;
  37. dbDriver *driver;
  38. struct field_info *Fi;
  39. if (filename) {
  40. /* open input file */
  41. if ((in_file = fopen(filename, "r")) == NULL)
  42. G_fatal_error(_("Unable to open input file <%s>"), filename);
  43. }
  44. AList = Vect_new_list();
  45. Points = Vect_new_line_struct();
  46. OPoints = Vect_new_line_struct();
  47. FPoints = Vect_new_line_struct();
  48. TPoints = Vect_new_line_struct();
  49. Cats = Vect_new_cats_struct();
  50. db_init_string(&sql);
  51. /* Create category index for input points */
  52. npoints = Vect_get_num_primitives(In, GV_POINT);
  53. Cidx = (CIDX *) G_malloc(npoints * sizeof(CIDX));
  54. nlines = Vect_get_num_lines(In);
  55. count = 0;
  56. for (line = 1; line <= nlines; line++) {
  57. type = Vect_read_line(In, NULL, Cats, line);
  58. if (type != GV_POINT)
  59. continue;
  60. Vect_cat_get(Cats, nfield, &cat);
  61. if (cat < 0)
  62. continue;
  63. Cidx[count].cat = cat;
  64. Cidx[count].line = line;
  65. count++;
  66. }
  67. if (count < npoints)
  68. G_warning(_("[%d] points without category (nfield: [%d])"),
  69. npoints - count, nfield);
  70. npoints = count;
  71. qsort((void *)Cidx, npoints, sizeof(CIDX), cmp);
  72. /* Create table */
  73. Fi = Vect_default_field_info(Out, 1, NULL, GV_1TABLE);
  74. Vect_map_add_dblink(Out, 1, NULL, Fi->table, GV_KEY_COLUMN, Fi->database,
  75. Fi->driver);
  76. driver = db_start_driver_open_database(Fi->driver, Fi->database);
  77. if (driver == NULL)
  78. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  79. Fi->database, Fi->driver);
  80. db_set_error_handler_driver(driver);
  81. sprintf(buf,
  82. "create table %s ( cat integer, id integer, fcat integer, tcat integer, "
  83. "sp integer, cost double precision, fdist double precision, tdist double precision )",
  84. Fi->table);
  85. db_set_string(&sql, buf);
  86. G_debug(2, "%s", db_get_string(&sql));
  87. if (db_execute_immediate(driver, &sql) != DB_OK) {
  88. G_fatal_error(_("Unable to create table: '%s'"), db_get_string(&sql));
  89. }
  90. if (db_create_index2(driver, Fi->table, GV_KEY_COLUMN) != DB_OK)
  91. G_warning(_("Cannot create index"));
  92. if (db_grant_on_table
  93. (driver, Fi->table, DB_PRIV_SELECT, DB_GROUP | DB_PUBLIC) != DB_OK)
  94. G_fatal_error(_("Cannot grant privileges on table <%s>"), Fi->table);
  95. db_begin_transaction(driver);
  96. /* Read stdin, find shortest path, and write connecting line and new database record */
  97. cat = 0;
  98. formaterr = nopoint = unreachable = 0;
  99. while (1) {
  100. if (!filename) {
  101. if (fgets(buf, sizeof(buf), stdin) == NULL)
  102. break;
  103. }
  104. else {
  105. if (G_getl2(buf, sizeof(buf) - 1, in_file) == 0)
  106. break;
  107. }
  108. double fdist, tdist;
  109. G_chop(buf);
  110. ret =
  111. sscanf(buf, "%d %lf %lf %lf %lf %s", &id, &fx, &fy, &tx, &ty,
  112. dummy);
  113. if (ret == 5) {
  114. input_mode = INPUT_MODE_COOR;
  115. }
  116. else {
  117. ret = sscanf(buf, "%d %d %d %s", &id, &fcat, &tcat, dummy);
  118. if (ret != 3) {
  119. G_warning(_("Wrong input format: %s"), buf);
  120. formaterr++;
  121. continue;
  122. }
  123. input_mode = INPUT_MODE_NODE;
  124. }
  125. G_debug(3, "mode = %d", input_mode);
  126. cat++; /* Output category */
  127. sp = SP_OK;
  128. cost = fdist = tdist = 0;
  129. if (input_mode == INPUT_MODE_NODE) {
  130. /* From */
  131. Citem =
  132. (CIDX *) bsearch((void *)&fcat, Cidx, npoints, sizeof(CIDX),
  133. cmp);
  134. if (Citem == NULL) {
  135. G_warning(_("No point with category [%d]"), fcat);
  136. sp = SP_NOPOINT;
  137. fline = 0;
  138. fnode = 0;
  139. nopoint++;
  140. }
  141. else {
  142. fline = Citem->line;
  143. type = Vect_read_line(In, Points, NULL, fline);
  144. fnode = Vect_find_node(In, Points->x[0], Points->y[0], Points->z[0], 0, 0);
  145. /* Vect_get_line_nodes(In, fline, &fnode, NULL); */
  146. }
  147. G_debug(3, "from: cat = %5d point(line) = %5d node = %5d", fcat,
  148. fline, fnode);
  149. /* To */
  150. Citem =
  151. (CIDX *) bsearch((void *)&tcat, Cidx, npoints, sizeof(CIDX),
  152. cmp);
  153. if (Citem == NULL) {
  154. G_warning(_("No point with category [%d]"), tcat);
  155. sp = SP_NOPOINT;
  156. tline = 0;
  157. tnode = 0;
  158. nopoint++;
  159. }
  160. else {
  161. tline = Citem->line;
  162. type = Vect_read_line(In, Points, NULL, tline);
  163. tnode = Vect_find_node(In, Points->x[0], Points->y[0], Points->z[0], 0, 0);
  164. /* Vect_get_line_nodes(In, tline, &tnode, NULL); */
  165. }
  166. G_debug(3, "to : cat = %5d point(line) = %5d node = %5d", tcat,
  167. tline, tnode);
  168. if (sp != SP_NOPOINT) {
  169. ret = Vect_net_shortest_path(In, fnode, tnode, AList, &cost);
  170. if (ret == -1) {
  171. sp = SP_UNREACHABLE;
  172. unreachable++;
  173. G_warning(_("Point with category [%d] is not reachable "
  174. "from point with category [%d]"), tcat, fcat);
  175. }
  176. else {
  177. /* Write new line connecting 'from' and 'to' */
  178. G_debug(3, "Number of arcs = %d, total costs = %f",
  179. AList->n_values, cost);
  180. Vect_reset_cats(Cats);
  181. Vect_cat_set(Cats, 1, cat);
  182. if (segments) {
  183. for (i = 0; i < AList->n_values; i++) {
  184. line = AList->value[i];
  185. Vect_read_line(In, Points, NULL, abs(line));
  186. if (line > 0) {
  187. Vect_write_line(Out, GV_LINE, Points, Cats);
  188. }
  189. else {
  190. Vect_reset_line(OPoints);
  191. Vect_append_points(OPoints, Points,
  192. GV_BACKWARD);
  193. Vect_write_line(Out, GV_LINE, OPoints, Cats);
  194. }
  195. }
  196. }
  197. else {
  198. Vect_reset_line(OPoints);
  199. for (i = 0; i < AList->n_values; i++) {
  200. line = AList->value[i];
  201. Vect_read_line(In, Points, NULL, abs(line));
  202. if (line > 0)
  203. Vect_append_points(OPoints, Points,
  204. GV_FORWARD);
  205. else
  206. Vect_append_points(OPoints, Points,
  207. GV_BACKWARD);
  208. }
  209. Vect_write_line(Out, GV_LINE, OPoints, Cats);
  210. }
  211. }
  212. }
  213. }
  214. else { /* INPUT_MODE_COOR */
  215. fcat = tcat = 0;
  216. ret =
  217. Vect_net_shortest_path_coor(In, fx, fy, 0.0, tx, ty, 0.0,
  218. maxdist, maxdist, &cost, OPoints,
  219. AList, FPoints, TPoints, &fdist,
  220. &tdist);
  221. if (ret == 0) {
  222. sp = SP_UNREACHABLE;
  223. unreachable++;
  224. G_warning(_("Point %f,%f is not reachable from "
  225. "point %f,%f"), tx, ty, fx, fy);
  226. }
  227. else {
  228. Vect_reset_cats(Cats);
  229. Vect_cat_set(Cats, 1, cat);
  230. if (segments) {
  231. /* From point to the first node */
  232. if (FPoints->n_points > 0)
  233. Vect_write_line(Out, GV_LINE, FPoints, Cats);
  234. /* On the network */
  235. for (i = 0; i < AList->n_values; i++) {
  236. line = AList->value[i];
  237. Vect_read_line(In, Points, NULL, abs(line));
  238. if (line > 0) {
  239. Vect_write_line(Out, GV_LINE, Points, Cats);
  240. }
  241. else {
  242. Vect_reset_line(OPoints);
  243. Vect_append_points(OPoints, Points, GV_BACKWARD);
  244. Vect_write_line(Out, GV_LINE, OPoints, Cats);
  245. }
  246. }
  247. /* From last node to point */
  248. if (TPoints->n_points > 0)
  249. Vect_write_line(Out, GV_LINE, TPoints, Cats);
  250. }
  251. else {
  252. Vect_write_line(Out, GV_LINE, OPoints, Cats);
  253. }
  254. }
  255. }
  256. sprintf(buf,
  257. "insert into %s values ( %d, %d, %d, %d, %d, %f, %f, %f)",
  258. Fi->table, cat, id, fcat, tcat, sp, cost, fdist, tdist);
  259. db_set_string(&sql, buf);
  260. G_debug(3, "%s", db_get_string(&sql));
  261. if (db_execute_immediate(driver, &sql) != DB_OK) {
  262. G_fatal_error(_("Cannot insert new record: %s"),
  263. db_get_string(&sql));
  264. }
  265. }
  266. db_commit_transaction(driver);
  267. db_close_database_shutdown_driver(driver);
  268. Vect_destroy_list(AList);
  269. Vect_destroy_line_struct(Points);
  270. Vect_destroy_line_struct(OPoints);
  271. Vect_destroy_cats_struct(Cats);
  272. if (filename)
  273. fclose(in_file);
  274. if (formaterr)
  275. G_warning(_("[%d] input format errors"), formaterr);
  276. if (nopoint)
  277. G_warning(_("[%d] points of given category missing"), nopoint);
  278. if (unreachable)
  279. G_warning(_("%d destination(s) unreachable (including points out "
  280. "of threshold)"), unreachable);
  281. return 1;
  282. }
  283. int cmp(const void *pa, const void *pb)
  284. {
  285. CIDX *p1 = (CIDX *) pa;
  286. CIDX *p2 = (CIDX *) pb;
  287. if (p1->cat < p2->cat)
  288. return -1;
  289. if (p1->cat > p2->cat)
  290. return 1;
  291. return 0;
  292. }