source: grass/trunk/general/g.proj/main.c

Last change on this file was 74220, checked in by mmetz, 5 years ago

g.proj: code cleanup for new option list_codes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id
  • Property svn:mime-type set to text/x-csrc
File size: 10.7 KB
Line 
1/*
2 ****************************************************************************
3 *
4 * MODULE: g.proj
5 * AUTHOR(S): Paul Kelly - paul-grass@stjohnspoint.co.uk
6 * Shell script style by Martin Landa <landa.martin gmail.com>
7 * PURPOSE: Provides a means of reporting the contents of GRASS
8 * projection information files and creating
9 * new projection information files.
10 * COPYRIGHT: (C) 2003-2015 by the GRASS Development Team
11 *
12 * This program is free software under the GNU General
13 * Public License (>=v2). Read the file COPYING that
14 * comes with GRASS for details.
15 *
16 *****************************************************************************/
17
18#include <stdlib.h>
19#include <string.h>
20#include <grass/gis.h>
21#include <grass/glocale.h>
22#include <grass/config.h>
23
24#include "local_proto.h"
25
26struct Key_Value *projinfo, *projunits, *projepsg;
27struct Cell_head cellhd;
28
29int main(int argc, char *argv[])
30{
31 struct Flag *printinfo, /* Print contents of PROJ_INFO & PROJ_UNITS */
32 *shellinfo, /* Print in shell script style */
33 *printproj4, /* Print projection in PROJ.4 format */
34 *datuminfo, /* Check if datum information is present */
35 *create, /* Create new projection files */
36#ifdef HAVE_OGR
37 *printwkt, /* Print projection in WKT format */
38 *esristyle, /* Use ESRI-style WKT format */
39#endif
40 *dontprettify, /* Print 'flat' output (no linebreaks) */
41 *forcedatumtrans; /* Force override of datumtrans parameters */
42
43 struct Option *location, /* Name of new location to create */
44#ifdef HAVE_OGR
45 *inepsg, /* EPSG projection code */
46 *inwkt, /* Input file with projection in WKT format */
47 *inproj4, /* Projection in PROJ.4 format */
48 *ingeo, /* Input geo-referenced file readable by
49 * GDAL or OGR */
50#endif
51 *listcodes, /* list codes of given authority */
52 *datum, /* datum to add (or replace existing datum) */
53 *dtrans; /* index to datum transform option */
54 struct GModule *module;
55
56 int formats;
57 const char *epsg = NULL;
58
59 G_set_program_name(argv[0]);
60 G_no_gisinit(); /* We don't call G_gisinit() here because it validates the
61 * mapset, whereas this module may legitmately be used
62 * (to create a new location) when none exists */
63
64 module = G_define_module();
65 G_add_keyword(_("general"));
66 G_add_keyword(_("projection"));
67 G_add_keyword(_("create location"));
68#ifdef HAVE_OGR
69 module->label =
70 _("Prints or modifies GRASS projection information files "
71 "(in various co-ordinate system descriptions).");
72 module->description =
73 _("Can also be used to create new GRASS locations.");
74#else
75 module->description =
76 _("Prints and manipulates GRASS projection information files.");
77#endif
78
79 printinfo = G_define_flag();
80 printinfo->key = 'p';
81 printinfo->guisection = _("Print");
82 printinfo->description =
83 _("Print projection information in conventional GRASS format");
84
85 shellinfo = G_define_flag();
86 shellinfo->key = 'g';
87 shellinfo->guisection = _("Print");
88 shellinfo->description =
89 _("Print projection information in shell script style");
90
91 datuminfo = G_define_flag();
92 datuminfo->key = 'd';
93 datuminfo->guisection = _("Print");
94 datuminfo->description =
95 _("Verify datum information and print transformation parameters");
96
97 printproj4 = G_define_flag();
98 printproj4->key = 'j';
99 printproj4->guisection = _("Print");
100 printproj4->description =
101 _("Print projection information in PROJ.4 format");
102
103 dontprettify = G_define_flag();
104 dontprettify->key = 'f';
105 dontprettify->guisection = _("Print");
106 dontprettify->description =
107 _("Print 'flat' output with no linebreaks (applies to "
108#ifdef HAVE_OGR
109 "WKT and "
110#endif
111 "PROJ.4 output)");
112
113#ifdef HAVE_OGR
114 printwkt = G_define_flag();
115 printwkt->key = 'w';
116 printwkt->guisection = _("Print");
117 printwkt->description = _("Print projection information in WKT format");
118
119 esristyle = G_define_flag();
120 esristyle->key = 'e';
121 esristyle->guisection = _("Print");
122 esristyle->description =
123 _("Use ESRI-style format (applies to WKT output only)");
124
125 ingeo = G_define_option();
126 ingeo->key = "georef";
127 ingeo->type = TYPE_STRING;
128 ingeo->key_desc = "file";
129 ingeo->required = NO;
130 ingeo->guisection = _("Specification");
131 ingeo->description = _("Name of georeferenced data file to read projection "
132 "information from");
133
134 inwkt = G_define_option();
135 inwkt->key = "wkt";
136 inwkt->type = TYPE_STRING;
137 inwkt->key_desc = "file";
138 inwkt->required = NO;
139 inwkt->guisection = _("Specification");
140 inwkt->label = _("Name of ASCII file containing a WKT projection "
141 "description");
142 inwkt->description = _("'-' for standard input");
143
144 inproj4 = G_define_option();
145 inproj4->key = "proj4";
146 inproj4->type = TYPE_STRING;
147 inproj4->key_desc = "params";
148 inproj4->required = NO;
149 inproj4->guisection = _("Specification");
150 inproj4->label = _("PROJ.4 projection description");
151 inproj4->description = _("'-' for standard input");
152
153 inepsg = G_define_option();
154 inepsg->key = "epsg";
155 inepsg->type = TYPE_INTEGER;
156 inepsg->key_desc = "code";
157 inepsg->required = NO;
158 inepsg->options = "1-1000000";
159 inepsg->guisection = _("Specification");
160 inepsg->description = _("EPSG projection code");
161#endif
162
163 listcodes = G_define_option();
164 listcodes->key = "list_codes";
165 listcodes->type = TYPE_STRING;
166 listcodes->required = NO;
167 listcodes->options = get_authority_names();
168 listcodes->guisection = _("Print");
169 listcodes->description = _("List codes for given authority, e.g. EPSG, and exit");
170
171 datum = G_define_option();
172 datum->key = "datum";
173 datum->type = TYPE_STRING;
174 datum->key_desc = "name";
175 datum->required = NO;
176 datum->guisection = _("Datum");
177 datum->label =
178 _("Datum (overrides any datum specified in input co-ordinate system)");
179 datum->description =
180 _("Accepts standard GRASS datum codes, or \"list\" to list and exit");
181
182 dtrans = G_define_option();
183 dtrans->key = "datum_trans";
184 dtrans->type = TYPE_INTEGER;
185 dtrans->key_desc = "index";
186 dtrans->required = NO;
187 dtrans->options = "-1-100";
188 dtrans->answer = "0";
189 dtrans->guisection = _("Datum");
190 dtrans->label = _("Index number of datum transform parameters");
191 dtrans->description = _("\"0\" for unspecified or \"-1\" to list and exit");
192
193 forcedatumtrans = G_define_flag();
194 forcedatumtrans->key = 't';
195 forcedatumtrans->guisection = _("Datum");
196 forcedatumtrans->description =
197 _("Force override of datum transformation information in input "
198 "co-ordinate system");
199
200 create = G_define_flag();
201 create->key = 'c';
202 create->guisection = _("Modify");
203 create->description = _("Modify current location projection files");
204
205 location = G_define_option();
206 location->key = "location";
207 location->type = TYPE_STRING;
208 location->key_desc = "name";
209 location->required = NO;
210 location->guisection = _("Create");
211 location->description = _("Name of new location to create");
212
213 if (G_parser(argc, argv))
214 exit(EXIT_FAILURE);
215
216
217 /* Initialisation & Validation */
218
219 /* list codes for given authority */
220 if (listcodes->answer) {
221 list_codes(listcodes->answer);
222 exit(EXIT_SUCCESS);
223 }
224
225#ifdef HAVE_OGR
226 /* -e implies -w */
227 if (esristyle->answer && !printwkt->answer)
228 printwkt->answer = 1;
229
230 formats = ((ingeo->answer ? 1 : 0) + (inwkt->answer ? 1 : 0) +
231 (inproj4->answer ? 1 : 0) + (inepsg->answer ? 1 : 0));
232 if (formats > 1)
233 G_fatal_error(_("Only one of '%s', '%s', '%s' or '%s' options may be specified"),
234 ingeo->key, inwkt->key, inproj4->key, inepsg->key);
235
236 /* List supported datums if requested; code originally
237 * from G_ask_datum_name() (formerly in libgis) */
238 if (datum->answer && strcmp(datum->answer, "list") == 0) {
239 const char *dat;
240 int i;
241
242 for (i = 0; (dat = G_datum_name(i)); i++) {
243 fprintf(stdout, "---\n%d\n%s\n%s\n%s ellipsoid\n",
244 i, dat, G_datum_description(i), G_datum_ellipsoid(i));
245 }
246
247 exit(EXIT_SUCCESS);
248 }
249
250 epsg = inepsg->answer;
251 projinfo = projunits = projepsg = NULL;
252
253 /* Input */
254 /* We can only have one input source, hence if..else construct */
255
256 if (formats == 0)
257#endif
258 /* Input is projection of current location */
259 input_currloc();
260#ifdef HAVE_OGR
261 else if (inwkt->answer)
262 /* Input in WKT format */
263 input_wkt(inwkt->answer);
264 else if (inproj4->answer)
265 /* Input in PROJ.4 format */
266 input_proj4(inproj4->answer);
267 else if (epsg)
268 /* Input from EPSG code */
269 input_epsg(atoi(epsg));
270 else
271 /* Input from georeferenced file */
272 input_georef(ingeo->answer);
273#endif
274
275 /* Consistency Check */
276
277 if ((cellhd.proj != PROJECTION_XY)
278 && (projinfo == NULL || projunits == NULL))
279 G_fatal_error(_("Projection files missing"));
280
281 /* Override input datum if requested */
282 if(datum->answer)
283 set_datum(datum->answer);
284
285 /* Set Datum Parameters if necessary or requested */
286 set_datumtrans(atoi(dtrans->answer), forcedatumtrans->answer);
287
288
289 /* Output */
290 /* Only allow one output format at a time, to reduce confusion */
291 formats = ((printinfo->answer ? 1 : 0) + (shellinfo->answer ? 1 : 0) +
292 (datuminfo->answer ? 1 : 0) +
293 (printproj4->answer ? 1 : 0) +
294#ifdef HAVE_OGR
295 (printwkt->answer ? 1 : 0) +
296#endif
297 (create->answer ? 1 : 0));
298 if (formats > 1)
299 G_fatal_error(_("Only one of -%c, -%c, -%c, -%c"
300#ifdef HAVE_OGR
301 ", -%c"
302#endif
303 " or -%c flags may be specified"),
304 printinfo->key, shellinfo->key, datuminfo->key, printproj4->key,
305#ifdef HAVE_OGR
306 printwkt->key,
307#endif
308 create->key);
309
310 if (printinfo->answer || shellinfo->answer)
311 print_projinfo(shellinfo->answer);
312 else if (datuminfo->answer)
313 print_datuminfo();
314 else if (printproj4->answer)
315 print_proj4(dontprettify->answer);
316#ifdef HAVE_OGR
317 else if (printwkt->answer)
318 print_wkt(esristyle->answer, dontprettify->answer);
319#endif
320 else if (location->answer)
321 create_location(location->answer);
322 else if (create->answer)
323 modify_projinfo();
324 else
325#ifdef HAVE_OGR
326 G_fatal_error(_("No output format specified, define one "
327 "of flags -%c, -%c, -%c, or -%c"),
328 printinfo->key, shellinfo->key, printproj4->key, printwkt->key);
329#else
330 G_fatal_error(_("No output format specified, define one "
331 "of flags -%c, -%c, or -%c"),
332 printinfo->key, shellinfo->key, printproj4->key);
333#endif
334
335 /* Tidy Up */
336 if (projinfo != NULL)
337 G_free_key_value(projinfo);
338 if (projunits != NULL)
339 G_free_key_value(projunits);
340 if (projepsg != NULL)
341 G_free_key_value(projepsg);
342
343 exit(EXIT_SUCCESS);
344
345}
Note: See TracBrowser for help on using the repository browser.