main.c 11 KB

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