path.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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;
  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, "cat", 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. sprintf(buf,
  81. "create table %s ( cat integer, id integer, fcat integer, tcat integer, "
  82. "sp integer, cost double precision, fdist double precision, tdist double precision )",
  83. Fi->table);
  84. db_set_string(&sql, buf);
  85. G_debug(2, db_get_string(&sql));
  86. if (db_execute_immediate(driver, &sql) != DB_OK) {
  87. db_close_database_shutdown_driver(driver);
  88. G_fatal_error(_("Unable to create table: '%s'"), db_get_string(&sql));
  89. }
  90. if (db_create_index2(driver, Fi->table, "cat") != 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 connectin 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. Vect_get_line_nodes(In, fline, &fnode, NULL);
  144. }
  145. G_debug(3, "from: cat = %5d point(line) = %5d node = %5d", fcat,
  146. fline, fnode);
  147. /* To */
  148. Citem =
  149. (CIDX *) bsearch((void *)&tcat, Cidx, npoints, sizeof(CIDX),
  150. cmp);
  151. if (Citem == NULL) {
  152. G_warning(_("No point with category [%d]"), tcat);
  153. sp = SP_NOPOINT;
  154. tline = 0;
  155. tnode = 0;
  156. nopoint++;
  157. }
  158. else {
  159. tline = Citem->line;
  160. Vect_get_line_nodes(In, tline, &tnode, NULL);
  161. }
  162. G_debug(3, "to : cat = %5d point(line) = %5d node = %5d", tcat,
  163. tline, tnode);
  164. if (sp != SP_NOPOINT) {
  165. ret = Vect_net_shortest_path(In, fnode, tnode, AList, &cost);
  166. if (ret == -1) {
  167. sp = SP_UNREACHABLE;
  168. unreachable++;
  169. G_warning(_("Point with category [%d] is not reachable "
  170. "from point with category [%d]"), tcat, fcat);
  171. }
  172. else {
  173. /* Write new line connecting 'from' and 'to' */
  174. G_debug(3, "Number of arcs = %d, total costs = %f",
  175. AList->n_values, cost);
  176. Vect_reset_cats(Cats);
  177. Vect_cat_set(Cats, 1, cat);
  178. if (segments) {
  179. for (i = 0; i < AList->n_values; i++) {
  180. line = AList->value[i];
  181. Vect_read_line(In, Points, NULL, abs(line));
  182. if (line > 0) {
  183. Vect_write_line(Out, GV_LINE, Points, Cats);
  184. }
  185. else {
  186. Vect_reset_line(OPoints);
  187. Vect_append_points(OPoints, Points,
  188. GV_BACKWARD);
  189. Vect_write_line(Out, GV_LINE, OPoints, Cats);
  190. }
  191. }
  192. }
  193. else {
  194. Vect_reset_line(OPoints);
  195. for (i = 0; i < AList->n_values; i++) {
  196. line = AList->value[i];
  197. Vect_read_line(In, Points, NULL, abs(line));
  198. if (line > 0)
  199. Vect_append_points(OPoints, Points,
  200. GV_FORWARD);
  201. else
  202. Vect_append_points(OPoints, Points,
  203. GV_BACKWARD);
  204. }
  205. Vect_write_line(Out, GV_LINE, OPoints, Cats);
  206. }
  207. }
  208. }
  209. }
  210. else { /* INPUT_MODE_COOR */
  211. fcat = tcat = 0;
  212. ret =
  213. Vect_net_shortest_path_coor(In, fx, fy, 0.0, tx, ty, 0.0,
  214. maxdist, maxdist, &cost, OPoints,
  215. AList, FPoints, TPoints, &fdist,
  216. &tdist);
  217. if (ret == 0) {
  218. sp = SP_UNREACHABLE;
  219. unreachable++;
  220. G_warning(_("Point %f,%f is not reachable from "
  221. "point %f,%f"), tx, ty, fx, fy);
  222. }
  223. else {
  224. Vect_reset_cats(Cats);
  225. Vect_cat_set(Cats, 1, cat);
  226. if (segments) {
  227. /* From point to the first node */
  228. if (FPoints->n_points > 0)
  229. Vect_write_line(Out, GV_LINE, FPoints, Cats);
  230. /* On the network */
  231. for (i = 0; i < AList->n_values; i++) {
  232. line = AList->value[i];
  233. Vect_read_line(In, Points, NULL, abs(line));
  234. if (line > 0) {
  235. Vect_write_line(Out, GV_LINE, Points, Cats);
  236. }
  237. else {
  238. Vect_reset_line(OPoints);
  239. Vect_append_points(OPoints, Points, GV_BACKWARD);
  240. Vect_write_line(Out, GV_LINE, OPoints, Cats);
  241. }
  242. }
  243. /* From last node to point */
  244. if (TPoints->n_points > 0)
  245. Vect_write_line(Out, GV_LINE, TPoints, Cats);
  246. }
  247. else {
  248. Vect_write_line(Out, GV_LINE, OPoints, Cats);
  249. }
  250. }
  251. }
  252. sprintf(buf,
  253. "insert into %s values ( %d, %d, %d, %d, %d, %f, %f, %f)",
  254. Fi->table, cat, id, fcat, tcat, sp, cost, fdist, tdist);
  255. db_set_string(&sql, buf);
  256. G_debug(3, db_get_string(&sql));
  257. if (db_execute_immediate(driver, &sql) != DB_OK) {
  258. db_close_database_shutdown_driver(driver);
  259. G_fatal_error(_("Cannot insert new record: %s"),
  260. db_get_string(&sql));
  261. }
  262. }
  263. db_commit_transaction(driver);
  264. db_close_database_shutdown_driver(driver);
  265. Vect_destroy_list(AList);
  266. Vect_destroy_line_struct(Points);
  267. Vect_destroy_line_struct(OPoints);
  268. Vect_destroy_cats_struct(Cats);
  269. if (filename)
  270. fclose(in_file);
  271. if (formaterr)
  272. G_warning(_("[%d] input format errors"), formaterr);
  273. if (nopoint)
  274. G_warning(_("[%d] points of given category missing"), nopoint);
  275. if (unreachable)
  276. G_warning(_("%d destination(s) unreachable (including points out "
  277. "of threshold)"), unreachable);
  278. return 1;
  279. }
  280. int cmp(const void *pa, const void *pb)
  281. {
  282. CIDX *p1 = (CIDX *) pa;
  283. CIDX *p2 = (CIDX *) pb;
  284. if (p1->cat < p2->cat)
  285. return -1;
  286. if (p1->cat > p2->cat)
  287. return 1;
  288. return 0;
  289. }