main.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /****************************************************************
  2. *
  3. * MODULE: v.net.bridge
  4. *
  5. * AUTHOR(S): Daniel Bundala
  6. *
  7. * PURPOSE: Computes bridges and articulation points
  8. *
  9. * COPYRIGHT: (C) 2002-2005 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 <stdio.h>
  18. #include <stdlib.h>
  19. #include <grass/gis.h>
  20. #include <grass/vector.h>
  21. #include <grass/glocale.h>
  22. #include <grass/neta.h>
  23. int main(int argc, char *argv[])
  24. {
  25. struct Map_info In, Out;
  26. static struct line_pnts *Points;
  27. struct line_cats *Cats;
  28. struct GModule *module; /* GRASS module for parsing arguments */
  29. struct Option *map_in, *map_out;
  30. struct Option *field_opt, *method_opt;
  31. int with_z;
  32. int layer, mask_type;
  33. dglGraph_s *graph;
  34. int i, bridges, articulations;
  35. struct ilist *bridge_list, *articulation_list;
  36. /* initialize GIS environment */
  37. G_gisinit(argv[0]); /* reads grass env, stores program name to G_program_name() */
  38. /* initialize module */
  39. module = G_define_module();
  40. G_add_keyword(_("vector"));
  41. G_add_keyword(_("network"));
  42. G_add_keyword(_("articulation points"));
  43. module->description = _("Computes bridges and articulation points in the network.");
  44. /* Define the different options as defined in gis.h */
  45. map_in = G_define_standard_option(G_OPT_V_INPUT);
  46. map_out = G_define_standard_option(G_OPT_V_OUTPUT);
  47. field_opt = G_define_standard_option(G_OPT_V_FIELD);
  48. method_opt = G_define_option();
  49. method_opt->key = "method";
  50. method_opt->type = TYPE_STRING;
  51. method_opt->required = YES;
  52. method_opt->multiple = NO;
  53. method_opt->options = "bridge,articulation";
  54. method_opt->descriptions = _("bridge;Finds bridges;"
  55. "articulation;Finds articulation points;");
  56. method_opt->description = _("Feature type");
  57. /* options and flags parser */
  58. if (G_parser(argc, argv))
  59. exit(EXIT_FAILURE);
  60. /* TODO: make an option for this */
  61. mask_type = GV_LINE | GV_BOUNDARY;
  62. Points = Vect_new_line_struct();
  63. Cats = Vect_new_cats_struct();
  64. Vect_check_input_output_name(map_in->answer, map_out->answer,
  65. GV_FATAL_EXIT);
  66. Vect_set_open_level(2);
  67. if (1 > Vect_open_old(&In, map_in->answer, ""))
  68. G_fatal_error(_("Unable to open vector map <%s>"),
  69. map_in->answer);
  70. with_z = Vect_is_3d(&In);
  71. if (0 > Vect_open_new(&Out, map_out->answer, with_z)) {
  72. Vect_close(&In);
  73. G_fatal_error(_("Unable to create vector map <%s>"), map_out->answer);
  74. }
  75. /* parse filter option and select appropriate lines */
  76. layer = atoi(field_opt->answer);
  77. Vect_net_build_graph(&In, mask_type, 0, 0, NULL, NULL, NULL, 0, 0);
  78. graph = &(In.graph);
  79. Vect_copy_head_data(&In, &Out);
  80. Vect_hist_copy(&In, &Out);
  81. Vect_hist_command(&Out);
  82. if (method_opt->answer[0] == 'b') {
  83. bridge_list = Vect_new_list();
  84. bridges = NetA_compute_bridges(graph, bridge_list);
  85. G_debug(3, "Bridges: %d", bridges);
  86. for (i = 0; i < bridges; i++) {
  87. int type =
  88. Vect_read_line(&In, Points, Cats, abs(bridge_list->value[i]));
  89. Vect_write_line(&Out, type, Points, Cats);
  90. }
  91. Vect_destroy_list(bridge_list);
  92. }
  93. else {
  94. articulation_list = Vect_new_list();
  95. articulations = NetA_articulation_points(graph, articulation_list);
  96. G_debug(3, "Articulation points: %d", articulations);
  97. for (i = 0; i < articulations; i++) {
  98. double x, y, z;
  99. Vect_get_node_coor(&In, articulation_list->value[i], &x, &y, &z);
  100. Vect_reset_line(Points);
  101. Vect_append_point(Points, x, y, z);
  102. Vect_write_line(&Out, GV_POINT, Points, Cats);
  103. }
  104. Vect_destroy_list(articulation_list);
  105. }
  106. Vect_build(&Out);
  107. Vect_close(&In);
  108. Vect_close(&Out);
  109. exit(EXIT_SUCCESS);
  110. }