do_proj.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651
  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, 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 <string.h>
  13. #include <ctype.h>
  14. #include <grass/gis.h>
  15. #include <grass/gprojects.h>
  16. #include <grass/glocale.h>
  17. #ifdef HAVE_PROJ_H
  18. /* just in case PROJ introduces PROJ_VERSION_NUM in a future version */
  19. #ifdef PROJ_VERSION_NUM
  20. #undef PROJ_VERSION_NUM
  21. #endif
  22. #define PROJ_VERSION_NUM ((PROJ_VERSION_MAJOR)*1000000+(PROJ_VERSION_MINOR)*10000+(PROJ_VERSION_PATCH)*100)
  23. #endif
  24. /* a couple defines to simplify reading the function */
  25. #define MULTIPLY_LOOP(x,y,c,m) \
  26. do {\
  27. for (i = 0; i < c; ++i) {\
  28. x[i] *= m; \
  29. y[i] *= m; \
  30. }\
  31. } while (0)
  32. #define DIVIDE_LOOP(x,y,c,m) \
  33. do {\
  34. for (i = 0; i < c; ++i) {\
  35. x[i] /= m; \
  36. y[i] /= m; \
  37. }\
  38. } while (0)
  39. static double METERS_in = 1.0, METERS_out = 1.0;
  40. #ifdef HAVE_PROJ_H
  41. #if PROJ_VERSION_MAJOR >= 6
  42. int get_pj_area(double *xmin, double *xmax, double *ymin, double *ymax)
  43. {
  44. struct Cell_head window;
  45. G_unset_window();
  46. G_get_window(&window);
  47. *xmin = window.west;
  48. *xmax = window.east;
  49. *ymin = window.south;
  50. *ymax = window.north;
  51. if (window.proj != PROJECTION_LL) {
  52. /* transform to ll equivalent */
  53. double estep, nstep;
  54. double x[85], y[85];
  55. int i;
  56. const char *projstr = NULL;
  57. char *indef = NULL;
  58. /* projection information of current location */
  59. struct Key_Value *in_proj_info, *in_unit_info;
  60. struct pj_info iproj, oproj, tproj; /* proj parameters */
  61. PJ *source_crs;
  62. /* read current projection info */
  63. if ((in_proj_info = G_get_projinfo()) == NULL) {
  64. G_warning(_("Can't get projection info of current location"));
  65. return 0;
  66. }
  67. if ((in_unit_info = G_get_projunits()) == NULL) {
  68. G_warning(_("Can't get projection units of current location"));
  69. return 0;
  70. }
  71. if (pj_get_kv(&iproj, in_proj_info, in_unit_info) < 0) {
  72. G_fatal_error(_("Can't get projection key values of current location"));
  73. return 0;
  74. }
  75. G_free_key_value(in_proj_info);
  76. G_free_key_value(in_unit_info);
  77. oproj.pj = NULL;
  78. tproj.def = NULL;
  79. source_crs = proj_get_source_crs(NULL, iproj.pj);
  80. if (source_crs) {
  81. projstr = proj_as_proj_string(NULL, source_crs, PJ_PROJ_5, NULL);
  82. if (projstr) {
  83. indef = G_store(projstr);
  84. proj_destroy(iproj.pj);
  85. iproj.pj = source_crs;
  86. }
  87. }
  88. if (indef == NULL)
  89. indef = G_store(iproj.def);
  90. G_asprintf(&tproj.def, "+proj=pipeline +step +inv %s",
  91. indef);
  92. tproj.pj = proj_create(PJ_DEFAULT_CTX, tproj.def);
  93. if (tproj.pj == NULL) {
  94. G_warning(_("proj_create() failed for '%s'"), tproj.def);
  95. G_free(indef);
  96. G_free(tproj.def);
  97. proj_destroy(tproj.pj);
  98. return 0;
  99. }
  100. projstr = proj_as_proj_string(NULL, tproj.pj, PJ_PROJ_5, NULL);
  101. if (projstr == NULL) {
  102. G_warning(_("proj_create() failed for '%s'"), tproj.def);
  103. G_free(indef);
  104. G_free(tproj.def);
  105. proj_destroy(tproj.pj);
  106. return 0;
  107. }
  108. G_free(indef);
  109. estep = (window.west + window.east) / 21.;
  110. nstep = (window.north + window.south) / 21.;
  111. for (i = 0; i < 20; i++) {
  112. x[i] = window.west + estep * (i + 1);
  113. y[i] = window.north;
  114. x[i + 20] = window.west + estep * (i + 1);
  115. y[i + 20] = window.south;
  116. x[i + 40] = window.west;
  117. y[i + 40] = window.south + nstep * (i + 1);
  118. x[i + 60] = window.east;
  119. y[i + 60] = window.south + nstep * (i + 1);
  120. }
  121. x[80] = window.west;
  122. y[80] = window.north;
  123. x[81] = window.west;
  124. y[81] = window.south;
  125. x[82] = window.east;
  126. y[82] = window.north;
  127. x[83] = window.east;
  128. y[83] = window.south;
  129. x[84] = (window.west + window.east) / 2.;
  130. y[84] = (window.north + window.south) / 2.;
  131. GPJ_transform_array(&iproj, &oproj, &tproj, PJ_FWD, x, y, NULL, 85);
  132. proj_destroy(tproj.pj);
  133. G_free(tproj.def);
  134. *xmin = *xmax = x[84];
  135. *ymin = *ymax = y[84];
  136. for (i = 0; i < 84; i++) {
  137. if (*xmin > x[i])
  138. *xmin = x[i];
  139. if (*xmax < x[i])
  140. *xmax = x[i];
  141. if (*ymin > y[i])
  142. *ymin = y[i];
  143. if (*ymax < y[i])
  144. *ymax = y[i];
  145. }
  146. }
  147. G_debug(1, "get_pj_area(): xmin %g, xmax %g, ymin %g, ymax %g",
  148. *xmin, *xmax, *ymin, *ymax);
  149. return 1;
  150. }
  151. char *get_pj_type_string(PJ *pj)
  152. {
  153. char *pj_type = NULL;
  154. switch (proj_get_type(pj)) {
  155. case PJ_TYPE_UNKNOWN:
  156. G_asprintf(&pj_type, "unknown");
  157. break;
  158. case PJ_TYPE_ELLIPSOID:
  159. G_asprintf(&pj_type, "ellipsoid");
  160. break;
  161. case PJ_TYPE_PRIME_MERIDIAN:
  162. G_asprintf(&pj_type, "prime meridian");
  163. break;
  164. case PJ_TYPE_GEODETIC_REFERENCE_FRAME:
  165. G_asprintf(&pj_type, "geodetic reference frame");
  166. break;
  167. case PJ_TYPE_DYNAMIC_GEODETIC_REFERENCE_FRAME:
  168. G_asprintf(&pj_type, "dynamic geodetic reference frame");
  169. break;
  170. case PJ_TYPE_VERTICAL_REFERENCE_FRAME:
  171. G_asprintf(&pj_type, "vertical reference frame");
  172. break;
  173. case PJ_TYPE_DYNAMIC_VERTICAL_REFERENCE_FRAME:
  174. G_asprintf(&pj_type, "dynamic vertical reference frame");
  175. break;
  176. case PJ_TYPE_DATUM_ENSEMBLE:
  177. G_asprintf(&pj_type, "datum ensemble");
  178. break;
  179. /** Abstract type, not returned by proj_get_type() */
  180. case PJ_TYPE_CRS:
  181. G_asprintf(&pj_type, "crs");
  182. break;
  183. case PJ_TYPE_GEODETIC_CRS:
  184. G_asprintf(&pj_type, "geodetic crs");
  185. break;
  186. case PJ_TYPE_GEOCENTRIC_CRS:
  187. G_asprintf(&pj_type, "geocentric crs");
  188. break;
  189. /** proj_get_type() will never return that type, but
  190. * PJ_TYPE_GEOGRAPHIC_2D_CRS or PJ_TYPE_GEOGRAPHIC_3D_CRS. */
  191. case PJ_TYPE_GEOGRAPHIC_CRS:
  192. G_asprintf(&pj_type, "geographic crs");
  193. break;
  194. case PJ_TYPE_GEOGRAPHIC_2D_CRS:
  195. G_asprintf(&pj_type, "geographic 2D crs");
  196. break;
  197. case PJ_TYPE_GEOGRAPHIC_3D_CRS:
  198. G_asprintf(&pj_type, "geographic 3D crs");
  199. break;
  200. case PJ_TYPE_VERTICAL_CRS:
  201. G_asprintf(&pj_type, "vertical crs");
  202. break;
  203. case PJ_TYPE_PROJECTED_CRS:
  204. G_asprintf(&pj_type, "projected crs");
  205. break;
  206. case PJ_TYPE_COMPOUND_CRS:
  207. G_asprintf(&pj_type, "compound crs");
  208. break;
  209. case PJ_TYPE_TEMPORAL_CRS:
  210. G_asprintf(&pj_type, "temporal crs");
  211. break;
  212. case PJ_TYPE_ENGINEERING_CRS:
  213. G_asprintf(&pj_type, "engineering crs");
  214. break;
  215. case PJ_TYPE_BOUND_CRS:
  216. G_asprintf(&pj_type, "bound crs");
  217. break;
  218. case PJ_TYPE_OTHER_CRS:
  219. G_asprintf(&pj_type, "other crs");
  220. break;
  221. case PJ_TYPE_CONVERSION:
  222. G_asprintf(&pj_type, "conversion");
  223. break;
  224. case PJ_TYPE_TRANSFORMATION:
  225. G_asprintf(&pj_type, "transformation");
  226. break;
  227. case PJ_TYPE_CONCATENATED_OPERATION:
  228. G_asprintf(&pj_type, "concatenated operation");
  229. break;
  230. case PJ_TYPE_OTHER_COORDINATE_OPERATION:
  231. G_asprintf(&pj_type, "other coordinate operation");
  232. break;
  233. }
  234. return pj_type;
  235. }
  236. #endif
  237. #endif
  238. /**
  239. * \brief Create a PROJ transformation object to transform coordinates
  240. * from an input SRS to an output SRS
  241. *
  242. * After the transformation has been initialized with this function,
  243. * coordinates can be transformed from input SRS to output SRS with
  244. * GPJ_transform() and direction = PJ_FWD, and back from output SRS to
  245. * input SRS with direction = OJ_INV.
  246. * If coordinates should be transformed between the input SRS and its
  247. * latlong equivalent, an uninitialized info_out with
  248. * info_out->pj = NULL can be passed to the function. In this case,
  249. * coordinates will be transformed between the input SRS and its
  250. * latlong equivalent, and for PROJ 5+, the transformation object is
  251. * created accordingly, while for PROJ 4, the output SRS is created as
  252. * latlong equivalent of the input SRS
  253. *
  254. * PROJ 5+:
  255. * info_in->pj must not be null
  256. * if info_out->pj is null, assume info_out to be the ll equivalent
  257. * of info_in
  258. * create info_trans as conversion from info_in to its ll equivalent
  259. * NOTE: this is the inverse of the logic of PROJ 5 which by default
  260. * converts from ll to a given SRS, not from a given SRS to ll
  261. * thus PROJ 5+ itself uses an inverse transformation in the
  262. * first step of the pipeline for proj_create_crs_to_crs()
  263. * if info_trans->def is not NULL, this pipeline definition will be
  264. * used to create a transformation object
  265. * PROJ 4:
  266. * info_in->pj must not be null
  267. * if info_out->pj is null, create info_out as ll equivalent
  268. * else do nothing, info_trans is not used
  269. *
  270. * \param info_in pointer to pj_info struct for input co-ordinate system
  271. * \param info_out pointer to pj_info struct for output co-ordinate system
  272. * \param info_trans pointer to pj_info struct for a transformation object (PROJ 5+)
  273. *
  274. * \return 1 on success, -1 on failure
  275. **/
  276. int GPJ_init_transform(const struct pj_info *info_in,
  277. const struct pj_info *info_out,
  278. struct pj_info *info_trans)
  279. {
  280. if (info_in->pj == NULL)
  281. G_fatal_error(_("Input coordinate system is NULL"));
  282. if (info_in->def == NULL)
  283. G_fatal_error(_("Input coordinate system definition is NULL"));
  284. #ifdef HAVE_PROJ_H
  285. #if PROJ_VERSION_MAJOR >= 6
  286. /* PROJ6+: enforce axis order easting, northing
  287. * +axis=enu (works with proj-4.8+) */
  288. info_trans->pj = NULL;
  289. info_trans->meters = 1.;
  290. info_trans->zone = 0;
  291. sprintf(info_trans->proj, "pipeline");
  292. /* user-provided pipeline */
  293. if (info_trans->def) {
  294. const char *projstr;
  295. /* create a pj from user-defined transformation pipeline */
  296. info_trans->pj = proj_create(PJ_DEFAULT_CTX, info_trans->def);
  297. if (info_trans->pj == NULL) {
  298. G_warning(_("proj_create() failed for '%s'"), info_trans->def);
  299. return -1;
  300. }
  301. projstr = proj_as_proj_string(NULL, info_trans->pj, PJ_PROJ_5, NULL);
  302. if (projstr == NULL) {
  303. G_warning(_("proj_create() failed for '%s'"), info_trans->def);
  304. return -1;
  305. }
  306. else {
  307. /* make sure axis order is easting, northing
  308. * proj_normalize_for_visualization() does not work here
  309. * because source and target CRS are unknown to PROJ
  310. * remove any "+step +proj=axisswap +order=2,1" ?
  311. * */
  312. info_trans->def = G_store(projstr);
  313. if (strstr(info_trans->def, "axisswap")) {
  314. G_warning(_("The transformation pipeline contains an '%s' step. "
  315. "Remove this step if easting and northing are swapped in the output."),
  316. "axisswap");
  317. }
  318. G_debug(1, "proj_create() pipeline: %s", info_trans->def);
  319. /* the user-provided PROJ pipeline is supposed to do
  320. * all the needed unit conversions */
  321. /* ugly hack */
  322. ((struct pj_info *)info_in)->meters = 1;
  323. ((struct pj_info *)info_out)->meters = 1;
  324. }
  325. }
  326. /* if no output CRS is defined,
  327. * assume info_out to be ll equivalent of info_in */
  328. else if (info_out->pj == NULL) {
  329. const char *projstr = NULL;
  330. char *indef = NULL;
  331. /* Even Rouault:
  332. * if info_in->def contains a +towgs84/+nadgrids clause,
  333. * this pipeline would apply it, whereas you probably only want
  334. * the reverse projection, and no datum shift.
  335. * The easiest would probably to mess up with the PROJ string.
  336. * Otherwise with the PROJ API, you could
  337. * instanciate a PJ object from the string,
  338. * check if it is a BoundCRS with proj_get_source_crs(),
  339. * and in that case, take the source CRS with proj_get_source_crs(),
  340. * and do the inverse transform on it */
  341. if (proj_get_type(info_in->pj) == PJ_TYPE_BOUND_CRS) {
  342. PJ *source_crs;
  343. G_debug(1, "transform to ll equivalent: found bound crs");
  344. source_crs = proj_get_source_crs(NULL, info_in->pj);
  345. if (source_crs) {
  346. projstr = proj_as_proj_string(NULL, source_crs, PJ_PROJ_5, NULL);
  347. if (projstr)
  348. indef = G_store(projstr);
  349. proj_destroy(source_crs);
  350. }
  351. }
  352. if (indef == NULL)
  353. indef = G_store(info_in->def);
  354. G_debug(1, "ll equivalent definition: %s", indef);
  355. /* what about axis order?
  356. * is it always enu?
  357. * probably yes, as long as there is no +proj=axisswap step */
  358. G_asprintf(&(info_trans->def), "+proj=pipeline +step +inv %s",
  359. indef);
  360. info_trans->pj = proj_create(PJ_DEFAULT_CTX, info_trans->def);
  361. if (info_trans->pj == NULL) {
  362. G_warning(_("proj_create() failed for '%s'"), info_trans->def);
  363. G_free(indef);
  364. return -1;
  365. }
  366. projstr = proj_as_proj_string(NULL, info_trans->pj, PJ_PROJ_5, NULL);
  367. if (projstr == NULL) {
  368. G_warning(_("proj_create() failed for '%s'"), info_trans->def);
  369. G_free(indef);
  370. return -1;
  371. }
  372. G_free(indef);
  373. }
  374. /* input and output CRS are available */
  375. else if (info_in->def && info_out->pj && info_out->def) {
  376. char *indef = NULL, *outdef = NULL;
  377. char *indefcrs = NULL, *outdefcrs = NULL;
  378. char *insrid = NULL, *outsrid = NULL;
  379. int use_insrid = 0, use_outsrid = 0;
  380. PJ *source_crs, *target_crs;
  381. PJ_AREA *pj_area = NULL;
  382. double xmin, xmax, ymin, ymax;
  383. int op_count = 0;
  384. /* remove any +towgs84/+nadgrids clause, see above
  385. * does not always remove +towgs84=0.000,0.000,0.000 ??? */
  386. G_debug(1, "source proj string: %s", info_in->def);
  387. G_debug(1, "source type: %s", get_pj_type_string(info_in->pj));
  388. indefcrs = info_in->def;
  389. if (proj_get_type(info_in->pj) == PJ_TYPE_BOUND_CRS) {
  390. source_crs = proj_get_source_crs(NULL, info_in->pj);
  391. if (source_crs) {
  392. const char *projstr;
  393. projstr = proj_as_proj_string(NULL, source_crs, PJ_PROJ_5, NULL);
  394. if (projstr) {
  395. indefcrs = G_store(projstr);
  396. G_message("Input CRS definition converted from '%s' to '%s'",
  397. info_in->def, indefcrs);
  398. }
  399. proj_destroy(source_crs);
  400. source_crs = NULL;
  401. }
  402. }
  403. G_debug(1, "target proj string: %s", info_out->def);
  404. G_debug(1, "target type: %s", get_pj_type_string(info_out->pj));
  405. outdefcrs = info_out->def;
  406. if (proj_get_type(info_out->pj) == PJ_TYPE_BOUND_CRS) {
  407. target_crs = proj_get_source_crs(NULL, info_out->pj);
  408. if (target_crs) {
  409. const char *projstr;
  410. projstr = proj_as_proj_string(NULL, target_crs, PJ_PROJ_5, NULL);
  411. if (projstr) {
  412. outdefcrs = G_store(projstr);
  413. G_message("Output CRS definition converted from '%s' to '%s'",
  414. info_out->def, outdefcrs);
  415. }
  416. proj_destroy(target_crs);
  417. target_crs = NULL;
  418. }
  419. }
  420. /* PROJ6+: EPSG must be uppercase EPSG */
  421. if (info_in->srid) {
  422. if (strncmp(info_in->srid, "epsg", 4) == 0)
  423. insrid = G_store_upper(info_in->srid);
  424. else
  425. insrid = G_store(info_in->srid);
  426. }
  427. if (info_out->srid) {
  428. if (strncmp(info_out->srid, "epsg", 4) == 0)
  429. outsrid = G_store_upper(info_out->srid);
  430. else
  431. outsrid = G_store(info_out->srid);
  432. }
  433. if (insrid) {
  434. G_asprintf(&indef, "%s", insrid);
  435. use_insrid = 1;
  436. }
  437. else {
  438. G_asprintf(&indef, "%s", indefcrs);
  439. }
  440. G_debug(1, "Input CRS definition: %s", indef);
  441. if (outsrid) {
  442. G_asprintf(&outdef, "%s", outsrid);
  443. use_outsrid = 1;
  444. }
  445. else {
  446. G_asprintf(&outdef, "%s", outdefcrs);
  447. }
  448. G_debug(1, "Output CRS definition: %s", outdef);
  449. /* check number of operations */
  450. source_crs = proj_create(NULL, indef);
  451. target_crs = proj_create(NULL, outdef);
  452. /* get pj_area */
  453. if (get_pj_area(&xmin, &xmax, &ymin, &ymax)) {
  454. pj_area = proj_area_create();
  455. proj_area_set_bbox(pj_area, xmin, ymin, xmax, ymax);
  456. }
  457. if (source_crs && target_crs) {
  458. PJ_OPERATION_FACTORY_CONTEXT *operation_ctx;
  459. PJ_OBJ_LIST *op_list;
  460. operation_ctx = proj_create_operation_factory_context(NULL, NULL);
  461. /* proj_create_operations() works only if both source_crs
  462. * and target_crs are found in the proj db
  463. * if any is not found, proj can not get a list of operations
  464. * and we have to take care of datumshift manually */
  465. /* constrain by area ? */
  466. op_list = proj_create_operations(NULL,
  467. source_crs,
  468. target_crs,
  469. operation_ctx);
  470. op_count = 0;
  471. if (op_list)
  472. op_count = proj_list_get_count(op_list);
  473. if (op_count > 1) {
  474. int i;
  475. G_warning(_("Found %d possible transformations"), op_count);
  476. for (i = 0; i < op_count; i++) {
  477. const char *str;
  478. const char *area_of_use, *projstr;
  479. double e, w, s, n;
  480. PJ_PROJ_INFO pj_info;
  481. PJ *op, *op_norm;
  482. op = proj_list_get(NULL, op_list, i);
  483. op_norm = proj_normalize_for_visualization(PJ_DEFAULT_CTX, op);
  484. if (!op_norm) {
  485. G_warning(_("proj_normalize_for_visualization() failed for operation %d"),
  486. i + 1);
  487. }
  488. else {
  489. proj_destroy(op);
  490. op = op_norm;
  491. }
  492. projstr = proj_as_proj_string(NULL, op,
  493. PJ_PROJ_5, NULL);
  494. pj_info = proj_pj_info(op);
  495. proj_get_area_of_use(NULL, op, &w, &s, &e, &n, &area_of_use);
  496. if (projstr) {
  497. G_important_message("************************");
  498. G_important_message(_("Operation %d:"), i + 1);
  499. G_important_message(_("Description: %s"),
  500. pj_info.description);
  501. G_important_message(" ");
  502. G_important_message(_("Area of use: %s"),
  503. area_of_use);
  504. if (pj_info.accuracy > 0) {
  505. G_important_message(" ");
  506. G_important_message(_("Accuracy within area of use: %g m"),
  507. pj_info.accuracy);
  508. }
  509. #if PROJ_VERSION_NUM >= 6020000
  510. str = proj_get_remarks(op);
  511. if (str && *str) {
  512. G_important_message(" ");
  513. G_important_message(_("Remarks: %s"), str);
  514. }
  515. str = proj_get_scope(op);
  516. if (str && *str) {
  517. G_important_message(" ");
  518. G_important_message(_("Scope: %s"), str);
  519. }
  520. #endif
  521. G_important_message(" ");
  522. G_important_message(_("PROJ string:"));
  523. G_important_message("%s", projstr);
  524. }
  525. proj_destroy(op);
  526. }
  527. G_important_message("************************");
  528. G_important_message(_("See also output of:"));
  529. G_important_message("projinfo -o PROJ -s \"%s\" -t \"%s\"",
  530. indef, outdef);
  531. G_important_message(_("Please provide the appropriate PROJ string with the %s option"),
  532. "pipeline");
  533. G_important_message("************************");
  534. }
  535. if (op_list)
  536. proj_list_destroy(op_list);
  537. proj_operation_factory_context_destroy(operation_ctx);
  538. }
  539. if (source_crs)
  540. proj_destroy(source_crs);
  541. if (target_crs)
  542. proj_destroy(target_crs);
  543. /* try proj_create_crs_to_crs() */
  544. G_debug(1, "trying %s to %s", indef, outdef);
  545. info_trans->pj = proj_create_crs_to_crs(PJ_DEFAULT_CTX,
  546. indef,
  547. outdef,
  548. pj_area);
  549. if (info_trans->pj) {
  550. const char *projstr = NULL;
  551. projstr = proj_as_proj_string(NULL, info_trans->pj,
  552. PJ_PROJ_5, NULL);
  553. if (projstr == NULL) {
  554. G_debug(1, "proj_create_crs_to_crs() failed with PROJ%d for input \"%s\", output \"%s\"",
  555. PROJ_VERSION_MAJOR, indef, outdef);
  556. G_asprintf(&indef, "%s", indefcrs);
  557. G_asprintf(&outdef, "%s", outdefcrs);
  558. G_debug(1, "trying %s to %s", indef, outdef);
  559. /* try proj_create_crs_to_crs() */
  560. proj_destroy(info_trans->pj);
  561. info_trans->pj = NULL;
  562. info_trans->pj = proj_create_crs_to_crs(PJ_DEFAULT_CTX,
  563. indef,
  564. outdef,
  565. NULL);
  566. }
  567. else {
  568. /* PROJ will do the unit conversion if set up from srid
  569. * -> disable unit conversion for GPJ_transform */
  570. /* ugly hack */
  571. if (use_insrid) {
  572. ((struct pj_info *)info_in)->meters = 1;
  573. }
  574. if (use_outsrid) {
  575. ((struct pj_info *)info_out)->meters = 1;
  576. }
  577. }
  578. }
  579. if (info_trans->pj) {
  580. const char *projstr;
  581. PJ *pj_norm = NULL;
  582. G_debug(1, "proj_create_crs_to_crs() succeeded with PROJ%d",
  583. PROJ_VERSION_MAJOR);
  584. projstr = proj_as_proj_string(NULL, info_trans->pj,
  585. PJ_PROJ_5, NULL);
  586. info_trans->def = G_store(projstr);
  587. if (projstr) {
  588. /* make sure axis order is easting, northing
  589. * proj_normalize_for_visualization() requires
  590. * source and target CRS
  591. * -> does not work with ll equivalent of input:
  592. * no target CRS in +proj=pipeline +step +inv %s */
  593. pj_norm = proj_normalize_for_visualization(PJ_DEFAULT_CTX, info_trans->pj);
  594. if (!pj_norm) {
  595. G_warning(_("proj_normalize_for_visualization() failed for '%s'"),
  596. info_trans->def);
  597. }
  598. else {
  599. projstr = proj_as_proj_string(NULL, pj_norm,
  600. PJ_PROJ_5, NULL);
  601. if (projstr && *projstr) {
  602. proj_destroy(info_trans->pj);
  603. info_trans->pj = pj_norm;
  604. info_trans->def = G_store(projstr);
  605. }
  606. else {
  607. proj_destroy(pj_norm);
  608. G_warning(_("No PROJ definition for normalized version of '%s'"),
  609. info_trans->def);
  610. }
  611. }
  612. if (op_count > 1) {
  613. G_important_message(_("Selected pipeline:"));
  614. G_important_message(_("%s"), info_trans->def);
  615. G_important_message("************************");
  616. }
  617. G_debug(1, "proj_create_crs_to_crs() pipeline: %s", info_trans->def);
  618. }
  619. else {
  620. proj_destroy(info_trans->pj);
  621. info_trans->pj = NULL;
  622. }
  623. }
  624. /* last try with proj_create() */
  625. if (info_trans->pj == NULL) {
  626. G_debug(1, "proj_create_crs_to_crs() failed with PROJ%d for input \"%s\", output \"%s\"",
  627. PROJ_VERSION_MAJOR, indef, outdef);
  628. G_warning("GPJ_init_transform(): falling back to proj_create()");
  629. if (insrid) {
  630. G_free(indef);
  631. }
  632. G_asprintf(&indef, "%s", info_in->def);
  633. if (outsrid) {
  634. G_free(outdef);
  635. }
  636. G_asprintf(&outdef, "%s", info_out->def);
  637. /* try proj_create() with +proj=pipeline +step +inv %s +step %s" */
  638. G_asprintf(&(info_trans->def), "+proj=pipeline +step +inv %s +step %s",
  639. indef, outdef);
  640. G_important_message(_("Using simplified pipeline '%s'"),
  641. info_trans->def);
  642. info_trans->pj = proj_create(PJ_DEFAULT_CTX, info_trans->def);
  643. G_debug(1, "proj_create() pipeline: %s", info_trans->def);
  644. }
  645. if (pj_area)
  646. proj_area_destroy(pj_area);
  647. if (insrid)
  648. G_free(insrid);
  649. if (outsrid)
  650. G_free(outsrid);
  651. G_free(indef);
  652. G_free(outdef);
  653. }
  654. if (info_trans->pj == NULL) {
  655. G_warning(_("proj_create() failed for '%s'"), info_trans->def);
  656. return -1;
  657. }
  658. #else /* PROJ 5 */
  659. info_trans->pj = NULL;
  660. info_trans->meters = 1.;
  661. info_trans->zone = 0;
  662. sprintf(info_trans->proj, "pipeline");
  663. /* user-provided pipeline */
  664. if (info_trans->def) {
  665. /* create a pj from user-defined transformation pipeline */
  666. info_trans->pj = proj_create(PJ_DEFAULT_CTX, info_trans->def);
  667. if (info_trans->pj == NULL) {
  668. G_warning(_("proj_create() failed for '%s'"), info_trans->def);
  669. return -1;
  670. }
  671. }
  672. /* if no output CRS is defined,
  673. * assume info_out to be ll equivalent of info_in */
  674. else if (info_out->pj == NULL) {
  675. G_asprintf(&(info_trans->def), "+proj=pipeline +step +inv %s",
  676. info_in->def);
  677. info_trans->pj = proj_create(PJ_DEFAULT_CTX, info_trans->def);
  678. if (info_trans->pj == NULL) {
  679. G_warning(_("proj_create() failed for '%s'"), info_trans->def);
  680. return -1;
  681. }
  682. }
  683. else if (info_in->def && info_out->pj && info_out->def) {
  684. char *indef = NULL, *outdef = NULL;
  685. char *insrid = NULL, *outsrid = NULL;
  686. /* PROJ5: EPSG must be lowercase epsg */
  687. if (info_in->srid) {
  688. if (strncmp(info_in->srid, "EPSG", 4) == 0)
  689. insrid = G_store_lower(info_in->srid);
  690. else
  691. insrid = G_store(info_in->srid);
  692. }
  693. if (info_out->pj && info_out->srid) {
  694. if (strncmp(info_out->srid, "EPSG", 4) == 0)
  695. outsrid = G_store_lower(info_out->srid);
  696. else
  697. outsrid = G_store(info_out->srid);
  698. }
  699. info_trans->pj = NULL;
  700. if (insrid && outsrid) {
  701. G_asprintf(&indef, "%s", insrid);
  702. G_asprintf(&outdef, "%s", outsrid);
  703. G_asprintf(&(info_trans->def), "+proj=pipeline +step +inv +init=%s +step +init=%s",
  704. indef, outdef);
  705. /* try proj_create_crs_to_crs() */
  706. info_trans->pj = proj_create_crs_to_crs(PJ_DEFAULT_CTX,
  707. indef,
  708. outdef,
  709. NULL);
  710. }
  711. if (info_trans->pj) {
  712. G_debug(1, "proj_create_crs_to_crs() succeeded with PROJ5");
  713. }
  714. else {
  715. if (indef) {
  716. G_free(indef);
  717. indef = NULL;
  718. }
  719. if (insrid) {
  720. G_asprintf(&indef, "+init=%s", insrid);
  721. }
  722. else {
  723. G_asprintf(&indef, "%s", info_in->def);
  724. }
  725. if (outdef) {
  726. G_free(outdef);
  727. outdef = NULL;
  728. }
  729. if (outsrid) {
  730. G_asprintf(&outdef, "+init=%s", outsrid);
  731. }
  732. else {
  733. G_asprintf(&outdef, "%s", info_out->def);
  734. }
  735. /* try proj_create() with +proj=pipeline +step +inv %s +step %s" */
  736. G_asprintf(&(info_trans->def), "+proj=pipeline +step +inv %s +step %s",
  737. indef, outdef);
  738. info_trans->pj = proj_create(PJ_DEFAULT_CTX, info_trans->def);
  739. }
  740. if (insrid)
  741. G_free(insrid);
  742. if (outsrid)
  743. G_free(outsrid);
  744. G_free(indef);
  745. G_free(outdef);
  746. }
  747. if (info_trans->pj == NULL) {
  748. G_warning(_("proj_create() failed for '%s'"), info_trans->def);
  749. return -1;
  750. }
  751. #endif
  752. #else /* PROJ 4 */
  753. if (info_out->pj == NULL) {
  754. if (GPJ_get_equivalent_latlong(info_out, info_in) < 0) {
  755. G_warning(_("Unable to create latlong equivalent for '%s'"),
  756. info_in->def);
  757. return -1;
  758. }
  759. }
  760. #endif
  761. return 1;
  762. }
  763. /* TODO: rename pj_ to GPJ_ to avoid symbol clash with PROJ lib */
  764. /**
  765. * \brief Re-project a point between two co-ordinate systems using a
  766. * transformation object prepared with GPJ_prepare_pj()
  767. *
  768. * This function takes pointers to three pj_info structures as arguments,
  769. * and projects a point between the input and output co-ordinate system.
  770. * The pj_info structure info_trans must have been initialized with
  771. * GPJ_init_transform().
  772. * The direction determines if a point is projected from input CRS to
  773. * output CRS (PJ_FWD) or from output CRS to input CRS (PJ_INV).
  774. * The easting, northing, and height of the point are contained in the
  775. * pointers passed to the function; these will be overwritten by the
  776. * coordinates of the transformed point.
  777. *
  778. * \param info_in pointer to pj_info struct for input co-ordinate system
  779. * \param info_out pointer to pj_info struct for output co-ordinate system
  780. * \param info_trans pointer to pj_info struct for a transformation object (PROJ 5+)
  781. * \param dir direction of the transformation (PJ_FWD or PJ_INV)
  782. * \param x Pointer to a double containing easting or longitude
  783. * \param y Pointer to a double containing northing or latitude
  784. * \param z Pointer to a double containing height, or NULL
  785. *
  786. * \return Return value from PROJ proj_trans() function
  787. **/
  788. int GPJ_transform(const struct pj_info *info_in,
  789. const struct pj_info *info_out,
  790. const struct pj_info *info_trans, int dir,
  791. double *x, double *y, double *z)
  792. {
  793. int ok = 0;
  794. #ifdef HAVE_PROJ_H
  795. /* PROJ 5+ variant */
  796. int in_is_ll, out_is_ll, in_deg2rad, out_rad2deg;
  797. PJ_COORD c;
  798. if (info_in->pj == NULL)
  799. G_fatal_error(_("No input projection"));
  800. if (info_trans->pj == NULL)
  801. G_fatal_error(_("No transformation object"));
  802. in_deg2rad = out_rad2deg = 1;
  803. if (dir == PJ_FWD) {
  804. /* info_in -> info_out */
  805. METERS_in = info_in->meters;
  806. in_is_ll = !strncmp(info_in->proj, "ll", 2);
  807. #if PROJ_VERSION_MAJOR >= 6
  808. /* PROJ 6+: conversion to radians is not always needed:
  809. * if proj_angular_input(info_trans->pj, dir) == 1
  810. * -> convert from degrees to radians */
  811. if (in_is_ll && proj_angular_input(info_trans->pj, dir) == 0) {
  812. in_deg2rad = 0;
  813. }
  814. #endif
  815. if (info_out->pj) {
  816. METERS_out = info_out->meters;
  817. out_is_ll = !strncmp(info_out->proj, "ll", 2);
  818. #if PROJ_VERSION_MAJOR >= 6
  819. /* PROJ 6+: conversion to radians is not always needed:
  820. * if proj_angular_input(info_trans->pj, dir) == 1
  821. * -> convert from degrees to radians */
  822. if (out_is_ll && proj_angular_output(info_trans->pj, dir) == 0) {
  823. out_rad2deg = 0;
  824. }
  825. #endif
  826. }
  827. else {
  828. METERS_out = 1.0;
  829. out_is_ll = 1;
  830. }
  831. }
  832. else {
  833. /* info_out -> info_in */
  834. METERS_out = info_in->meters;
  835. out_is_ll = !strncmp(info_in->proj, "ll", 2);
  836. #if PROJ_VERSION_MAJOR >= 6
  837. /* PROJ 6+: conversion to radians is not always needed:
  838. * if proj_angular_input(info_trans->pj, dir) == 1
  839. * -> convert from degrees to radians */
  840. if (out_is_ll && proj_angular_output(info_trans->pj, dir) == 0) {
  841. out_rad2deg = 0;
  842. }
  843. #endif
  844. if (info_out->pj) {
  845. METERS_in = info_out->meters;
  846. in_is_ll = !strncmp(info_out->proj, "ll", 2);
  847. #if PROJ_VERSION_MAJOR >= 6
  848. /* PROJ 6+: conversion to radians is not always needed:
  849. * if proj_angular_input(info_trans->pj, dir) == 1
  850. * -> convert from degrees to radians */
  851. if (in_is_ll && proj_angular_input(info_trans->pj, dir) == 0) {
  852. in_deg2rad = 0;
  853. }
  854. #endif
  855. }
  856. else {
  857. METERS_in = 1.0;
  858. in_is_ll = 1;
  859. }
  860. }
  861. /* prepare */
  862. if (in_is_ll) {
  863. if (in_deg2rad) {
  864. /* convert degrees to radians */
  865. c.lpzt.lam = (*x) / RAD_TO_DEG;
  866. c.lpzt.phi = (*y) / RAD_TO_DEG;
  867. }
  868. else {
  869. c.lpzt.lam = (*x);
  870. c.lpzt.phi = (*y);
  871. }
  872. c.lpzt.z = 0;
  873. if (z)
  874. c.lpzt.z = *z;
  875. c.lpzt.t = 0;
  876. }
  877. else {
  878. /* convert to meters */
  879. c.xyzt.x = *x * METERS_in;
  880. c.xyzt.y = *y * METERS_in;
  881. c.xyzt.z = 0;
  882. if (z)
  883. c.xyzt.z = *z;
  884. c.xyzt.t = 0;
  885. }
  886. /* transform */
  887. c = proj_trans(info_trans->pj, dir, c);
  888. ok = proj_errno(info_trans->pj);
  889. if (ok < 0) {
  890. G_warning(_("proj_trans() failed: %s"), proj_errno_string(ok));
  891. return ok;
  892. }
  893. /* output */
  894. if (out_is_ll) {
  895. /* convert to degrees */
  896. if (out_rad2deg) {
  897. /* convert radians to degrees */
  898. *x = c.lp.lam * RAD_TO_DEG;
  899. *y = c.lp.phi * RAD_TO_DEG;
  900. }
  901. else {
  902. *x = c.lp.lam;
  903. *y = c.lp.phi;
  904. }
  905. if (z)
  906. *z = c.lpzt.z;
  907. }
  908. else {
  909. /* convert to map units */
  910. *x = c.xyzt.x / METERS_out;
  911. *y = c.xyzt.y / METERS_out;
  912. if (z)
  913. *z = c.xyzt.z;
  914. }
  915. #else
  916. /* PROJ 4 variant */
  917. double u, v;
  918. double h = 0.0;
  919. const struct pj_info *p_in, *p_out;
  920. if (info_out == NULL)
  921. G_fatal_error(_("No output projection"));
  922. if (dir == PJ_FWD) {
  923. p_in = info_in;
  924. p_out = info_out;
  925. }
  926. else {
  927. p_in = info_out;
  928. p_out = info_in;
  929. }
  930. METERS_in = p_in->meters;
  931. METERS_out = p_out->meters;
  932. if (z)
  933. h = *z;
  934. if (strncmp(p_in->proj, "ll", 2) == 0) {
  935. u = (*x) / RAD_TO_DEG;
  936. v = (*y) / RAD_TO_DEG;
  937. }
  938. else {
  939. u = *x * METERS_in;
  940. v = *y * METERS_in;
  941. }
  942. ok = pj_transform(p_in->pj, p_out->pj, 1, 0, &u, &v, &h);
  943. if (ok < 0) {
  944. G_warning(_("pj_transform() failed: %s"), pj_strerrno(ok));
  945. return ok;
  946. }
  947. if (strncmp(p_out->proj, "ll", 2) == 0) {
  948. *x = u * RAD_TO_DEG;
  949. *y = v * RAD_TO_DEG;
  950. }
  951. else {
  952. *x = u / METERS_out;
  953. *y = v / METERS_out;
  954. }
  955. if (z)
  956. *z = h;
  957. #endif
  958. return ok;
  959. }
  960. /**
  961. * \brief Re-project an array of points between two co-ordinate systems
  962. * using a transformation object prepared with GPJ_prepare_pj()
  963. *
  964. * This function takes pointers to three pj_info structures as arguments,
  965. * and projects an array of pointd between the input and output
  966. * co-ordinate system. The pj_info structure info_trans must have been
  967. * initialized with GPJ_init_transform().
  968. * The direction determines if a point is projected from input CRS to
  969. * output CRS (PJ_FWD) or from output CRS to input CRS (PJ_INV).
  970. * The easting, northing, and height of the point are contained in the
  971. * pointers passed to the function; these will be overwritten by the
  972. * coordinates of the transformed point.
  973. *
  974. * \param info_in pointer to pj_info struct for input co-ordinate system
  975. * \param info_out pointer to pj_info struct for output co-ordinate system
  976. * \param info_trans pointer to pj_info struct for a transformation object (PROJ 5+)
  977. * \param dir direction of the transformation (PJ_FWD or PJ_INV)
  978. * \param x pointer to an array of type double containing easting or longitude
  979. * \param y pointer to an array of type double containing northing or latitude
  980. * \param z pointer to an array of type double containing height, or NULL
  981. * \param n number of points in the arrays to be transformed
  982. *
  983. * \return Return value from PROJ proj_trans() function
  984. **/
  985. int GPJ_transform_array(const struct pj_info *info_in,
  986. const struct pj_info *info_out,
  987. const struct pj_info *info_trans, int dir,
  988. double *x, double *y, double *z, int n)
  989. {
  990. int ok;
  991. int i;
  992. int has_z = 1;
  993. #ifdef HAVE_PROJ_H
  994. /* PROJ 5+ variant */
  995. int in_is_ll, out_is_ll, in_deg2rad, out_rad2deg;
  996. PJ_COORD c;
  997. if (info_trans->pj == NULL)
  998. G_fatal_error(_("No transformation object"));
  999. in_deg2rad = out_rad2deg = 1;
  1000. if (dir == PJ_FWD) {
  1001. /* info_in -> info_out */
  1002. METERS_in = info_in->meters;
  1003. in_is_ll = !strncmp(info_in->proj, "ll", 2);
  1004. #if PROJ_VERSION_MAJOR >= 6
  1005. /* PROJ 6+: conversion to radians is not always needed:
  1006. * if proj_angular_input(info_trans->pj, dir) == 1
  1007. * -> convert from degrees to radians */
  1008. if (in_is_ll && proj_angular_input(info_trans->pj, dir) == 0) {
  1009. in_deg2rad = 0;
  1010. }
  1011. #endif
  1012. if (info_out->pj) {
  1013. METERS_out = info_out->meters;
  1014. out_is_ll = !strncmp(info_out->proj, "ll", 2);
  1015. #if PROJ_VERSION_MAJOR >= 6
  1016. /* PROJ 6+: conversion to radians is not always needed:
  1017. * if proj_angular_input(info_trans->pj, dir) == 1
  1018. * -> convert from degrees to radians */
  1019. if (out_is_ll && proj_angular_output(info_trans->pj, dir) == 0) {
  1020. out_rad2deg = 0;
  1021. }
  1022. #endif
  1023. }
  1024. else {
  1025. METERS_out = 1.0;
  1026. out_is_ll = 1;
  1027. }
  1028. }
  1029. else {
  1030. /* info_out -> info_in */
  1031. METERS_out = info_in->meters;
  1032. out_is_ll = !strncmp(info_in->proj, "ll", 2);
  1033. #if PROJ_VERSION_MAJOR >= 6
  1034. /* PROJ 6+: conversion to radians is not always needed:
  1035. * if proj_angular_input(info_trans->pj, dir) == 1
  1036. * -> convert from degrees to radians */
  1037. if (out_is_ll && proj_angular_output(info_trans->pj, dir) == 0) {
  1038. out_rad2deg = 0;
  1039. }
  1040. #endif
  1041. if (info_out->pj) {
  1042. METERS_in = info_out->meters;
  1043. in_is_ll = !strncmp(info_out->proj, "ll", 2);
  1044. #if PROJ_VERSION_MAJOR >= 6
  1045. /* PROJ 6+: conversion to degrees is not always needed:
  1046. * if proj_angular_output(info_trans->pj, dir) == 1
  1047. * -> convert from degrees to radians */
  1048. if (in_is_ll && proj_angular_input(info_trans->pj, dir) == 0) {
  1049. in_deg2rad = 0;
  1050. }
  1051. #endif
  1052. }
  1053. else {
  1054. METERS_in = 1.0;
  1055. in_is_ll = 1;
  1056. }
  1057. }
  1058. if (z == NULL) {
  1059. z = G_malloc(sizeof(double) * n);
  1060. /* they say memset is only guaranteed for chars ;-( */
  1061. for (i = 0; i < n; i++)
  1062. z[i] = 0.0;
  1063. has_z = 0;
  1064. }
  1065. ok = 0;
  1066. if (in_is_ll) {
  1067. c.lpzt.t = 0;
  1068. if (out_is_ll) {
  1069. /* what is more costly ?
  1070. * calling proj_trans for each point
  1071. * or having three loops over all points ?
  1072. * proj_trans_array() itself calls proj_trans() in a loop
  1073. * -> one loop over all points is better than
  1074. * three loops over all points
  1075. */
  1076. for (i = 0; i < n; i++) {
  1077. if (in_deg2rad) {
  1078. /* convert degrees to radians */
  1079. c.lpzt.lam = (*x) / RAD_TO_DEG;
  1080. c.lpzt.phi = (*y) / RAD_TO_DEG;
  1081. }
  1082. else {
  1083. c.lpzt.lam = (*x);
  1084. c.lpzt.phi = (*y);
  1085. }
  1086. c.lpzt.z = z[i];
  1087. c = proj_trans(info_trans->pj, dir, c);
  1088. if ((ok = proj_errno(info_trans->pj)) < 0)
  1089. break;
  1090. if (out_rad2deg) {
  1091. /* convert radians to degrees */
  1092. x[i] = c.lp.lam * RAD_TO_DEG;
  1093. y[i] = c.lp.phi * RAD_TO_DEG;
  1094. }
  1095. else {
  1096. x[i] = c.lp.lam;
  1097. y[i] = c.lp.phi;
  1098. }
  1099. }
  1100. }
  1101. else {
  1102. for (i = 0; i < n; i++) {
  1103. if (in_deg2rad) {
  1104. /* convert degrees to radians */
  1105. c.lpzt.lam = (*x) / RAD_TO_DEG;
  1106. c.lpzt.phi = (*y) / RAD_TO_DEG;
  1107. }
  1108. else {
  1109. c.lpzt.lam = (*x);
  1110. c.lpzt.phi = (*y);
  1111. }
  1112. c.lpzt.z = z[i];
  1113. c = proj_trans(info_trans->pj, dir, c);
  1114. if ((ok = proj_errno(info_trans->pj)) < 0)
  1115. break;
  1116. /* convert to map units */
  1117. x[i] = c.xy.x / METERS_out;
  1118. y[i] = c.xy.y / METERS_out;
  1119. }
  1120. }
  1121. }
  1122. else {
  1123. c.xyzt.t = 0;
  1124. if (out_is_ll) {
  1125. for (i = 0; i < n; i++) {
  1126. /* convert to meters */
  1127. c.xyzt.x = x[i] * METERS_in;
  1128. c.xyzt.y = y[i] * METERS_in;
  1129. c.xyzt.z = z[i];
  1130. c = proj_trans(info_trans->pj, dir, c);
  1131. if ((ok = proj_errno(info_trans->pj)) < 0)
  1132. break;
  1133. if (out_rad2deg) {
  1134. /* convert radians to degrees */
  1135. x[i] = c.lp.lam * RAD_TO_DEG;
  1136. y[i] = c.lp.phi * RAD_TO_DEG;
  1137. }
  1138. else {
  1139. x[i] = c.lp.lam;
  1140. y[i] = c.lp.phi;
  1141. }
  1142. }
  1143. }
  1144. else {
  1145. for (i = 0; i < n; i++) {
  1146. /* convert to meters */
  1147. c.xyzt.x = x[i] * METERS_in;
  1148. c.xyzt.y = y[i] * METERS_in;
  1149. c.xyzt.z = z[i];
  1150. c = proj_trans(info_trans->pj, dir, c);
  1151. if ((ok = proj_errno(info_trans->pj)) < 0)
  1152. break;
  1153. /* convert to map units */
  1154. x[i] = c.xy.x / METERS_out;
  1155. y[i] = c.xy.y / METERS_out;
  1156. }
  1157. }
  1158. }
  1159. if (!has_z)
  1160. G_free(z);
  1161. if (ok < 0) {
  1162. G_warning(_("proj_trans() failed: %s"), proj_errno_string(ok));
  1163. }
  1164. #else
  1165. /* PROJ 4 variant */
  1166. const struct pj_info *p_in, *p_out;
  1167. if (dir == PJ_FWD) {
  1168. p_in = info_in;
  1169. p_out = info_out;
  1170. }
  1171. else {
  1172. p_in = info_out;
  1173. p_out = info_in;
  1174. }
  1175. METERS_in = p_in->meters;
  1176. METERS_out = p_out->meters;
  1177. if (z == NULL) {
  1178. z = G_malloc(sizeof(double) * n);
  1179. /* they say memset is only guaranteed for chars ;-( */
  1180. for (i = 0; i < n; ++i)
  1181. z[i] = 0.0;
  1182. has_z = 0;
  1183. }
  1184. if (strncmp(p_in->proj, "ll", 2) == 0) {
  1185. if (strncmp(p_out->proj, "ll", 2) == 0) {
  1186. DIVIDE_LOOP(x, y, n, RAD_TO_DEG);
  1187. ok = pj_transform(info_in->pj, info_out->pj, n, 1, x, y, z);
  1188. MULTIPLY_LOOP(x, y, n, RAD_TO_DEG);
  1189. }
  1190. else {
  1191. DIVIDE_LOOP(x, y, n, RAD_TO_DEG);
  1192. ok = pj_transform(info_in->pj, info_out->pj, n, 1, x, y, z);
  1193. DIVIDE_LOOP(x, y, n, METERS_out);
  1194. }
  1195. }
  1196. else {
  1197. if (strncmp(p_out->proj, "ll", 2) == 0) {
  1198. MULTIPLY_LOOP(x, y, n, METERS_in);
  1199. ok = pj_transform(info_in->pj, info_out->pj, n, 1, x, y, z);
  1200. MULTIPLY_LOOP(x, y, n, RAD_TO_DEG);
  1201. }
  1202. else {
  1203. MULTIPLY_LOOP(x, y, n, METERS_in);
  1204. ok = pj_transform(info_in->pj, info_out->pj, n, 1, x, y, z);
  1205. DIVIDE_LOOP(x, y, n, METERS_out);
  1206. }
  1207. }
  1208. if (!has_z)
  1209. G_free(z);
  1210. if (ok < 0)
  1211. G_warning(_("pj_transform() failed: %s"), pj_strerrno(ok));
  1212. #endif
  1213. return ok;
  1214. }
  1215. /*
  1216. * old API, to be deleted
  1217. */
  1218. /**
  1219. * \brief Re-project a point between two co-ordinate systems
  1220. *
  1221. * This function takes pointers to two pj_info structures as arguments,
  1222. * and projects a point between the co-ordinate systems represented by them.
  1223. * The easting and northing of the point are contained in two pointers passed
  1224. * to the function; these will be overwritten by the co-ordinates of the
  1225. * re-projected point.
  1226. *
  1227. * \param x Pointer to a double containing easting or longitude
  1228. * \param y Pointer to a double containing northing or latitude
  1229. * \param info_in pointer to pj_info struct for input co-ordinate system
  1230. * \param info_out pointer to pj_info struct for output co-ordinate system
  1231. *
  1232. * \return Return value from PROJ proj_trans() function
  1233. **/
  1234. int pj_do_proj(double *x, double *y,
  1235. const struct pj_info *info_in, const struct pj_info *info_out)
  1236. {
  1237. int ok;
  1238. #ifdef HAVE_PROJ_H
  1239. struct pj_info info_trans;
  1240. PJ_COORD c;
  1241. if (GPJ_init_transform(info_in, info_out, &info_trans) < 0) {
  1242. return -1;
  1243. }
  1244. METERS_in = info_in->meters;
  1245. METERS_out = info_out->meters;
  1246. if (strncmp(info_in->proj, "ll", 2) == 0) {
  1247. /* convert to radians */
  1248. c.lpzt.lam = (*x) / RAD_TO_DEG;
  1249. c.lpzt.phi = (*y) / RAD_TO_DEG;
  1250. c.lpzt.z = 0;
  1251. c.lpzt.t = 0;
  1252. c = proj_trans(info_trans.pj, PJ_FWD, c);
  1253. ok = proj_errno(info_trans.pj);
  1254. if (strncmp(info_out->proj, "ll", 2) == 0) {
  1255. /* convert to degrees */
  1256. *x = c.lp.lam * RAD_TO_DEG;
  1257. *y = c.lp.phi * RAD_TO_DEG;
  1258. }
  1259. else {
  1260. /* convert to map units */
  1261. *x = c.xy.x / METERS_out;
  1262. *y = c.xy.y / METERS_out;
  1263. }
  1264. }
  1265. else {
  1266. /* convert to meters */
  1267. c.xyzt.x = *x * METERS_in;
  1268. c.xyzt.y = *y * METERS_in;
  1269. c.xyzt.z = 0;
  1270. c.xyzt.t = 0;
  1271. c = proj_trans(info_trans.pj, PJ_FWD, c);
  1272. ok = proj_errno(info_trans.pj);
  1273. if (strncmp(info_out->proj, "ll", 2) == 0) {
  1274. /* convert to degrees */
  1275. *x = c.lp.lam * RAD_TO_DEG;
  1276. *y = c.lp.phi * RAD_TO_DEG;
  1277. }
  1278. else {
  1279. /* convert to map units */
  1280. *x = c.xy.x / METERS_out;
  1281. *y = c.xy.y / METERS_out;
  1282. }
  1283. }
  1284. proj_destroy(info_trans.pj);
  1285. if (ok < 0) {
  1286. G_warning(_("proj_trans() failed: %d"), ok);
  1287. }
  1288. #else
  1289. double u, v;
  1290. double h = 0.0;
  1291. METERS_in = info_in->meters;
  1292. METERS_out = info_out->meters;
  1293. if (strncmp(info_in->proj, "ll", 2) == 0) {
  1294. if (strncmp(info_out->proj, "ll", 2) == 0) {
  1295. u = (*x) / RAD_TO_DEG;
  1296. v = (*y) / RAD_TO_DEG;
  1297. ok = pj_transform(info_in->pj, info_out->pj, 1, 0, &u, &v, &h);
  1298. *x = u * RAD_TO_DEG;
  1299. *y = v * RAD_TO_DEG;
  1300. }
  1301. else {
  1302. u = (*x) / RAD_TO_DEG;
  1303. v = (*y) / RAD_TO_DEG;
  1304. ok = pj_transform(info_in->pj, info_out->pj, 1, 0, &u, &v, &h);
  1305. *x = u / METERS_out;
  1306. *y = v / METERS_out;
  1307. }
  1308. }
  1309. else {
  1310. if (strncmp(info_out->proj, "ll", 2) == 0) {
  1311. u = *x * METERS_in;
  1312. v = *y * METERS_in;
  1313. ok = pj_transform(info_in->pj, info_out->pj, 1, 0, &u, &v, &h);
  1314. *x = u * RAD_TO_DEG;
  1315. *y = v * RAD_TO_DEG;
  1316. }
  1317. else {
  1318. u = *x * METERS_in;
  1319. v = *y * METERS_in;
  1320. ok = pj_transform(info_in->pj, info_out->pj, 1, 0, &u, &v, &h);
  1321. *x = u / METERS_out;
  1322. *y = v / METERS_out;
  1323. }
  1324. }
  1325. if (ok < 0) {
  1326. G_warning(_("pj_transform() failed: %s"), pj_strerrno(ok));
  1327. }
  1328. #endif
  1329. return ok;
  1330. }
  1331. /**
  1332. * \brief Re-project an array of points between two co-ordinate systems with
  1333. * optional ellipsoidal height conversion
  1334. *
  1335. * This function takes pointers to two pj_info structures as arguments,
  1336. * and projects an array of points between the co-ordinate systems
  1337. * represented by them. Pointers to the three arrays of easting, northing,
  1338. * and ellipsoidal height of the point (this one may be NULL) are passed
  1339. * to the function; these will be overwritten by the co-ordinates of the
  1340. * re-projected points.
  1341. *
  1342. * \param count Number of points in the arrays to be transformed
  1343. * \param x Pointer to an array of type double containing easting or longitude
  1344. * \param y Pointer to an array of type double containing northing or latitude
  1345. * \param h Pointer to an array of type double containing ellipsoidal height.
  1346. * May be null in which case a two-dimensional re-projection will be
  1347. * done
  1348. * \param info_in pointer to pj_info struct for input co-ordinate system
  1349. * \param info_out pointer to pj_info struct for output co-ordinate system
  1350. *
  1351. * \return Return value from PROJ proj_trans() function
  1352. **/
  1353. int pj_do_transform(int count, double *x, double *y, double *h,
  1354. const struct pj_info *info_in, const struct pj_info *info_out)
  1355. {
  1356. int ok;
  1357. int i;
  1358. int has_h = 1;
  1359. #ifdef HAVE_PROJ_H
  1360. struct pj_info info_trans;
  1361. PJ_COORD c;
  1362. if (GPJ_init_transform(info_in, info_out, &info_trans) < 0) {
  1363. return -1;
  1364. }
  1365. METERS_in = info_in->meters;
  1366. METERS_out = info_out->meters;
  1367. if (h == NULL) {
  1368. h = G_malloc(sizeof *h * count);
  1369. /* they say memset is only guaranteed for chars ;-( */
  1370. for (i = 0; i < count; ++i)
  1371. h[i] = 0.0;
  1372. has_h = 0;
  1373. }
  1374. ok = 0;
  1375. if (strncmp(info_in->proj, "ll", 2) == 0) {
  1376. c.lpzt.t = 0;
  1377. if (strncmp(info_out->proj, "ll", 2) == 0) {
  1378. for (i = 0; i < count; i++) {
  1379. /* convert to radians */
  1380. c.lpzt.lam = x[i] / RAD_TO_DEG;
  1381. c.lpzt.phi = y[i] / RAD_TO_DEG;
  1382. c.lpzt.z = h[i];
  1383. c = proj_trans(info_trans.pj, PJ_FWD, c);
  1384. if ((ok = proj_errno(info_trans.pj)) < 0)
  1385. break;
  1386. /* convert to degrees */
  1387. x[i] = c.lp.lam * RAD_TO_DEG;
  1388. y[i] = c.lp.phi * RAD_TO_DEG;
  1389. }
  1390. }
  1391. else {
  1392. for (i = 0; i < count; i++) {
  1393. /* convert to radians */
  1394. c.lpzt.lam = x[i] / RAD_TO_DEG;
  1395. c.lpzt.phi = y[i] / RAD_TO_DEG;
  1396. c.lpzt.z = h[i];
  1397. c = proj_trans(info_trans.pj, PJ_FWD, c);
  1398. if ((ok = proj_errno(info_trans.pj)) < 0)
  1399. break;
  1400. /* convert to map units */
  1401. x[i] = c.xy.x / METERS_out;
  1402. y[i] = c.xy.y / METERS_out;
  1403. }
  1404. }
  1405. }
  1406. else {
  1407. c.xyzt.t = 0;
  1408. if (strncmp(info_out->proj, "ll", 2) == 0) {
  1409. for (i = 0; i < count; i++) {
  1410. /* convert to meters */
  1411. c.xyzt.x = x[i] * METERS_in;
  1412. c.xyzt.y = y[i] * METERS_in;
  1413. c.xyzt.z = h[i];
  1414. c = proj_trans(info_trans.pj, PJ_FWD, c);
  1415. if ((ok = proj_errno(info_trans.pj)) < 0)
  1416. break;
  1417. /* convert to degrees */
  1418. x[i] = c.lp.lam * RAD_TO_DEG;
  1419. y[i] = c.lp.phi * RAD_TO_DEG;
  1420. }
  1421. }
  1422. else {
  1423. for (i = 0; i < count; i++) {
  1424. /* convert to meters */
  1425. c.xyzt.x = x[i] * METERS_in;
  1426. c.xyzt.y = y[i] * METERS_in;
  1427. c.xyzt.z = h[i];
  1428. c = proj_trans(info_trans.pj, PJ_FWD, c);
  1429. if ((ok = proj_errno(info_trans.pj)) < 0)
  1430. break;
  1431. /* convert to map units */
  1432. x[i] = c.xy.x / METERS_out;
  1433. y[i] = c.xy.y / METERS_out;
  1434. }
  1435. }
  1436. }
  1437. if (!has_h)
  1438. G_free(h);
  1439. proj_destroy(info_trans.pj);
  1440. if (ok < 0) {
  1441. G_warning(_("proj_trans() failed: %d"), ok);
  1442. }
  1443. #else
  1444. METERS_in = info_in->meters;
  1445. METERS_out = info_out->meters;
  1446. if (h == NULL) {
  1447. h = G_malloc(sizeof *h * count);
  1448. /* they say memset is only guaranteed for chars ;-( */
  1449. for (i = 0; i < count; ++i)
  1450. h[i] = 0.0;
  1451. has_h = 0;
  1452. }
  1453. if (strncmp(info_in->proj, "ll", 2) == 0) {
  1454. if (strncmp(info_out->proj, "ll", 2) == 0) {
  1455. DIVIDE_LOOP(x, y, count, RAD_TO_DEG);
  1456. ok = pj_transform(info_in->pj, info_out->pj, count, 1, x, y, h);
  1457. MULTIPLY_LOOP(x, y, count, RAD_TO_DEG);
  1458. }
  1459. else {
  1460. DIVIDE_LOOP(x, y, count, RAD_TO_DEG);
  1461. ok = pj_transform(info_in->pj, info_out->pj, count, 1, x, y, h);
  1462. DIVIDE_LOOP(x, y, count, METERS_out);
  1463. }
  1464. }
  1465. else {
  1466. if (strncmp(info_out->proj, "ll", 2) == 0) {
  1467. MULTIPLY_LOOP(x, y, count, METERS_in);
  1468. ok = pj_transform(info_in->pj, info_out->pj, count, 1, x, y, h);
  1469. MULTIPLY_LOOP(x, y, count, RAD_TO_DEG);
  1470. }
  1471. else {
  1472. MULTIPLY_LOOP(x, y, count, METERS_in);
  1473. ok = pj_transform(info_in->pj, info_out->pj, count, 1, x, y, h);
  1474. DIVIDE_LOOP(x, y, count, METERS_out);
  1475. }
  1476. }
  1477. if (!has_h)
  1478. G_free(h);
  1479. if (ok < 0) {
  1480. G_warning(_("pj_transform() failed: %s"), pj_strerrno(ok));
  1481. }
  1482. #endif
  1483. return ok;
  1484. }