do_proj.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630
  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. PJ *source_crs;
  332. /* Even Rouault:
  333. * if info_in->def contains a +towgs84/+nadgrids clause,
  334. * this pipeline would apply it, whereas you probably only want
  335. * the reverse projection, and no datum shift.
  336. * The easiest would probably to mess up with the PROJ string.
  337. * Otherwise with the PROJ API, you could
  338. * instanciate a PJ object from the string,
  339. * check if it is a BoundCRS with proj_get_source_crs(),
  340. * and in that case, take the source CRS with proj_get_source_crs(),
  341. * and do the inverse transform on it */
  342. source_crs = proj_get_source_crs(NULL, info_in->pj);
  343. if (source_crs) {
  344. projstr = proj_as_proj_string(NULL, source_crs, PJ_PROJ_5, NULL);
  345. if (projstr)
  346. indef = G_store(projstr);
  347. proj_destroy(source_crs);
  348. }
  349. if (indef == NULL)
  350. indef = G_store(info_in->def);
  351. /* what about axis order?
  352. * is it always enu?
  353. * probably yes, as long as there is no +proj=axisswap step */
  354. G_asprintf(&(info_trans->def), "+proj=pipeline +step +inv %s",
  355. indef);
  356. info_trans->pj = proj_create(PJ_DEFAULT_CTX, info_trans->def);
  357. if (info_trans->pj == NULL) {
  358. G_warning(_("proj_create() failed for '%s'"), info_trans->def);
  359. G_free(indef);
  360. return -1;
  361. }
  362. projstr = proj_as_proj_string(NULL, info_trans->pj, PJ_PROJ_5, NULL);
  363. if (projstr == NULL) {
  364. G_warning(_("proj_create() failed for '%s'"), info_trans->def);
  365. G_free(indef);
  366. return -1;
  367. }
  368. G_free(indef);
  369. }
  370. /* input and output CRS are available */
  371. else if (info_in->def && info_out->pj && info_out->def) {
  372. char *indef = NULL, *outdef = NULL;
  373. char *indefcrs = NULL, *outdefcrs = NULL;
  374. char *insrid = NULL, *outsrid = NULL;
  375. int use_insrid = 0, use_outsrid = 0;
  376. PJ *source_crs, *target_crs;
  377. PJ_AREA *pj_area = NULL;
  378. double xmin, xmax, ymin, ymax;
  379. int op_count = 0;
  380. /* remove any +towgs84/+nadgrids clause, see above
  381. * does not always remove +towgs84=0.000,0.000,0.000 ??? */
  382. G_debug(1, "source proj string: %s", info_in->def);
  383. G_debug(1, "source type: %s", get_pj_type_string(info_in->pj));
  384. indefcrs = info_in->def;
  385. source_crs = proj_get_source_crs(NULL, info_in->pj);
  386. if (source_crs) {
  387. const char *projstr;
  388. projstr = proj_as_proj_string(NULL, source_crs, PJ_PROJ_5, NULL);
  389. if (projstr) {
  390. indefcrs = G_store(projstr);
  391. G_debug(1, "Input CRS definition converted from '%s' to '%s'",
  392. info_in->def, indefcrs);
  393. }
  394. proj_destroy(source_crs);
  395. source_crs = NULL;
  396. }
  397. G_debug(1, "target proj string: %s", info_out->def);
  398. G_debug(1, "target type: %s", get_pj_type_string(info_out->pj));
  399. outdefcrs = info_out->def;
  400. target_crs = proj_get_source_crs(NULL, info_out->pj);
  401. if (target_crs) {
  402. const char *projstr;
  403. projstr = proj_as_proj_string(NULL, target_crs, PJ_PROJ_5, NULL);
  404. if (projstr) {
  405. outdefcrs = G_store(projstr);
  406. G_debug(1, "Output CRS definition converted from '%s' to '%s'",
  407. info_out->def, outdefcrs);
  408. }
  409. proj_destroy(target_crs);
  410. target_crs = NULL;
  411. }
  412. /* PROJ6+: EPSG must be uppercase EPSG */
  413. if (info_in->srid) {
  414. if (strncmp(info_in->srid, "epsg", 4) == 0)
  415. insrid = G_store_upper(info_in->srid);
  416. else
  417. insrid = G_store(info_in->srid);
  418. }
  419. if (info_out->srid) {
  420. if (strncmp(info_out->srid, "epsg", 4) == 0)
  421. outsrid = G_store_upper(info_out->srid);
  422. else
  423. outsrid = G_store(info_out->srid);
  424. }
  425. if (insrid) {
  426. G_asprintf(&indef, "%s", insrid);
  427. use_insrid = 1;
  428. }
  429. else {
  430. G_asprintf(&indef, "%s", indefcrs);
  431. }
  432. G_debug(1, "Input CRS definition: %s", indef);
  433. if (outsrid) {
  434. G_asprintf(&outdef, "%s", outsrid);
  435. use_outsrid = 1;
  436. }
  437. else {
  438. G_asprintf(&outdef, "%s", outdefcrs);
  439. }
  440. G_debug(1, "Output CRS definition: %s", outdef);
  441. /* check number of operations */
  442. source_crs = proj_create(NULL, indef);
  443. target_crs = proj_create(NULL, outdef);
  444. /* get pj_area */
  445. if (get_pj_area(&xmin, &xmax, &ymin, &ymax)) {
  446. pj_area = proj_area_create();
  447. proj_area_set_bbox(pj_area, xmin, ymin, xmax, ymax);
  448. }
  449. if (source_crs && target_crs) {
  450. PJ_OPERATION_FACTORY_CONTEXT *operation_ctx;
  451. PJ_OBJ_LIST *op_list;
  452. operation_ctx = proj_create_operation_factory_context(NULL, NULL);
  453. /* proj_create_operations() works only if both source_crs
  454. * and target_crs are found in the proj db
  455. * if any is not found, proj can not get a list of operations
  456. * and we have to take care of datumshift manually */
  457. /* constrain by area ? */
  458. op_list = proj_create_operations(NULL,
  459. source_crs,
  460. target_crs,
  461. operation_ctx);
  462. op_count = 0;
  463. if (op_list)
  464. op_count = proj_list_get_count(op_list);
  465. if (op_count > 1) {
  466. int i;
  467. G_warning(_("Found %d possible transformations"), op_count);
  468. for (i = 0; i < op_count; i++) {
  469. const char *str;
  470. const char *area_of_use, *projstr;
  471. double e, w, s, n;
  472. PJ_PROJ_INFO pj_info;
  473. PJ *op, *op_norm;
  474. op = proj_list_get(NULL, op_list, i);
  475. op_norm = proj_normalize_for_visualization(PJ_DEFAULT_CTX, op);
  476. if (!op_norm) {
  477. G_warning(_("proj_normalize_for_visualization() failed for operation %d"),
  478. i + 1);
  479. }
  480. else {
  481. proj_destroy(op);
  482. op = op_norm;
  483. }
  484. projstr = proj_as_proj_string(NULL, op,
  485. PJ_PROJ_5, NULL);
  486. pj_info = proj_pj_info(op);
  487. proj_get_area_of_use(NULL, op, &w, &s, &e, &n, &area_of_use);
  488. if (projstr) {
  489. G_important_message("************************");
  490. G_important_message(_("Operation %d:"), i + 1);
  491. G_important_message(_("Description: %s"),
  492. pj_info.description);
  493. G_important_message(" ");
  494. G_important_message(_("Area of use: %s"),
  495. area_of_use);
  496. if (pj_info.accuracy > 0) {
  497. G_important_message(" ");
  498. G_important_message(_("Accuracy within area of use: %g m"),
  499. pj_info.accuracy);
  500. }
  501. #if PROJ_VERSION_NUM >= 6020000
  502. str = proj_get_remarks(op);
  503. if (str && *str) {
  504. G_important_message(" ");
  505. G_important_message(_("Remarks: %s"), str);
  506. }
  507. str = proj_get_scope(op);
  508. if (str && *str) {
  509. G_important_message(" ");
  510. G_important_message(_("Scope: %s"), str);
  511. }
  512. #endif
  513. G_important_message(" ");
  514. G_important_message(_("PROJ string:"));
  515. G_important_message("%s", projstr);
  516. }
  517. proj_destroy(op);
  518. }
  519. G_important_message("************************");
  520. G_important_message(_("See also output of:"));
  521. G_important_message("projinfo -o PROJ -s \"%s\" -t \"%s\"",
  522. indef, outdef);
  523. G_important_message(_("Please provide the appropriate PROJ string with the %s option"),
  524. "pipeline");
  525. G_important_message("************************");
  526. }
  527. if (op_list)
  528. proj_list_destroy(op_list);
  529. proj_operation_factory_context_destroy(operation_ctx);
  530. }
  531. if (source_crs)
  532. proj_destroy(source_crs);
  533. if (target_crs)
  534. proj_destroy(target_crs);
  535. /* try proj_create_crs_to_crs() */
  536. G_debug(1, "trying %s to %s", indef, outdef);
  537. info_trans->pj = proj_create_crs_to_crs(PJ_DEFAULT_CTX,
  538. indef,
  539. outdef,
  540. pj_area);
  541. if (info_trans->pj) {
  542. const char *projstr = NULL;
  543. projstr = proj_as_proj_string(NULL, info_trans->pj,
  544. PJ_PROJ_5, NULL);
  545. if (projstr == NULL) {
  546. G_debug(1, "proj_create_crs_to_crs() failed with PROJ%d for input \"%s\", output \"%s\"",
  547. PROJ_VERSION_MAJOR, indef, outdef);
  548. G_asprintf(&indef, "%s", indefcrs);
  549. G_asprintf(&outdef, "%s", outdefcrs);
  550. G_debug(1, "trying %s to %s", indef, outdef);
  551. /* try proj_create_crs_to_crs() */
  552. proj_destroy(info_trans->pj);
  553. info_trans->pj = NULL;
  554. info_trans->pj = proj_create_crs_to_crs(PJ_DEFAULT_CTX,
  555. indef,
  556. outdef,
  557. NULL);
  558. }
  559. else {
  560. /* PROJ will do the unit conversion if set up from srid
  561. * -> disable unit conversion for GPJ_transform */
  562. /* ugly hack */
  563. if (use_insrid) {
  564. ((struct pj_info *)info_in)->meters = 1;
  565. }
  566. if (use_outsrid) {
  567. ((struct pj_info *)info_out)->meters = 1;
  568. }
  569. }
  570. }
  571. if (info_trans->pj) {
  572. const char *projstr;
  573. PJ *pj_norm = NULL;
  574. G_debug(1, "proj_create_crs_to_crs() succeeded with PROJ%d",
  575. PROJ_VERSION_MAJOR);
  576. projstr = proj_as_proj_string(NULL, info_trans->pj,
  577. PJ_PROJ_5, NULL);
  578. info_trans->def = G_store(projstr);
  579. if (projstr) {
  580. /* make sure axis order is easting, northing
  581. * proj_normalize_for_visualization() requires
  582. * source and target CRS
  583. * -> does not work with ll equivalent of input:
  584. * no target CRS in +proj=pipeline +step +inv %s */
  585. pj_norm = proj_normalize_for_visualization(PJ_DEFAULT_CTX, info_trans->pj);
  586. if (!pj_norm) {
  587. G_warning(_("proj_normalize_for_visualization() failed for '%s'"),
  588. info_trans->def);
  589. }
  590. else {
  591. proj_destroy(info_trans->pj);
  592. info_trans->pj = pj_norm;
  593. projstr = proj_as_proj_string(NULL, info_trans->pj,
  594. PJ_PROJ_5, NULL);
  595. info_trans->def = G_store(projstr);
  596. }
  597. if (op_count > 1) {
  598. G_important_message(_("Selected pipeline:"));
  599. G_important_message(_("%s"), info_trans->def);
  600. G_important_message("************************");
  601. }
  602. G_debug(1, "proj_create_crs_to_crs() pipeline: %s", info_trans->def);
  603. }
  604. else {
  605. proj_destroy(info_trans->pj);
  606. info_trans->pj = NULL;
  607. }
  608. }
  609. /* last try with proj_create() */
  610. if (info_trans->pj == NULL) {
  611. G_debug(1, "proj_create_crs_to_crs() failed with PROJ%d for input \"%s\", output \"%s\"",
  612. PROJ_VERSION_MAJOR, indef, outdef);
  613. G_warning("GPJ_init_transform(): falling back to proj_create()");
  614. if (insrid) {
  615. G_free(indef);
  616. }
  617. G_asprintf(&indef, "%s", info_in->def);
  618. if (outsrid) {
  619. G_free(outdef);
  620. }
  621. G_asprintf(&outdef, "%s", info_out->def);
  622. /* try proj_create() with +proj=pipeline +step +inv %s +step %s" */
  623. G_asprintf(&(info_trans->def), "+proj=pipeline +step +inv %s +step %s",
  624. indef, outdef);
  625. info_trans->pj = proj_create(PJ_DEFAULT_CTX, info_trans->def);
  626. }
  627. if (pj_area)
  628. proj_area_destroy(pj_area);
  629. if (insrid)
  630. G_free(insrid);
  631. if (outsrid)
  632. G_free(outsrid);
  633. G_free(indef);
  634. G_free(outdef);
  635. }
  636. if (info_trans->pj == NULL) {
  637. G_warning(_("proj_create() failed for '%s'"), info_trans->def);
  638. return -1;
  639. }
  640. #else /* PROJ 5 */
  641. info_trans->pj = NULL;
  642. info_trans->meters = 1.;
  643. info_trans->zone = 0;
  644. sprintf(info_trans->proj, "pipeline");
  645. /* user-provided pipeline */
  646. if (info_trans->def) {
  647. /* create a pj from user-defined transformation pipeline */
  648. info_trans->pj = proj_create(PJ_DEFAULT_CTX, info_trans->def);
  649. if (info_trans->pj == NULL) {
  650. G_warning(_("proj_create() failed for '%s'"), info_trans->def);
  651. return -1;
  652. }
  653. }
  654. /* if no output CRS is defined,
  655. * assume info_out to be ll equivalent of info_in */
  656. else if (info_out->pj == NULL) {
  657. G_asprintf(&(info_trans->def), "+proj=pipeline +step +inv %s",
  658. info_in->def);
  659. info_trans->pj = proj_create(PJ_DEFAULT_CTX, info_trans->def);
  660. if (info_trans->pj == NULL) {
  661. G_warning(_("proj_create() failed for '%s'"), info_trans->def);
  662. return -1;
  663. }
  664. }
  665. else if (info_in->def && info_out->pj && info_out->def) {
  666. char *indef = NULL, *outdef = NULL;
  667. char *insrid = NULL, *outsrid = NULL;
  668. /* PROJ5: EPSG must be lowercase epsg */
  669. if (info_in->srid) {
  670. if (strncmp(info_in->srid, "EPSG", 4) == 0)
  671. insrid = G_store_lower(info_in->srid);
  672. else
  673. insrid = G_store(info_in->srid);
  674. }
  675. if (info_out->pj && info_out->srid) {
  676. if (strncmp(info_out->srid, "EPSG", 4) == 0)
  677. outsrid = G_store_lower(info_out->srid);
  678. else
  679. outsrid = G_store(info_out->srid);
  680. }
  681. info_trans->pj = NULL;
  682. if (insrid && outsrid) {
  683. G_asprintf(&indef, "%s", insrid);
  684. G_asprintf(&outdef, "%s", outsrid);
  685. G_asprintf(&(info_trans->def), "+proj=pipeline +step +inv +init=%s +step +init=%s",
  686. indef, outdef);
  687. /* try proj_create_crs_to_crs() */
  688. info_trans->pj = proj_create_crs_to_crs(PJ_DEFAULT_CTX,
  689. indef,
  690. outdef,
  691. NULL);
  692. }
  693. if (info_trans->pj) {
  694. G_debug(1, "proj_create_crs_to_crs() succeeded with PROJ5");
  695. }
  696. else {
  697. if (indef) {
  698. G_free(indef);
  699. indef = NULL;
  700. }
  701. if (insrid) {
  702. G_asprintf(&indef, "+init=%s", insrid);
  703. }
  704. else {
  705. G_asprintf(&indef, "%s", info_in->def);
  706. }
  707. if (outdef) {
  708. G_free(outdef);
  709. outdef = NULL;
  710. }
  711. if (outsrid) {
  712. G_asprintf(&outdef, "+init=%s", outsrid);
  713. }
  714. else {
  715. G_asprintf(&outdef, "%s", info_out->def);
  716. }
  717. /* try proj_create() with +proj=pipeline +step +inv %s +step %s" */
  718. G_asprintf(&(info_trans->def), "+proj=pipeline +step +inv %s +step %s",
  719. indef, outdef);
  720. info_trans->pj = proj_create(PJ_DEFAULT_CTX, info_trans->def);
  721. }
  722. if (insrid)
  723. G_free(insrid);
  724. if (outsrid)
  725. G_free(outsrid);
  726. G_free(indef);
  727. G_free(outdef);
  728. }
  729. if (info_trans->pj == NULL) {
  730. G_warning(_("proj_create() failed for '%s'"), info_trans->def);
  731. return -1;
  732. }
  733. #endif
  734. #else /* PROJ 4 */
  735. if (info_out->pj == NULL) {
  736. if (GPJ_get_equivalent_latlong(info_out, info_in) < 0) {
  737. G_warning(_("Unable to create latlong equivalent for '%s'"),
  738. info_in->def);
  739. return -1;
  740. }
  741. }
  742. #endif
  743. return 1;
  744. }
  745. /* TODO: rename pj_ to GPJ_ to avoid symbol clash with PROJ lib */
  746. /**
  747. * \brief Re-project a point between two co-ordinate systems using a
  748. * transformation object prepared with GPJ_prepare_pj()
  749. *
  750. * This function takes pointers to three pj_info structures as arguments,
  751. * and projects a point between the input and output co-ordinate system.
  752. * The pj_info structure info_trans must have been initialized with
  753. * GPJ_init_transform().
  754. * The direction determines if a point is projected from input CRS to
  755. * output CRS (PJ_FWD) or from output CRS to input CRS (PJ_INV).
  756. * The easting, northing, and height of the point are contained in the
  757. * pointers passed to the function; these will be overwritten by the
  758. * coordinates of the transformed point.
  759. *
  760. * \param info_in pointer to pj_info struct for input co-ordinate system
  761. * \param info_out pointer to pj_info struct for output co-ordinate system
  762. * \param info_trans pointer to pj_info struct for a transformation object (PROJ 5+)
  763. * \param dir direction of the transformation (PJ_FWD or PJ_INV)
  764. * \param x Pointer to a double containing easting or longitude
  765. * \param y Pointer to a double containing northing or latitude
  766. * \param z Pointer to a double containing height, or NULL
  767. *
  768. * \return Return value from PROJ proj_trans() function
  769. **/
  770. int GPJ_transform(const struct pj_info *info_in,
  771. const struct pj_info *info_out,
  772. const struct pj_info *info_trans, int dir,
  773. double *x, double *y, double *z)
  774. {
  775. int ok = 0;
  776. #ifdef HAVE_PROJ_H
  777. /* PROJ 5+ variant */
  778. int in_is_ll, out_is_ll, in_deg2rad, out_rad2deg;
  779. PJ_COORD c;
  780. if (info_in->pj == NULL)
  781. G_fatal_error(_("No input projection"));
  782. if (info_trans->pj == NULL)
  783. G_fatal_error(_("No transformation object"));
  784. in_deg2rad = out_rad2deg = 1;
  785. if (dir == PJ_FWD) {
  786. /* info_in -> info_out */
  787. METERS_in = info_in->meters;
  788. in_is_ll = !strncmp(info_in->proj, "ll", 2);
  789. #if PROJ_VERSION_MAJOR >= 6
  790. /* PROJ 6+: conversion to radians is not always needed:
  791. * if proj_angular_input(info_trans->pj, dir) == 1
  792. * -> convert from degrees to radians */
  793. if (in_is_ll && proj_angular_input(info_trans->pj, dir) == 0) {
  794. in_deg2rad = 0;
  795. }
  796. #endif
  797. if (info_out->pj) {
  798. METERS_out = info_out->meters;
  799. out_is_ll = !strncmp(info_out->proj, "ll", 2);
  800. #if PROJ_VERSION_MAJOR >= 6
  801. /* PROJ 6+: conversion to radians is not always needed:
  802. * if proj_angular_input(info_trans->pj, dir) == 1
  803. * -> convert from degrees to radians */
  804. if (out_is_ll && proj_angular_output(info_trans->pj, dir) == 0) {
  805. out_rad2deg = 0;
  806. }
  807. #endif
  808. }
  809. else {
  810. METERS_out = 1.0;
  811. out_is_ll = 1;
  812. }
  813. }
  814. else {
  815. /* info_out -> info_in */
  816. METERS_out = info_in->meters;
  817. out_is_ll = !strncmp(info_in->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_input(info_trans->pj, dir) == 0) {
  823. out_rad2deg = 0;
  824. }
  825. #endif
  826. if (info_out->pj) {
  827. METERS_in = info_out->meters;
  828. in_is_ll = !strncmp(info_out->proj, "ll", 2);
  829. #if PROJ_VERSION_MAJOR >= 6
  830. /* PROJ 6+: conversion to radians is not always needed:
  831. * if proj_angular_input(info_trans->pj, dir) == 1
  832. * -> convert from degrees to radians */
  833. if (in_is_ll && proj_angular_output(info_trans->pj, dir) == 0) {
  834. in_deg2rad = 0;
  835. }
  836. #endif
  837. }
  838. else {
  839. METERS_in = 1.0;
  840. in_is_ll = 1;
  841. }
  842. }
  843. /* prepare */
  844. if (in_is_ll) {
  845. if (in_deg2rad) {
  846. /* convert degrees to radians */
  847. c.lpzt.lam = (*x) / RAD_TO_DEG;
  848. c.lpzt.phi = (*y) / RAD_TO_DEG;
  849. }
  850. else {
  851. c.lpzt.lam = (*x);
  852. c.lpzt.phi = (*y);
  853. }
  854. c.lpzt.z = 0;
  855. if (z)
  856. c.lpzt.z = *z;
  857. c.lpzt.t = 0;
  858. }
  859. else {
  860. /* convert to meters */
  861. c.xyzt.x = *x * METERS_in;
  862. c.xyzt.y = *y * METERS_in;
  863. c.xyzt.z = 0;
  864. if (z)
  865. c.xyzt.z = *z;
  866. c.xyzt.t = 0;
  867. }
  868. /* transform */
  869. c = proj_trans(info_trans->pj, dir, c);
  870. ok = proj_errno(info_trans->pj);
  871. if (ok < 0) {
  872. G_warning(_("proj_trans() failed: %s"), proj_errno_string(ok));
  873. return ok;
  874. }
  875. /* output */
  876. if (out_is_ll) {
  877. /* convert to degrees */
  878. if (out_rad2deg) {
  879. /* convert radians to degrees */
  880. *x = c.lp.lam * RAD_TO_DEG;
  881. *y = c.lp.phi * RAD_TO_DEG;
  882. }
  883. else {
  884. *x = c.lp.lam;
  885. *y = c.lp.phi;
  886. }
  887. if (z)
  888. *z = c.lpzt.z;
  889. }
  890. else {
  891. /* convert to map units */
  892. *x = c.xyzt.x / METERS_out;
  893. *y = c.xyzt.y / METERS_out;
  894. if (z)
  895. *z = c.xyzt.z;
  896. }
  897. #else
  898. /* PROJ 4 variant */
  899. double u, v;
  900. double h = 0.0;
  901. const struct pj_info *p_in, *p_out;
  902. if (info_out == NULL)
  903. G_fatal_error(_("No output projection"));
  904. if (dir == PJ_FWD) {
  905. p_in = info_in;
  906. p_out = info_out;
  907. }
  908. else {
  909. p_in = info_out;
  910. p_out = info_in;
  911. }
  912. METERS_in = p_in->meters;
  913. METERS_out = p_out->meters;
  914. if (z)
  915. h = *z;
  916. if (strncmp(p_in->proj, "ll", 2) == 0) {
  917. u = (*x) / RAD_TO_DEG;
  918. v = (*y) / RAD_TO_DEG;
  919. }
  920. else {
  921. u = *x * METERS_in;
  922. v = *y * METERS_in;
  923. }
  924. ok = pj_transform(p_in->pj, p_out->pj, 1, 0, &u, &v, &h);
  925. if (ok < 0) {
  926. G_warning(_("pj_transform() failed: %s"), pj_strerrno(ok));
  927. return ok;
  928. }
  929. if (strncmp(p_out->proj, "ll", 2) == 0) {
  930. *x = u * RAD_TO_DEG;
  931. *y = v * RAD_TO_DEG;
  932. }
  933. else {
  934. *x = u / METERS_out;
  935. *y = v / METERS_out;
  936. }
  937. if (z)
  938. *z = h;
  939. #endif
  940. return ok;
  941. }
  942. /**
  943. * \brief Re-project an array of points between two co-ordinate systems
  944. * using a transformation object prepared with GPJ_prepare_pj()
  945. *
  946. * This function takes pointers to three pj_info structures as arguments,
  947. * and projects an array of pointd between the input and output
  948. * co-ordinate system. The pj_info structure info_trans must have been
  949. * initialized with GPJ_init_transform().
  950. * The direction determines if a point is projected from input CRS to
  951. * output CRS (PJ_FWD) or from output CRS to input CRS (PJ_INV).
  952. * The easting, northing, and height of the point are contained in the
  953. * pointers passed to the function; these will be overwritten by the
  954. * coordinates of the transformed point.
  955. *
  956. * \param info_in pointer to pj_info struct for input co-ordinate system
  957. * \param info_out pointer to pj_info struct for output co-ordinate system
  958. * \param info_trans pointer to pj_info struct for a transformation object (PROJ 5+)
  959. * \param dir direction of the transformation (PJ_FWD or PJ_INV)
  960. * \param x pointer to an array of type double containing easting or longitude
  961. * \param y pointer to an array of type double containing northing or latitude
  962. * \param z pointer to an array of type double containing height, or NULL
  963. * \param n number of points in the arrays to be transformed
  964. *
  965. * \return Return value from PROJ proj_trans() function
  966. **/
  967. int GPJ_transform_array(const struct pj_info *info_in,
  968. const struct pj_info *info_out,
  969. const struct pj_info *info_trans, int dir,
  970. double *x, double *y, double *z, int n)
  971. {
  972. int ok;
  973. int i;
  974. int has_z = 1;
  975. #ifdef HAVE_PROJ_H
  976. /* PROJ 5+ variant */
  977. int in_is_ll, out_is_ll, in_deg2rad, out_rad2deg;
  978. PJ_COORD c;
  979. if (info_trans->pj == NULL)
  980. G_fatal_error(_("No transformation object"));
  981. in_deg2rad = out_rad2deg = 1;
  982. if (dir == PJ_FWD) {
  983. /* info_in -> info_out */
  984. METERS_in = info_in->meters;
  985. in_is_ll = !strncmp(info_in->proj, "ll", 2);
  986. #if PROJ_VERSION_MAJOR >= 6
  987. /* PROJ 6+: conversion to radians is not always needed:
  988. * if proj_angular_input(info_trans->pj, dir) == 1
  989. * -> convert from degrees to radians */
  990. if (in_is_ll && proj_angular_input(info_trans->pj, dir) == 0) {
  991. in_deg2rad = 0;
  992. }
  993. #endif
  994. if (info_out->pj) {
  995. METERS_out = info_out->meters;
  996. out_is_ll = !strncmp(info_out->proj, "ll", 2);
  997. #if PROJ_VERSION_MAJOR >= 6
  998. /* PROJ 6+: conversion to radians is not always needed:
  999. * if proj_angular_input(info_trans->pj, dir) == 1
  1000. * -> convert from degrees to radians */
  1001. if (out_is_ll && proj_angular_output(info_trans->pj, dir) == 0) {
  1002. out_rad2deg = 0;
  1003. }
  1004. #endif
  1005. }
  1006. else {
  1007. METERS_out = 1.0;
  1008. out_is_ll = 1;
  1009. }
  1010. }
  1011. else {
  1012. /* info_out -> info_in */
  1013. METERS_out = info_in->meters;
  1014. out_is_ll = !strncmp(info_in->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_input(info_trans->pj, dir) == 0) {
  1020. out_rad2deg = 0;
  1021. }
  1022. #endif
  1023. if (info_out->pj) {
  1024. METERS_in = info_out->meters;
  1025. in_is_ll = !strncmp(info_out->proj, "ll", 2);
  1026. #if PROJ_VERSION_MAJOR >= 6
  1027. /* PROJ 6+: conversion to degrees is not always needed:
  1028. * if proj_angular_output(info_trans->pj, dir) == 1
  1029. * -> convert from degrees to radians */
  1030. if (in_is_ll && proj_angular_output(info_trans->pj, dir) == 0) {
  1031. in_deg2rad = 0;
  1032. }
  1033. #endif
  1034. }
  1035. else {
  1036. METERS_in = 1.0;
  1037. in_is_ll = 1;
  1038. }
  1039. }
  1040. if (z == NULL) {
  1041. z = G_malloc(sizeof(double) * n);
  1042. /* they say memset is only guaranteed for chars ;-( */
  1043. for (i = 0; i < n; i++)
  1044. z[i] = 0.0;
  1045. has_z = 0;
  1046. }
  1047. ok = 0;
  1048. if (in_is_ll) {
  1049. c.lpzt.t = 0;
  1050. if (out_is_ll) {
  1051. /* what is more costly ?
  1052. * calling proj_trans for each point
  1053. * or having three loops over all points ?
  1054. * proj_trans_array() itself calls proj_trans() in a loop
  1055. * -> one loop over all points is better than
  1056. * three loops over all points
  1057. */
  1058. for (i = 0; i < n; i++) {
  1059. if (in_deg2rad) {
  1060. /* convert degrees to radians */
  1061. c.lpzt.lam = (*x) / RAD_TO_DEG;
  1062. c.lpzt.phi = (*y) / RAD_TO_DEG;
  1063. }
  1064. else {
  1065. c.lpzt.lam = (*x);
  1066. c.lpzt.phi = (*y);
  1067. }
  1068. c.lpzt.z = z[i];
  1069. c = proj_trans(info_trans->pj, dir, c);
  1070. if ((ok = proj_errno(info_trans->pj)) < 0)
  1071. break;
  1072. if (out_rad2deg) {
  1073. /* convert radians to degrees */
  1074. x[i] = c.lp.lam * RAD_TO_DEG;
  1075. y[i] = c.lp.phi * RAD_TO_DEG;
  1076. }
  1077. else {
  1078. x[i] = c.lp.lam;
  1079. y[i] = c.lp.phi;
  1080. }
  1081. }
  1082. }
  1083. else {
  1084. for (i = 0; i < n; i++) {
  1085. if (in_deg2rad) {
  1086. /* convert degrees to radians */
  1087. c.lpzt.lam = (*x) / RAD_TO_DEG;
  1088. c.lpzt.phi = (*y) / RAD_TO_DEG;
  1089. }
  1090. else {
  1091. c.lpzt.lam = (*x);
  1092. c.lpzt.phi = (*y);
  1093. }
  1094. c.lpzt.z = z[i];
  1095. c = proj_trans(info_trans->pj, dir, c);
  1096. if ((ok = proj_errno(info_trans->pj)) < 0)
  1097. break;
  1098. /* convert to map units */
  1099. x[i] = c.xy.x / METERS_out;
  1100. y[i] = c.xy.y / METERS_out;
  1101. }
  1102. }
  1103. }
  1104. else {
  1105. c.xyzt.t = 0;
  1106. if (out_is_ll) {
  1107. for (i = 0; i < n; i++) {
  1108. /* convert to meters */
  1109. c.xyzt.x = x[i] * METERS_in;
  1110. c.xyzt.y = y[i] * METERS_in;
  1111. c.xyzt.z = z[i];
  1112. c = proj_trans(info_trans->pj, dir, c);
  1113. if ((ok = proj_errno(info_trans->pj)) < 0)
  1114. break;
  1115. if (out_rad2deg) {
  1116. /* convert radians to degrees */
  1117. x[i] = c.lp.lam * RAD_TO_DEG;
  1118. y[i] = c.lp.phi * RAD_TO_DEG;
  1119. }
  1120. else {
  1121. x[i] = c.lp.lam;
  1122. y[i] = c.lp.phi;
  1123. }
  1124. }
  1125. }
  1126. else {
  1127. for (i = 0; i < n; i++) {
  1128. /* convert to meters */
  1129. c.xyzt.x = x[i] * METERS_in;
  1130. c.xyzt.y = y[i] * METERS_in;
  1131. c.xyzt.z = z[i];
  1132. c = proj_trans(info_trans->pj, dir, c);
  1133. if ((ok = proj_errno(info_trans->pj)) < 0)
  1134. break;
  1135. /* convert to map units */
  1136. x[i] = c.xy.x / METERS_out;
  1137. y[i] = c.xy.y / METERS_out;
  1138. }
  1139. }
  1140. }
  1141. if (!has_z)
  1142. G_free(z);
  1143. if (ok < 0) {
  1144. G_warning(_("proj_trans() failed: %s"), proj_errno_string(ok));
  1145. }
  1146. #else
  1147. /* PROJ 4 variant */
  1148. const struct pj_info *p_in, *p_out;
  1149. if (dir == PJ_FWD) {
  1150. p_in = info_in;
  1151. p_out = info_out;
  1152. }
  1153. else {
  1154. p_in = info_out;
  1155. p_out = info_in;
  1156. }
  1157. METERS_in = p_in->meters;
  1158. METERS_out = p_out->meters;
  1159. if (z == NULL) {
  1160. z = G_malloc(sizeof(double) * n);
  1161. /* they say memset is only guaranteed for chars ;-( */
  1162. for (i = 0; i < n; ++i)
  1163. z[i] = 0.0;
  1164. has_z = 0;
  1165. }
  1166. if (strncmp(p_in->proj, "ll", 2) == 0) {
  1167. if (strncmp(p_out->proj, "ll", 2) == 0) {
  1168. DIVIDE_LOOP(x, y, n, RAD_TO_DEG);
  1169. ok = pj_transform(info_in->pj, info_out->pj, n, 1, x, y, z);
  1170. MULTIPLY_LOOP(x, y, n, RAD_TO_DEG);
  1171. }
  1172. else {
  1173. DIVIDE_LOOP(x, y, n, RAD_TO_DEG);
  1174. ok = pj_transform(info_in->pj, info_out->pj, n, 1, x, y, z);
  1175. DIVIDE_LOOP(x, y, n, METERS_out);
  1176. }
  1177. }
  1178. else {
  1179. if (strncmp(p_out->proj, "ll", 2) == 0) {
  1180. MULTIPLY_LOOP(x, y, n, METERS_in);
  1181. ok = pj_transform(info_in->pj, info_out->pj, n, 1, x, y, z);
  1182. MULTIPLY_LOOP(x, y, n, RAD_TO_DEG);
  1183. }
  1184. else {
  1185. MULTIPLY_LOOP(x, y, n, METERS_in);
  1186. ok = pj_transform(info_in->pj, info_out->pj, n, 1, x, y, z);
  1187. DIVIDE_LOOP(x, y, n, METERS_out);
  1188. }
  1189. }
  1190. if (!has_z)
  1191. G_free(z);
  1192. if (ok < 0)
  1193. G_warning(_("pj_transform() failed: %s"), pj_strerrno(ok));
  1194. #endif
  1195. return ok;
  1196. }
  1197. /*
  1198. * old API, to be deleted
  1199. */
  1200. /**
  1201. * \brief Re-project a point between two co-ordinate systems
  1202. *
  1203. * This function takes pointers to two pj_info structures as arguments,
  1204. * and projects a point between the co-ordinate systems represented by them.
  1205. * The easting and northing of the point are contained in two pointers passed
  1206. * to the function; these will be overwritten by the co-ordinates of the
  1207. * re-projected point.
  1208. *
  1209. * \param x Pointer to a double containing easting or longitude
  1210. * \param y Pointer to a double containing northing or latitude
  1211. * \param info_in pointer to pj_info struct for input co-ordinate system
  1212. * \param info_out pointer to pj_info struct for output co-ordinate system
  1213. *
  1214. * \return Return value from PROJ proj_trans() function
  1215. **/
  1216. int pj_do_proj(double *x, double *y,
  1217. const struct pj_info *info_in, const struct pj_info *info_out)
  1218. {
  1219. int ok;
  1220. #ifdef HAVE_PROJ_H
  1221. struct pj_info info_trans;
  1222. PJ_COORD c;
  1223. if (GPJ_init_transform(info_in, info_out, &info_trans) < 0) {
  1224. return -1;
  1225. }
  1226. METERS_in = info_in->meters;
  1227. METERS_out = info_out->meters;
  1228. if (strncmp(info_in->proj, "ll", 2) == 0) {
  1229. /* convert to radians */
  1230. c.lpzt.lam = (*x) / RAD_TO_DEG;
  1231. c.lpzt.phi = (*y) / RAD_TO_DEG;
  1232. c.lpzt.z = 0;
  1233. c.lpzt.t = 0;
  1234. c = proj_trans(info_trans.pj, PJ_FWD, c);
  1235. ok = proj_errno(info_trans.pj);
  1236. if (strncmp(info_out->proj, "ll", 2) == 0) {
  1237. /* convert to degrees */
  1238. *x = c.lp.lam * RAD_TO_DEG;
  1239. *y = c.lp.phi * RAD_TO_DEG;
  1240. }
  1241. else {
  1242. /* convert to map units */
  1243. *x = c.xy.x / METERS_out;
  1244. *y = c.xy.y / METERS_out;
  1245. }
  1246. }
  1247. else {
  1248. /* convert to meters */
  1249. c.xyzt.x = *x * METERS_in;
  1250. c.xyzt.y = *y * METERS_in;
  1251. c.xyzt.z = 0;
  1252. c.xyzt.t = 0;
  1253. c = proj_trans(info_trans.pj, PJ_FWD, c);
  1254. ok = proj_errno(info_trans.pj);
  1255. if (strncmp(info_out->proj, "ll", 2) == 0) {
  1256. /* convert to degrees */
  1257. *x = c.lp.lam * RAD_TO_DEG;
  1258. *y = c.lp.phi * RAD_TO_DEG;
  1259. }
  1260. else {
  1261. /* convert to map units */
  1262. *x = c.xy.x / METERS_out;
  1263. *y = c.xy.y / METERS_out;
  1264. }
  1265. }
  1266. proj_destroy(info_trans.pj);
  1267. if (ok < 0) {
  1268. G_warning(_("proj_trans() failed: %d"), ok);
  1269. }
  1270. #else
  1271. double u, v;
  1272. double h = 0.0;
  1273. METERS_in = info_in->meters;
  1274. METERS_out = info_out->meters;
  1275. if (strncmp(info_in->proj, "ll", 2) == 0) {
  1276. if (strncmp(info_out->proj, "ll", 2) == 0) {
  1277. u = (*x) / RAD_TO_DEG;
  1278. v = (*y) / RAD_TO_DEG;
  1279. ok = pj_transform(info_in->pj, info_out->pj, 1, 0, &u, &v, &h);
  1280. *x = u * RAD_TO_DEG;
  1281. *y = v * RAD_TO_DEG;
  1282. }
  1283. else {
  1284. u = (*x) / RAD_TO_DEG;
  1285. v = (*y) / RAD_TO_DEG;
  1286. ok = pj_transform(info_in->pj, info_out->pj, 1, 0, &u, &v, &h);
  1287. *x = u / METERS_out;
  1288. *y = v / METERS_out;
  1289. }
  1290. }
  1291. else {
  1292. if (strncmp(info_out->proj, "ll", 2) == 0) {
  1293. u = *x * METERS_in;
  1294. v = *y * METERS_in;
  1295. ok = pj_transform(info_in->pj, info_out->pj, 1, 0, &u, &v, &h);
  1296. *x = u * RAD_TO_DEG;
  1297. *y = v * RAD_TO_DEG;
  1298. }
  1299. else {
  1300. u = *x * METERS_in;
  1301. v = *y * METERS_in;
  1302. ok = pj_transform(info_in->pj, info_out->pj, 1, 0, &u, &v, &h);
  1303. *x = u / METERS_out;
  1304. *y = v / METERS_out;
  1305. }
  1306. }
  1307. if (ok < 0) {
  1308. G_warning(_("pj_transform() failed: %s"), pj_strerrno(ok));
  1309. }
  1310. #endif
  1311. return ok;
  1312. }
  1313. /**
  1314. * \brief Re-project an array of points between two co-ordinate systems with
  1315. * optional ellipsoidal height conversion
  1316. *
  1317. * This function takes pointers to two pj_info structures as arguments,
  1318. * and projects an array of points between the co-ordinate systems
  1319. * represented by them. Pointers to the three arrays of easting, northing,
  1320. * and ellipsoidal height of the point (this one may be NULL) are passed
  1321. * to the function; these will be overwritten by the co-ordinates of the
  1322. * re-projected points.
  1323. *
  1324. * \param count Number of points in the arrays to be transformed
  1325. * \param x Pointer to an array of type double containing easting or longitude
  1326. * \param y Pointer to an array of type double containing northing or latitude
  1327. * \param h Pointer to an array of type double containing ellipsoidal height.
  1328. * May be null in which case a two-dimensional re-projection will be
  1329. * done
  1330. * \param info_in pointer to pj_info struct for input co-ordinate system
  1331. * \param info_out pointer to pj_info struct for output co-ordinate system
  1332. *
  1333. * \return Return value from PROJ proj_trans() function
  1334. **/
  1335. int pj_do_transform(int count, double *x, double *y, double *h,
  1336. const struct pj_info *info_in, const struct pj_info *info_out)
  1337. {
  1338. int ok;
  1339. int i;
  1340. int has_h = 1;
  1341. #ifdef HAVE_PROJ_H
  1342. struct pj_info info_trans;
  1343. PJ_COORD c;
  1344. if (GPJ_init_transform(info_in, info_out, &info_trans) < 0) {
  1345. return -1;
  1346. }
  1347. METERS_in = info_in->meters;
  1348. METERS_out = info_out->meters;
  1349. if (h == NULL) {
  1350. h = G_malloc(sizeof *h * count);
  1351. /* they say memset is only guaranteed for chars ;-( */
  1352. for (i = 0; i < count; ++i)
  1353. h[i] = 0.0;
  1354. has_h = 0;
  1355. }
  1356. ok = 0;
  1357. if (strncmp(info_in->proj, "ll", 2) == 0) {
  1358. c.lpzt.t = 0;
  1359. if (strncmp(info_out->proj, "ll", 2) == 0) {
  1360. for (i = 0; i < count; i++) {
  1361. /* convert to radians */
  1362. c.lpzt.lam = x[i] / RAD_TO_DEG;
  1363. c.lpzt.phi = y[i] / RAD_TO_DEG;
  1364. c.lpzt.z = h[i];
  1365. c = proj_trans(info_trans.pj, PJ_FWD, c);
  1366. if ((ok = proj_errno(info_trans.pj)) < 0)
  1367. break;
  1368. /* convert to degrees */
  1369. x[i] = c.lp.lam * RAD_TO_DEG;
  1370. y[i] = c.lp.phi * RAD_TO_DEG;
  1371. }
  1372. }
  1373. else {
  1374. for (i = 0; i < count; i++) {
  1375. /* convert to radians */
  1376. c.lpzt.lam = x[i] / RAD_TO_DEG;
  1377. c.lpzt.phi = y[i] / RAD_TO_DEG;
  1378. c.lpzt.z = h[i];
  1379. c = proj_trans(info_trans.pj, PJ_FWD, c);
  1380. if ((ok = proj_errno(info_trans.pj)) < 0)
  1381. break;
  1382. /* convert to map units */
  1383. x[i] = c.xy.x / METERS_out;
  1384. y[i] = c.xy.y / METERS_out;
  1385. }
  1386. }
  1387. }
  1388. else {
  1389. c.xyzt.t = 0;
  1390. if (strncmp(info_out->proj, "ll", 2) == 0) {
  1391. for (i = 0; i < count; i++) {
  1392. /* convert to meters */
  1393. c.xyzt.x = x[i] * METERS_in;
  1394. c.xyzt.y = y[i] * METERS_in;
  1395. c.xyzt.z = h[i];
  1396. c = proj_trans(info_trans.pj, PJ_FWD, c);
  1397. if ((ok = proj_errno(info_trans.pj)) < 0)
  1398. break;
  1399. /* convert to degrees */
  1400. x[i] = c.lp.lam * RAD_TO_DEG;
  1401. y[i] = c.lp.phi * RAD_TO_DEG;
  1402. }
  1403. }
  1404. else {
  1405. for (i = 0; i < count; i++) {
  1406. /* convert to meters */
  1407. c.xyzt.x = x[i] * METERS_in;
  1408. c.xyzt.y = y[i] * METERS_in;
  1409. c.xyzt.z = h[i];
  1410. c = proj_trans(info_trans.pj, PJ_FWD, c);
  1411. if ((ok = proj_errno(info_trans.pj)) < 0)
  1412. break;
  1413. /* convert to map units */
  1414. x[i] = c.xy.x / METERS_out;
  1415. y[i] = c.xy.y / METERS_out;
  1416. }
  1417. }
  1418. }
  1419. if (!has_h)
  1420. G_free(h);
  1421. proj_destroy(info_trans.pj);
  1422. if (ok < 0) {
  1423. G_warning(_("proj_trans() failed: %d"), ok);
  1424. }
  1425. #else
  1426. METERS_in = info_in->meters;
  1427. METERS_out = info_out->meters;
  1428. if (h == NULL) {
  1429. h = G_malloc(sizeof *h * count);
  1430. /* they say memset is only guaranteed for chars ;-( */
  1431. for (i = 0; i < count; ++i)
  1432. h[i] = 0.0;
  1433. has_h = 0;
  1434. }
  1435. if (strncmp(info_in->proj, "ll", 2) == 0) {
  1436. if (strncmp(info_out->proj, "ll", 2) == 0) {
  1437. DIVIDE_LOOP(x, y, count, RAD_TO_DEG);
  1438. ok = pj_transform(info_in->pj, info_out->pj, count, 1, x, y, h);
  1439. MULTIPLY_LOOP(x, y, count, RAD_TO_DEG);
  1440. }
  1441. else {
  1442. DIVIDE_LOOP(x, y, count, RAD_TO_DEG);
  1443. ok = pj_transform(info_in->pj, info_out->pj, count, 1, x, y, h);
  1444. DIVIDE_LOOP(x, y, count, METERS_out);
  1445. }
  1446. }
  1447. else {
  1448. if (strncmp(info_out->proj, "ll", 2) == 0) {
  1449. MULTIPLY_LOOP(x, y, count, METERS_in);
  1450. ok = pj_transform(info_in->pj, info_out->pj, count, 1, x, y, h);
  1451. MULTIPLY_LOOP(x, y, count, RAD_TO_DEG);
  1452. }
  1453. else {
  1454. MULTIPLY_LOOP(x, y, count, METERS_in);
  1455. ok = pj_transform(info_in->pj, info_out->pj, count, 1, x, y, h);
  1456. DIVIDE_LOOP(x, y, count, METERS_out);
  1457. }
  1458. }
  1459. if (!has_h)
  1460. G_free(h);
  1461. if (ok < 0) {
  1462. G_warning(_("pj_transform() failed: %s"), pj_strerrno(ok));
  1463. }
  1464. #endif
  1465. return ok;
  1466. }