main.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /***************************************************************
  2. *
  3. * MODULE: v.net
  4. *
  5. * AUTHOR(S): Radim Blazek
  6. * Operation 'connect' added by Martin Landa
  7. * <landa.martin gmail.com>, 2007/07
  8. *
  9. * PURPOSE: Network maintenance
  10. *
  11. * COPYRIGHT: (C) 2001 by the GRASS Development Team
  12. *
  13. * This program is free software under the
  14. * GNU General Public License (>=v2).
  15. * Read the file COPYING that comes with GRASS
  16. * for details.
  17. *
  18. **************************************************************/
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <grass/gis.h>
  23. #include <grass/glocale.h>
  24. #include <grass/Vect.h>
  25. #include "proto.h"
  26. int main(int argc, char **argv)
  27. {
  28. struct GModule *module;
  29. struct Option *input, *points;
  30. struct Option *output;
  31. struct Option *action;
  32. struct Option *afield_opt, *nfield_opt, *thresh_opt;
  33. struct Flag *cats_flag;
  34. struct Map_info In, Out, Points;
  35. int afield, nfield;
  36. int act;
  37. double thresh;
  38. /* Initialize the GIS calls */
  39. G_gisinit(argv[0]);
  40. module = G_define_module();
  41. module->keywords = _("vector, networking");
  42. module->description = _("Network maintenance.");
  43. /* Define the options */
  44. input = G_define_standard_option(G_OPT_V_INPUT);
  45. points = G_define_standard_option(G_OPT_V_INPUT);
  46. points->key = "points";
  47. points->label = _("Name of input point vector map");
  48. points->description = _("Required for operation 'connect'");
  49. points->required = NO;
  50. output = G_define_standard_option(G_OPT_V_OUTPUT);
  51. output->required = NO;
  52. action = G_define_option();
  53. action->key = "operation";
  54. action->type = TYPE_STRING;
  55. action->required = NO;
  56. action->multiple = NO;
  57. action->answer = "nodes";
  58. action->options = "nodes,connect,report,nreport";
  59. action->description = _("Operation to be performed");
  60. action->descriptions =
  61. _("nodes;new point is placed on each node (line end) "
  62. "if doesn't exist;"
  63. "connect;connect still unconnected points to vector network "
  64. "by inserting new line(s);" "report;print to standard output "
  65. "{line_category start_point_category end_point_category};"
  66. "nreport;print to standard output "
  67. "{point_category line_category[,line_category...]}");
  68. afield_opt = G_define_standard_option(G_OPT_V_FIELD);
  69. afield_opt->key = "alayer";
  70. afield_opt->label = _("Arc layer (network)");
  71. nfield_opt = G_define_standard_option(G_OPT_V_FIELD);
  72. nfield_opt->key = "nlayer";
  73. nfield_opt->answer = "2";
  74. nfield_opt->label = _("Node layer (points)");
  75. thresh_opt = G_define_option();
  76. thresh_opt->key = "thresh";
  77. thresh_opt->type = TYPE_DOUBLE;
  78. thresh_opt->required = NO;
  79. thresh_opt->multiple = NO;
  80. thresh_opt->label = "Threshold";
  81. thresh_opt->description =
  82. _("Required for operation 'connect'. Connect points in given threshold.");
  83. cats_flag = G_define_flag();
  84. cats_flag->key = 'c';
  85. cats_flag->label = _("Assign unique categories to new points");
  86. cats_flag->description = _("For operation 'nodes'");
  87. if (G_parser(argc, argv))
  88. exit(EXIT_FAILURE);
  89. afield = atoi(afield_opt->answer);
  90. nfield = atoi(nfield_opt->answer);
  91. thresh = 0.0;
  92. if (strcmp(action->answer, "nodes") == 0)
  93. act = TOOL_NODES;
  94. else if (strcmp(action->answer, "connect") == 0)
  95. act = TOOL_CONNECT;
  96. else if (strcmp(action->answer, "report") == 0)
  97. act = TOOL_REPORT;
  98. else if (strcmp(action->answer, "nreport") == 0)
  99. act = TOOL_NREPORT;
  100. else
  101. G_fatal_error(_("Unknown operation"));
  102. if (act == TOOL_NODES || act == TOOL_CONNECT) {
  103. if (output->answer == NULL)
  104. G_fatal_error(_("Output vector map must be specified"));
  105. }
  106. if (act == TOOL_CONNECT) {
  107. if (points->answer == NULL)
  108. G_fatal_error(_("Point vector map must be specified"));
  109. if (thresh_opt->answer == NULL)
  110. G_fatal_error(_("Threshold value must be specified"));
  111. thresh = atof(thresh_opt->answer);
  112. if (thresh < 0.0)
  113. G_fatal_error(_("Threshold value must be >= 0"));
  114. }
  115. /* open input map */
  116. Vect_set_open_level(2);
  117. Vect_open_old(&In, input->answer, "");
  118. if (act == TOOL_NODES || act == TOOL_CONNECT) { /* nodes */
  119. int is3d;
  120. Vect_check_input_output_name(input->answer, output->answer,
  121. GV_FATAL_EXIT);
  122. if (act == TOOL_CONNECT) {
  123. /* open points map */
  124. Vect_set_open_level(1);
  125. Vect_set_fatal_error(GV_FATAL_PRINT);
  126. if (Vect_open_old(&Points, points->answer, "") == -1) {
  127. Vect_close(&In);
  128. G_fatal_error(_("Unable to open vector map <%s>"),
  129. points->answer);
  130. }
  131. }
  132. /* create output map */
  133. is3d = Vect_is_3d(&In);
  134. Vect_set_fatal_error(GV_FATAL_PRINT);
  135. if (1 > Vect_open_new(&Out, output->answer, is3d)) {
  136. Vect_close(&In);
  137. G_fatal_error(_("Unable to open vector map <%s> at topology level %d"),
  138. output->answer, 2);
  139. }
  140. Vect_copy_head_data(&In, &Out);
  141. Vect_hist_copy(&In, &Out);
  142. Vect_hist_command(&Out);
  143. if (act == TOOL_NODES) {
  144. nodes(&In, &Out, cats_flag->answer, nfield);
  145. }
  146. else { /* TOOL_CONNECT */
  147. int narcs;
  148. narcs = connect_arcs(&In, &Points, &Out, nfield, thresh);
  149. G_message(_("%d arcs added to network (nlayer: [%d])"), narcs,
  150. nfield);
  151. Vect_close(&Points);
  152. }
  153. if (Vect_copy_tables(&In, &Out, 0))
  154. G_warning(_("Failed to copy attribute table to output map"));
  155. /* support */
  156. Vect_build_partial(&Out, GV_BUILD_NONE);
  157. Vect_build(&Out);
  158. Vect_close(&In);
  159. Vect_close(&Out);
  160. }
  161. else { /* report */
  162. report(&In, afield, nfield, act);
  163. }
  164. return (EXIT_SUCCESS);
  165. }