1 
2 /****************************************************************
3  *
4  * MODULE:     v.support
5  *
6  * AUTHOR(S):  Markus Neteler
7  *
8  * PURPOSE:    updates metadata of vector map
9  *
10  * COPYRIGHT:  (C) 2007, 2017 by the GRASS Development Team
11  *
12  *             This program is free software under the
13  *             GNU General Public License (>=v2).
14  *             Read the file COPYING that comes with GRASS
15  *             for details.
16  *
17  ****************************************************************/
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <grass/gis.h>
23 #include <grass/vector.h>
24 #include <grass/glocale.h>
25 
main(int argc,char * argv[])26 int main(int argc, char *argv[])
27 {
28     struct Map_info Map;
29     struct GModule *module;
30     struct Option *map, *organization, *date, *person, *map_name, *map_date,
31 	*scale, *comment, *zone, *thresh, *cmdhist;
32     struct Flag *r_flag, *h_flag;
33 
34     /* initialize GIS environment */
35     G_gisinit(argv[0]);
36 
37     /* initialize module */
38     module = G_define_module();
39     G_add_keyword(_("vector"));
40     G_add_keyword(_("metadata"));
41     module->description = _("Updates vector map metadata.");
42 
43     /* Define the different options as defined in gis.h */
44     map = G_define_standard_option(G_OPT_V_MAP);
45 
46     organization = G_define_option();
47     organization->key = "organization";
48     organization->key_desc = "phrase";
49     organization->type = TYPE_STRING;
50     organization->required = NO;
51     organization->description =
52 	_("Organization where vector map was created");
53 
54     /* don't predefine answers to not overwrite existing information */
55     date = G_define_option();
56     date->key = "date";
57     date->key_desc = "datestring";
58     date->type = TYPE_STRING;
59     date->required = NO;
60     date->description =
61 	_("Date of vector map digitization (e.g., \"15 Mar 2007\")");
62 
63     person = G_define_option();
64     person->key = "person";
65     person->key_desc = "phrase";
66     person->type = TYPE_STRING;
67     person->required = NO;
68     person->description = _("Person who created vector map");
69 
70     map_name = G_define_option();
71     map_name->key = "map_name";
72     map_name->key_desc = "phrase";
73     map_name->type = TYPE_STRING;
74     map_name->required = NO;
75     map_name->description = _("Vector map title");
76 
77     map_date = G_define_option();
78     map_date->key = "map_date";
79     map_date->key_desc = "datestring";
80     map_date->type = TYPE_STRING;
81     map_date->required = NO;
82     map_date->description =
83 	_("Date when the source map was originally produced");
84 
85     scale = G_define_option();
86     scale->key = "scale";
87     scale->type = TYPE_INTEGER;
88     scale->required = NO;
89     scale->description = _("Vector map scale number (e.g., 24000)");
90 
91     zone = G_define_option();
92     zone->key = "zone";
93     zone->type = TYPE_INTEGER;
94     zone->required = NO;
95     zone->description = _("Vector map projection zone");
96 
97     thresh = G_define_option();
98     thresh->key = "threshold";
99     thresh->type = TYPE_DOUBLE;
100     thresh->required = NO;
101     thresh->description =
102 	_("Vector map digitizing threshold number (e.g., 0.5)");
103 
104     comment = G_define_option();
105     comment->key = "comment";
106     comment->key_desc = "phrase";
107     comment->type = TYPE_STRING;
108     comment->required = NO;
109     comment->description =
110 	_("Text to append to the comment line of the map's metadata file");
111 
112     cmdhist = G_define_option();
113     cmdhist->key = "cmdhist";
114     cmdhist->key_desc = "command";
115     cmdhist->type = TYPE_STRING;
116     cmdhist->required = NO;
117     cmdhist->description =
118 	_("Command line to store into vector map history file (used for vector scripts)");
119 
120     r_flag = G_define_flag();
121     r_flag->key = 'r';
122     r_flag->description = _("Replace comment instead of appending it");
123 
124     h_flag = G_define_flag();
125     h_flag->key = 'h';
126     h_flag->description = _("Replace command line instead of appending it");
127 
128     /* options and flags parser */
129     if (G_parser(argc, argv))
130 	exit(EXIT_FAILURE);
131 
132     Vect_set_open_level(1);
133     if (Vect_open_old(&Map, map->answer, "") < 1)
134 	G_fatal_error(_("Unable to open vector map <%s>"), map->answer);
135 
136     /* modify 'head' file */
137     Vect_read_header(&Map);
138 
139     if (organization->answer)
140 	Vect_set_organization(&Map, organization->answer);
141     if (date->answer)
142 	Vect_set_date(&Map, date->answer);
143     if (person->answer)
144 	Vect_set_person(&Map, person->answer);
145     if (map_name->answer)
146 	Vect_set_map_name(&Map, map_name->answer);
147     if (map_date->answer)
148 	Vect_set_map_date(&Map, map_date->answer);
149 
150     if (scale->answer) {
151 	int scalenum = atoi(scale->answer);
152 
153 	if (scalenum == 0)
154 	    scalenum = 1;
155 	Vect_set_scale(&Map, scalenum);
156     }
157 
158     if (zone->answer)
159 	Vect_set_zone(&Map, atoi(zone->answer));
160 
161     if (thresh->answer)
162 	Vect_set_thresh(&Map, atof(thresh->answer));
163 
164     if (comment->answer) {	/* apparently only one line comments allowed, so we use space to delimit */
165 	char *temp;
166 
167 	if (r_flag->answer || strlen(Vect_get_comment(&Map)) == 0) {	/* check if new/replacing or adding */
168 	    G_asprintf(&temp, "%s", comment->answer);
169 	}
170 	else {
171 	    G_asprintf(&temp, "%s %s", Vect_get_comment(&Map), comment->answer);
172 	}
173 	Vect_set_comment(&Map, temp);
174     }
175 
176     Vect_write_header(&Map);
177 
178 
179     /* modify 'hist' file */
180     if (cmdhist->answer) {
181 	char buf[2000];		/* derived from Vect_hist_command() */
182 
183 	/* Open history file for modification */
184 	sprintf(buf, "%s/%s", GV_DIRECTORY, Map.name);
185 	if (h_flag->answer)
186 	    Map.hist_fp = G_fopen_new(buf, GV_HIST_ELEMENT);
187 	else
188 	    Map.hist_fp = G_fopen_modify(buf, GV_HIST_ELEMENT);
189 	if (Map.hist_fp == NULL) {
190 	    G_warning(_("Unable to open history file for vector map <%s>"),
191 		      Vect_get_full_name(&Map));
192 	    Vect_close(&Map);
193 	    exit(EXIT_FAILURE);
194 	}
195 	if (!h_flag->answer) {
196 	    G_fseek(Map.hist_fp, (long)0, SEEK_END);
197 	    Vect_hist_write(&Map,
198 			    "---------------------------------------------------------------------------------\n");
199 	}
200 	Vect_hist_write(&Map, "COMMAND: ");
201 	Vect_hist_write(&Map, cmdhist->answer);
202 	Vect_hist_write(&Map, "\n");
203 	sprintf(buf, "GISDBASE: %s\n", G_gisdbase());
204 	Vect_hist_write(&Map, buf);
205 	sprintf(buf, "LOCATION: %s MAPSET: %s USER: %s DATE: %s\n",
206 		G_location(), G_mapset(), G_whoami(), G_date());
207 	Vect_hist_write(&Map, buf);
208     }
209 
210     Vect_close(&Map);
211 
212     exit(EXIT_SUCCESS);
213 }
214