main.c 5.2 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. * 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, 2011-2012 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, *cats;
  33. int format, dp, field, ret, region, old_format, header, type;
  34. int ver, pnt;
  35. struct cat_list *clist;
  36. clist = NULL;
  37. G_gisinit(argv[0]);
  38. module = G_define_module();
  39. G_add_keyword(_("vector"));
  40. G_add_keyword(_("export"));
  41. G_add_keyword(_("output"));
  42. G_add_keyword("ASCII");
  43. module->label =
  44. _("Exports a vector map to a GRASS ASCII vector representation.");
  45. module->description = _("By default only features with category are exported. "
  46. "To export all features use 'layer=-1'.");
  47. parse_args(argc, argv, &input, &output, &format, &dp, &delim,
  48. &field_name, &columns, &where, &region, &old_format, &header,
  49. &cats, &type);
  50. if (format == GV_ASCII_FORMAT_STD && columns) {
  51. G_warning(_("Parameter '%s' ignored in standard mode"), "column");
  52. }
  53. ver = 5;
  54. pnt = 0;
  55. if (old_format)
  56. ver = 4;
  57. if (ver == 4 && format == GV_ASCII_FORMAT_POINT) {
  58. G_fatal_error(_("Format '%s' is not supported for old version"), "point");
  59. }
  60. if (ver == 4 && strcmp(output, "-") == 0) {
  61. G_fatal_error(_("Parameter '%s' must be given for old version"), "output");
  62. }
  63. /* open with topology only if needed */
  64. if (format == GV_ASCII_FORMAT_WKT ||
  65. (format == GV_ASCII_FORMAT_STD && (where || clist))) {
  66. if (Vect_open_old2(&Map, input, "", field_name) < 2) /* topology required for areas */
  67. G_warning(_("Unable to open vector map <%s> at topology level. "
  68. "Areas will not be processed."),
  69. input);
  70. }
  71. else {
  72. Vect_set_open_level(1); /* topology not needed */
  73. if (Vect_open_old2(&Map, input, "", field_name) < 0)
  74. G_fatal_error(_("Unable to open vector map <%s>"), input);
  75. if (Vect_maptype(&Map) != GV_FORMAT_NATIVE) {
  76. /* require topological level for external formats
  77. centroids are read from topo */
  78. Vect_close(&Map);
  79. Vect_set_open_level(2);
  80. if (Vect_open_old2(&Map, input, "", field_name) < 0)
  81. G_fatal_error(_("Unable to open vector map <%s>"), input);
  82. }
  83. }
  84. field = Vect_get_field_number(&Map, field_name);
  85. if (cats) {
  86. clist = Vect_new_cat_list();
  87. clist->field = field;
  88. if (clist->field < 1)
  89. G_fatal_error(_("Layer <%s> not found"), field_name);
  90. ret = Vect_str_to_cat_list(cats, clist);
  91. if (ret > 0)
  92. G_fatal_error(n_("%d error in <%s> option",
  93. "%d errors in <%s> option",
  94. ret),
  95. ret, "cats");
  96. }
  97. if (strcmp(output, "-") != 0) {
  98. if (ver == 4) {
  99. ascii = G_fopen_new("dig_ascii", output);
  100. }
  101. else if (strcmp(output, "-") == 0) {
  102. ascii = stdout;
  103. }
  104. else {
  105. ascii = fopen(output, "w");
  106. }
  107. if (ascii == NULL) {
  108. G_fatal_error(_("Unable to open file <%s>"), output);
  109. }
  110. }
  111. else {
  112. ascii = stdout;
  113. }
  114. if (format == GV_ASCII_FORMAT_STD) {
  115. Vect_write_ascii_head(ascii, &Map);
  116. fprintf(ascii, "VERTI:%s", HOST_NEWLINE);
  117. }
  118. /* Open dig_att */
  119. att = NULL;
  120. if (ver == 4 && !pnt) {
  121. if (G_find_file("dig_att", output, G_mapset()) != NULL)
  122. G_fatal_error(_("dig_att file already exist"));
  123. if ((att = G_fopen_new("dig_att", output)) == NULL)
  124. G_fatal_error(_("Unable to open dig_att file <%s>"),
  125. output);
  126. }
  127. if (where || columns || clist)
  128. G_message(_("Fetching data..."));
  129. ret = Vect_write_ascii(ascii, att, &Map, ver, format, dp, delim,
  130. region, type, field, clist, (const char *)where,
  131. (const char **)columns, header);
  132. if (ret == 0) {
  133. if (format == GV_ASCII_FORMAT_POINT) {
  134. G_warning(_("No points found, nothing to be exported"));
  135. }
  136. else {
  137. G_warning(_("No features found, nothing to be exported"));
  138. }
  139. }
  140. else if (ret < 0) {
  141. G_warning(_("An error occurred, nothing to be exported"));
  142. }
  143. if (ascii != NULL)
  144. fclose(ascii);
  145. if (att != NULL)
  146. fclose(att);
  147. Vect_close(&Map);
  148. if (cats)
  149. Vect_destroy_cat_list(clist);
  150. exit(EXIT_SUCCESS);
  151. }