out.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. ****************************************************************************
  3. *
  4. * MODULE: v.out.ascii
  5. * AUTHOR(S): Michael Higgins, U.S. Army Construction Engineering Research Laboratory
  6. * James Westervelt, U.S. Army Construction Engineering Research Laboratory
  7. * Radim Blazek, ITC-Irst, Trento, Italy
  8. *
  9. * PURPOSE: v.out.ascii: writes GRASS vector data as ASCII files
  10. * COPYRIGHT: (C) 2000-2008 by the GRASS Development Team
  11. *
  12. * This program is free software under the GNU General Public
  13. * License (>=v2). Read the file COPYING that comes with GRASS
  14. * for details.
  15. *
  16. ****************************************************************************
  17. */
  18. /* @(#)b_a_dig.c 2.1 6/26/87 */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <string.h>
  23. #include <grass/gis.h>
  24. #include <grass/Vect.h>
  25. #include <grass/glocale.h>
  26. #include "local_proto.h"
  27. int main(int argc, char *argv[])
  28. {
  29. FILE *ascii, *att;
  30. struct Option *input, *output, *format_opt, *dp_opt, *delim_opt;
  31. struct Flag *verf, *region_flag;
  32. int format, dp;
  33. char *fs;
  34. struct Map_info Map;
  35. int ver = 5, pnt = 0;
  36. struct GModule *module;
  37. G_gisinit(argv[0]);
  38. module = G_define_module();
  39. module->keywords = _("vector");
  40. module->description =
  41. _("Converts a GRASS binary vector map to a GRASS ASCII vector map.");
  42. input = G_define_standard_option(G_OPT_V_INPUT);
  43. output = G_define_option();
  44. output->key = "output";
  45. output->type = TYPE_STRING;
  46. output->required = NO;
  47. output->multiple = NO;
  48. output->gisprompt = "new_file,file,output";
  49. output->description =
  50. _
  51. ("Path to resulting ASCII file or ASCII vector name if '-o' is defined");
  52. format_opt = G_define_option();
  53. format_opt->key = "format";
  54. format_opt->type = TYPE_STRING;
  55. format_opt->required = NO;
  56. format_opt->multiple = NO;
  57. format_opt->options = "point,standard";
  58. format_opt->answer = "point";
  59. format_opt->description = _("Output format");
  60. delim_opt = G_define_option();
  61. delim_opt->key = "fs";
  62. delim_opt->type = TYPE_STRING;
  63. delim_opt->required = NO;
  64. delim_opt->description = _("Field separator (points mode)");
  65. delim_opt->answer = "|";
  66. dp_opt = G_define_option();
  67. dp_opt->key = "dp";
  68. dp_opt->type = TYPE_INTEGER;
  69. dp_opt->required = NO;
  70. dp_opt->options = "0-32";
  71. dp_opt->answer = "8"; /*This value is taken from the lib settings in G_format_easting() */
  72. dp_opt->description =
  73. _("Number of significant digits (floating point only)");
  74. verf = G_define_flag();
  75. verf->key = 'o';
  76. verf->description = _("Create old (version 4) ASCII file");
  77. region_flag = G_define_flag();
  78. region_flag->key = 'r';
  79. region_flag->description =
  80. _
  81. ("Only export points falling within current 3D region (points mode)");
  82. if (G_parser(argc, argv))
  83. exit(EXIT_FAILURE);
  84. if (format_opt->answer[0] == 'p')
  85. format = FORMAT_POINT;
  86. else
  87. format = FORMAT_ALL;
  88. if (verf->answer)
  89. ver = 4;
  90. if (ver == 4 && format == FORMAT_POINT) {
  91. G_fatal_error(_("Format 'point' is not supported for old version"));
  92. }
  93. if (ver == 4 && output->answer == NULL) {
  94. G_fatal_error(_("'output' must be given for old version"));
  95. }
  96. /* the field separator */
  97. fs = delim_opt->answer;
  98. if (strcmp(fs, "\\t") == 0)
  99. fs = "\t";
  100. if (strcmp(fs, "tab") == 0)
  101. fs = "\t";
  102. if (strcmp(fs, "space") == 0)
  103. fs = " ";
  104. /*The precision of the output */
  105. if (dp_opt->answer) {
  106. if (sscanf(dp_opt->answer, "%d", &dp) != 1)
  107. G_fatal_error(_("Failed to interprete 'dp' parameter as an integer"));
  108. }
  109. Vect_set_open_level(1); /* only need level I */
  110. Vect_open_old(&Map, input->answer, "");
  111. if (output->answer) {
  112. if (ver == 4) {
  113. ascii = G_fopen_new("dig_ascii", output->answer);
  114. }
  115. else if (strcmp(output->answer, "-") == 0) {
  116. ascii = stdout;
  117. }
  118. else {
  119. ascii = fopen(output->answer, "w");
  120. }
  121. if (ascii == NULL) {
  122. G_fatal_error(_("Unable to open file <%s>"), output->answer);
  123. }
  124. }
  125. else {
  126. ascii = stdout;
  127. }
  128. if (format == FORMAT_ALL) {
  129. write_head(ascii, &Map);
  130. fprintf(ascii, "VERTI:\n");
  131. }
  132. /* Open dig_att */
  133. att = NULL;
  134. if (ver == 4 && !pnt) {
  135. if (G_find_file("dig_att", output->answer, G_mapset()) != NULL)
  136. G_fatal_error(_("dig_att file already exist"));
  137. if ((att = G_fopen_new("dig_att", output->answer)) == NULL)
  138. G_fatal_error(_("Unable to open dig_att file <%s>\n"),
  139. output->answer);
  140. }
  141. bin_to_asc(ascii, att, &Map, ver, format, dp, fs, region_flag->answer);
  142. if (ascii != NULL)
  143. fclose(ascii);
  144. if (att != NULL)
  145. fclose(att);
  146. Vect_close(&Map);
  147. exit(EXIT_SUCCESS);
  148. }