do_proj.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /**
  2. \file do_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 <string.h>
  13. #include <ctype.h>
  14. #include <grass/gis.h>
  15. #include <grass/gprojects.h>
  16. #include <grass/glocale.h>
  17. /* a couple defines to simplify reading the function */
  18. #define MULTIPLY_LOOP(x,y,c,m) \
  19. do {\
  20. int i; \
  21. for (i = 0; i < c; ++i) {\
  22. x[i] *= m; \
  23. y[i] *= m; \
  24. }\
  25. } while (0)
  26. #define DIVIDE_LOOP(x,y,c,m) \
  27. do {\
  28. int i; \
  29. for (i = 0; i < c; ++i) {\
  30. x[i] /= m; \
  31. y[i] /= m; \
  32. }\
  33. } while (0)
  34. static double METERS_in = 1.0, METERS_out = 1.0;
  35. #ifdef HAVE_PROJ_H
  36. static char *gpj_get_def(PJ *P)
  37. {
  38. char *pjdef;
  39. PJ_PROJ_INFO pj_proj_info = proj_pj_info(P);
  40. pjdef = G_store(pj_proj_info.definition);
  41. return pjdef;
  42. }
  43. #endif
  44. /* TODO: rename pj_ to GPJ_ to avoid symbol clash with PROJ lib */
  45. /**
  46. * \brief Re-project a point between two co-ordinate systems
  47. *
  48. * This function takes pointers to two pj_info structures as arguments,
  49. * and projects a point between the co-ordinate systems represented by them.
  50. * The easting and northing of the point are contained in two pointers passed
  51. * to the function; these will be overwritten by the co-ordinates of the
  52. * re-projected point.
  53. *
  54. * \param x Pointer to a double containing easting or longitude
  55. * \param y Pointer to a double containing northing or latitude
  56. * \param info_in pointer to pj_info struct for input co-ordinate system
  57. * \param info_out pointer to pj_info struct for output co-ordinate system
  58. *
  59. * \return Return value from PROJ proj_trans() function
  60. **/
  61. int pj_do_proj(double *x, double *y,
  62. const struct pj_info *info_in, const struct pj_info *info_out)
  63. {
  64. int ok;
  65. #ifdef HAVE_PROJ_H
  66. PJ *P;
  67. char *projdef, *projdefin, *projdefout;
  68. PJ_COORD c;
  69. projdefin = gpj_get_def(info_in->pj);
  70. projdefout = gpj_get_def(info_out->pj);
  71. projdef = NULL;
  72. G_asprintf(&projdef, "+proj=pipeline +step %s +inv +step %s", projdefin, projdefout);
  73. P = proj_create(PJ_DEFAULT_CTX, projdef);
  74. if (P == NULL)
  75. G_fatal_error(_("proj_create() failed"));
  76. G_free(projdefin);
  77. G_free(projdefout);
  78. G_free(projdef);
  79. METERS_in = info_in->meters;
  80. METERS_out = info_out->meters;
  81. if (strncmp(info_in->proj, "ll", 2) == 0) {
  82. /* convert to radians */
  83. c.lp.lam = (*x) / RAD_TO_DEG;
  84. c.lp.phi = (*y) / RAD_TO_DEG;
  85. c.lp.z = 0;
  86. c.lp.t = 0;
  87. c = proj_trans(P, PJ_FWD, c);
  88. ok = proj_errno(P);
  89. if (strncmp(info_out->proj, "ll", 2) == 0) {
  90. /* convert to degrees */
  91. *x = c.lp.lam * RAD_TO_DEG;
  92. *y = c.lp.phi * RAD_TO_DEG;
  93. }
  94. else {
  95. /* convert to map units */
  96. *x = c.xy.x / METERS_out;
  97. *y = c.xy.y / METERS_out;
  98. }
  99. }
  100. else {
  101. /* convert to meters */
  102. c.xy.x = *x * METERS_in;
  103. c.xy.y = *y * METERS_in;
  104. c.xy.z = 0;
  105. c.xy.t = 0;
  106. c = proj_trans(P, PJ_FWD, c);
  107. ok = proj_errno(P);
  108. if (strncmp(info_out->proj, "ll", 2) == 0) {
  109. /* convert to degrees */
  110. *x = c.lp.lam * RAD_TO_DEG;
  111. *y = c.lp.phi * RAD_TO_DEG;
  112. }
  113. else {
  114. /* convert to map units */
  115. *x = c.xy.x / METERS_out;
  116. *y = c.xy.y / METERS_out;
  117. }
  118. }
  119. proj_destroy(P);
  120. if (ok < 0) {
  121. G_warning(_("proj_trans() failed: %d"), ok);
  122. }
  123. #else
  124. double u, v;
  125. double h = 0.0;
  126. METERS_in = info_in->meters;
  127. METERS_out = info_out->meters;
  128. if (strncmp(info_in->proj, "ll", 2) == 0) {
  129. if (strncmp(info_out->proj, "ll", 2) == 0) {
  130. u = (*x) / RAD_TO_DEG;
  131. v = (*y) / RAD_TO_DEG;
  132. ok = pj_transform(info_in->pj, info_out->pj, 1, 0, &u, &v, &h);
  133. *x = u * RAD_TO_DEG;
  134. *y = v * RAD_TO_DEG;
  135. }
  136. else {
  137. u = (*x) / RAD_TO_DEG;
  138. v = (*y) / RAD_TO_DEG;
  139. ok = pj_transform(info_in->pj, info_out->pj, 1, 0, &u, &v, &h);
  140. *x = u / METERS_out;
  141. *y = v / METERS_out;
  142. }
  143. }
  144. else {
  145. if (strncmp(info_out->proj, "ll", 2) == 0) {
  146. u = *x * METERS_in;
  147. v = *y * METERS_in;
  148. ok = pj_transform(info_in->pj, info_out->pj, 1, 0, &u, &v, &h);
  149. *x = u * RAD_TO_DEG;
  150. *y = v * RAD_TO_DEG;
  151. }
  152. else {
  153. u = *x * METERS_in;
  154. v = *y * METERS_in;
  155. ok = pj_transform(info_in->pj, info_out->pj, 1, 0, &u, &v, &h);
  156. *x = u / METERS_out;
  157. *y = v / METERS_out;
  158. }
  159. }
  160. if (ok < 0) {
  161. G_warning(_("pj_transform() failed: %s"), pj_strerrno(ok));
  162. }
  163. #endif
  164. return ok;
  165. }
  166. /**
  167. * \brief Re-project an array of points between two co-ordinate systems with
  168. * optional ellipsoidal height conversion
  169. *
  170. * This function takes pointers to two pj_info structures as arguments,
  171. * and projects an array of points between the co-ordinate systems
  172. * represented by them. Pointers to the three arrays of easting, northing,
  173. * and ellipsoidal height of the point (this one may be NULL) are passed
  174. * to the function; these will be overwritten by the co-ordinates of the
  175. * re-projected points.
  176. *
  177. * \param count Number of points in the arrays to be transformed
  178. * \param x Pointer to an array of type double containing easting or longitude
  179. * \param y Pointer to an array of type double containing northing or latitude
  180. * \param h Pointer to an array of type double containing ellipsoidal height.
  181. * May be null in which case a two-dimensional re-projection will be
  182. * done
  183. * \param info_in pointer to pj_info struct for input co-ordinate system
  184. * \param info_out pointer to pj_info struct for output co-ordinate system
  185. *
  186. * \return Return value from PROJ proj_trans() function
  187. **/
  188. int pj_do_transform(int count, double *x, double *y, double *h,
  189. const struct pj_info *info_in, const struct pj_info *info_out)
  190. {
  191. int ok;
  192. int has_h = 1;
  193. #ifdef HAVE_PROJ_H
  194. int i;
  195. PJ *P;
  196. char *projdef, *projdefin, *projdefout;
  197. PJ_COORD c;
  198. projdefin = gpj_get_def(info_in->pj);
  199. projdefout = gpj_get_def(info_out->pj);
  200. projdef = NULL;
  201. G_asprintf(&projdef, "+proj=pipeline +step %s +inv +step %s", projdefin, projdefout);
  202. P = proj_create(PJ_DEFAULT_CTX, projdef);
  203. if (P == NULL)
  204. G_fatal_error(_("proj_create() failed"));
  205. G_free(projdefin);
  206. G_free(projdefout);
  207. G_free(projdef);
  208. METERS_in = info_in->meters;
  209. METERS_out = info_out->meters;
  210. if (h == NULL) {
  211. h = G_malloc(sizeof *h * count);
  212. /* they say memset is only guaranteed for chars ;-( */
  213. for (i = 0; i < count; ++i)
  214. h[i] = 0.0;
  215. has_h = 0;
  216. }
  217. ok = 0;
  218. if (strncmp(info_in->proj, "ll", 2) == 0) {
  219. c.lp.t = 0;
  220. if (strncmp(info_out->proj, "ll", 2) == 0) {
  221. for (i = 0; i < count; i++) {
  222. /* convert to radians */
  223. c.lp.lam = x[i] / RAD_TO_DEG;
  224. c.lp.phi = y[i] / RAD_TO_DEG;
  225. c.lp.z = h[i];
  226. c = proj_trans(P, PJ_FWD, c);
  227. if ((ok = proj_errno(P)) < 0)
  228. break;
  229. /* convert to degrees */
  230. x[i] = c.lp.lam * RAD_TO_DEG;
  231. y[i] = c.lp.phi * RAD_TO_DEG;
  232. }
  233. }
  234. else {
  235. for (i = 0; i < count; i++) {
  236. /* convert to radians */
  237. c.lp.lam = x[i] / RAD_TO_DEG;
  238. c.lp.phi = y[i] / RAD_TO_DEG;
  239. c.lp.z = h[i];
  240. c = proj_trans(P, PJ_FWD, c);
  241. if ((ok = proj_errno(P)) < 0)
  242. break;
  243. /* convert to map units */
  244. x[i] = c.xy.x / METERS_out;
  245. y[i] = c.xy.y / METERS_out;
  246. }
  247. }
  248. }
  249. else {
  250. c.xy.t = 0;
  251. if (strncmp(info_out->proj, "ll", 2) == 0) {
  252. for (i = 0; i < count; i++) {
  253. /* convert to meters */
  254. c.xy.x = x[i] * METERS_in;
  255. c.xy.y = y[i] * METERS_in;
  256. c.xy.z = h[i];
  257. c = proj_trans(P, PJ_FWD, c);
  258. if ((ok = proj_errno(P)) < 0)
  259. break;
  260. /* convert to degrees */
  261. x[i] = c.lp.lam * RAD_TO_DEG;
  262. y[i] = c.lp.phi * RAD_TO_DEG;
  263. }
  264. }
  265. else {
  266. for (i = 0; i < count; i++) {
  267. /* convert to meters */
  268. c.xy.x = x[i] * METERS_in;
  269. c.xy.y = y[i] * METERS_in;
  270. c.xy.z = h[i];
  271. c = proj_trans(P, PJ_FWD, c);
  272. if ((ok = proj_errno(P)) < 0)
  273. break;
  274. /* convert to map units */
  275. x[i] = c.xy.x / METERS_out;
  276. y[i] = c.xy.y / METERS_out;
  277. }
  278. }
  279. }
  280. if (!has_h)
  281. G_free(h);
  282. proj_destroy(P);
  283. if (ok < 0) {
  284. G_warning(_("proj_trans() failed: %d"), ok);
  285. #else
  286. METERS_in = info_in->meters;
  287. METERS_out = info_out->meters;
  288. if (h == NULL) {
  289. int i;
  290. h = G_malloc(sizeof *h * count);
  291. /* they say memset is only guaranteed for chars ;-( */
  292. for (i = 0; i < count; ++i)
  293. h[i] = 0.0;
  294. has_h = 0;
  295. }
  296. if (strncmp(info_in->proj, "ll", 2) == 0) {
  297. if (strncmp(info_out->proj, "ll", 2) == 0) {
  298. DIVIDE_LOOP(x, y, count, RAD_TO_DEG);
  299. ok = pj_transform(info_in->pj, info_out->pj, count, 1, x, y, h);
  300. MULTIPLY_LOOP(x, y, count, RAD_TO_DEG);
  301. }
  302. else {
  303. DIVIDE_LOOP(x, y, count, RAD_TO_DEG);
  304. ok = pj_transform(info_in->pj, info_out->pj, count, 1, x, y, h);
  305. DIVIDE_LOOP(x, y, count, METERS_out);
  306. }
  307. }
  308. else {
  309. if (strncmp(info_out->proj, "ll", 2) == 0) {
  310. MULTIPLY_LOOP(x, y, count, METERS_in);
  311. ok = pj_transform(info_in->pj, info_out->pj, count, 1, x, y, h);
  312. MULTIPLY_LOOP(x, y, count, RAD_TO_DEG);
  313. }
  314. else {
  315. MULTIPLY_LOOP(x, y, count, METERS_in);
  316. ok = pj_transform(info_in->pj, info_out->pj, count, 1, x, y, h);
  317. DIVIDE_LOOP(x, y, count, METERS_out);
  318. }
  319. }
  320. if (!has_h)
  321. G_free(h);
  322. if (ok < 0) {
  323. G_warning(_("pj_transform() failed: %s"), pj_strerrno(ok));
  324. #endif
  325. }
  326. return ok;
  327. }