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