main.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. int ret;
  29. G_gisinit(argv[0]);
  30. module = G_define_module();
  31. G_add_keyword(_("vector"));
  32. G_add_keyword(_("geometry"));
  33. G_add_keyword(_("3D"));
  34. module->description =
  35. _("Performs transformation of 2D vector features to 3D.");
  36. parse_args(&opt);
  37. if (G_parser(argc, argv))
  38. exit(EXIT_FAILURE);
  39. type = Vect_option_to_types(opt.type);
  40. if (!opt.reverse->answer) {
  41. if ((!opt.height->answer && !opt.column->answer) ||
  42. (opt.height->answer && opt.column->answer)) {
  43. G_fatal_error(_("Either '%s' or '%s' parameter have to be used"),
  44. opt.height->key, opt.column->key);
  45. }
  46. }
  47. else {
  48. if (opt.height->answer) {
  49. G_warning(_("Parameters '%s' ignored"), opt.height->key);
  50. }
  51. }
  52. if (opt.reverse->answer && opt.table->answer) {
  53. G_fatal_error(_("Attribute table required"));
  54. }
  55. Vect_check_input_output_name(opt.input->answer, opt.output->answer,
  56. GV_FATAL_EXIT);
  57. /* open input vector, topology not needed */
  58. Vect_set_open_level(1);
  59. if (Vect_open_old2(&In, opt.input->answer, "", opt.field->answer) < 1)
  60. G_fatal_error(_("Unable to open vector map <%s>"), opt.input->answer);
  61. if (opt.reverse->answer && !Vect_is_3d(&In)) {
  62. Vect_close(&In);
  63. G_fatal_error(_("Vector map <%s> is 2D"), opt.input->answer);
  64. }
  65. if (!opt.reverse->answer && Vect_is_3d(&In)) {
  66. Vect_close(&In);
  67. G_fatal_error(_("Vector map <%s> is 3D"), opt.input->answer);
  68. }
  69. /* create output vector */
  70. Vect_set_open_level(2);
  71. if (Vect_open_new(&Out, opt.output->answer,
  72. opt.reverse->answer ? WITHOUT_Z : WITH_Z) == -1)
  73. G_fatal_error(_("Unable to create vector map <%s>"),
  74. opt.output->answer);
  75. /* copy history & header */
  76. Vect_hist_copy(&In, &Out);
  77. Vect_hist_command(&Out);
  78. Vect_copy_head_data(&In, &Out);
  79. if (opt.reverse->answer && !opt.table->answer) {
  80. G_message(_("Copying attributes..."));
  81. if (Vect_copy_tables(&In, &Out, 0) == -1) {
  82. G_warning(_("Unable to copy attributes"));
  83. }
  84. }
  85. G_message(_("Transforming features..."));
  86. ret = 0;
  87. if (opt.reverse->answer) {
  88. /* 3d -> 2d */
  89. ret = trans3d(&In, &Out, type, opt.field->answer, opt.column->answer);
  90. }
  91. else {
  92. /* 2d -> 3d */
  93. double height = 0.;
  94. if (opt.height->answer) {
  95. height = atof(opt.height->answer);
  96. }
  97. ret = trans2d(&In, &Out, type, height, opt.field->answer, opt.column->answer);
  98. }
  99. if (ret < 0) {
  100. Vect_close(&In);
  101. Vect_close(&Out);
  102. Vect_delete(opt.output->answer);
  103. G_fatal_error(_("%s failed"), G_program_name());
  104. }
  105. if (!opt.reverse->answer && !opt.table->answer) {
  106. G_message(_("Copying attributes..."));
  107. if (Vect_copy_tables(&In, &Out, 0) == -1) {
  108. G_warning(_("Unable to copy attributes"));
  109. }
  110. }
  111. Vect_close(&In);
  112. Vect_build(&Out);
  113. if (!opt.reverse->answer) {
  114. Vect_get_map_box(&Out, &box);
  115. G_message(_("Vertical extent of vector map <%s>: B: %f T: %f"),
  116. opt.output->answer, box.B, box.T);
  117. }
  118. Vect_close(&Out);
  119. exit(EXIT_SUCCESS);
  120. }