main.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /****************************************************************************
  2. *
  3. * MODULE: v.to.3d
  4. *
  5. * AUTHOR(S): Martin Landa <landa.martin gmail.com>
  6. *
  7. * PURPOSE: Performs transformation of 2D vector features to 3D
  8. *
  9. * COPYRIGHT: (C) 2008-2010 by the GRASS Development Team
  10. *
  11. * This program is free software under the GNU General Public
  12. * License (>=v2). Read the file COPYING that comes with GRASS
  13. * for details.
  14. *
  15. *****************************************************************************/
  16. #include <stdlib.h>
  17. #include <grass/gis.h>
  18. #include <grass/vector.h>
  19. #include <grass/glocale.h>
  20. #include "local_proto.h"
  21. int main(int argc, char **argv)
  22. {
  23. struct GModule *module;
  24. struct opts opt;
  25. struct Map_info In, Out;
  26. struct bound_box box;
  27. int type;
  28. G_gisinit(argv[0]);
  29. module = G_define_module();
  30. G_add_keyword(_("vector"));
  31. G_add_keyword(_("geometry"));
  32. G_add_keyword(_("3D"));
  33. module->description =
  34. _("Performs transformation of 2D vector features to 3D.");
  35. parse_args(&opt);
  36. if (G_parser(argc, argv))
  37. exit(EXIT_FAILURE);
  38. type = Vect_option_to_types(opt.type);
  39. if (!opt.reverse->answer) {
  40. if ((!opt.height->answer && !opt.column->answer) ||
  41. (opt.height->answer && opt.column->answer)) {
  42. G_fatal_error(_("Either '%s' or '%s' parameter have to be used"),
  43. opt.height->key, opt.column->key);
  44. }
  45. }
  46. else {
  47. if (opt.height->answer) {
  48. G_warning(_("Parameters '%s' ignored"), opt.height->key);
  49. }
  50. }
  51. if (opt.reverse->answer && opt.table->answer) {
  52. G_fatal_error(_("Attribute table required"));
  53. }
  54. Vect_check_input_output_name(opt.input->answer, opt.output->answer,
  55. G_FATAL_EXIT);
  56. /* open input vector, topology not needed */
  57. Vect_set_open_level(1);
  58. if (Vect_open_old2(&In, opt.input->answer, "", opt.field->answer) < 1)
  59. G_fatal_error(_("Unable to open vector map <%s>"), opt.input->answer);
  60. Vect_set_error_handler_io(&In, &Out);
  61. if (opt.reverse->answer && !Vect_is_3d(&In)) {
  62. G_fatal_error(_("Vector map <%s> is 2D"), opt.input->answer);
  63. }
  64. if (!opt.reverse->answer && Vect_is_3d(&In)) {
  65. G_fatal_error(_("Vector map <%s> is 3D"), opt.input->answer);
  66. }
  67. /* create output vector */
  68. Vect_set_open_level(2);
  69. if (Vect_open_new(&Out, opt.output->answer,
  70. opt.reverse->answer ? WITHOUT_Z : WITH_Z) == -1)
  71. G_fatal_error(_("Unable to create vector map <%s>"),
  72. opt.output->answer);
  73. /* copy history & header */
  74. Vect_hist_copy(&In, &Out);
  75. Vect_hist_command(&Out);
  76. Vect_copy_head_data(&In, &Out);
  77. if (opt.reverse->answer && !opt.table->answer) {
  78. G_message(_("Copying attributes..."));
  79. if (Vect_copy_tables(&In, &Out, 0) == -1) {
  80. G_warning(_("Unable to copy attributes"));
  81. }
  82. }
  83. if (opt.reverse->answer) {
  84. /* 3d -> 2d */
  85. trans3d(&In, &Out, type, opt.field->answer, opt.column->answer);
  86. }
  87. else {
  88. /* 2d -> 3d */
  89. double height = 0.;
  90. if (opt.height->answer) {
  91. height = atof(opt.height->answer);
  92. }
  93. trans2d(&In, &Out, type, height, opt.field->answer, opt.column->answer);
  94. }
  95. if (!opt.reverse->answer && !opt.table->answer) {
  96. G_message(_("Copying attributes..."));
  97. if (Vect_copy_tables(&In, &Out, 0) == -1) {
  98. G_warning(_("Unable to copy attributes"));
  99. }
  100. }
  101. Vect_close(&In);
  102. Vect_build(&Out);
  103. if (!opt.reverse->answer) {
  104. Vect_get_map_box(&Out, &box);
  105. G_message(_("Vertical extent of vector map <%s>: B: %f T: %f"),
  106. opt.output->answer, box.B, box.T);
  107. }
  108. Vect_close(&Out);
  109. exit(EXIT_SUCCESS);
  110. }