main.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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-2007, 2011 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, *projunits;
  24. struct Cell_head cellhd;
  25. int main(int argc, char *argv[])
  26. {
  27. struct Flag *printinfo, /* Print contents of PROJ_INFO & PROJ_UNITS */
  28. *shellinfo, /* Print in shell script style */
  29. *printproj4, /* Print projection in PROJ.4 format */
  30. *datuminfo, /* Check if datum information is present */
  31. *create, /* Create new projection files */
  32. #ifdef HAVE_OGR
  33. *printwkt, /* Print projection in WKT format */
  34. *esristyle, /* Use ESRI-style WKT format */
  35. #endif
  36. *dontprettify, /* Print 'flat' output (no linebreaks) */
  37. *forcedatumtrans; /* Force override of datumtrans parameters */
  38. struct Option *location, /* Name of new location to create */
  39. #ifdef HAVE_OGR
  40. *inepsg, /* EPSG projection code */
  41. *inwkt, /* Input file with projection in WKT format */
  42. *inproj4, /* Projection in PROJ.4 format */
  43. *ingeo, /* Input geo-referenced file readable by
  44. * GDAL or OGR */
  45. #endif
  46. *dtrans; /* index to datum transform option */
  47. struct GModule *module;
  48. int formats;
  49. G_set_program_name(argv[0]);
  50. G_no_gisinit(); /* We don't call G_gisinit() here because it validates the
  51. * mapset, whereas this module may legitmately be used
  52. * (to create a new location) when none exists */
  53. module = G_define_module();
  54. G_add_keyword(_("general"));
  55. G_add_keyword(_("projection"));
  56. G_add_keyword(_("create location"));
  57. #ifdef HAVE_OGR
  58. module->label =
  59. _("Prints and manipulates GRASS projection information files "
  60. "(in various co-ordinate system descriptions).");
  61. module->description =
  62. _("Can also be used to create new GRASS locations.");
  63. #else
  64. module->description =
  65. _("Prints and manipulates GRASS projection information files.");
  66. #endif
  67. printinfo = G_define_flag();
  68. printinfo->key = 'p';
  69. printinfo->guisection = _("Print");
  70. printinfo->description =
  71. _("Print projection information in conventional GRASS format");
  72. shellinfo = G_define_flag();
  73. shellinfo->key = 'g';
  74. shellinfo->guisection = _("Print");
  75. shellinfo->description =
  76. _("Print projection information in shell script style");
  77. datuminfo = G_define_flag();
  78. datuminfo->key = 'd';
  79. datuminfo->guisection = _("Print");
  80. datuminfo->description =
  81. _("Verify datum information and print transformation parameters");
  82. printproj4 = G_define_flag();
  83. printproj4->key = 'j';
  84. printproj4->guisection = _("Print");
  85. printproj4->description =
  86. _("Print projection information in PROJ.4 format");
  87. dontprettify = G_define_flag();
  88. dontprettify->key = 'f';
  89. dontprettify->guisection = _("Print");
  90. dontprettify->description =
  91. _("Print 'flat' output with no linebreaks (applies to "
  92. #ifdef HAVE_OGR
  93. "WKT and "
  94. #endif
  95. "PROJ.4 output)");
  96. #ifdef HAVE_OGR
  97. printwkt = G_define_flag();
  98. printwkt->key = 'w';
  99. printwkt->guisection = _("Print");
  100. printwkt->description = _("Print projection information in WKT format");
  101. esristyle = G_define_flag();
  102. esristyle->key = 'e';
  103. esristyle->guisection = _("Print");
  104. esristyle->description =
  105. _("Use ESRI-style format (applies to WKT output only)");
  106. ingeo = G_define_option();
  107. ingeo->key = "georef";
  108. ingeo->type = TYPE_STRING;
  109. ingeo->key_desc = "file";
  110. ingeo->required = NO;
  111. ingeo->guisection = _("Specification");
  112. ingeo->description = _("Name of georeferenced data file to read projection "
  113. "information from");
  114. inwkt = G_define_option();
  115. inwkt->key = "wkt";
  116. inwkt->type = TYPE_STRING;
  117. inwkt->key_desc = "file";
  118. inwkt->required = NO;
  119. inwkt->guisection = _("Specification");
  120. inwkt->label = _("Name of ASCII file containing a WKT projection "
  121. "description");
  122. inwkt->description = _("'-' for standard input");
  123. inproj4 = G_define_option();
  124. inproj4->key = "proj4";
  125. inproj4->type = TYPE_STRING;
  126. inproj4->key_desc = "params";
  127. inproj4->required = NO;
  128. inproj4->guisection = _("Specification");
  129. inproj4->label = _("PROJ.4 projection description");
  130. inproj4->description = _("'-' for standard input");
  131. inepsg = G_define_option();
  132. inepsg->key = "epsg";
  133. inepsg->type = TYPE_INTEGER;
  134. inepsg->required = NO;
  135. inepsg->options = "1-1000000";
  136. inepsg->guisection = _("Specification");
  137. inepsg->description = _("EPSG projection code");
  138. #endif
  139. dtrans = G_define_option();
  140. dtrans->key = "datumtrans";
  141. dtrans->type = TYPE_INTEGER;
  142. dtrans->required = NO;
  143. dtrans->options = "-1-100";
  144. dtrans->answer = "0";
  145. dtrans->guisection = _("Datum");
  146. dtrans->label = _("Index number of datum transform parameters");
  147. dtrans->description = _("\"0\" for unspecified or \"-1\" to list and exit");
  148. forcedatumtrans = G_define_flag();
  149. forcedatumtrans->key = 't';
  150. forcedatumtrans->guisection = _("Datum");
  151. forcedatumtrans->description =
  152. _("Force override of datum transformation information in input "
  153. "co-ordinate system");
  154. create = G_define_flag();
  155. create->key = 'c';
  156. create->guisection = _("Modify");
  157. create->description = _("Create new projection files (modifies current "
  158. "location)");
  159. location = G_define_option();
  160. location->key = "location";
  161. location->type = TYPE_STRING;
  162. location->key_desc = "name";
  163. location->required = NO;
  164. location->guisection = _("Create");
  165. location->description = _("Name of new location to create");
  166. if (G_parser(argc, argv))
  167. exit(EXIT_FAILURE);
  168. /* Initialisation & Validation */
  169. #ifdef HAVE_OGR
  170. /* -e implies -w */
  171. if (esristyle->answer && !printwkt->answer)
  172. printwkt->answer = 1;
  173. formats = ((ingeo->answer ? 1 : 0) + (inwkt->answer ? 1 : 0) +
  174. (inproj4->answer ? 1 : 0) + (inepsg->answer ? 1 : 0));
  175. if (formats > 1)
  176. G_fatal_error(_("Only one of '%s', '%s', '%s' or '%s' options may be specified"),
  177. ingeo->key, inwkt->key, inproj4->key, inepsg->key);
  178. /* Input */
  179. /* We can only have one input source, hence if..else construct */
  180. if (formats == 0)
  181. #endif
  182. /* Input is projection of current location */
  183. input_currloc();
  184. #ifdef HAVE_OGR
  185. else if (inwkt->answer)
  186. /* Input in WKT format */
  187. input_wkt(inwkt->answer);
  188. else if (inproj4->answer)
  189. /* Input in PROJ.4 format */
  190. input_proj4(inproj4->answer);
  191. else if (inepsg->answer)
  192. /* Input from EPSG code */
  193. input_epsg(atoi(inepsg->answer));
  194. else
  195. /* Input from georeferenced file */
  196. input_georef(ingeo->answer);
  197. #endif
  198. /* Consistency Check */
  199. if ((cellhd.proj != PROJECTION_XY)
  200. && (projinfo == NULL || projunits == NULL))
  201. G_fatal_error(_("Projection files missing"));
  202. /* Set Datum Parameters if necessary or requested */
  203. set_datumtrans(atoi(dtrans->answer), forcedatumtrans->answer);
  204. /* Output */
  205. /* Only allow one output format at a time, to reduce confusion */
  206. formats = ((printinfo->answer ? 1 : 0) + (shellinfo->answer ? 1 : 0) +
  207. (datuminfo->answer ? 1 : 0) +
  208. (printproj4->answer ? 1 : 0) +
  209. #ifdef HAVE_OGR
  210. (printwkt->answer ? 1 : 0) +
  211. #endif
  212. (create->answer ? 1 : 0));
  213. if (formats > 1)
  214. G_fatal_error(_("Only one of -%c, -%c, -%c, -%c"
  215. #ifdef HAVE_OGR
  216. ", -%c"
  217. #endif
  218. " or -%c flags may be specified"),
  219. printinfo->key, shellinfo->key, datuminfo->key, printproj4->key,
  220. #ifdef HAVE_OGR
  221. printwkt->key,
  222. #endif
  223. create->key);
  224. if (printinfo->answer || shellinfo->answer)
  225. print_projinfo(shellinfo->answer);
  226. else if (datuminfo->answer)
  227. print_datuminfo();
  228. else if (printproj4->answer)
  229. print_proj4(dontprettify->answer);
  230. #ifdef HAVE_OGR
  231. else if (printwkt->answer)
  232. print_wkt(esristyle->answer, dontprettify->answer);
  233. #endif
  234. else if (location->answer)
  235. create_location(location->answer);
  236. else if (create->answer)
  237. modify_projinfo();
  238. else
  239. #ifdef HAVE_OGR
  240. G_fatal_error(_("No output format specified, define one "
  241. "of flags -%c, -%c, -%c, or -%c"),
  242. printinfo->key, shellinfo->key, printproj4->key, printwkt->key);
  243. #else
  244. G_fatal_error(_("No output format specified, define one "
  245. "of flags -%c, -%c, or -%c"),
  246. printinfo->key, shellinfo->key, printproj4->key);
  247. #endif
  248. #ifdef HAVE_OGR
  249. if (create->answer && inepsg->answer) {
  250. #else
  251. if (create->answer){
  252. #endif
  253. /* preserve epsg code for user records only (not used by grass's pj routines) */
  254. FILE *fp;
  255. char path[GPATH_MAX];
  256. /* if inputs were not clean it should of failed by now */
  257. if (location->answer) {
  258. snprintf(path, sizeof(path), "%s/%s/%s/%s", G_gisdbase(),
  259. location->answer, "PERMANENT", "PROJ_EPSG");
  260. path[sizeof(path)-1] = '\0';
  261. }
  262. else
  263. G_file_name(path, "", "PROJ_EPSG", "PERMANENT");
  264. fp = fopen(path, "w");
  265. #ifdef HAVE_OGR
  266. fprintf(fp, "epsg: %s\n", inepsg->answer);
  267. #endif
  268. fclose(fp);
  269. }
  270. /* Tidy Up */
  271. if (projinfo != NULL)
  272. G_free_key_value(projinfo);
  273. if (projunits != NULL)
  274. G_free_key_value(projunits);
  275. exit(EXIT_SUCCESS);
  276. }