select.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <math.h>
  5. #include <grass/gis.h>
  6. #include <grass/display.h>
  7. #include <grass/vector.h>
  8. #include <grass/colors.h>
  9. #include <grass/glocale.h>
  10. #include "proto.h"
  11. #define WDTH 5
  12. int display(struct Map_info *Map, struct line_pnts *Points,
  13. const struct color_rgb *color, int first, int last, int be_bold)
  14. {
  15. int from, to;
  16. D_RGB_color(color->r, color->g, color->b);
  17. if (first)
  18. from = 0;
  19. else
  20. from = 1;
  21. if (last)
  22. to = Points->n_points;
  23. else
  24. to = Points->n_points - 1;
  25. if (be_bold)
  26. D_line_width(2);
  27. D_polyline_abs(&Points->x[from], &Points->y[from], to - from);
  28. if (be_bold)
  29. D_line_width(0);
  30. return 0;
  31. }
  32. /* Same as path() but get start/stop from the command line (for non-interactive use)
  33. Hamish Bowman March 2007 */
  34. int coor_path(struct Map_info *Map, const struct color_rgb *hcolor,
  35. int be_bold, double start_x, double start_y,
  36. double end_x, double end_y)
  37. {
  38. int ret;
  39. double nx, ny, fx, fy, tx, ty, msize, maxdist;
  40. struct line_pnts *Points;
  41. int start_node, end_node;
  42. double fdist, tdist, cost;
  43. Points = Vect_new_line_struct();
  44. msize = 10 * (D_d_to_u_col(2.0) - D_d_to_u_col(1.0)); /* do it better */
  45. G_debug(1, "msize = %f\n", msize);
  46. /*
  47. x1 = D_d_to_u_col ((double)(screen_x-WDTH));
  48. y1 = D_d_to_u_row ((double)(screen_y-WDTH));
  49. x2 = D_d_to_u_col ((double)(screen_x+WDTH));
  50. y2 = D_d_to_u_row ((double)(screen_y+WDTH));
  51. x1 = fabs ( x2 - x1 );
  52. y1 = fabs ( y2 - y1 );
  53. if ( x1 > y1 ) maxdist = x1;
  54. else maxdist = y1;
  55. */
  56. /** maxdist = 10 pixels on the display (WDTH*2); ?
  57. ** ie related to zoom level ?? just use msize ?? **/
  58. maxdist = msize;
  59. G_debug(1, "Maximum distance in map units = %f\n", maxdist);
  60. /* Vect_find_node(): find number of nearest node, 0 if not found */
  61. start_node = Vect_find_node(Map, start_x, start_y, 0.0, maxdist, 0);
  62. if (start_node > 0) {
  63. Vect_get_node_coor(Map, start_node, &nx, &ny, NULL);
  64. fprintf(stderr, _("Node %d: %f %f\n"), start_node, nx, ny);
  65. }
  66. if (start_node > 0) {
  67. fx = nx;
  68. fy = ny;
  69. }
  70. else {
  71. fx = start_x;
  72. fy = start_y;
  73. }
  74. D_RGB_color(hcolor->r, hcolor->g, hcolor->b);
  75. D_plot_icon(fx, fy, G_ICON_BOX, 0.0, msize);
  76. end_node = Vect_find_node(Map, end_x, end_y, 0.0, maxdist, 0);
  77. if (end_node > 0) {
  78. Vect_get_node_coor(Map, end_node, &nx, &ny, NULL);
  79. fprintf(stderr, _("Node %d: %f %f\n"), end_node, nx, ny);
  80. }
  81. if (end_node > 0) {
  82. tx = nx;
  83. ty = ny;
  84. }
  85. else {
  86. tx = end_x;
  87. ty = end_y;
  88. }
  89. D_RGB_color(hcolor->r, hcolor->g, hcolor->b);
  90. D_plot_icon(tx, ty, G_ICON_CROSS, 0.0, msize);
  91. G_debug(2, "find path %f %f -> %f %f", fx, fy, tx, ty);
  92. ret =
  93. Vect_net_shortest_path_coor(Map, fx, fy, 0.0, tx, ty, 0.0,
  94. 5 * maxdist, 5 * maxdist, &cost, Points,
  95. NULL, NULL, NULL, NULL, &fdist, &tdist);
  96. if (ret == 0) {
  97. fprintf(stdout, _("Destination unreachable\n"));
  98. }
  99. else {
  100. fprintf(stdout, _("Costs on the network = %f\n"), cost);
  101. fprintf(stdout, _(" Distance to the network = %f, "
  102. "distance from the network = %f\n\n"),
  103. fdist, tdist);
  104. display(Map, Points, hcolor, 1, 1, be_bold);
  105. }
  106. return 0;
  107. }