get_proj.c 14 KB

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