main.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. * Martin Landa, CTU in Prague, Czech Republic (v.out.ascii.db merged & update (OGR) for GRASS7)
  9. *
  10. * PURPOSE: Writes GRASS vector data as ASCII files
  11. * COPYRIGHT: (C) 2000-2009 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General
  14. * Public License (>=v2). Read the file COPYING that comes
  15. * with GRASS for details.
  16. *
  17. ****************************************************************************
  18. */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <string.h>
  23. #include <grass/gis.h>
  24. #include <grass/vector.h>
  25. #include <grass/glocale.h>
  26. #include "local_proto.h"
  27. int main(int argc, char *argv[])
  28. {
  29. struct GModule *module;
  30. struct Map_info Map;
  31. FILE *ascii, *att;
  32. char *input, *output, *delim, **columns, *where, *field_name;
  33. int format, dp, field, ret, region, old_format;
  34. int ver, pnt;
  35. G_gisinit(argv[0]);
  36. module = G_define_module();
  37. G_add_keyword(_("vector"));
  38. G_add_keyword(_("export"));
  39. G_add_keyword(_("ascii"));
  40. module->description =
  41. _("Exports a vector map to a GRASS ASCII vector representation.");
  42. parse_args(argc, argv, &input, &output, &format, &dp, &delim,
  43. &field_name, &columns, &where, &region, &old_format);
  44. if (format == GV_ASCII_FORMAT_STD && columns) {
  45. G_warning(_("Parameter 'column' ignored in standard mode"));
  46. }
  47. ver = 5;
  48. pnt = 0;
  49. if (old_format)
  50. ver = 4;
  51. if (ver == 4 && format == GV_ASCII_FORMAT_POINT) {
  52. G_fatal_error(_("Format 'point' is not supported for old version"));
  53. }
  54. if (ver == 4 && strcmp(output, "-") == 0) {
  55. G_fatal_error(_("'output' must be given for old version"));
  56. }
  57. if (format != GV_ASCII_FORMAT_WKT) {
  58. Vect_set_open_level(1); /* topology not needed */
  59. if (Vect_open_old2(&Map, input, "", field_name) < 0)
  60. G_fatal_error(_("Unable to open vector map <%s>"),
  61. input);
  62. }
  63. else {
  64. if (Vect_open_old2(&Map, input, "", field_name) < 2) /* topology required for polygons */
  65. G_warning(_("Unable to open vector map <%s> at topology level. "
  66. "Only points, lines can be processed."),
  67. input);
  68. }
  69. field = Vect_get_field_number(&Map, field_name);
  70. if (strcmp(output, "-") != 0) {
  71. if (ver == 4) {
  72. ascii = G_fopen_new("dig_ascii", output);
  73. }
  74. else if (strcmp(output, "-") == 0) {
  75. ascii = stdout;
  76. }
  77. else {
  78. ascii = fopen(output, "w");
  79. }
  80. if (ascii == NULL) {
  81. G_fatal_error(_("Unable to open file <%s>"), output);
  82. }
  83. }
  84. else {
  85. ascii = stdout;
  86. }
  87. if (format == GV_ASCII_FORMAT_STD) {
  88. Vect_write_ascii_head(ascii, &Map);
  89. fprintf(ascii, "VERTI:\n");
  90. }
  91. /* Open dig_att */
  92. att = NULL;
  93. if (ver == 4 && !pnt) {
  94. if (G_find_file("dig_att", output, G_mapset()) != NULL)
  95. G_fatal_error(_("dig_att file already exist"));
  96. if ((att = G_fopen_new("dig_att", output)) == NULL)
  97. G_fatal_error(_("Unable to open dig_att file <%s>"),
  98. output);
  99. }
  100. if (where || columns)
  101. G_message(_("Fetching data..."));
  102. ret = Vect_write_ascii(ascii, att, &Map, ver, format, dp, delim,
  103. region, field, where,
  104. columns);
  105. if (ret < 1) {
  106. if (format == GV_ASCII_FORMAT_POINT) {
  107. G_warning(_("No points found, nothing to be exported"));
  108. }
  109. else {
  110. G_warning(_("No features found, nothing to be exported"));
  111. }
  112. }
  113. if (ascii != NULL)
  114. fclose(ascii);
  115. if (att != NULL)
  116. fclose(att);
  117. Vect_close(&Map);
  118. exit(EXIT_SUCCESS);
  119. }