main.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /****************************************************************
  2. *
  3. * MODULE: v.net.path
  4. *
  5. * AUTHOR(S): Radim Blazek
  6. *
  7. * PURPOSE: Shortest path on vector network
  8. *
  9. * COPYRIGHT: (C) 2002 by the GRASS Development Team
  10. *
  11. * This program is free software under the
  12. * GNU General Public License (>=v2).
  13. * Read the file COPYING that comes with GRASS
  14. * for details.
  15. *
  16. ****************************************************************/
  17. #include <stdlib.h>
  18. #include <grass/gis.h>
  19. #include <grass/vector.h>
  20. #include <grass/glocale.h>
  21. int path(struct Map_info *, struct Map_info *, char *, int, double, int);
  22. int main(int argc, char **argv)
  23. {
  24. struct Option *input_opt, *output_opt, *afield_opt, *nfield_opt, *afcol,
  25. *abcol, *ncol, *type_opt;
  26. struct Option *max_dist, *file_opt;
  27. struct Flag *geo_f, *segments_f;
  28. struct GModule *module;
  29. struct Map_info In, Out;
  30. int type, afield, nfield, geo;
  31. double maxdist;
  32. /* Initialize the GIS calls */
  33. G_gisinit(argv[0]);
  34. module = G_define_module();
  35. G_add_keyword(_("vector"));
  36. G_add_keyword(_("networking"));
  37. module->description = _("Finds shortest path on vector network.");
  38. input_opt = G_define_standard_option(G_OPT_V_INPUT);
  39. output_opt = G_define_standard_option(G_OPT_V_OUTPUT);
  40. type_opt = G_define_standard_option(G_OPT_V_TYPE);
  41. type_opt->options = "line,boundary";
  42. type_opt->answer = "line,boundary";
  43. type_opt->description = _("Arc type");
  44. afield_opt = G_define_standard_option(G_OPT_V_FIELD);
  45. afield_opt->key = "alayer";
  46. afield_opt->answer = "1";
  47. afield_opt->description = _("Arc layer");
  48. nfield_opt = G_define_standard_option(G_OPT_V_FIELD);
  49. nfield_opt->key = "nlayer";
  50. nfield_opt->answer = "2";
  51. nfield_opt->description = _("Node layer");
  52. file_opt = G_define_standard_option(G_OPT_F_INPUT);
  53. file_opt->key = "file";
  54. file_opt->required = NO;
  55. file_opt->description = _("Name of file containing start and end points. "
  56. "If not given, read from stdin");
  57. afcol = G_define_option();
  58. afcol->key = "afcolumn";
  59. afcol->type = TYPE_STRING;
  60. afcol->required = NO;
  61. afcol->description = _("Arc forward/both direction(s) cost column");
  62. abcol = G_define_option();
  63. abcol->key = "abcolumn";
  64. abcol->type = TYPE_STRING;
  65. abcol->required = NO;
  66. abcol->description = _("Arc backward direction cost column");
  67. ncol = G_define_option();
  68. ncol->key = "ncolumn";
  69. ncol->type = TYPE_STRING;
  70. ncol->required = NO;
  71. ncol->description = _("Node cost column");
  72. max_dist = G_define_option();
  73. max_dist->key = "dmax";
  74. max_dist->type = TYPE_DOUBLE;
  75. max_dist->required = NO;
  76. max_dist->answer = "1000";
  77. max_dist->label = _("Maximum distance to the network");
  78. max_dist->description = _("If start/end are given as coordinates. "
  79. "If start/end point is outside this threshold, "
  80. "the path is not found "
  81. "and error message is printed. To speed up the process, keep this "
  82. "value as low as possible.");
  83. geo_f = G_define_flag();
  84. geo_f->key = 'g';
  85. geo_f->description =
  86. _("Use geodesic calculation for longitude-latitude locations");
  87. segments_f = G_define_flag();
  88. segments_f->key = 's';
  89. segments_f->description = _("Write output as original input segments, "
  90. "not each path as one line.");
  91. if (G_parser(argc, argv))
  92. exit(EXIT_FAILURE);
  93. type = Vect_option_to_types(type_opt);
  94. afield = atoi(afield_opt->answer);
  95. nfield = atoi(nfield_opt->answer);
  96. maxdist = atof(max_dist->answer);
  97. if (geo_f->answer) {
  98. geo = 1;
  99. if (G_projection() != PROJECTION_LL)
  100. G_warning(_("The current projection is not longitude-latitude"));
  101. }
  102. else
  103. geo = 0;
  104. Vect_check_input_output_name(input_opt->answer, output_opt->answer,
  105. GV_FATAL_EXIT);
  106. Vect_set_open_level(2);
  107. Vect_open_old(&In, input_opt->answer, "");
  108. Vect_set_fatal_error(GV_FATAL_PRINT);
  109. if (1 > Vect_open_new(&Out, output_opt->answer, Vect_is_3d(&In))) {
  110. Vect_close(&In);
  111. G_fatal_error(_("Unable to create vector map <%s>"),
  112. output_opt->answer);
  113. }
  114. Vect_hist_command(&Out);
  115. Vect_net_build_graph(&In, type, afield, nfield, afcol->answer,
  116. abcol->answer, ncol->answer, geo, 0);
  117. path(&In, &Out, file_opt->answer, nfield, maxdist, segments_f->answer);
  118. Vect_close(&In);
  119. Vect_build(&Out);
  120. Vect_close(&Out);
  121. exit(EXIT_SUCCESS);
  122. }