main.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /****************************************************************
  2. *
  3. * MODULE: d.extract
  4. *
  5. * AUTHOR(S): Radim Blazek, Markus Neteler
  6. *
  7. * PURPOSE: A graphical vector extractor
  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 <string.h>
  19. #include <grass/gis.h>
  20. #include <grass/display.h>
  21. #include <grass/colors.h>
  22. #include <grass/vector.h>
  23. #include <grass/dbmi.h>
  24. #include <grass/glocale.h>
  25. int extract(struct Map_info *, struct Map_info *, int,
  26. const struct color_rgb *, const struct color_rgb *);
  27. int main(int argc, char **argv)
  28. {
  29. struct Option *input, *output, *type_opt;
  30. struct Option *color_opt, *hcolor_opt;
  31. struct GModule *module;
  32. char *mapset;
  33. struct Map_info In, Out;
  34. int type;
  35. struct color_rgb color, hcolor;
  36. int r, g, b;
  37. struct field_info *Fi, *Fin;
  38. int i, n, tbtype, ret;
  39. /* Initialize the GIS calls */
  40. G_gisinit(argv[0]);
  41. module = G_define_module();
  42. G_add_keyword(_("display"));
  43. G_add_keyword(_("vector"));
  44. module->description =
  45. _("Selects and extracts vectors with mouse into new vector map.");
  46. input = G_define_standard_option(G_OPT_V_INPUT);
  47. output = G_define_standard_option(G_OPT_V_OUTPUT);
  48. type_opt = G_define_standard_option(G_OPT_V_TYPE);
  49. type_opt->options = "point,line,boundary,centroid,area,face";
  50. type_opt->answer = "point,line,boundary,centroid,area,face";
  51. color_opt = G_define_option();
  52. color_opt->key = "color";
  53. color_opt->type = TYPE_STRING;
  54. color_opt->answer = "black";
  55. color_opt->description = _("Original line color");
  56. hcolor_opt = G_define_option();
  57. hcolor_opt->key = "hcolor";
  58. hcolor_opt->type = TYPE_STRING;
  59. hcolor_opt->answer = "red";
  60. hcolor_opt->description = _("Highlight color");
  61. if (G_parser(argc, argv))
  62. exit(EXIT_FAILURE);
  63. type = Vect_option_to_types(type_opt);
  64. if (R_open_driver() != 0)
  65. G_fatal_error(_("No graphics device selected"));
  66. color = G_standard_color_rgb(BLACK);
  67. if (G_str_to_color(color_opt->answer, &r, &g, &b)) {
  68. color.r = r;
  69. color.g = g;
  70. color.b = b;
  71. }
  72. hcolor = G_standard_color_rgb(RED);
  73. if (G_str_to_color(hcolor_opt->answer, &r, &g, &b)) {
  74. hcolor.r = r;
  75. hcolor.g = g;
  76. hcolor.b = b;
  77. }
  78. mapset = G_find_vector2(input->answer, NULL);
  79. if (mapset == NULL)
  80. G_fatal_error(_("Vector map <%s> not found"), input->answer);
  81. Vect_set_open_level(2);
  82. if (Vect_open_old(&In, input->answer, mapset) < 0)
  83. G_fatal_error(_("Unable to open vector map <%s>"), input->answer);
  84. if (Vect_open_new(&Out, output->answer, Vect_is_3d(&In)) < 0)
  85. G_fatal_error(_("Unable to create vector map <%s>"), output->answer);
  86. Vect_copy_head_data(&In, &Out);
  87. Vect_hist_copy(&In, &Out);
  88. Vect_hist_command(&Out);
  89. D_setup(0);
  90. G_setup_plot(D_get_d_north(), D_get_d_south(), D_get_d_west(),
  91. D_get_d_east(), D_move_abs, D_cont_abs);
  92. extract(&In, &Out, type, &color, &hcolor);
  93. R_close_driver();
  94. /* Copy tables */
  95. G_message(_("Copying tables..."));
  96. n = Vect_get_num_dblinks(&In);
  97. tbtype = GV_1TABLE;
  98. if (n > 1)
  99. tbtype = GV_MTABLE;
  100. for (i = 0; i < n; i++) {
  101. Fi = Vect_get_dblink(&In, i);
  102. if (Fi == NULL) {
  103. G_warning(_("Cannot get db link info -> cannot copy table."));
  104. continue;
  105. }
  106. Fin = Vect_default_field_info(&Out, Fi->number, Fi->name, tbtype);
  107. G_debug(3, "Copy drv:db:table '%s:%s:%s' to '%s:%s:%s'",
  108. Fi->driver, Fi->database, Fi->table, Fin->driver,
  109. Fin->database, Fin->table);
  110. Vect_map_add_dblink(&Out, Fi->number, Fi->name, Fin->table, Fi->key,
  111. Fin->database, Fin->driver);
  112. ret = db_copy_table(Fi->driver, Fi->database, Fi->table,
  113. Fin->driver, Vect_subst_var(Fin->database, &Out),
  114. Fin->table);
  115. if (ret == DB_FAILED) {
  116. G_warning("Unable to copy table");
  117. continue;
  118. }
  119. } /* for of copy table */
  120. Vect_build(&Out, stdout);
  121. Vect_close(&In);
  122. Vect_close(&Out);
  123. exit(EXIT_SUCCESS);
  124. }