main.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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(_("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->options = "line,boundary";
  51. type_opt->answer = "line,boundary";
  52. type_opt->description = _("Arc type");
  53. coor_opt = G_define_option();
  54. coor_opt->key = "coor";
  55. coor_opt->key_desc = "x1,y1,x2,y2";
  56. coor_opt->type = TYPE_STRING;
  57. coor_opt->required = YES;
  58. coor_opt->description = _("Starting and ending coordinates");
  59. afield_opt = G_define_standard_option(G_OPT_V_FIELD);
  60. afield_opt->key = "alayer";
  61. afield_opt->answer = "1";
  62. afield_opt->description = _("Arc layer");
  63. nfield_opt = G_define_standard_option(G_OPT_V_FIELD);
  64. nfield_opt->key = "nlayer";
  65. nfield_opt->answer = "2";
  66. nfield_opt->description = _("Node layer");
  67. afcol = G_define_option();
  68. afcol->key = "afcol";
  69. afcol->type = TYPE_STRING;
  70. afcol->required = NO;
  71. afcol->description = _("Arc forward/both direction(s) cost column");
  72. abcol = G_define_option();
  73. abcol->key = "abcol";
  74. abcol->type = TYPE_STRING;
  75. abcol->required = NO;
  76. abcol->description = _("Arc backward direction cost column");
  77. ncol = G_define_option();
  78. ncol->key = "ncol";
  79. ncol->type = TYPE_STRING;
  80. ncol->required = NO;
  81. ncol->description = _("Node cost column");
  82. color_opt = G_define_option();
  83. color_opt->key = "color";
  84. color_opt->type = TYPE_STRING;
  85. color_opt->answer = DEFAULT_FG_COLOR;
  86. color_opt->description = _("Original line color");
  87. color_opt->gisprompt = "old_color,color,color";
  88. color_opt->guisection = _("Rendering");
  89. hcolor_opt = G_define_option();
  90. hcolor_opt->key = "hcolor";
  91. hcolor_opt->type = TYPE_STRING;
  92. hcolor_opt->answer = "red";
  93. hcolor_opt->description = _("Highlight color");
  94. hcolor_opt->gisprompt = "old_color,color,color";
  95. hcolor_opt->guisection = _("Rendering");
  96. bgcolor_opt = G_define_option();
  97. bgcolor_opt->key = "bgcolor";
  98. bgcolor_opt->type = TYPE_STRING;
  99. bgcolor_opt->answer = DEFAULT_BG_COLOR;
  100. bgcolor_opt->description = _("Background color");
  101. bgcolor_opt->gisprompt = "old_color,color,color";
  102. bgcolor_opt->guisection = _("Rendering");
  103. geo_f = G_define_flag();
  104. geo_f->key = 'g';
  105. geo_f->description =
  106. _("Use geodesic calculation for longitude-latitude locations");
  107. bold_f = G_define_flag();
  108. bold_f->key = 'b';
  109. bold_f->description = _("Render bold lines");
  110. bold_f->guisection = _("Rendering");
  111. if (G_parser(argc, argv))
  112. exit(EXIT_FAILURE);
  113. type = Vect_option_to_types(type_opt);
  114. afield = atoi(afield_opt->answer);
  115. nfield = atoi(nfield_opt->answer);
  116. if (coor_opt->answers[0] == NULL)
  117. G_fatal_error(_("No coordinates given"));
  118. if (!G_scan_easting(coor_opt->answers[0], &x1, G_projection()))
  119. G_fatal_error(_("%s - illegal x value"), coor_opt->answers[0]);
  120. if (!G_scan_northing(coor_opt->answers[1], &y1, G_projection()))
  121. G_fatal_error(_("%s - illegal y value"), coor_opt->answers[1]);
  122. if (!G_scan_easting(coor_opt->answers[2], &x2, G_projection()))
  123. G_fatal_error(_("%s - illegal x value"), coor_opt->answers[2]);
  124. if (!G_scan_northing(coor_opt->answers[3], &y2, G_projection()))
  125. G_fatal_error(_("%s - illegal y value"), coor_opt->answers[3]);
  126. if (D_open_driver() != 0)
  127. G_fatal_error(_("No graphics device selected. "
  128. "Use d.mon to select graphics device."));
  129. color = G_standard_color_rgb(BLACK);
  130. if (G_str_to_color(color_opt->answer, &r, &g, &b)) {
  131. color.r = r;
  132. color.g = g;
  133. color.b = b;
  134. }
  135. hcolor = G_standard_color_rgb(RED);
  136. if (G_str_to_color(hcolor_opt->answer, &r, &g, &b)) {
  137. hcolor.r = r;
  138. hcolor.g = g;
  139. hcolor.b = b;
  140. }
  141. bgcolor = G_standard_color_rgb(WHITE);
  142. if (G_str_to_color(bgcolor_opt->answer, &r, &g, &b)) {
  143. bgcolor.r = r;
  144. bgcolor.g = g;
  145. bgcolor.b = b;
  146. }
  147. if (geo_f->answer) {
  148. geo = 1;
  149. if (G_projection() != PROJECTION_LL)
  150. G_fatal_error(_("The current projection is not longitude-latitude"));
  151. }
  152. else
  153. geo = 0;
  154. Vect_set_open_level(2);
  155. Vect_open_old(&Map, map->answer, "");
  156. D_setup(0);
  157. Vect_net_build_graph(&Map, type, afield, nfield, afcol->answer,
  158. abcol->answer, ncol->answer, geo, 0);
  159. coor_path(&Map, &hcolor, bold_f->answer, x1, y1, x2, y2);
  160. D_close_driver();
  161. Vect_close(&Map);
  162. exit(EXIT_SUCCESS);
  163. }