get_proj.c 15 KB

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