main.c 5.2 KB

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