path.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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, int tucfield, int use_ttb)
  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. double fdist, tdist;
  101. if (!filename) {
  102. if (fgets(buf, sizeof(buf), stdin) == NULL)
  103. break;
  104. }
  105. else {
  106. if (G_getl2(buf, sizeof(buf) - 1, in_file) == 0)
  107. break;
  108. }
  109. G_chop(buf);
  110. sp = SP_OK;
  111. ret =
  112. sscanf(buf, "%d %lf %lf %lf %lf %s", &id, &fx, &fy, &tx, &ty,
  113. dummy);
  114. if (ret == 5) {
  115. input_mode = INPUT_MODE_COOR;
  116. if (fx == tx && fy == ty) {
  117. G_warning(_("From and to are identical (id %d)"), id);
  118. continue;
  119. }
  120. }
  121. else {
  122. ret = sscanf(buf, "%d %d %d %s", &id, &fcat, &tcat, dummy);
  123. if (ret != 3) {
  124. G_warning(_("Wrong input format: %s"), buf);
  125. formaterr++;
  126. continue;
  127. }
  128. input_mode = INPUT_MODE_NODE;
  129. /* From */
  130. Citem =
  131. (CIDX *) bsearch((void *)&fcat, Cidx, npoints, sizeof(CIDX),
  132. cmp);
  133. if (Citem == NULL) {
  134. G_warning(_("No point with category [%d]"), fcat);
  135. sp = SP_NOPOINT;
  136. fline = 0;
  137. fnode = 0;
  138. nopoint++;
  139. }
  140. else {
  141. fline = Citem->line;
  142. type = Vect_read_line(In, Points, NULL, fline);
  143. fnode =
  144. Vect_find_node(In, Points->x[0], Points->y[0],
  145. Points->z[0], 0, 0);
  146. if (fnode == 0)
  147. sp = SP_NOPOINT;
  148. /* Vect_get_line_nodes(In, fline, &fnode, NULL); */
  149. }
  150. G_debug(3, "from: cat = %5d point(line) = %5d node = %5d", fcat,
  151. fline, fnode);
  152. /* To */
  153. Citem =
  154. (CIDX *) bsearch((void *)&tcat, Cidx, npoints, sizeof(CIDX),
  155. cmp);
  156. if (Citem == NULL) {
  157. G_warning(_("No point with category [%d]"), tcat);
  158. sp = SP_NOPOINT;
  159. tline = 0;
  160. tnode = 0;
  161. nopoint++;
  162. }
  163. else {
  164. tline = Citem->line;
  165. type = Vect_read_line(In, Points, NULL, tline);
  166. tnode =
  167. Vect_find_node(In, Points->x[0], Points->y[0],
  168. Points->z[0], 0, 0);
  169. if (tnode == 0)
  170. sp = SP_NOPOINT;
  171. /* Vect_get_line_nodes(In, tline, &tnode, NULL); */
  172. }
  173. G_debug(3, "to : cat = %5d point(line) = %5d node = %5d", tcat,
  174. tline, tnode);
  175. if (sp == SP_NOPOINT)
  176. continue;
  177. if (fnode == tnode) {
  178. G_warning(_("From and to are identical (id %d)"), id);
  179. continue;
  180. }
  181. }
  182. G_debug(3, "mode = %d", input_mode);
  183. cat++; /* Output category */
  184. cost = fdist = tdist = 0;
  185. if (input_mode == INPUT_MODE_NODE) {
  186. if (use_ttb)
  187. ret =
  188. Vect_net_ttb_shortest_path(In, fnode, 0, tnode, 0,
  189. tucfield, AList, &cost);
  190. else
  191. ret =
  192. Vect_net_shortest_path(In, fnode, tnode, AList,
  193. &cost);
  194. if (ret == -1) {
  195. sp = SP_UNREACHABLE;
  196. unreachable++;
  197. G_warning(_("Point with category [%d] is not reachable "
  198. "from point with category [%d]"), tcat, fcat);
  199. }
  200. else {
  201. /* Write new line connecting 'from' and 'to' */
  202. G_debug(3, "Number of arcs = %d, total costs = %f",
  203. AList->n_values, cost);
  204. Vect_reset_cats(Cats);
  205. Vect_cat_set(Cats, 1, cat);
  206. if (segments) {
  207. for (i = 0; i < AList->n_values; i++) {
  208. line = AList->value[i];
  209. Vect_read_line(In, Points, NULL, abs(line));
  210. if (line > 0) {
  211. Vect_write_line(Out, GV_LINE, Points, Cats);
  212. }
  213. else {
  214. Vect_reset_line(OPoints);
  215. Vect_append_points(OPoints, Points,
  216. GV_BACKWARD);
  217. Vect_write_line(Out, GV_LINE, OPoints, Cats);
  218. }
  219. }
  220. }
  221. else {
  222. Vect_reset_line(OPoints);
  223. for (i = 0; i < AList->n_values; i++) {
  224. line = AList->value[i];
  225. Vect_read_line(In, Points, NULL, abs(line));
  226. if (line > 0)
  227. Vect_append_points(OPoints, Points,
  228. GV_FORWARD);
  229. else
  230. Vect_append_points(OPoints, Points,
  231. GV_BACKWARD);
  232. OPoints->n_points--;
  233. }
  234. if (AList->n_values > 0 && OPoints->n_points > 0) {
  235. OPoints->n_points++;
  236. Vect_write_line(Out, GV_LINE, OPoints, Cats);
  237. }
  238. }
  239. }
  240. }
  241. else { /* INPUT_MODE_COOR */
  242. fcat = tcat = 0;
  243. if (use_ttb)
  244. ret =
  245. Vect_net_ttb_shortest_path_coor(In, fx, fy, 0.0, tx, ty,
  246. 0.0, maxdist, maxdist,
  247. tucfield, &cost,
  248. OPoints, AList, NULL,
  249. FPoints, TPoints, &fdist,
  250. &tdist);
  251. else
  252. ret =
  253. Vect_net_shortest_path_coor(In, fx, fy, 0.0, tx, ty, 0.0,
  254. maxdist, maxdist, &cost,
  255. OPoints, AList, NULL, FPoints,
  256. TPoints, &fdist, &tdist);
  257. if (ret == 0) {
  258. sp = SP_UNREACHABLE;
  259. unreachable++;
  260. G_warning(_("Point %f,%f is not reachable from "
  261. "point %f,%f"), tx, ty, fx, fy);
  262. }
  263. else {
  264. Vect_reset_cats(Cats);
  265. Vect_cat_set(Cats, 1, cat);
  266. if (segments) {
  267. /* From point to the first node */
  268. if (FPoints->n_points > 0)
  269. Vect_write_line(Out, GV_LINE, FPoints, Cats);
  270. /* On the network */
  271. for (i = 0; i < AList->n_values; i++) {
  272. line = AList->value[i];
  273. Vect_read_line(In, Points, NULL, abs(line));
  274. if (line > 0) {
  275. Vect_write_line(Out, GV_LINE, Points, Cats);
  276. }
  277. else {
  278. Vect_reset_line(OPoints);
  279. Vect_append_points(OPoints, Points, GV_BACKWARD);
  280. Vect_write_line(Out, GV_LINE, OPoints, Cats);
  281. }
  282. }
  283. /* From last node to point */
  284. if (TPoints->n_points > 0)
  285. Vect_write_line(Out, GV_LINE, TPoints, Cats);
  286. }
  287. else {
  288. if (OPoints->n_points > 0)
  289. Vect_write_line(Out, GV_LINE, OPoints, Cats);
  290. }
  291. }
  292. }
  293. sprintf(buf,
  294. "insert into %s values ( %d, %d, %d, %d, %d, %f, %f, %f)",
  295. Fi->table, cat, id, fcat, tcat, sp, cost, fdist, tdist);
  296. db_set_string(&sql, buf);
  297. G_debug(3, "%s", db_get_string(&sql));
  298. if (db_execute_immediate(driver, &sql) != DB_OK) {
  299. G_fatal_error(_("Cannot insert new record: %s"),
  300. db_get_string(&sql));
  301. }
  302. }
  303. db_commit_transaction(driver);
  304. db_close_database_shutdown_driver(driver);
  305. Vect_destroy_list(AList);
  306. Vect_destroy_line_struct(Points);
  307. Vect_destroy_line_struct(OPoints);
  308. Vect_destroy_cats_struct(Cats);
  309. if (filename)
  310. fclose(in_file);
  311. if (formaterr)
  312. G_warning(_("[%d] input format errors"), formaterr);
  313. if (nopoint)
  314. G_warning(_("[%d] points of given category missing"), nopoint);
  315. if (unreachable)
  316. G_warning(_("%d destination(s) unreachable (including points out "
  317. "of threshold)"), unreachable);
  318. return 1;
  319. }
  320. int cmp(const void *pa, const void *pb)
  321. {
  322. CIDX *p1 = (CIDX *) pa;
  323. CIDX *p2 = (CIDX *) pb;
  324. if (p1->cat < p2->cat)
  325. return -1;
  326. if (p1->cat > p2->cat)
  327. return 1;
  328. return 0;
  329. }