main.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /****************************************************************
  2. *
  3. * MODULE: d.path
  4. *
  5. * AUTHOR(S): Radim Blazek
  6. *
  7. * PURPOSE: shortest path networking on vector map
  8. * Uses the DGLib from Roberto Micarelli
  9. *
  10. * COPYRIGHT: (C) 2002 by the GRASS Development Team
  11. *
  12. * This program is free software under the
  13. * GNU General Public License (>=v2).
  14. * Read the file COPYING that comes with GRASS
  15. * for details.
  16. *
  17. ****************************************************************/
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <grass/gis.h>
  21. #include <grass/raster.h>
  22. #include <grass/display.h>
  23. #include <grass/colors.h>
  24. #include <grass/vector.h>
  25. #include <grass/dbmi.h>
  26. #include <grass/glocale.h>
  27. #include "proto.h"
  28. int main(int argc, char **argv)
  29. {
  30. struct Option *map, *afield_opt, *nfield_opt, *afcol, *abcol, *ncol,
  31. *type_opt;
  32. struct Option *color_opt, *hcolor_opt, *bgcolor_opt, *coor_opt;
  33. struct Flag *geo_f, *bold_f;
  34. struct GModule *module;
  35. struct Map_info Map;
  36. int type, afield, nfield, geo;
  37. struct color_rgb color, hcolor, bgcolor;
  38. int r, g, b;
  39. double x1, y1, x2, y2;
  40. /* Initialize the GIS calls */
  41. G_gisinit(argv[0]);
  42. module = G_define_module();
  43. G_add_keyword(_("display"));
  44. G_add_keyword(_("network"));
  45. G_add_keyword(_("shortest path"));
  46. module->description =
  47. _("Finds shortest path for selected starting and ending node.");
  48. map = G_define_standard_option(G_OPT_V_MAP);
  49. type_opt = G_define_standard_option(G_OPT_V_TYPE);
  50. type_opt->key = "arc_type";
  51. type_opt->options = "line,boundary";
  52. type_opt->answer = "line,boundary";
  53. type_opt->description = _("Arc type");
  54. coor_opt = G_define_standard_option(G_OPT_M_COORDS);
  55. coor_opt->key_desc = "x1,y1,x2,y2";
  56. coor_opt->required = YES;
  57. coor_opt->description = _("Starting and ending coordinates");
  58. afield_opt = G_define_standard_option(G_OPT_V_FIELD);
  59. afield_opt->key = "arc_layer";
  60. afield_opt->answer = "1";
  61. afield_opt->label = _("Arc layer");
  62. nfield_opt = G_define_standard_option(G_OPT_V_FIELD);
  63. nfield_opt->key = "node_layer";
  64. nfield_opt->answer = "2";
  65. nfield_opt->label = _("Node layer");
  66. afcol = G_define_option();
  67. afcol->key = "arc_column";
  68. afcol->type = TYPE_STRING;
  69. afcol->required = NO;
  70. afcol->description = _("Arc forward/both direction(s) cost column (number)");
  71. abcol = G_define_option();
  72. abcol->key = "arc_backward_column";
  73. abcol->type = TYPE_STRING;
  74. abcol->required = NO;
  75. abcol->description = _("Arc backward direction cost column (number)");
  76. ncol = G_define_option();
  77. ncol->key = "node_column";
  78. ncol->type = TYPE_STRING;
  79. ncol->required = NO;
  80. ncol->description = _("Node cost column");
  81. color_opt = G_define_option();
  82. color_opt->key = "color";
  83. color_opt->type = TYPE_STRING;
  84. color_opt->answer = DEFAULT_FG_COLOR;
  85. color_opt->description = _("Original line color");
  86. color_opt->gisprompt = "old_color,color,color";
  87. color_opt->guisection = _("Rendering");
  88. hcolor_opt = G_define_option();
  89. hcolor_opt->key = "highlight_color";
  90. hcolor_opt->type = TYPE_STRING;
  91. hcolor_opt->answer = "red";
  92. hcolor_opt->description = _("Highlight color");
  93. hcolor_opt->gisprompt = "old_color,color,color";
  94. hcolor_opt->guisection = _("Rendering");
  95. bgcolor_opt = G_define_option();
  96. bgcolor_opt->key = "bgcolor";
  97. bgcolor_opt->type = TYPE_STRING;
  98. bgcolor_opt->answer = DEFAULT_BG_COLOR;
  99. bgcolor_opt->description = _("Background color");
  100. bgcolor_opt->gisprompt = "old_color,color,color";
  101. bgcolor_opt->guisection = _("Rendering");
  102. geo_f = G_define_flag();
  103. geo_f->key = 'g';
  104. geo_f->description =
  105. _("Use geodesic calculation for longitude-latitude locations");
  106. bold_f = G_define_flag();
  107. bold_f->key = 'b';
  108. bold_f->description = _("Render bold lines");
  109. bold_f->guisection = _("Rendering");
  110. if (G_parser(argc, argv))
  111. exit(EXIT_FAILURE);
  112. type = Vect_option_to_types(type_opt);
  113. afield = atoi(afield_opt->answer);
  114. nfield = atoi(nfield_opt->answer);
  115. if (coor_opt->answers[0] == NULL)
  116. G_fatal_error(_("No coordinates given"));
  117. if (!G_scan_easting(coor_opt->answers[0], &x1, G_projection()))
  118. G_fatal_error(_("%s - illegal x value"), coor_opt->answers[0]);
  119. if (!G_scan_northing(coor_opt->answers[1], &y1, G_projection()))
  120. G_fatal_error(_("%s - illegal y value"), coor_opt->answers[1]);
  121. if (!G_scan_easting(coor_opt->answers[2], &x2, G_projection()))
  122. G_fatal_error(_("%s - illegal x value"), coor_opt->answers[2]);
  123. if (!G_scan_northing(coor_opt->answers[3], &y2, G_projection()))
  124. G_fatal_error(_("%s - illegal y value"), coor_opt->answers[3]);
  125. D_open_driver();
  126. color = G_standard_color_rgb(BLACK);
  127. if (G_str_to_color(color_opt->answer, &r, &g, &b)) {
  128. color.r = r;
  129. color.g = g;
  130. color.b = b;
  131. }
  132. hcolor = G_standard_color_rgb(RED);
  133. if (G_str_to_color(hcolor_opt->answer, &r, &g, &b)) {
  134. hcolor.r = r;
  135. hcolor.g = g;
  136. hcolor.b = b;
  137. }
  138. bgcolor = G_standard_color_rgb(WHITE);
  139. if (G_str_to_color(bgcolor_opt->answer, &r, &g, &b)) {
  140. bgcolor.r = r;
  141. bgcolor.g = g;
  142. bgcolor.b = b;
  143. }
  144. if (geo_f->answer) {
  145. geo = 1;
  146. if (G_projection() != PROJECTION_LL)
  147. G_fatal_error(_("The current projection is not longitude-latitude"));
  148. }
  149. else
  150. geo = 0;
  151. Vect_set_open_level(2);
  152. if (Vect_open_old(&Map, map->answer, "") < 0)
  153. G_fatal_error(_("Unable to open vector map <%s>"), map->answer);
  154. D_setup(0);
  155. Vect_net_build_graph(&Map, type, afield, nfield, afcol->answer,
  156. abcol->answer, ncol->answer, geo, 0);
  157. coor_path(&Map, &hcolor, bold_f->answer, x1, y1, x2, y2);
  158. D_close_driver();
  159. Vect_close(&Map);
  160. exit(EXIT_SUCCESS);
  161. }