select.c 3.2 KB

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