net.c 29 KB

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