get_proj.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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, Markus Metz
  6. (C) 2003-2008, 2018 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 nopt;
  26. /* TODO: rename pj_ to GPJ_ to avoid symbol clash with PROJ lib */
  27. /**
  28. * \brief Create a pj_info struct Co-ordinate System definition from a set of
  29. * PROJ_INFO / PROJ_UNITS-style key-value pairs
  30. *
  31. * This function takes a GRASS-style co-ordinate system definition as stored
  32. * in the PROJ_INFO and PROJ_UNITS files and processes it to create a pj_info
  33. * representation for use in re-projecting with pj_do_proj(). In addition to
  34. * the parameters passed to it it may also make reference to the system
  35. * ellipse.table and datum.table files if necessary.
  36. *
  37. * \param info Pointer to a pj_info struct (which must already exist) into
  38. * which the co-ordinate system definition will be placed
  39. * \param in_proj_keys PROJ_INFO-style key-value pairs
  40. * \param in_units_keys PROJ_UNITS-style key-value pairs
  41. *
  42. * \return -1 on error (unable to initialise PROJ.4)
  43. * 2 if "default" 3-parameter datum shift values from datum.table
  44. * were used
  45. * 3 if an unrecognised datum name was passed on to PROJ.4 (and
  46. * initialization was successful)
  47. * 1 otherwise
  48. **/
  49. int pj_get_kv(struct pj_info *info, const struct Key_Value *in_proj_keys,
  50. const struct Key_Value *in_units_keys)
  51. {
  52. const char *str;
  53. int i;
  54. double a, es, rf;
  55. int returnval = 1;
  56. char buffa[300], factbuff[50];
  57. int deflen;
  58. char proj_in[250], *datum, *params;
  59. #ifdef HAVE_PROJ_H
  60. PJ *pj;
  61. PJ_CONTEXT *pjc;
  62. #else
  63. projPJ *pj;
  64. #endif
  65. proj_in[0] = '\0';
  66. info->zone = 0;
  67. info->meters = 1.0;
  68. info->proj[0] = '\0';
  69. info->def = NULL;
  70. info->pj = NULL;
  71. str = G_find_key_value("meters", in_units_keys);
  72. if (str != NULL) {
  73. strcpy(factbuff, str);
  74. if (strlen(factbuff) > 0)
  75. sscanf(factbuff, "%lf", &(info->meters));
  76. }
  77. str = G_find_key_value("name", in_proj_keys);
  78. if (str != NULL) {
  79. sprintf(proj_in, "%s", str);
  80. }
  81. str = G_find_key_value("proj", in_proj_keys);
  82. if (str != NULL) {
  83. sprintf(info->proj, "%s", str);
  84. }
  85. if (strlen(info->proj) <= 0)
  86. sprintf(info->proj, "ll");
  87. nopt = 0;
  88. for (i = 0; i < in_proj_keys->nitems; i++) {
  89. /* the name parameter is just for grasses use */
  90. if (strcmp(in_proj_keys->key[i], "name") == 0) {
  91. continue;
  92. /* zone handled separately at end of loop */
  93. }
  94. else if (strcmp(in_proj_keys->key[i], "zone") == 0) {
  95. continue;
  96. /* Datum and ellipsoid-related parameters will be handled
  97. * separately after end of this loop PK */
  98. }
  99. else if (strcmp(in_proj_keys->key[i], "datum") == 0
  100. || strcmp(in_proj_keys->key[i], "dx") == 0
  101. || strcmp(in_proj_keys->key[i], "dy") == 0
  102. || strcmp(in_proj_keys->key[i], "dz") == 0
  103. || strcmp(in_proj_keys->key[i], "datumparams") == 0
  104. || strcmp(in_proj_keys->key[i], "nadgrids") == 0
  105. || strcmp(in_proj_keys->key[i], "towgs84") == 0
  106. || strcmp(in_proj_keys->key[i], "ellps") == 0
  107. || strcmp(in_proj_keys->key[i], "a") == 0
  108. || strcmp(in_proj_keys->key[i], "b") == 0
  109. || strcmp(in_proj_keys->key[i], "es") == 0
  110. || strcmp(in_proj_keys->key[i], "f") == 0
  111. || strcmp(in_proj_keys->key[i], "rf") == 0) {
  112. continue;
  113. /* PROJ.4 uses longlat instead of ll as 'projection name' */
  114. }
  115. else if (strcmp(in_proj_keys->key[i], "proj") == 0) {
  116. if (strcmp(in_proj_keys->value[i], "ll") == 0)
  117. sprintf(buffa, "proj=longlat");
  118. else
  119. sprintf(buffa, "proj=%s", in_proj_keys->value[i]);
  120. /* 'One-sided' PROJ.4 flags will have the value in
  121. * the key-value pair set to 'defined' and only the
  122. * key needs to be passed on. */
  123. }
  124. else if (strcmp(in_proj_keys->value[i], "defined") == 0)
  125. sprintf(buffa, "%s", in_proj_keys->key[i]);
  126. else
  127. sprintf(buffa, "%s=%s",
  128. in_proj_keys->key[i], in_proj_keys->value[i]);
  129. alloc_options(buffa);
  130. }
  131. str = G_find_key_value("zone", in_proj_keys);
  132. if (str != NULL) {
  133. if (sscanf(str, "%d", &(info->zone)) != 1) {
  134. G_fatal_error(_("Invalid zone %s specified"), str);
  135. }
  136. if (info->zone < 0) {
  137. /* if zone is negative, write abs(zone) and define south */
  138. info->zone = -info->zone;
  139. if (G_find_key_value("south", in_proj_keys) == NULL) {
  140. sprintf(buffa, "south");
  141. alloc_options(buffa);
  142. }
  143. }
  144. sprintf(buffa, "zone=%d", info->zone);
  145. alloc_options(buffa);
  146. }
  147. if ((GPJ__get_ellipsoid_params(in_proj_keys, &a, &es, &rf) == 0)
  148. && (str = G_find_key_value("ellps", in_proj_keys)) != NULL) {
  149. /* Default values were returned but an ellipsoid name not recognised
  150. * by GRASS is present---perhaps it will be recognised by
  151. * PROJ.4 even though it wasn't by GRASS */
  152. sprintf(buffa, "ellps=%s", str);
  153. alloc_options(buffa);
  154. }
  155. else {
  156. sprintf(buffa, "a=%.16g", a);
  157. alloc_options(buffa);
  158. /* Cannot use es directly because the OSRImportFromProj4()
  159. * function in OGR only accepts b or rf as the 2nd parameter */
  160. if (es == 0)
  161. sprintf(buffa, "b=%.16g", a);
  162. else
  163. sprintf(buffa, "rf=%.16g", rf);
  164. alloc_options(buffa);
  165. }
  166. /* Workaround to stop PROJ reading values from defaults file when
  167. * rf (and sometimes ellps) is not specified */
  168. if (G_find_key_value("no_defs", in_proj_keys) == NULL) {
  169. sprintf(buffa, "no_defs");
  170. alloc_options(buffa);
  171. }
  172. /* If datum parameters are present in the PROJ_INFO keys, pass them on */
  173. if (GPJ__get_datum_params(in_proj_keys, &datum, &params) == 2) {
  174. sprintf(buffa, "%s", params);
  175. alloc_options(buffa);
  176. G_free(params);
  177. /* else if a datum name is present take it and look up the parameters
  178. * from the datum.table file */
  179. }
  180. else if (datum != NULL) {
  181. if (GPJ_get_default_datum_params_by_name(datum, &params) > 0) {
  182. sprintf(buffa, "%s", params);
  183. alloc_options(buffa);
  184. returnval = 2;
  185. G_free(params);
  186. /* else just pass the datum name on and hope it is recognised by
  187. * PROJ.4 even though it isn't recognised by GRASS */
  188. }
  189. else {
  190. sprintf(buffa, "datum=%s", datum);
  191. alloc_options(buffa);
  192. returnval = 3;
  193. }
  194. /* else there'll be no datum transformation taking place here... */
  195. }
  196. else {
  197. returnval = 4;
  198. }
  199. G_free(datum);
  200. #ifdef HAVE_PROJ_H
  201. pjc = proj_context_create();
  202. if (!(pj = proj_create_argv(pjc, nopt, opt_in))) {
  203. #else
  204. /* Set finder function for locating datum conversion tables PK */
  205. pj_set_finder(FINDERFUNC);
  206. if (!(pj = pj_init(nopt, opt_in))) {
  207. #endif
  208. strcpy(buffa,
  209. _("Unable to initialise PROJ with the following parameter list:"));
  210. for (i = 0; i < nopt; i++) {
  211. char err[50];
  212. sprintf(err, " +%s", opt_in[i]);
  213. strcat(buffa, err);
  214. }
  215. G_warning("%s", buffa);
  216. #ifndef HAVE_PROJ_H
  217. G_warning(_("The PROJ error message: %s"), pj_strerrno(pj_errno));
  218. #endif
  219. return -1;
  220. }
  221. #ifdef HAVE_PROJ_H
  222. int perr = proj_errno(pj);
  223. if (perr)
  224. G_fatal_error("PROJ 5 error %d", perr);
  225. #endif
  226. info->pj = pj;
  227. deflen = 0;
  228. for (i = 0; i < nopt; i++)
  229. deflen += strlen(opt_in[i]) + 2;
  230. info->def = G_malloc(deflen + 1);
  231. sprintf(buffa, "+%s ", opt_in[0]);
  232. strcpy(info->def, buffa);
  233. G_free(opt_in[0]);
  234. for (i = 1; i < nopt; i++) {
  235. sprintf(buffa, "+%s ", opt_in[i]);
  236. strcat(info->def, buffa);
  237. G_free(opt_in[i]);
  238. }
  239. return returnval;
  240. }
  241. static void alloc_options(char *buffa)
  242. {
  243. int nsize;
  244. nsize = strlen(buffa);
  245. opt_in[nopt++] = (char *)G_malloc(nsize + 1);
  246. sprintf(opt_in[nopt - 1], "%s", buffa);
  247. return;
  248. }
  249. /**
  250. * \brief Create a pj_info struct Co-ordinate System definition from a
  251. * string with a sequence of key=value pairs
  252. *
  253. * This function takes a GRASS- or PROJ style co-ordinate system definition
  254. * and processes it to create a pj_info representation for use in
  255. * re-projecting with pj_do_proj(). In addition to the parameters passed
  256. * to it it may also make reference to the system ellipse.table and
  257. * datum.table files if necessary.
  258. *
  259. * \param info Pointer to a pj_info struct (which must already exist) into
  260. * which the co-ordinate system definition will be placed
  261. * \param str input string with projection definition
  262. * \param in_units_keys PROJ_UNITS-style key-value pairs
  263. *
  264. * \return -1 on error (unable to initialise PROJ.4)
  265. * 1 on success
  266. **/
  267. int pj_get_string(struct pj_info *info, char *str)
  268. {
  269. char *s;
  270. int i, nsize;
  271. char zonebuff[50], buffa[300];
  272. int deflen;
  273. #ifdef HAVE_PROJ_H
  274. PJ *pj;
  275. PJ_CONTEXT *pjc;
  276. #else
  277. projPJ *pj;
  278. #endif
  279. info->zone = 0;
  280. info->proj[0] = '\0';
  281. info->meters = 1.0;
  282. info->def = NULL;
  283. info->pj = NULL;
  284. nopt = 0;
  285. if ((str == NULL) || (str[0] == '\0')) {
  286. /* Null Pointer or empty string is supplied for parameters,
  287. * implying latlong projection; just need to set proj
  288. * parameter and call pj_init PK */
  289. sprintf(info->proj, "ll");
  290. sprintf(buffa, "proj=latlong ellps=WGS84");
  291. alloc_options(buffa);
  292. }
  293. else {
  294. /* Parameters have been provided; parse through them but don't
  295. * bother with most of the checks in pj_get_kv; assume the
  296. * programmer knows what he / she is doing when using this
  297. * function rather than reading a PROJ_INFO file PK */
  298. s = str;
  299. while (s = strtok(s, " \t\n"), s) {
  300. if (strncmp(s, "+unfact=", 8) == 0) {
  301. s = s + 8;
  302. info->meters = atof(s);
  303. }
  304. else {
  305. if (strncmp(s, "+", 1) == 0)
  306. ++s;
  307. if (nsize = strlen(s), nsize) {
  308. if (nopt >= MAX_PARGS) {
  309. fprintf(stderr, "nopt = %d, s=%s\n", nopt, str);
  310. G_fatal_error(_("Option input overflowed option table"));
  311. }
  312. if (strncmp("zone=", s, 5) == 0) {
  313. sprintf(zonebuff, "%s", s + 5);
  314. sscanf(zonebuff, "%d", &(info->zone));
  315. }
  316. if (strncmp("proj=", s, 5) == 0) {
  317. sprintf(info->proj, "%s", s + 5);
  318. if (strcmp(info->proj, "ll") == 0)
  319. sprintf(buffa, "proj=latlong");
  320. else
  321. sprintf(buffa, "%s", s);
  322. }
  323. else {
  324. sprintf(buffa, "%s", s);
  325. }
  326. alloc_options(buffa);
  327. }
  328. }
  329. s = 0;
  330. }
  331. }
  332. #ifdef HAVE_PROJ_H
  333. pjc = proj_context_create();
  334. if (!(pj = proj_create_argv(pjc, nopt, opt_in))) {
  335. G_warning(_("Unable to initialize pj"));
  336. return -1;
  337. }
  338. #else
  339. /* Set finder function for locating datum conversion tables PK */
  340. pj_set_finder(FINDERFUNC);
  341. if (!(pj = pj_init(nopt, opt_in))) {
  342. G_warning(_("Unable to initialize pj cause: %s"),
  343. pj_strerrno(pj_errno));
  344. return -1;
  345. }
  346. #endif
  347. info->pj = pj;
  348. deflen = 0;
  349. for (i = 0; i < nopt; i++)
  350. deflen += strlen(opt_in[i]) + 2;
  351. info->def = G_malloc(deflen + 1);
  352. sprintf(buffa, "+%s ", opt_in[0]);
  353. strcpy(info->def, buffa);
  354. G_free(opt_in[0]);
  355. for (i = 1; i < nopt; i++) {
  356. sprintf(buffa, "+%s ", opt_in[i]);
  357. strcat(info->def, buffa);
  358. G_free(opt_in[i]);
  359. }
  360. return 1;
  361. }
  362. #ifndef HAVE_PROJ_H
  363. /* GPJ_get_equivalent_latlong(): only available with PROJ 4 API
  364. * with the new PROJ 5+ API, use pjold directly with PJ_FWD/PJ_INV transformation
  365. */
  366. /**
  367. * \brief Define a latitude / longitude co-ordinate system with the same
  368. * ellipsoid and datum parameters as an existing projected system
  369. *
  370. * This function is useful when projected co-ordinates need to be simply
  371. * converted to and from latitude / longitude.
  372. *
  373. * \param pjnew Pointer to pj_info struct for geographic co-ordinate system
  374. * that will be created
  375. * \param pjold Pointer to pj_info struct for existing projected co-ordinate
  376. * system
  377. *
  378. * \return 1 on success; -1 if there was an error (i.e. if the PROJ.4
  379. * pj_latlong_from_proj() function returned NULL)
  380. **/
  381. int GPJ_get_equivalent_latlong(struct pj_info *pjnew, struct pj_info *pjold)
  382. {
  383. char *deftmp;
  384. pjnew->meters = 1.;
  385. pjnew->zone = 0;
  386. pjnew->def = NULL;
  387. sprintf(pjnew->proj, "ll");
  388. if ((pjnew->pj = pj_latlong_from_proj(pjold->pj)) == NULL)
  389. return -1;
  390. deftmp = pj_get_def(pjnew->pj, 1);
  391. pjnew->def = G_store(deftmp);
  392. pj_dalloc(deftmp);
  393. return 1;
  394. }
  395. #endif
  396. /* set_proj_lib()
  397. * 'finder function' for use with PROJ.4 pj_set_finder() function */
  398. const char *set_proj_lib(const char *name)
  399. {
  400. const char *gisbase = G_gisbase();
  401. static char *buf = NULL;
  402. static size_t buf_len;
  403. size_t len = strlen(gisbase) + sizeof(GRIDDIR) + strlen(name) + 1;
  404. if (buf_len < len) {
  405. if (buf != NULL)
  406. G_free(buf);
  407. buf_len = len + 20;
  408. buf = G_malloc(buf_len);
  409. }
  410. sprintf(buf, "%s%s/%s", gisbase, GRIDDIR, name);
  411. return buf;
  412. }
  413. /**
  414. * \brief Print projection parameters as used by PROJ.4 for input and
  415. * output co-ordinate systems
  416. *
  417. * \param iproj 'Input' co-ordinate system
  418. * \param oproj 'Output' co-ordinate system
  419. *
  420. * \return 1 on success, -1 on error (i.e. if the PROJ-style definition
  421. * is NULL for either co-ordinate system)
  422. **/
  423. int pj_print_proj_params(const struct pj_info *iproj, const struct pj_info *oproj)
  424. {
  425. char *str;
  426. if (iproj) {
  427. str = iproj->def;
  428. if (str != NULL) {
  429. fprintf(stderr, "%s: %s\n", _("Input Projection Parameters"),
  430. str);
  431. fprintf(stderr, "%s: %.16g\n", _("Input Unit Factor"),
  432. iproj->meters);
  433. }
  434. else
  435. return -1;
  436. }
  437. if (oproj) {
  438. str = oproj->def;
  439. if (str != NULL) {
  440. fprintf(stderr, "%s: %s\n", _("Output Projection Parameters"),
  441. str);
  442. fprintf(stderr, "%s: %.16g\n", _("Output Unit Factor"),
  443. oproj->meters);
  444. }
  445. else
  446. return -1;
  447. }
  448. return 1;
  449. }