net.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  1. /*!
  2. * \file lib/vector/Vlib/net.c
  3. *
  4. * \brief Vector library - net releated fns
  5. *
  6. * Higher level functions for reading/writing/manipulating vectors.
  7. *
  8. * (C) 2001-2009 by the GRASS Development Team
  9. *
  10. * This program is free software under the GNU General Public License
  11. * (>=v2). Read the file COPYING that comes with GRASS for details.
  12. *
  13. * \author Radim Blazek
  14. */
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <fcntl.h>
  18. #include <grass/dbmi.h>
  19. #include <grass/vector.h>
  20. #include <grass/glocale.h>
  21. static int From_node; /* from node set in SP and used by clipper for first arc */
  22. static int clipper(dglGraph_s * pgraph,
  23. dglSPClipInput_s * pargIn,
  24. dglSPClipOutput_s * pargOut, void *pvarg)
  25. { /* caller's pointer */
  26. dglInt32_t cost;
  27. dglInt32_t from;
  28. G_debug(3, "Net: clipper()");
  29. from = dglNodeGet_Id(pgraph, pargIn->pnNodeFrom);
  30. G_debug(3, " Edge = %d NodeFrom = %d NodeTo = %d edge cost = %d",
  31. (int)dglEdgeGet_Id(pgraph, pargIn->pnEdge),
  32. (int)from, (int)dglNodeGet_Id(pgraph, pargIn->pnNodeTo),
  33. (int)pargOut->nEdgeCost);
  34. if (from != From_node) { /* do not clip first */
  35. if (dglGet_NodeAttrSize(pgraph) > 0) {
  36. memcpy(&cost, dglNodeGet_Attr(pgraph, pargIn->pnNodeFrom),
  37. sizeof(cost));
  38. if (cost == -1) { /* closed, cannot go from this node except it is 'from' node */
  39. G_debug(3, " closed node");
  40. return 1;
  41. }
  42. else {
  43. G_debug(3, " EdgeCost += %d (node)", (int)cost);
  44. pargOut->nEdgeCost += cost;
  45. }
  46. }
  47. }
  48. else {
  49. G_debug(3, " don't clip first node");
  50. }
  51. return 0;
  52. }
  53. /*!
  54. \brief Build network graph.
  55. Internal format for edge costs is integer, costs are multiplied
  56. before conversion to int by 1000 and for lenghts LL without geo flag by 1000000.
  57. The same multiplication factor is used for nodes.
  58. Costs in database column may be 'integer' or 'double precision' number >= 0
  59. or -1 for infinity i.e. arc or node is closed and cannot be traversed
  60. If record in table is not found for arcs, arc is skip.
  61. If record in table is not found for node, costs for node are set to 0.
  62. \param Map vector map
  63. \param ltype line type for arcs
  64. \param afield arc costs field (if 0, use length)
  65. \param nfield node costs field (if 0, do not use node costs)
  66. \param afcol column with forward costs for arc
  67. \param abcol column with backward costs for arc (if NULL, back costs = forward costs),
  68. \param ncol column with costs for nodes (if NULL, do not use node costs),
  69. \param geo use geodesic calculation for length (LL),
  70. \param algorithm not used (in future code for algorithm)
  71. \return 0 on success, 1 on error
  72. */
  73. int
  74. Vect_net_build_graph(struct Map_info *Map,
  75. int ltype,
  76. int afield,
  77. int nfield,
  78. const char *afcol,
  79. const char *abcol,
  80. const char *ncol, int geo, int algorithm)
  81. {
  82. int i, j, from, to, line, nlines, nnodes, ret, type, cat, skipped, cfound;
  83. int dofw, dobw;
  84. struct line_pnts *Points;
  85. struct line_cats *Cats;
  86. double dcost, bdcost, ll;
  87. int cost, bcost;
  88. dglGraph_s *gr;
  89. dglInt32_t dgl_cost;
  90. dglInt32_t opaqueset[16] =
  91. { 360000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  92. struct field_info *Fi;
  93. dbDriver *driver = NULL;
  94. dbHandle handle;
  95. dbString stmt;
  96. dbColumn *Column;
  97. dbCatValArray fvarr, bvarr;
  98. int fctype = 0, bctype = 0, nrec;
  99. /* TODO int costs -> double (waiting for dglib) */
  100. G_debug(1, "Vect_build_graph(): ltype = %d, afield = %d, nfield = %d",
  101. ltype, afield, nfield);
  102. G_debug(1, " afcol = %s, abcol = %s, ncol = %s", afcol, abcol, ncol);
  103. G_message(_("Building graph..."));
  104. Map->dgraph.line_type = ltype;
  105. Points = Vect_new_line_struct();
  106. Cats = Vect_new_cats_struct();
  107. ll = 0;
  108. if (G_projection() == 3)
  109. ll = 1; /* LL */
  110. if (afcol == NULL && ll && !geo)
  111. Map->dgraph.cost_multip = 1000000;
  112. else
  113. Map->dgraph.cost_multip = 1000;
  114. nlines = Vect_get_num_lines(Map);
  115. nnodes = Vect_get_num_nodes(Map);
  116. gr = &(Map->dgraph.graph_s);
  117. /* Allocate space for costs, later replace by functions reading costs from graph */
  118. Map->dgraph.edge_fcosts = (double *)G_malloc((nlines + 1) * sizeof(double));
  119. Map->dgraph.edge_bcosts = (double *)G_malloc((nlines + 1) * sizeof(double));
  120. Map->dgraph.node_costs = (double *)G_malloc((nnodes + 1) * sizeof(double));
  121. /* Set to -1 initially */
  122. for (i = 1; i <= nlines; i++) {
  123. Map->dgraph.edge_fcosts[i] = -1; /* forward */
  124. Map->dgraph.edge_bcosts[i] = -1; /* backward */
  125. }
  126. for (i = 1; i <= nnodes; i++) {
  127. Map->dgraph.node_costs[i] = 0;
  128. }
  129. if (ncol != NULL)
  130. dglInitialize(gr, (dglByte_t) 1, sizeof(dglInt32_t), (dglInt32_t) 0,
  131. opaqueset);
  132. else
  133. dglInitialize(gr, (dglByte_t) 1, (dglInt32_t) 0, (dglInt32_t) 0,
  134. opaqueset);
  135. if (gr == NULL)
  136. G_fatal_error(_("Unable to build network graph"));
  137. db_init_handle(&handle);
  138. db_init_string(&stmt);
  139. if (abcol != NULL && afcol == NULL)
  140. G_fatal_error(_("Forward costs column not specified"));
  141. /* --- Add arcs --- */
  142. /* Open db connection */
  143. if (afcol != NULL) {
  144. /* Get field info */
  145. if (afield < 1)
  146. G_fatal_error(_("Arc field < 1"));
  147. Fi = Vect_get_field(Map, afield);
  148. if (Fi == NULL)
  149. G_fatal_error(_("Database connection not defined for layer %d"),
  150. afield);
  151. /* Open database */
  152. driver = db_start_driver_open_database(Fi->driver, Fi->database);
  153. if (driver == NULL)
  154. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  155. Fi->database, Fi->driver);
  156. /* Load costs to array */
  157. if (db_get_column(driver, Fi->table, afcol, &Column) != DB_OK)
  158. G_fatal_error(_("Column <%s> not found in table <%s>"),
  159. afcol, Fi->table);
  160. fctype = db_sqltype_to_Ctype(db_get_column_sqltype(Column));
  161. if (fctype != DB_C_TYPE_INT && fctype != DB_C_TYPE_DOUBLE)
  162. G_fatal_error(_("Data type of column <%s> not supported (must be numeric)"),
  163. afcol);
  164. db_CatValArray_init(&fvarr);
  165. nrec =
  166. db_select_CatValArray(driver, Fi->table, Fi->key, afcol, NULL,
  167. &fvarr);
  168. G_debug(1, "forward costs: nrec = %d", nrec);
  169. if (abcol != NULL) {
  170. if (db_get_column(driver, Fi->table, abcol, &Column) != DB_OK)
  171. G_fatal_error(_("Column <%s> not found in table <%s>"),
  172. abcol, Fi->table);
  173. bctype = db_sqltype_to_Ctype(db_get_column_sqltype(Column));
  174. if (bctype != DB_C_TYPE_INT && bctype != DB_C_TYPE_DOUBLE)
  175. G_fatal_error(_("Data type of column <%s> not supported (must be numeric)"),
  176. abcol);
  177. db_CatValArray_init(&bvarr);
  178. nrec =
  179. db_select_CatValArray(driver, Fi->table, Fi->key, abcol, NULL,
  180. &bvarr);
  181. G_debug(1, "backward costs: nrec = %d", nrec);
  182. }
  183. }
  184. skipped = 0;
  185. G_message(_("Registering arcs..."));
  186. for (i = 1; i <= nlines; i++) {
  187. G_percent(i, nlines, 1); /* must be before any continue */
  188. dofw = dobw = 1;
  189. type = Vect_read_line(Map, Points, Cats, i);
  190. if (!(type & ltype & (GV_LINE | GV_BOUNDARY)))
  191. continue;
  192. Vect_get_line_nodes(Map, i, &from, &to);
  193. if (afcol != NULL) {
  194. if (!(Vect_cat_get(Cats, afield, &cat))) {
  195. G_debug(2,
  196. "Category of field %d not attached to the line %d -> line skipped",
  197. afield, i);
  198. skipped += 2; /* Both directions */
  199. continue;
  200. }
  201. else {
  202. if (fctype == DB_C_TYPE_INT) {
  203. ret = db_CatValArray_get_value_int(&fvarr, cat, &cost);
  204. dcost = cost;
  205. }
  206. else { /* DB_C_TYPE_DOUBLE */
  207. ret =
  208. db_CatValArray_get_value_double(&fvarr, cat, &dcost);
  209. }
  210. if (ret != DB_OK) {
  211. G_warning(_("Database record for line %d (cat = %d, "
  212. "forward/both direction(s)) not found "
  213. "(forward/both direction(s) of line skipped)"),
  214. i, cat);
  215. dofw = 0;
  216. }
  217. if (abcol != NULL) {
  218. if (bctype == DB_C_TYPE_INT) {
  219. ret =
  220. db_CatValArray_get_value_int(&bvarr, cat, &bcost);
  221. bdcost = bcost;
  222. }
  223. else { /* DB_C_TYPE_DOUBLE */
  224. ret =
  225. db_CatValArray_get_value_double(&bvarr, cat,
  226. &bdcost);
  227. }
  228. if (ret != DB_OK) {
  229. G_warning(_("Database record for line %d (cat = %d, "
  230. "backword direction) not found"
  231. "(direction of line skipped)"), i, cat);
  232. dobw = 0;
  233. }
  234. }
  235. else {
  236. if (dofw)
  237. bdcost = dcost;
  238. else
  239. dobw = 0;
  240. }
  241. }
  242. }
  243. else {
  244. if (ll) {
  245. if (geo)
  246. dcost = Vect_line_geodesic_length(Points);
  247. else
  248. dcost = Vect_line_length(Points);
  249. }
  250. else
  251. dcost = Vect_line_length(Points);
  252. bdcost = dcost;
  253. }
  254. if (dofw && dcost != -1) {
  255. cost = (dglInt32_t) Map->dgraph.cost_multip * dcost;
  256. G_debug(5, "Add arc %d from %d to %d cost = %d", i, from, to,
  257. cost);
  258. ret =
  259. dglAddEdge(gr, (dglInt32_t) from, (dglInt32_t) to,
  260. (dglInt32_t) cost, (dglInt32_t) i);
  261. Map->dgraph.edge_fcosts[i] = dcost;
  262. if (ret < 0)
  263. G_fatal_error("Cannot add network arc");
  264. }
  265. G_debug(5, "bdcost = %f edge_bcosts = %f", bdcost,
  266. Map->dgraph.edge_bcosts[i]);
  267. if (dobw && bdcost != -1) {
  268. bcost = (dglInt32_t) Map->dgraph.cost_multip * bdcost;
  269. G_debug(5, "Add arc %d from %d to %d bcost = %d", -i, to, from,
  270. bcost);
  271. ret =
  272. dglAddEdge(gr, (dglInt32_t) to, (dglInt32_t) from,
  273. (dglInt32_t) bcost, (dglInt32_t) - i);
  274. Map->dgraph.edge_bcosts[i] = bdcost;
  275. if (ret < 0)
  276. G_fatal_error(_("Cannot add network arc"));
  277. }
  278. }
  279. if (afcol != NULL && skipped > 0)
  280. G_debug(2, "%d lines missing category of field %d skipped", skipped,
  281. afield);
  282. if (afcol != NULL) {
  283. db_close_database_shutdown_driver(driver);
  284. db_CatValArray_free(&fvarr);
  285. if (abcol != NULL) {
  286. db_CatValArray_free(&bvarr);
  287. }
  288. }
  289. /* Set node attributes */
  290. G_debug(2, "Register nodes");
  291. if (ncol != NULL) {
  292. double x, y, z;
  293. struct bound_box box;
  294. struct boxlist *List;
  295. List = Vect_new_boxlist(0);
  296. G_debug(2, "Set nodes' costs");
  297. if (nfield < 1)
  298. G_fatal_error("Node field < 1");
  299. G_message(_("Setting node costs..."));
  300. Fi = Vect_get_field(Map, nfield);
  301. if (Fi == NULL)
  302. G_fatal_error(_("Database connection not defined for layer %d"),
  303. nfield);
  304. driver = db_start_driver_open_database(Fi->driver, Fi->database);
  305. if (driver == NULL)
  306. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  307. Fi->database, Fi->driver);
  308. /* Load costs to array */
  309. if (db_get_column(driver, Fi->table, ncol, &Column) != DB_OK)
  310. G_fatal_error(_("Column <%s> not found in table <%s>"),
  311. ncol, Fi->table);
  312. fctype = db_sqltype_to_Ctype(db_get_column_sqltype(Column));
  313. if (fctype != DB_C_TYPE_INT && fctype != DB_C_TYPE_DOUBLE)
  314. G_fatal_error(_("Data type of column <%s> not supported (must be numeric)"),
  315. ncol);
  316. db_CatValArray_init(&fvarr);
  317. nrec =
  318. db_select_CatValArray(driver, Fi->table, Fi->key, ncol, NULL,
  319. &fvarr);
  320. G_debug(1, "node costs: nrec = %d", nrec);
  321. for (i = 1; i <= nnodes; i++) {
  322. /* TODO: what happens if we set attributes of not existing node (skipped lines,
  323. * nodes without lines) */
  324. /* select points at node */
  325. Vect_get_node_coor(Map, i, &x, &y, &z);
  326. box.E = box.W = x;
  327. box.N = box.S = y;
  328. box.T = box.B = z;
  329. Vect_select_lines_by_box(Map, &box, GV_POINT, List);
  330. G_debug(2, " node = %d nlines = %d", i, List->n_values);
  331. cfound = 0;
  332. dcost = 0;
  333. for (j = 0; j < List->n_values; j++) {
  334. line = List->id[j];
  335. G_debug(2, " line (%d) = %d", j, line);
  336. type = Vect_read_line(Map, NULL, Cats, line);
  337. if (!(type & GV_POINT))
  338. continue;
  339. if (Vect_cat_get(Cats, nfield, &cat)) { /* point with category of field found */
  340. /* Set costs */
  341. if (fctype == DB_C_TYPE_INT) {
  342. ret =
  343. db_CatValArray_get_value_int(&fvarr, cat, &cost);
  344. dcost = cost;
  345. }
  346. else { /* DB_C_TYPE_DOUBLE */
  347. ret =
  348. db_CatValArray_get_value_double(&fvarr, cat,
  349. &dcost);
  350. }
  351. if (ret != DB_OK) {
  352. G_warning(_("Database record for node %d (cat = %d) not found "
  353. "(cost set to 0)"), i, cat);
  354. }
  355. cfound = 1;
  356. break;
  357. }
  358. }
  359. if (!cfound) {
  360. G_debug(2,
  361. "Category of field %d not attached to any points in node %d"
  362. "(costs set to 0)", nfield, i);
  363. }
  364. if (dcost == -1) { /* closed */
  365. cost = -1;
  366. }
  367. else {
  368. cost = (dglInt32_t) Map->dgraph.cost_multip * dcost;
  369. }
  370. dgl_cost = cost;
  371. G_debug(3, "Set node's cost to %d", cost);
  372. dglNodeSet_Attr(gr, dglGetNode(gr, (dglInt32_t) i), &dgl_cost);
  373. Map->dgraph.node_costs[i] = dcost;
  374. }
  375. db_close_database_shutdown_driver(driver);
  376. db_CatValArray_free(&fvarr);
  377. Vect_destroy_boxlist(List);
  378. }
  379. G_message(_("Flattening the graph..."));
  380. ret = dglFlatten(gr);
  381. if (ret < 0)
  382. G_fatal_error(_("GngFlatten error"));
  383. /* init SP cache */
  384. /* disable to debug dglib cache */
  385. dglInitializeSPCache(gr, &(Map->dgraph.spCache));
  386. G_message(_("Graph was built"));
  387. return 0;
  388. }
  389. /*!
  390. \brief Find shortest path.
  391. Costs for 'from' and 'to' nodes are not considered (SP found even if
  392. 'from' or 'to' are 'closed' (costs = -1) and costs of these
  393. nodes are not added to SP costs result.
  394. \param Map vector map
  395. \param from from node
  396. \param to to node
  397. \param[out] List list of line ids (path)
  398. \param[out] cost costs value
  399. \return number of segments
  400. \return 0 is correct for from = to, or List == NULL ? sum of costs is better return value,
  401. \return -1 : destination unreachable
  402. */
  403. int
  404. Vect_net_shortest_path(struct Map_info *Map, int from, int to,
  405. struct ilist *List, double *cost)
  406. {
  407. int i, line, *pclip, cArc, nRet;
  408. dglSPReport_s *pSPReport;
  409. dglInt32_t nDistance;
  410. int use_cache = 1; /* set to 0 to disable dglib cache */
  411. G_debug(3, "Vect_net_shortest_path(): from = %d, to = %d", from, to);
  412. /* Note : if from == to dgl goes to nearest node and returns back (dgl feature) =>
  413. * check here for from == to */
  414. if (List != NULL)
  415. Vect_reset_list(List);
  416. /* Check if from and to are identical, otherwise dglib returns path to neares node and back! */
  417. if (from == to) {
  418. if (cost != NULL)
  419. *cost = 0;
  420. return 0;
  421. }
  422. From_node = from;
  423. pclip = NULL;
  424. if (List != NULL) {
  425. if (use_cache) {
  426. nRet =
  427. dglShortestPath(&(Map->dgraph.graph_s), &pSPReport, (dglInt32_t) from,
  428. (dglInt32_t) to, clipper, pclip, &(Map->dgraph.spCache));
  429. }
  430. else {
  431. nRet =
  432. dglShortestPath(&(Map->dgraph.graph_s), &pSPReport, (dglInt32_t) from,
  433. (dglInt32_t) to, clipper, pclip, NULL);
  434. }
  435. }
  436. else {
  437. if (use_cache) {
  438. nRet =
  439. dglShortestDistance(&(Map->dgraph.graph_s), &nDistance, (dglInt32_t) from,
  440. (dglInt32_t) to, clipper, pclip, &(Map->dgraph.spCache));
  441. }
  442. else {
  443. nRet =
  444. dglShortestDistance(&(Map->dgraph.graph_s), &nDistance, (dglInt32_t) from,
  445. (dglInt32_t) to, clipper, pclip, NULL);
  446. }
  447. }
  448. if (nRet == 0) {
  449. /* G_warning("Destination node %d is unreachable from node %d\n" , to , from); */
  450. if (cost != NULL)
  451. *cost = PORT_DOUBLE_MAX;
  452. return -1;
  453. }
  454. else if (nRet < 0) {
  455. G_warning(_("dglShortestPath error: %s"), dglStrerror(&(Map->dgraph.graph_s)));
  456. return -1;
  457. }
  458. if (List != NULL) {
  459. for (i = 0; i < pSPReport->cArc; i++) {
  460. line = dglEdgeGet_Id(&(Map->dgraph.graph_s), pSPReport->pArc[i].pnEdge);
  461. G_debug(2, "From %ld to %ld - cost %ld user %d distance %ld", pSPReport->pArc[i].nFrom, pSPReport->pArc[i].nTo, dglEdgeGet_Cost(&(Map->dgraph.graph_s), pSPReport->pArc[i].pnEdge) / Map->dgraph.cost_multip, /* this is the cost from clip() */
  462. line, pSPReport->pArc[i].nDistance);
  463. Vect_list_append(List, line);
  464. }
  465. }
  466. if (cost != NULL) {
  467. if (List != NULL)
  468. *cost = (double)pSPReport->nDistance / Map->dgraph.cost_multip;
  469. else
  470. *cost = (double)nDistance / Map->dgraph.cost_multip;
  471. }
  472. if (List != NULL) {
  473. cArc = pSPReport->cArc;
  474. dglFreeSPReport(&(Map->dgraph.graph_s), pSPReport);
  475. }
  476. else
  477. cArc = 0;
  478. return (cArc);
  479. }
  480. /*!
  481. \brief Get graph structure
  482. Graph is built by Vect_net_build_graph().
  483. Returns NULL when graph is not built.
  484. \param Map pointer to Map_info struct
  485. \return pointer to dglGraph_s struct or NULL
  486. */
  487. dglGraph_s *Vect_net_get_graph(struct Map_info *Map)
  488. {
  489. return &(Map->dgraph.graph_s);
  490. }
  491. /*!
  492. \brief Returns in cost for given direction in *cost.
  493. cost is set to -1 if closed.
  494. \param Map vector map
  495. \param line line id
  496. \param direction direction (GV_FORWARD, GV_BACKWARD)
  497. \param[out] cost
  498. \return 1 OK
  499. \return 0 does not exist (was not inserted)
  500. */
  501. int
  502. Vect_net_get_line_cost(const struct Map_info *Map, int line, int direction,
  503. double *cost)
  504. {
  505. /* dglInt32_t *pEdge; */
  506. G_debug(5, "Vect_net_get_line_cost(): line = %d, dir = %d", line,
  507. direction);
  508. if (direction == GV_FORWARD) {
  509. /* V1 has no index by line-id -> array used */
  510. /*
  511. pEdge = dglGetEdge(&(Map->dgraph.graph_s), line);
  512. if (pEdge == NULL)
  513. return 0;
  514. *cost = (double) dglEdgeGet_Cost(&(Map->dgraph.graph_s), pEdge);
  515. */
  516. if (Map->dgraph.edge_fcosts[line] == -1) {
  517. *cost = -1;
  518. return 0;
  519. }
  520. else
  521. *cost = Map->dgraph.edge_fcosts[line];
  522. }
  523. else if (direction == GV_BACKWARD) {
  524. /*
  525. pEdge = dglGetEdge(&(Map->dgraph.graph_s), -line);
  526. if (pEdge == NULL)
  527. return 0;
  528. *cost = (double) dglEdgeGet_Cost(&(Map->dgraph.graph_s), pEdge);
  529. */
  530. if (Map->dgraph.edge_bcosts[line] == -1) {
  531. *cost = -1;
  532. return 0;
  533. }
  534. else
  535. *cost = Map->dgraph.edge_bcosts[line];
  536. G_debug(5, "Vect_net_get_line_cost(): edge_bcosts = %f",
  537. Map->dgraph.edge_bcosts[line]);
  538. }
  539. else {
  540. G_fatal_error(_("Wrong line direction in Vect_net_get_line_cost()"));
  541. }
  542. return 1;
  543. }
  544. /*!
  545. \brief Get cost of node
  546. \param Map vector map
  547. \param node node id
  548. \param[out] cost costs value
  549. \return 1
  550. */
  551. int Vect_net_get_node_cost(const struct Map_info *Map, int node, double *cost)
  552. {
  553. G_debug(3, "Vect_net_get_node_cost(): node = %d", node);
  554. *cost = Map->dgraph.node_costs[node];
  555. G_debug(3, " -> cost = %f", *cost);
  556. return 1;
  557. }
  558. /*!
  559. \brief Find nearest node(s) on network.
  560. \param Map vetor map
  561. \param x,y,z point coordinates (z coordinate NOT USED !)
  562. \param direction (GV_FORWARD - from point to net, GV_BACKWARD - from net to point)
  563. \param maxdist maximum distance to the network
  564. \param[out] node1 pointer where to store the node number (or NULL)
  565. \param[out] node2 pointer where to store the node number (or NULL)
  566. \param[out] ln pointer where to store the nearest line number (or NULL)
  567. \param[out] costs1 pointer where to store costs on nearest line to node1 (not costs from x,y,z to the line) (or NULL)
  568. \param[out] costs2 pointer where to store costs on nearest line to node2 (not costs from x,y,z to the line) (or NULL)
  569. \param[out] Points1 pointer to structure where to store vertices on nearest line to node1 (or NULL)
  570. \param[out] Points2 pointer to structure where to store vertices on nearest line to node2 (or NULL)
  571. \param[out] pointer where to distance to the line (or NULL)
  572. \param[out] distance
  573. \return number of nodes found (0,1,2)
  574. */
  575. int Vect_net_nearest_nodes(struct Map_info *Map,
  576. double x, double y, double z,
  577. int direction, double maxdist,
  578. int *node1, int *node2, int *ln, double *costs1,
  579. double *costs2, struct line_pnts *Points1,
  580. struct line_pnts *Points2, double *distance)
  581. {
  582. int line, n1, n2, nnodes;
  583. int npoints;
  584. int segment; /* nearest line segment (first is 1) */
  585. static struct line_pnts *Points = NULL;
  586. double cx, cy, cz, c1, c2;
  587. double along; /* distance along the line to nearest point */
  588. double length;
  589. G_debug(3, "Vect_net_nearest_nodes() x = %f y = %f", x, y);
  590. /* Reset */
  591. if (node1)
  592. *node1 = 0;
  593. if (node2)
  594. *node2 = 0;
  595. if (ln)
  596. *ln = 0;
  597. if (costs1)
  598. *costs1 = PORT_DOUBLE_MAX;
  599. if (costs2)
  600. *costs2 = PORT_DOUBLE_MAX;
  601. if (Points1)
  602. Vect_reset_line(Points1);
  603. if (Points2)
  604. Vect_reset_line(Points2);
  605. if (distance)
  606. *distance = PORT_DOUBLE_MAX;
  607. if (!Points)
  608. Points = Vect_new_line_struct();
  609. /* Find nearest line */
  610. line = Vect_find_line(Map, x, y, z, Map->dgraph.line_type, maxdist, 0, 0);
  611. if (line < 1)
  612. return 0;
  613. Vect_read_line(Map, Points, NULL, line);
  614. npoints = Points->n_points;
  615. Vect_get_line_nodes(Map, line, &n1, &n2);
  616. segment =
  617. Vect_line_distance(Points, x, y, z, 0, &cx, &cy, &cz, distance, NULL,
  618. &along);
  619. G_debug(4, "line = %d n1 = %d n2 = %d segment = %d", line, n1, n2,
  620. segment);
  621. /* Check first or last point and return one node in that case */
  622. G_debug(4, "cx = %f cy = %f first = %f %f last = %f %f", cx, cy,
  623. Points->x[0], Points->y[0], Points->x[npoints - 1],
  624. Points->y[npoints - 1]);
  625. if (Points->x[0] == cx && Points->y[0] == cy) {
  626. if (node1)
  627. *node1 = n1;
  628. if (ln)
  629. *ln = line;
  630. if (costs1)
  631. *costs1 = 0;
  632. if (Points1) {
  633. Vect_append_point(Points1, x, y, z);
  634. Vect_append_point(Points1, cx, cy, cz);
  635. }
  636. G_debug(3, "first node nearest");
  637. return 1;
  638. }
  639. if (Points->x[npoints - 1] == cx && Points->y[npoints - 1] == cy) {
  640. if (node1)
  641. *node1 = n2;
  642. if (ln)
  643. *ln = line;
  644. if (costs1)
  645. *costs1 = 0;
  646. if (Points1) {
  647. Vect_append_point(Points1, x, y, z);
  648. Vect_append_point(Points1, cx, cy, cz);
  649. }
  650. G_debug(3, "last node nearest");
  651. return 1;
  652. }
  653. nnodes = 2;
  654. /* c1 - costs to get from/to the first vertex */
  655. /* c2 - costs to get from/to the last vertex */
  656. if (direction == GV_FORWARD) { /* from point to net */
  657. Vect_net_get_line_cost(Map, line, GV_BACKWARD, &c1);
  658. Vect_net_get_line_cost(Map, line, GV_FORWARD, &c2);
  659. }
  660. else {
  661. Vect_net_get_line_cost(Map, line, GV_FORWARD, &c1);
  662. Vect_net_get_line_cost(Map, line, GV_BACKWARD, &c2);
  663. }
  664. if (c1 < 0)
  665. nnodes--;
  666. if (c2 < 0)
  667. nnodes--;
  668. if (nnodes == 0)
  669. return 0; /* both directions closed */
  670. length = Vect_line_length(Points);
  671. if (ln)
  672. *ln = line;
  673. if (nnodes == 1 && c1 < 0) { /* first direction is closed, return node2 as node1 */
  674. if (node1)
  675. *node1 = n2;
  676. if (costs1) { /* to node 2, i.e. forward */
  677. *costs1 = c2 * (length - along) / length;
  678. }
  679. if (Points1) { /* to node 2, i.e. forward */
  680. int i;
  681. if (direction == GV_FORWARD) { /* from point to net */
  682. Vect_append_point(Points1, x, y, z);
  683. Vect_append_point(Points1, cx, cy, cz);
  684. for (i = segment; i < npoints; i++)
  685. Vect_append_point(Points1, Points->x[i], Points->y[i],
  686. Points->z[i]);
  687. }
  688. else {
  689. for (i = npoints - 1; i >= segment; i--)
  690. Vect_append_point(Points1, Points->x[i], Points->y[i],
  691. Points->z[i]);
  692. Vect_append_point(Points1, cx, cy, cz);
  693. Vect_append_point(Points1, x, y, z);
  694. }
  695. }
  696. }
  697. else {
  698. if (node1)
  699. *node1 = n1;
  700. if (node2)
  701. *node2 = n2;
  702. if (costs1) { /* to node 1, i.e. backward */
  703. *costs1 = c1 * along / length;
  704. }
  705. if (costs2) { /* to node 2, i.e. forward */
  706. *costs2 = c2 * (length - along) / length;
  707. }
  708. if (Points1) { /* to node 1, i.e. backward */
  709. int i;
  710. if (direction == GV_FORWARD) { /* from point to net */
  711. Vect_append_point(Points1, x, y, z);
  712. Vect_append_point(Points1, cx, cy, cz);
  713. for (i = segment - 1; i >= 0; i--)
  714. Vect_append_point(Points1, Points->x[i], Points->y[i],
  715. Points->z[i]);
  716. }
  717. else {
  718. for (i = 0; i < segment; i++)
  719. Vect_append_point(Points1, Points->x[i], Points->y[i],
  720. Points->z[i]);
  721. Vect_append_point(Points1, cx, cy, cz);
  722. Vect_append_point(Points1, x, y, z);
  723. }
  724. }
  725. if (Points2) { /* to node 2, i.e. forward */
  726. int i;
  727. if (direction == GV_FORWARD) { /* from point to net */
  728. Vect_append_point(Points2, x, y, z);
  729. Vect_append_point(Points2, cx, cy, cz);
  730. for (i = segment; i < npoints; i++)
  731. Vect_append_point(Points2, Points->x[i], Points->y[i],
  732. Points->z[i]);
  733. }
  734. else {
  735. for (i = npoints - 1; i >= segment; i--)
  736. Vect_append_point(Points2, Points->x[i], Points->y[i],
  737. Points->z[i]);
  738. Vect_append_point(Points2, cx, cy, cz);
  739. Vect_append_point(Points2, x, y, z);
  740. }
  741. }
  742. }
  743. return nnodes;
  744. }
  745. /*!
  746. \brief Find shortest path on network between 2 points given by coordinates.
  747. \param Map vector map
  748. \param fx,fy,fz from point x coordinate (z ignored)
  749. \param tx,ty,tz to point x coordinate (z ignored)
  750. \param fmax maximum distance to the network from 'from'
  751. \param tmax maximum distance to the network from 'to'
  752. \param[out] costs pointer where to store costs on the network (or NULL)
  753. \param[out] Points pointer to the structure where to store vertices of shortest path (or NULL)
  754. \param[out] List pointer to the structure where list of lines on the network is stored (or NULL)
  755. \param[out] FPoints pointer to the structure where to store line from 'from' to first network node (or NULL)
  756. \param[out] TPoints pointer to the structure where to store line from last network node to 'to' (or NULL)
  757. \param[out] fdist distance from 'from' to the net (or NULL)
  758. \param[out] tdist distance from 'to' to the net (or NULL)
  759. \return 1 OK
  760. \return 0 not reachable
  761. */
  762. int
  763. Vect_net_shortest_path_coor(struct Map_info *Map,
  764. double fx, double fy, double fz, double tx,
  765. double ty, double tz, double fmax, double tmax,
  766. double *costs, struct line_pnts *Points,
  767. struct ilist *List, struct line_pnts *FPoints,
  768. struct line_pnts *TPoints, double *fdist,
  769. double *tdist)
  770. {
  771. return Vect_net_shortest_path_coor2(Map, fx, fy, fz, tx, ty, tz, fmax, tmax,
  772. costs, Points, List, NULL, FPoints, TPoints, fdist, tdist);
  773. }
  774. /*!
  775. \brief Find shortest path on network between 2 points given by coordinates.
  776. \param Map vector map
  777. \param fx,fy,fz from point x coordinate (z ignored)
  778. \param tx,ty,tz to point x coordinate (z ignored)
  779. \param fmax maximum distance to the network from 'from'
  780. \param tmax maximum distance to the network from 'to'
  781. \param costs pointer where to store costs on the network (or NULL)
  782. \param Points pointer to the structure where to store vertices of shortest path (or NULL)
  783. \param List pointer to the structure where list of lines on the network is stored (or NULL)
  784. \param NodesList pointer to the structure where list of nodes on the network is stored (or NULL)
  785. \param FPoints pointer to the structure where to store line from 'from' to first network node (or NULL)
  786. \param TPoints pointer to the structure where to store line from last network node to 'to' (or NULL)
  787. \param fdist distance from 'from' to the net (or NULL)
  788. \param tdist distance from 'to' to the net (or NULL)
  789. \return 1 OK, 0 not reachable
  790. */
  791. int
  792. Vect_net_shortest_path_coor2(struct Map_info *Map,
  793. double fx, double fy, double fz, double tx,
  794. double ty, double tz, double fmax, double tmax,
  795. double *costs, struct line_pnts *Points,
  796. struct ilist *List, struct ilist *NodesList,
  797. struct line_pnts *FPoints,
  798. struct line_pnts *TPoints, double *fdist,
  799. double *tdist)
  800. {
  801. int fnode[2], tnode[2]; /* nearest nodes, *node[1] is 0 if only one was found */
  802. double fcosts[2], tcosts[2], cur_cst; /* costs to nearest nodes on the network */
  803. int nfnodes, ntnodes, fline, tline;
  804. static struct line_pnts *APoints, *SPoints, *fPoints[2], *tPoints[2];
  805. static struct ilist *LList;
  806. static int first = 1;
  807. int reachable, shortcut;
  808. int i, j, fn = 0, tn = 0;
  809. /* from/to_point_node is set if from/to point projected to line
  810. *falls exactly on node (shortcut -> fline == tline) */
  811. int from_point_node = 0;
  812. int to_point_node = 0;
  813. G_debug(3, "Vect_net_shortest_path_coor()");
  814. if (first) {
  815. APoints = Vect_new_line_struct();
  816. SPoints = Vect_new_line_struct();
  817. fPoints[0] = Vect_new_line_struct();
  818. fPoints[1] = Vect_new_line_struct();
  819. tPoints[0] = Vect_new_line_struct();
  820. tPoints[1] = Vect_new_line_struct();
  821. LList = Vect_new_list();
  822. first = 0;
  823. }
  824. /* Reset */
  825. if (costs)
  826. *costs = PORT_DOUBLE_MAX;
  827. if (Points)
  828. Vect_reset_line(Points);
  829. if (fdist)
  830. *fdist = 0;
  831. if (tdist)
  832. *tdist = 0;
  833. if (List)
  834. List->n_values = 0;
  835. if (FPoints)
  836. Vect_reset_line(FPoints);
  837. if (TPoints)
  838. Vect_reset_line(TPoints);
  839. if (NodesList != NULL)
  840. Vect_reset_list(NodesList);
  841. /* Find nearest nodes */
  842. fnode[0] = fnode[1] = tnode[0] = tnode[1] = 0;
  843. nfnodes =
  844. Vect_net_nearest_nodes(Map, fx, fy, fz, GV_FORWARD, fmax, &(fnode[0]),
  845. &(fnode[1]), &fline, &(fcosts[0]),
  846. &(fcosts[1]), fPoints[0], fPoints[1], fdist);
  847. if (nfnodes == 0)
  848. return 0;
  849. if (nfnodes == 1 && fPoints[0]->n_points < 3) {
  850. from_point_node = fnode[0];
  851. }
  852. ntnodes =
  853. Vect_net_nearest_nodes(Map, tx, ty, tz, GV_BACKWARD, tmax,
  854. &(tnode[0]), &(tnode[1]), &tline, &(tcosts[0]),
  855. &(tcosts[1]), tPoints[0], tPoints[1], tdist);
  856. if (ntnodes == 0)
  857. return 0;
  858. if (ntnodes == 1 && tPoints[0]->n_points < 3) {
  859. to_point_node = tnode[0];
  860. }
  861. G_debug(3, "fline = %d tline = %d", fline, tline);
  862. reachable = shortcut = 0;
  863. cur_cst = PORT_DOUBLE_MAX;
  864. /* It may happen, that 2 points are at the same line. */
  865. /* TODO?: it could also happen that fline != tline but both points are on the same
  866. * line if they fall on node but a different line was found. This case is correctly
  867. * handled as normal non shortcut, but it could be added here. In that case
  868. * NodesList collection must be changed */
  869. if (fline == tline && (nfnodes > 1 || ntnodes > 1)) {
  870. double len, flen, tlen, c, fseg, tseg;
  871. double fcx, fcy, fcz, tcx, tcy, tcz;
  872. Vect_read_line(Map, APoints, NULL, fline);
  873. len = Vect_line_length(APoints);
  874. /* distance along the line */
  875. fseg =
  876. Vect_line_distance(APoints, fx, fy, fz, 0, &fcx, &fcy, &fcz, NULL,
  877. NULL, &flen);
  878. tseg =
  879. Vect_line_distance(APoints, tx, ty, tz, 0, &tcx, &tcy, &tcz, NULL,
  880. NULL, &tlen);
  881. Vect_reset_line(SPoints);
  882. if (flen == tlen) {
  883. cur_cst = 0;
  884. reachable = shortcut = 1;
  885. }
  886. else if (flen < tlen) {
  887. Vect_net_get_line_cost(Map, fline, GV_FORWARD, &c);
  888. if (c >= 0) {
  889. cur_cst = c * (tlen - flen) / len;
  890. Vect_append_point(SPoints, fx, fy, fz);
  891. Vect_append_point(SPoints, fcx, fcy, fcz);
  892. for (i = fseg; i < tseg; i++)
  893. Vect_append_point(SPoints, APoints->x[i], APoints->y[i],
  894. APoints->z[i]);
  895. Vect_append_point(SPoints, tcx, tcy, tcz);
  896. Vect_append_point(SPoints, tx, ty, tz);
  897. reachable = shortcut = 1;
  898. }
  899. }
  900. else { /* flen > tlen */
  901. Vect_net_get_line_cost(Map, fline, GV_BACKWARD, &c);
  902. if (c >= 0) {
  903. cur_cst = c * (flen - tlen) / len;
  904. Vect_append_point(SPoints, fx, fy, fz);
  905. Vect_append_point(SPoints, fcx, fcy, fcz);
  906. for (i = fseg - 1; i >= tseg; i--)
  907. Vect_append_point(SPoints, APoints->x[i], APoints->y[i],
  908. APoints->z[i]);
  909. Vect_append_point(SPoints, tcx, tcy, tcz);
  910. Vect_append_point(SPoints, tx, ty, tz);
  911. reachable = shortcut = 1;
  912. }
  913. }
  914. }
  915. /* Find the shortest variant from maximum 4 */
  916. for (i = 0; i < nfnodes; i++) {
  917. for (j = 0; j < ntnodes; j++) {
  918. double ncst, cst;
  919. int ret;
  920. G_debug(3, "i = %d fnode = %d j = %d tnode = %d", i, fnode[i], j,
  921. tnode[j]);
  922. ret =
  923. Vect_net_shortest_path(Map, fnode[i], tnode[j], NULL, &ncst);
  924. if (ret == -1)
  925. continue; /* not reachable */
  926. cst = fcosts[i] + ncst + tcosts[j];
  927. if (reachable == 0 || cst < cur_cst) {
  928. cur_cst = cst;
  929. fn = i;
  930. tn = j;
  931. shortcut = 0;
  932. }
  933. reachable = 1;
  934. }
  935. }
  936. G_debug(3, "reachable = %d shortcut = %d cur_cst = %f", reachable,
  937. shortcut, cur_cst);
  938. if (reachable) {
  939. if (shortcut) {
  940. if (Points)
  941. Vect_append_points(Points, SPoints, GV_FORWARD);
  942. if (NodesList) {
  943. /* Check if from/to point projected to line falls on node and
  944. *add it to the list */
  945. if (from_point_node > 0)
  946. Vect_list_append(NodesList, from_point_node);
  947. if (to_point_node > 0)
  948. Vect_list_append(NodesList, to_point_node);
  949. }
  950. }
  951. else {
  952. if (NodesList) {
  953. /* it can happen that starting point falls on node but SP starts
  954. * form the other node, add it in that case,
  955. * similarly for to point below */
  956. if (from_point_node > 0 && from_point_node != fnode[fn]) {
  957. Vect_list_append(NodesList, from_point_node);
  958. }
  959. /* add starting net SP search node */
  960. Vect_list_append(NodesList, fnode[fn]);
  961. }
  962. Vect_net_shortest_path(Map, fnode[fn], tnode[tn], LList,
  963. NULL);
  964. G_debug(3, "Number of lines %d", LList->n_values);
  965. if (Points)
  966. Vect_append_points(Points, fPoints[fn], GV_FORWARD);
  967. if (FPoints)
  968. Vect_append_points(FPoints, fPoints[fn], GV_FORWARD);
  969. for (i = 0; i < LList->n_values; i++) {
  970. int line;
  971. line = LList->value[i];
  972. G_debug(3, "i = %d line = %d", i, line);
  973. if (Points) {
  974. Vect_read_line(Map, APoints, NULL, abs(line));
  975. if (line > 0)
  976. Vect_append_points(Points, APoints, GV_FORWARD);
  977. else
  978. Vect_append_points(Points, APoints, GV_BACKWARD);
  979. }
  980. if (NodesList) {
  981. int node, node1, node2;
  982. Vect_get_line_nodes(Map, abs(line), &node1, &node2);
  983. /* add the second node, the first of first segmet was alread added */
  984. if (line > 0)
  985. node = node2;
  986. else
  987. node = node1;
  988. Vect_list_append(NodesList, node);
  989. }
  990. if (List)
  991. Vect_list_append(List, line);
  992. }
  993. if (Points)
  994. Vect_append_points(Points, tPoints[tn], GV_FORWARD);
  995. if (TPoints)
  996. Vect_append_points(TPoints, tPoints[tn], GV_FORWARD);
  997. if (NodesList) {
  998. if (to_point_node > 0 && to_point_node != tnode[tn]) {
  999. Vect_list_append(NodesList, to_point_node);
  1000. }
  1001. }
  1002. }
  1003. if (costs)
  1004. *costs = cur_cst;
  1005. }
  1006. return reachable;
  1007. }