get_proj.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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. #if PROJ_VERSION_MAJOR >= 6
  211. /* without type=crs, PROJ6 does not recognize what this is,
  212. * a crs or some kind of coordinate operation, falling through to
  213. * PJ_TYPE_OTHER_COORDINATE_OPERATION */
  214. alloc_options("type=crs");
  215. #endif
  216. pjc = proj_context_create();
  217. if (!(pj = proj_create_argv(pjc, nopt, opt_in))) {
  218. #else
  219. /* Set finder function for locating datum conversion tables PK */
  220. pj_set_finder(FINDERFUNC);
  221. if (!(pj = pj_init(nopt, opt_in))) {
  222. #endif
  223. strcpy(buffa,
  224. _("Unable to initialise PROJ with the following parameter list:"));
  225. for (i = 0; i < nopt; i++) {
  226. char err[50];
  227. sprintf(err, " +%s", opt_in[i]);
  228. strcat(buffa, err);
  229. }
  230. G_warning("%s", buffa);
  231. #ifndef HAVE_PROJ_H
  232. G_warning(_("The PROJ error message: %s"), pj_strerrno(pj_errno));
  233. #endif
  234. return -1;
  235. }
  236. #ifdef HAVE_PROJ_H
  237. int perr = proj_errno(pj);
  238. if (perr)
  239. G_fatal_error("PROJ 5 error %d", perr);
  240. #endif
  241. info->pj = pj;
  242. deflen = 0;
  243. for (i = 0; i < nopt; i++)
  244. deflen += strlen(opt_in[i]) + 2;
  245. info->def = G_malloc(deflen + 1);
  246. sprintf(buffa, "+%s ", opt_in[0]);
  247. strcpy(info->def, buffa);
  248. G_free(opt_in[0]);
  249. for (i = 1; i < nopt; i++) {
  250. sprintf(buffa, "+%s ", opt_in[i]);
  251. strcat(info->def, buffa);
  252. G_free(opt_in[i]);
  253. }
  254. return returnval;
  255. }
  256. static void alloc_options(char *buffa)
  257. {
  258. int nsize;
  259. nsize = strlen(buffa);
  260. opt_in[nopt++] = (char *)G_malloc(nsize + 1);
  261. sprintf(opt_in[nopt - 1], "%s", buffa);
  262. return;
  263. }
  264. /**
  265. * \brief Create a pj_info struct Co-ordinate System definition from a
  266. * string with a sequence of key=value pairs
  267. *
  268. * This function takes a GRASS- or PROJ style co-ordinate system definition
  269. * and processes it to create a pj_info representation for use in
  270. * re-projecting with pj_do_proj(). In addition to the parameters passed
  271. * to it it may also make reference to the system ellipse.table and
  272. * datum.table files if necessary.
  273. *
  274. * \param info Pointer to a pj_info struct (which must already exist) into
  275. * which the co-ordinate system definition will be placed
  276. * \param str input string with projection definition
  277. * \param in_units_keys PROJ_UNITS-style key-value pairs
  278. *
  279. * \return -1 on error (unable to initialise PROJ.4)
  280. * 1 on success
  281. **/
  282. int pj_get_string(struct pj_info *info, char *str)
  283. {
  284. char *s;
  285. int i, nsize;
  286. char zonebuff[50], buffa[300];
  287. int deflen;
  288. #ifdef HAVE_PROJ_H
  289. PJ *pj;
  290. PJ_CONTEXT *pjc;
  291. #else
  292. projPJ *pj;
  293. #endif
  294. info->zone = 0;
  295. info->proj[0] = '\0';
  296. info->meters = 1.0;
  297. info->def = NULL;
  298. info->srid = NULL;
  299. info->pj = NULL;
  300. nopt = 0;
  301. if ((str == NULL) || (str[0] == '\0')) {
  302. /* Null Pointer or empty string is supplied for parameters,
  303. * implying latlong projection; just need to set proj
  304. * parameter and call pj_init PK */
  305. sprintf(info->proj, "ll");
  306. sprintf(buffa, "proj=latlong ellps=WGS84");
  307. alloc_options(buffa);
  308. }
  309. else {
  310. /* Parameters have been provided; parse through them but don't
  311. * bother with most of the checks in pj_get_kv; assume the
  312. * programmer knows what he / she is doing when using this
  313. * function rather than reading a PROJ_INFO file PK */
  314. s = str;
  315. while (s = strtok(s, " \t\n"), s) {
  316. if (strncmp(s, "+unfact=", 8) == 0) {
  317. s = s + 8;
  318. info->meters = atof(s);
  319. }
  320. else {
  321. if (strncmp(s, "+", 1) == 0)
  322. ++s;
  323. if (nsize = strlen(s), nsize) {
  324. if (nopt >= MAX_PARGS) {
  325. fprintf(stderr, "nopt = %d, s=%s\n", nopt, str);
  326. G_fatal_error(_("Option input overflowed option table"));
  327. }
  328. if (strncmp("zone=", s, 5) == 0) {
  329. sprintf(zonebuff, "%s", s + 5);
  330. sscanf(zonebuff, "%d", &(info->zone));
  331. }
  332. if (strncmp(s, "init=", 5) == 0) {
  333. info->srid = G_store(s + 6);
  334. }
  335. if (strncmp("proj=", s, 5) == 0) {
  336. sprintf(info->proj, "%s", s + 5);
  337. if (strcmp(info->proj, "ll") == 0)
  338. sprintf(buffa, "proj=latlong");
  339. else
  340. sprintf(buffa, "%s", s);
  341. }
  342. else {
  343. sprintf(buffa, "%s", s);
  344. }
  345. alloc_options(buffa);
  346. }
  347. }
  348. s = 0;
  349. }
  350. }
  351. #ifdef HAVE_PROJ_H
  352. #if PROJ_VERSION_MAJOR >= 6
  353. /* without type=crs, PROJ6 does not recognize what this is,
  354. * a crs or some kind of coordinate operation, falling through to
  355. * PJ_TYPE_OTHER_COORDINATE_OPERATION */
  356. alloc_options("type=crs");
  357. #endif
  358. pjc = proj_context_create();
  359. if (!(pj = proj_create_argv(pjc, nopt, opt_in))) {
  360. G_warning(_("Unable to initialize pj cause: %s"),
  361. proj_errno_string(proj_context_errno(pjc)));
  362. return -1;
  363. }
  364. #else
  365. /* Set finder function for locating datum conversion tables PK */
  366. pj_set_finder(FINDERFUNC);
  367. if (!(pj = pj_init(nopt, opt_in))) {
  368. G_warning(_("Unable to initialize pj cause: %s"),
  369. pj_strerrno(pj_errno));
  370. return -1;
  371. }
  372. #endif
  373. info->pj = pj;
  374. deflen = 0;
  375. for (i = 0; i < nopt; i++)
  376. deflen += strlen(opt_in[i]) + 2;
  377. info->def = G_malloc(deflen + 1);
  378. sprintf(buffa, "+%s ", opt_in[0]);
  379. strcpy(info->def, buffa);
  380. G_free(opt_in[0]);
  381. for (i = 1; i < nopt; i++) {
  382. sprintf(buffa, "+%s ", opt_in[i]);
  383. strcat(info->def, buffa);
  384. G_free(opt_in[i]);
  385. }
  386. return 1;
  387. }
  388. #ifndef HAVE_PROJ_H
  389. /* GPJ_get_equivalent_latlong(): only available with PROJ 4 API
  390. * with the new PROJ 5+ API, use pjold directly with PJ_FWD/PJ_INV transformation
  391. */
  392. /**
  393. * \brief Define a latitude / longitude co-ordinate system with the same
  394. * ellipsoid and datum parameters as an existing projected system
  395. *
  396. * This function is useful when projected co-ordinates need to be simply
  397. * converted to and from latitude / longitude.
  398. *
  399. * \param pjnew Pointer to pj_info struct for geographic co-ordinate system
  400. * that will be created
  401. * \param pjold Pointer to pj_info struct for existing projected co-ordinate
  402. * system
  403. *
  404. * \return 1 on success; -1 if there was an error (i.e. if the PROJ.4
  405. * pj_latlong_from_proj() function returned NULL)
  406. **/
  407. int GPJ_get_equivalent_latlong(struct pj_info *pjnew, struct pj_info *pjold)
  408. {
  409. char *deftmp;
  410. pjnew->meters = 1.;
  411. pjnew->zone = 0;
  412. pjnew->def = NULL;
  413. sprintf(pjnew->proj, "ll");
  414. if ((pjnew->pj = pj_latlong_from_proj(pjold->pj)) == NULL)
  415. return -1;
  416. deftmp = pj_get_def(pjnew->pj, 1);
  417. pjnew->def = G_store(deftmp);
  418. pj_dalloc(deftmp);
  419. return 1;
  420. }
  421. #endif
  422. /* set_proj_lib()
  423. * 'finder function' for use with PROJ.4 pj_set_finder() function */
  424. const char *set_proj_lib(const char *name)
  425. {
  426. const char *gisbase = G_gisbase();
  427. static char *buf = NULL;
  428. static size_t buf_len;
  429. size_t len = strlen(gisbase) + sizeof(GRIDDIR) + strlen(name) + 1;
  430. if (buf_len < len) {
  431. if (buf != NULL)
  432. G_free(buf);
  433. buf_len = len + 20;
  434. buf = G_malloc(buf_len);
  435. }
  436. sprintf(buf, "%s%s/%s", gisbase, GRIDDIR, name);
  437. return buf;
  438. }
  439. /**
  440. * \brief Print projection parameters as used by PROJ.4 for input and
  441. * output co-ordinate systems
  442. *
  443. * \param iproj 'Input' co-ordinate system
  444. * \param oproj 'Output' co-ordinate system
  445. *
  446. * \return 1 on success, -1 on error (i.e. if the PROJ-style definition
  447. * is NULL for either co-ordinate system)
  448. **/
  449. int pj_print_proj_params(const struct pj_info *iproj, const struct pj_info *oproj)
  450. {
  451. char *str;
  452. if (iproj) {
  453. str = iproj->def;
  454. if (str != NULL) {
  455. fprintf(stderr, "%s: %s\n", _("Input Projection Parameters"),
  456. str);
  457. fprintf(stderr, "%s: %.16g\n", _("Input Unit Factor"),
  458. iproj->meters);
  459. }
  460. else
  461. return -1;
  462. }
  463. if (oproj) {
  464. str = oproj->def;
  465. if (str != NULL) {
  466. fprintf(stderr, "%s: %s\n", _("Output Projection Parameters"),
  467. str);
  468. fprintf(stderr, "%s: %.16g\n", _("Output Unit Factor"),
  469. oproj->meters);
  470. }
  471. else
  472. return -1;
  473. }
  474. return 1;
  475. }