get_proj.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /**
  2. \file get_proj.c
  3. \brief GProj library - Functions for re-projecting point data
  4. \author Original Author unknown, probably Soil Conservation Service,
  5. Eric Miller, Paul Kelly
  6. (C) 2003-2008 by the GRASS Development Team
  7. This program is free software under the GNU General Public
  8. License (>=v2). Read the file COPYING that comes with GRASS
  9. for details.
  10. **/
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <ctype.h>
  14. #include <math.h>
  15. #include <string.h>
  16. #include <grass/gis.h>
  17. #include <grass/gprojects.h>
  18. #include <grass/glocale.h>
  19. /* Finder function for datum conversion lookup tables */
  20. #define FINDERFUNC set_proj_lib
  21. #define PERMANENT "PERMANENT"
  22. #define MAX_PARGS 100
  23. static void alloc_options(char *);
  24. static char *opt_in[MAX_PARGS];
  25. static int nopt1;
  26. /**
  27. * \brief Create a pj_info struct Co-ordinate System definition from a set of
  28. * PROJ_INFO / PROJ_UNITS-style key-value pairs
  29. *
  30. * This function takes a GRASS-style co-ordinate system definition as stored
  31. * in the PROJ_INFO and PROJ_UNITS files and processes it to create a pj_info
  32. * representation for use in re-projecting with pj_do_proj(). In addition to
  33. * the parameters passed to it it may also make reference to the system
  34. * ellipse.table and datum.table files if necessary.
  35. *
  36. * \param info Pointer to a pj_info struct (which must already exist) into
  37. * which the co-ordinate system definition will be placed
  38. * \param in_proj_keys PROJ_INFO-style key-value pairs
  39. * \param in_units_keys PROJ_UNITS-style key-value pairs
  40. *
  41. * \return -1 on error (unable to initialise PROJ.4)
  42. * 2 if "default" 3-parameter datum shift values from datum.table
  43. * were used
  44. * 3 if an unrecognised datum name was passed on to PROJ.4 (and
  45. * initialization was successful
  46. * 1 otherwise
  47. **/
  48. int pj_get_kv(struct pj_info *info, const struct Key_Value *in_proj_keys,
  49. const struct Key_Value *in_units_keys)
  50. {
  51. const char *str;
  52. int i;
  53. double a, es, rf;
  54. int returnval = 1;
  55. char buffa[300], factbuff[50];
  56. char proj_in[50], *datum, *params;
  57. projPJ *pj;
  58. proj_in[0] = '\0';
  59. info->zone = 0;
  60. info->meters = 1.0;
  61. info->proj[0] = '\0';
  62. str = G_find_key_value("meters", in_units_keys);
  63. if (str != NULL) {
  64. strcpy(factbuff, str);
  65. if (strlen(factbuff) > 0)
  66. sscanf(factbuff, "%lf", &(info->meters));
  67. }
  68. str = G_find_key_value("name", in_proj_keys);
  69. if (str != NULL) {
  70. sprintf(proj_in, "%s", str);
  71. }
  72. str = G_find_key_value("proj", in_proj_keys);
  73. if (str != NULL) {
  74. sprintf(info->proj, "%s", str);
  75. }
  76. if (strlen(info->proj) <= 0)
  77. sprintf(info->proj, "ll");
  78. nopt1 = 0;
  79. for (i = 0; i < in_proj_keys->nitems; i++) {
  80. /* the name parameter is just for grasses use */
  81. if (strcmp(in_proj_keys->key[i], "name") == 0) {
  82. continue;
  83. /* zone handled separately at end of loop */
  84. }
  85. else if (strcmp(in_proj_keys->key[i], "zone") == 0) {
  86. continue;
  87. /* Datum and ellipsoid-related parameters will be handled
  88. * separately after end of this loop PK */
  89. }
  90. else if (strcmp(in_proj_keys->key[i], "datum") == 0
  91. || strcmp(in_proj_keys->key[i], "dx") == 0
  92. || strcmp(in_proj_keys->key[i], "dy") == 0
  93. || strcmp(in_proj_keys->key[i], "dz") == 0
  94. || strcmp(in_proj_keys->key[i], "datumparams") == 0
  95. || strcmp(in_proj_keys->key[i], "nadgrids") == 0
  96. || strcmp(in_proj_keys->key[i], "towgs84") == 0
  97. || strcmp(in_proj_keys->key[i], "ellps") == 0
  98. || strcmp(in_proj_keys->key[i], "a") == 0
  99. || strcmp(in_proj_keys->key[i], "b") == 0
  100. || strcmp(in_proj_keys->key[i], "es") == 0
  101. || strcmp(in_proj_keys->key[i], "f") == 0
  102. || strcmp(in_proj_keys->key[i], "rf") == 0) {
  103. continue;
  104. /* PROJ.4 uses longlat instead of ll as 'projection name' */
  105. }
  106. else if (strcmp(in_proj_keys->key[i], "proj") == 0) {
  107. if (strcmp(in_proj_keys->value[i], "ll") == 0)
  108. sprintf(buffa, "proj=longlat");
  109. else
  110. sprintf(buffa, "proj=%s", in_proj_keys->value[i]);
  111. /* 'One-sided' PROJ.4 flags will have the value in
  112. * the key-value pair set to 'defined' and only the
  113. * key needs to be passed on. */
  114. }
  115. else if (strcmp(in_proj_keys->value[i], "defined") == 0)
  116. sprintf(buffa, "%s", in_proj_keys->key[i]);
  117. else
  118. sprintf(buffa, "%s=%s",
  119. in_proj_keys->key[i], in_proj_keys->value[i]);
  120. alloc_options(buffa);
  121. }
  122. str = G_find_key_value("zone", in_proj_keys);
  123. if (str != NULL) {
  124. if (sscanf(str, "%d", &(info->zone)) != 1) {
  125. G_fatal_error(_("Invalid zone %s specified"), str);
  126. }
  127. if (info->zone < 0) {
  128. /* if zone is negative, write abs(zone) and define south */
  129. info->zone = -info->zone;
  130. if (G_find_key_value("south", in_proj_keys) == NULL) {
  131. sprintf(buffa, "south");
  132. alloc_options(buffa);
  133. }
  134. }
  135. sprintf(buffa, "zone=%d", info->zone);
  136. alloc_options(buffa);
  137. }
  138. if ((GPJ__get_ellipsoid_params(in_proj_keys, &a, &es, &rf) == 0)
  139. && (str = G_find_key_value("ellps", in_proj_keys)) != NULL) {
  140. /* Default values were returned but an ellipsoid name not recognised
  141. * by GRASS is present---perhaps it will be recognised by
  142. * PROJ.4 even though it wasn't by GRASS */
  143. sprintf(buffa, "ellps=%s", str);
  144. alloc_options(buffa);
  145. }
  146. else {
  147. sprintf(buffa, "a=%.16g", a);
  148. alloc_options(buffa);
  149. /* Cannot use es directly because the OSRImportFromProj4()
  150. * function in OGR only accepts b or rf as the 2nd parameter */
  151. if (es == 0)
  152. sprintf(buffa, "b=%.16g", a);
  153. else
  154. sprintf(buffa, "rf=%.16g", rf);
  155. alloc_options(buffa);
  156. }
  157. /* Workaround to stop PROJ reading values from defaults file when
  158. * rf (and sometimes ellps) is not specified */
  159. if (G_find_key_value("no_defs", in_proj_keys) == NULL) {
  160. sprintf(buffa, "no_defs");
  161. alloc_options(buffa);
  162. }
  163. /* If datum parameters are present in the PROJ_INFO keys, pass them on */
  164. if (GPJ__get_datum_params(in_proj_keys, &datum, &params) == 2) {
  165. sprintf(buffa, "%s", params);
  166. alloc_options(buffa);
  167. G_free(params);
  168. /* else if a datum name is present take it and look up the parameters
  169. * from the datum.table file */
  170. }
  171. else if (datum != NULL) {
  172. if (GPJ_get_default_datum_params_by_name(datum, &params) > 0) {
  173. sprintf(buffa, "%s", params);
  174. alloc_options(buffa);
  175. returnval = 2;
  176. G_free(params);
  177. /* else just pass the datum name on and hope it is recognised by
  178. * PROJ.4 even though it isn't recognised by GRASS */
  179. }
  180. else {
  181. sprintf(buffa, "datum=%s", datum);
  182. alloc_options(buffa);
  183. returnval = 3;
  184. }
  185. /* else there'll be no datum transformation taking place here... */
  186. }
  187. else {
  188. returnval = 4;
  189. }
  190. G_free(datum);
  191. /* Set finder function for locating datum conversion tables PK */
  192. pj_set_finder(FINDERFUNC);
  193. if (!(pj = pj_init(nopt1, opt_in))) {
  194. strcpy(buffa,
  195. _("Unable to initialise PROJ.4 with the following parameter list:"));
  196. for (i = 0; i < nopt1; i++) {
  197. char err[50];
  198. sprintf(err, " +%s", opt_in[i]);
  199. strcat(buffa, err);
  200. }
  201. G_warning("%s", buffa);
  202. G_warning(_("The error message: %s"), pj_strerrno(pj_errno));
  203. return -1;
  204. }
  205. info->pj = pj;
  206. return returnval;
  207. }
  208. static void alloc_options(char *buffa)
  209. {
  210. int nsize;
  211. nsize = strlen(buffa);
  212. opt_in[nopt1++] = (char *)G_malloc(nsize + 1);
  213. sprintf(opt_in[nopt1 - 1], "%s", buffa);
  214. return;
  215. }
  216. int pj_get_string(struct pj_info *info, char *str)
  217. {
  218. char *opt_in[MAX_PARGS];
  219. char *s;
  220. int nopt = 0;
  221. int nsize;
  222. char zonebuff[50], buffa[300];
  223. projPJ *pj;
  224. info->zone = 0;
  225. info->proj[0] = '\0';
  226. info->meters = 1.0;
  227. if ((str == NULL) || (str[0] == '\0')) {
  228. /* Null Pointer or empty string is supplied for parameters,
  229. * implying latlong projection; just need to set proj
  230. * parameter and call pj_init PK */
  231. sprintf(info->proj, "ll");
  232. sprintf(buffa, "proj=latlong ellps=WGS84");
  233. nsize = strlen(buffa);
  234. opt_in[nopt] = (char *)G_malloc(nsize + 1);
  235. sprintf(opt_in[nopt++], "%s", buffa);
  236. }
  237. else {
  238. /* Parameters have been provided; parse through them but don't
  239. * bother with most of the checks in pj_get_kv; assume the
  240. * programmer knows what he / she is doing when using this
  241. * function rather than reading a PROJ_INFO file PK */
  242. s = str;
  243. while (s = strtok(s, " \t\n"), s) {
  244. if (strncmp(s, "+unfact=", 8) == 0) {
  245. s = s + 8;
  246. info->meters = atof(s);
  247. }
  248. else {
  249. if (strncmp(s, "+", 1) == 0)
  250. ++s;
  251. if (nsize = strlen(s), nsize) {
  252. if (nopt >= MAX_PARGS) {
  253. fprintf(stderr, "nopt = %d, s=%s\n", nopt, str);
  254. G_fatal_error(_("Option input overflowed option table"));
  255. }
  256. if (strncmp("zone=", s, 5) == 0) {
  257. sprintf(zonebuff, "%s", s + 5);
  258. sscanf(zonebuff, "%d", &(info->zone));
  259. }
  260. if (strncmp("proj=", s, 5) == 0) {
  261. sprintf(info->proj, "%s", s + 5);
  262. if (strcmp(info->proj, "ll") == 0)
  263. sprintf(buffa, "proj=latlong");
  264. else
  265. sprintf(buffa, "%s", s);
  266. }
  267. else {
  268. sprintf(buffa, "%s", s);
  269. }
  270. nsize = strlen(buffa);
  271. opt_in[nopt] = (char *)G_malloc(nsize + 1);
  272. sprintf(opt_in[nopt++], "%s", buffa);
  273. }
  274. }
  275. s = 0;
  276. }
  277. }
  278. /* Set finder function for locating datum conversion tables PK */
  279. pj_set_finder(FINDERFUNC);
  280. if (!(pj = pj_init(nopt, opt_in))) {
  281. G_warning(_("Unable to initialize pj cause: %s"),
  282. pj_strerrno(pj_errno));
  283. return -1;
  284. }
  285. info->pj = pj;
  286. return 1;
  287. }
  288. /**
  289. * \brief Define a latitude / longitude co-ordinate system with the same
  290. * ellipsoid and datum parameters as an existing projected system
  291. *
  292. * This function is useful when projected co-ordinates need to be simply
  293. * converted to and from latitude / longitude.
  294. *
  295. * \param pjnew Pointer to pj_info struct for geographic co-ordinate system
  296. * that will be created
  297. * \param pjold Pointer to pj_info struct for existing projected co-ordinate
  298. * system
  299. *
  300. * \return 1 on success; -1 if there was an error (i.e. if the PROJ.4
  301. * pj_latlong_from_proj() function returned NULL)
  302. **/
  303. int GPJ_get_equivalent_latlong(struct pj_info *pjnew, struct pj_info *pjold)
  304. {
  305. pjnew->meters = 1.;
  306. pjnew->zone = 0;
  307. sprintf(pjnew->proj, "ll");
  308. if ((pjnew->pj = pj_latlong_from_proj(pjold->pj)) == NULL)
  309. return -1;
  310. else
  311. return 1;
  312. }
  313. /* set_proj_lib()
  314. * 'finder function' for use with PROJ.4 pj_set_finder() function */
  315. const char *set_proj_lib(const char *name)
  316. {
  317. const char *gisbase = G_gisbase();
  318. static char *buf = NULL;
  319. static size_t buf_len;
  320. size_t len = strlen(gisbase) + sizeof(GRIDDIR) + strlen(name) + 1;
  321. if (buf_len < len) {
  322. if (buf != NULL)
  323. G_free(buf);
  324. buf_len = len + 20;
  325. buf = G_malloc(buf_len);
  326. }
  327. sprintf(buf, "%s%s/%s", gisbase, GRIDDIR, name);
  328. return buf;
  329. }
  330. /**
  331. * \brief Print projection parameters as used by PROJ.4 for input and
  332. * output co-ordinate systems
  333. *
  334. * \param iproj 'Input' co-ordinate system
  335. * \param oproj 'Output' co-ordinate system
  336. *
  337. * \return 1 on success, -1 on error (i.e. if PROJ.4 pj_get_def() function
  338. * returned NULL for either co-ordinate system)
  339. **/
  340. int pj_print_proj_params(const struct pj_info *iproj, const struct pj_info *oproj)
  341. {
  342. char *str;
  343. if (iproj) {
  344. str = pj_get_def(iproj->pj, 1);
  345. if (str != NULL) {
  346. fprintf(stderr, "%s: %s\n", _("Input Projection Parameters"),
  347. str);
  348. pj_dalloc(str);
  349. fprintf(stderr, "%s: %.16g\n", _("Input Unit Factor"),
  350. iproj->meters);
  351. }
  352. else
  353. return -1;
  354. }
  355. if (oproj) {
  356. str = pj_get_def(oproj->pj, 1);
  357. if (str != NULL) {
  358. fprintf(stderr, "%s: %s\n", _("Output Projection Parameters"),
  359. str);
  360. pj_dalloc(str);
  361. fprintf(stderr, "%s: %.16g\n", _("Output Unit Factor"),
  362. oproj->meters);
  363. }
  364. else
  365. return -1;
  366. }
  367. return 1;
  368. }