convert.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. /*!
  2. \file lib/proj/convert.c
  3. \brief GProj Library - Functions for manipulating co-ordinate
  4. system representations
  5. (C) 2003-2008, 2012 by the GRASS Development Team
  6. This program is free software under the GNU General Public License
  7. (>=v2). Read the file COPYING that comes with GRASS for details.
  8. \author Paul Kelly, Frank Warmerdam
  9. */
  10. #include <grass/config.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <math.h>
  15. #include <grass/gis.h>
  16. #include <grass/gprojects.h>
  17. #include <grass/glocale.h>
  18. #ifdef HAVE_OGR
  19. #include <cpl_csv.h>
  20. #include "local_proto.h"
  21. /* GRASS relative location of OGR co-ordinate system lookup tables */
  22. #define CSVDIR "/etc/proj/ogr_csv"
  23. static void DatumNameMassage(char **);
  24. #endif
  25. /*!
  26. * \brief Converts a GRASS co-ordinate system representation to WKT style.
  27. *
  28. * Takes a GRASS co-ordinate system as specified by two sets of
  29. * key/value pairs derived from the PROJ_INFO and PROJ_UNITS files,
  30. * and converts it to the 'Well Known Text' format popularised by
  31. * proprietary GIS
  32. *
  33. * \param proj_info Set of GRASS PROJ_INFO key/value pairs
  34. * \param proj_units Set of GRASS PROJ_UNIT key/value pairs
  35. * \param esri_style boolean Output ESRI-style WKT (Use OSRMorphToESRI()
  36. * function provided by OGR library)
  37. * \param prettify boolean Use linebreaks and indents to 'prettify' output
  38. * WKT string (Use OSRExportToPrettyWkt() function in OGR)
  39. *
  40. * \return Pointer to a string containing the co-ordinate system in
  41. * WKT format
  42. * \return NULL on error
  43. */
  44. char *GPJ_grass_to_wkt(const struct Key_Value *proj_info,
  45. const struct Key_Value *proj_units,
  46. int esri_style, int prettify)
  47. {
  48. #ifdef HAVE_OGR
  49. OGRSpatialReferenceH hSRS;
  50. char *wkt, *local_wkt;
  51. hSRS = GPJ_grass_to_osr(proj_info, proj_units);
  52. if (hSRS == NULL)
  53. return NULL;
  54. if (esri_style)
  55. OSRMorphToESRI(hSRS);
  56. if (prettify)
  57. OSRExportToPrettyWkt(hSRS, &wkt, 0);
  58. else
  59. OSRExportToWkt(hSRS, &wkt);
  60. local_wkt = G_store(wkt);
  61. CPLFree(wkt);
  62. OSRDestroySpatialReference(hSRS);
  63. return local_wkt;
  64. #else
  65. G_warning(_("GRASS is not compiled with OGR support"));
  66. return NULL;
  67. #endif
  68. }
  69. #ifdef HAVE_OGR
  70. /*!
  71. * \brief Converts a GRASS co-ordinate system to an OGRSpatialReferenceH object.
  72. *
  73. * \param proj_info Set of GRASS PROJ_INFO key/value pairs
  74. * \param proj_units Set of GRASS PROJ_UNIT key/value pairs
  75. *
  76. * \return OGRSpatialReferenceH object representing the co-ordinate system
  77. * defined by proj_info and proj_units or NULL if it fails
  78. */
  79. OGRSpatialReferenceH GPJ_grass_to_osr(const struct Key_Value * proj_info,
  80. const struct Key_Value * proj_units)
  81. {
  82. struct pj_info pjinfo;
  83. char *proj4, *proj4mod, *wkt, *modwkt, *startmod, *lastpart;
  84. OGRSpatialReferenceH hSRS, hSRS2;
  85. OGRErr errcode;
  86. struct gpj_datum dstruct;
  87. struct gpj_ellps estruct;
  88. size_t len;
  89. const char *ellpskv, *unit, *unfact;
  90. char *ellps, *ellpslong, *datum, *params, *towgs84, *datumlongname,
  91. *start, *end;
  92. const char *sysname, *osrunit, *osrunfact;
  93. double a, es, rf;
  94. int haveparams = 0;
  95. if ((proj_info == NULL) || (proj_units == NULL))
  96. return NULL;
  97. hSRS = OSRNewSpatialReference(NULL);
  98. if (pj_get_kv(&pjinfo, proj_info, proj_units) < 0) {
  99. G_warning(_("Unable parse GRASS PROJ_INFO file"));
  100. return NULL;
  101. }
  102. if ((proj4 = pj_get_def(pjinfo.pj, 0)) == NULL) {
  103. G_warning(_("Unable get PROJ.4-style parameter string"));
  104. return NULL;
  105. }
  106. pj_free(pjinfo.pj);
  107. unit = G_find_key_value("unit", proj_units);
  108. unfact = G_find_key_value("meters", proj_units);
  109. if (unfact != NULL && (strcmp(pjinfo.proj, "ll") != 0))
  110. G_asprintf(&proj4mod, "%s +to_meter=%s", proj4, unfact);
  111. else
  112. proj4mod = G_store(proj4);
  113. pj_dalloc(proj4);
  114. if ((errcode = OSRImportFromProj4(hSRS, proj4mod)) != OGRERR_NONE) {
  115. G_warning(_("OGR can't parse PROJ.4-style parameter string: "
  116. "%s (OGR Error code was %d)"), proj4mod, errcode);
  117. return NULL;
  118. }
  119. G_free(proj4mod);
  120. if ((errcode = OSRExportToWkt(hSRS, &wkt)) != OGRERR_NONE) {
  121. G_warning(_("OGR can't get WKT-style parameter string "
  122. "(OGR Error code was %d)"), errcode);
  123. return NULL;
  124. }
  125. ellpskv = G_find_key_value("ellps", proj_info);
  126. GPJ__get_ellipsoid_params(proj_info, &a, &es, &rf);
  127. haveparams = GPJ__get_datum_params(proj_info, &datum, &params);
  128. if(ellpskv != NULL)
  129. ellps = G_store(ellpskv);
  130. else
  131. ellps = NULL;
  132. if ((datum == NULL) || (GPJ_get_datum_by_name(datum, &dstruct) < 0)) {
  133. datumlongname = G_store("unknown");
  134. if (ellps == NULL)
  135. ellps = G_store("unnamed");
  136. }
  137. else {
  138. datumlongname = G_store(dstruct.longname);
  139. if (ellps == NULL)
  140. ellps = G_store(dstruct.ellps);
  141. GPJ_free_datum(&dstruct);
  142. }
  143. G_free(datum);
  144. if (GPJ_get_ellipsoid_by_name(ellps, &estruct) > 0) {
  145. ellpslong = G_store(estruct.longname);
  146. DatumNameMassage(&ellpslong);
  147. GPJ_free_ellps(&estruct);
  148. }
  149. else
  150. ellpslong = G_store(ellps);
  151. startmod = strstr(wkt, "GEOGCS");
  152. lastpart = strstr(wkt, "PRIMEM");
  153. len = strlen(wkt) - strlen(startmod);
  154. wkt[len] = '\0';
  155. if (haveparams == 2) {
  156. /* Only put datum params into the WKT if they were specifically
  157. * specified in PROJ_INFO */
  158. char *paramkey, *paramvalue;
  159. paramkey = strtok(params, "=");
  160. paramvalue = params + strlen(paramkey) + 1;
  161. if (G_strcasecmp(paramkey, "towgs84") == 0)
  162. G_asprintf(&towgs84, ",TOWGS84[%s]", paramvalue);
  163. else
  164. towgs84 = G_store("");
  165. G_free(params);
  166. }
  167. else
  168. towgs84 = G_store("");
  169. sysname = OSRGetAttrValue(hSRS, "PROJCS", 0);
  170. if (sysname == NULL) {
  171. /* Not a projected co-ordinate system */
  172. start = G_store("");
  173. end = G_store("");
  174. }
  175. else {
  176. if ((strcmp(sysname, "unnamed") == 0) &&
  177. (G_find_key_value("name", proj_info) != NULL))
  178. G_asprintf(&start, "PROJCS[\"%s\",",
  179. G_find_key_value("name", proj_info));
  180. else
  181. start = G_store(wkt);
  182. osrunit = OSRGetAttrValue(hSRS, "UNIT", 0);
  183. osrunfact = OSRGetAttrValue(hSRS, "UNIT", 1);
  184. if ((unfact == NULL) || (G_strcasecmp(osrunit, "unknown") != 0))
  185. end = G_store("");
  186. else {
  187. char *buff;
  188. double unfactf = atof(unfact);
  189. G_asprintf(&buff, ",UNIT[\"%s\",", osrunit);
  190. startmod = strstr(lastpart, buff);
  191. len = strlen(lastpart) - strlen(startmod);
  192. lastpart[len] = '\0';
  193. G_free(buff);
  194. if (unit == NULL)
  195. unit = "unknown";
  196. G_asprintf(&end, ",UNIT[\"%s\",%.16g]]", unit, unfactf);
  197. }
  198. }
  199. OSRDestroySpatialReference(hSRS);
  200. G_asprintf(&modwkt,
  201. "%sGEOGCS[\"%s\",DATUM[\"%s\",SPHEROID[\"%s\",%.16g,%.16g]%s],%s%s",
  202. start, ellps, datumlongname, ellpslong, a, rf, towgs84,
  203. lastpart, end);
  204. hSRS2 = OSRNewSpatialReference(modwkt);
  205. G_free(modwkt);
  206. CPLFree(wkt);
  207. G_free(start);
  208. G_free(ellps);
  209. G_free(datumlongname);
  210. G_free(ellpslong);
  211. G_free(towgs84);
  212. G_free(end);
  213. return hSRS2;
  214. }
  215. /*!
  216. * \brief Converts an OGRSpatialReferenceH object to a GRASS co-ordinate system.
  217. *
  218. * \param cellhd Pointer to a GRASS Cell_head structure that will have its
  219. * projection-related members populated with appropriate values
  220. * \param projinfo Pointer to a pointer which will have a GRASS Key_Value
  221. * structure allocated containing a set of GRASS PROJ_INFO values
  222. * \param projunits Pointer to a pointer which will have a GRASS Key_Value
  223. * structure allocated containing a set of GRASS PROJ_UNITS values
  224. * \param hSRS OGRSpatialReferenceH object containing the co-ordinate
  225. * system to be converted
  226. * \param datumtrans Index number of datum parameter set to use, 0 to leave
  227. * unspecifed
  228. *
  229. * \return 2 if a projected or lat/long co-ordinate system has been
  230. * defined
  231. * \return 1 if an unreferenced XY co-ordinate system has
  232. * been defined
  233. */
  234. int GPJ_osr_to_grass(struct Cell_head *cellhd, struct Key_Value **projinfo,
  235. struct Key_Value **projunits, OGRSpatialReferenceH hSRS,
  236. int datumtrans)
  237. {
  238. struct Key_Value *temp_projinfo;
  239. char *pszProj4 = NULL, *pszRemaining;
  240. char *pszProj = NULL;
  241. char *datum = NULL;
  242. struct gpj_datum dstruct;
  243. if (hSRS == NULL)
  244. goto default_to_xy;
  245. /* Set finder function for locating OGR csv co-ordinate system tables */
  246. SetCSVFilenameHook(GPJ_set_csv_loc);
  247. /* Hopefully this doesn't do any harm if it wasn't in ESRI format
  248. * to start with... */
  249. OSRMorphFromESRI(hSRS);
  250. /* -------------------------------------------------------------------- */
  251. /* Set cellhd for well known coordinate systems. */
  252. /* -------------------------------------------------------------------- */
  253. if (!OSRIsGeographic(hSRS) && !OSRIsProjected(hSRS))
  254. goto default_to_xy;
  255. if (cellhd) {
  256. int bNorth;
  257. if (OSRIsGeographic(hSRS)) {
  258. cellhd->proj = PROJECTION_LL;
  259. cellhd->zone = 0;
  260. }
  261. else if (OSRGetUTMZone(hSRS, &bNorth) != 0) {
  262. cellhd->proj = PROJECTION_UTM;
  263. cellhd->zone = OSRGetUTMZone(hSRS, &bNorth);
  264. if (!bNorth)
  265. cellhd->zone *= -1;
  266. }
  267. else {
  268. cellhd->proj = PROJECTION_OTHER;
  269. cellhd->zone = 0;
  270. }
  271. }
  272. /* -------------------------------------------------------------------- */
  273. /* Get the coordinate system definition in PROJ.4 format. */
  274. /* -------------------------------------------------------------------- */
  275. if (OSRExportToProj4(hSRS, &pszProj4) != OGRERR_NONE)
  276. goto default_to_xy;
  277. /* -------------------------------------------------------------------- */
  278. /* Parse the PROJ.4 string into key/value pairs. Do a bit of */
  279. /* extra work to "GRASSify" the result. */
  280. /* -------------------------------------------------------------------- */
  281. temp_projinfo = G_create_key_value();
  282. /* Create "local" copy of proj4 string so we can modify and free it
  283. * using GRASS functions */
  284. pszRemaining = G_store(pszProj4);
  285. CPLFree(pszProj4);
  286. pszProj4 = pszRemaining;
  287. while ((pszRemaining = strstr(pszRemaining, "+")) != NULL) {
  288. char *pszToken, *pszValue;
  289. pszRemaining++;
  290. /* Advance pszRemaining to end of this token[=value] pair */
  291. pszToken = pszRemaining;
  292. while (*pszRemaining != ' ' && *pszRemaining != '\0')
  293. pszRemaining++;
  294. if (*pszRemaining == ' ') {
  295. *pszRemaining = '\0';
  296. pszRemaining++;
  297. }
  298. /* parse token, and value */
  299. if (strstr(pszToken, "=") != NULL) {
  300. pszValue = strstr(pszToken, "=");
  301. *pszValue = '\0';
  302. pszValue++;
  303. }
  304. else
  305. pszValue = "defined";
  306. if (G_strcasecmp(pszToken, "proj") == 0) {
  307. /* The ll projection is known as longlat in PROJ.4 */
  308. if (G_strcasecmp(pszValue, "longlat") == 0)
  309. pszValue = "ll";
  310. pszProj = pszValue;
  311. continue;
  312. }
  313. /* Ellipsoid and datum handled separately below */
  314. if (G_strcasecmp(pszToken, "ellps") == 0
  315. || G_strcasecmp(pszToken, "a") == 0
  316. || G_strcasecmp(pszToken, "b") == 0
  317. || G_strcasecmp(pszToken, "es") == 0
  318. || G_strcasecmp(pszToken, "rf") == 0
  319. || G_strcasecmp(pszToken, "datum") == 0)
  320. continue;
  321. /* We will handle units separately */
  322. if (G_strcasecmp(pszToken, "to_meter") == 0
  323. || G_strcasecmp(pszToken, "units") == 0)
  324. continue;
  325. G_set_key_value(pszToken, pszValue, temp_projinfo);
  326. }
  327. *projinfo = G_create_key_value();
  328. /* -------------------------------------------------------------------- */
  329. /* Derive the user name for the projection. */
  330. /* -------------------------------------------------------------------- */
  331. if (pszProj) {
  332. char path[4095];
  333. char name[80];
  334. sprintf(path, "%s/etc/proj/projections", G_gisbase());
  335. if (G_lookup_key_value_from_file(path, pszProj, name, sizeof(name)) >
  336. 0)
  337. G_set_key_value("name", name, *projinfo);
  338. else
  339. G_set_key_value("name", pszProj, *projinfo);
  340. G_set_key_value("proj", pszProj, *projinfo);
  341. }
  342. else
  343. G_warning(_("No projection name! Projection parameters likely to be meaningless."));
  344. /* -------------------------------------------------------------------- */
  345. /* Find the GRASS datum name and choose parameters either */
  346. /* interactively or not. */
  347. /* -------------------------------------------------------------------- */
  348. {
  349. const char *pszDatumNameConst = OSRGetAttrValue(hSRS, "DATUM", 0);
  350. struct datum_list *list, *listhead;
  351. char *dum1, *dum2, *pszDatumName;
  352. int paramspresent =
  353. GPJ__get_datum_params(temp_projinfo, &dum1, &dum2);
  354. if (pszDatumNameConst) {
  355. /* Need to make a new copy of the string so we don't mess
  356. * around with the memory inside the OGRSpatialReferenceH? */
  357. pszDatumName = G_store(pszDatumNameConst);
  358. DatumNameMassage(&pszDatumName);
  359. list = listhead = read_datum_table();
  360. while (list != NULL) {
  361. if (G_strcasecmp(pszDatumName, list->longname) == 0) {
  362. datum = G_store(list->name);
  363. break;
  364. }
  365. list = list->next;
  366. }
  367. free_datum_list(listhead);
  368. if (datum == NULL) {
  369. if (paramspresent < 2)
  370. /* Only give warning if no parameters present */
  371. G_warning(_("Datum <%s> not recognised by GRASS and no parameters found"),
  372. pszDatumName);
  373. }
  374. else {
  375. G_set_key_value("datum", datum, *projinfo);
  376. if (paramspresent < 2) {
  377. /* If no datum parameters were imported from the OSR
  378. * object then we should use the set specified by datumtrans */
  379. char *params, *chosenparams = NULL;
  380. int paramsets;
  381. paramsets =
  382. GPJ_get_default_datum_params_by_name(datum, &params);
  383. if (paramsets < 0)
  384. G_warning(_("Datum <%s> apparently recognised by GRASS but no parameters found. "
  385. "You may want to look into this."), datum);
  386. else if (datumtrans > paramsets) {
  387. G_warning(_("Invalid transformation number %d; valid range is 1 to %d. "
  388. "Leaving datum transform parameters unspecified."),
  389. datumtrans, paramsets);
  390. datumtrans = 0;
  391. }
  392. if (paramsets > 0) {
  393. struct gpj_datum_transform_list *list, *old;
  394. list = GPJ_get_datum_transform_by_name(datum);
  395. if (list != NULL) {
  396. do {
  397. if (list->count == datumtrans)
  398. chosenparams = G_store(list->params);
  399. old = list;
  400. list = list->next;
  401. GPJ_free_datum_transform(old);
  402. } while (list != NULL);
  403. }
  404. }
  405. if (chosenparams != NULL) {
  406. char *paramkey, *paramvalue;
  407. paramkey = strtok(chosenparams, "=");
  408. paramvalue = chosenparams + strlen(paramkey) + 1;
  409. G_set_key_value(paramkey, paramvalue, *projinfo);
  410. G_free(chosenparams);
  411. }
  412. if (paramsets > 0)
  413. G_free(params);
  414. }
  415. }
  416. G_free(pszDatumName);
  417. }
  418. }
  419. /* -------------------------------------------------------------------- */
  420. /* Determine an appropriate GRASS ellipsoid name if possible, or */
  421. /* else just put a and es values into PROJ_INFO */
  422. /* -------------------------------------------------------------------- */
  423. if ((datum != NULL) && (GPJ_get_datum_by_name(datum, &dstruct) > 0)) {
  424. /* Use ellps name associated with datum */
  425. G_set_key_value("ellps", dstruct.ellps, *projinfo);
  426. GPJ_free_datum(&dstruct);
  427. G_free(datum);
  428. }
  429. else {
  430. /* If we can't determine the ellipsoid from the datum, derive it
  431. * directly from "SPHEROID" parameters in WKT */
  432. const char *pszSemiMajor = OSRGetAttrValue(hSRS, "SPHEROID", 1);
  433. const char *pszInvFlat = OSRGetAttrValue(hSRS, "SPHEROID", 2);
  434. if (pszSemiMajor != NULL && pszInvFlat != NULL) {
  435. char *ellps = NULL;
  436. struct ellps_list *list, *listhead;
  437. double a = atof(pszSemiMajor), invflat = atof(pszInvFlat), flat;
  438. double es;
  439. /* Allow for incorrect WKT describing a sphere where InvFlat
  440. * is given as 0 rather than inf */
  441. if (invflat > 0)
  442. flat = 1 / invflat;
  443. else
  444. flat = 0;
  445. es = flat * (2.0 - flat);
  446. list = listhead = read_ellipsoid_table(0);
  447. while (list != NULL) {
  448. /* Try and match a and es against GRASS defined ellipsoids;
  449. * accept first one that matches. These numbers were found
  450. * by trial and error and could be fine-tuned, or possibly
  451. * a direct comparison of IEEE floating point values used. */
  452. if ((a == list->a || fabs(a - list->a) < 0.1 || fabs(1 - a / list->a) < 0.0000001) && ((es == 0 && list->es == 0) || /* Special case for sphere */
  453. (invflat == list->rf || fabs(invflat - list->rf) < 0.0000001))) {
  454. ellps = G_store(list->name);
  455. break;
  456. }
  457. list = list->next;
  458. }
  459. if (listhead != NULL)
  460. free_ellps_list(listhead);
  461. if (ellps == NULL) {
  462. /* If we weren't able to find a matching ellps name, set
  463. * a and es values directly from WKT-derived data */
  464. char es_str[100];
  465. G_set_key_value("a", (char *)pszSemiMajor, *projinfo);
  466. sprintf(es_str, "%.16g", es);
  467. G_set_key_value("es", es_str, *projinfo);
  468. }
  469. else {
  470. /* else specify the GRASS ellps name for readability */
  471. G_set_key_value("ellps", ellps, *projinfo);
  472. G_free(ellps);
  473. }
  474. }
  475. }
  476. /* -------------------------------------------------------------------- */
  477. /* Finally append the detailed projection parameters to the end */
  478. /* -------------------------------------------------------------------- */
  479. {
  480. int i;
  481. for (i = 0; i < temp_projinfo->nitems; i++)
  482. G_set_key_value(temp_projinfo->key[i],
  483. temp_projinfo->value[i], *projinfo);
  484. G_free_key_value(temp_projinfo);
  485. }
  486. G_free(pszProj4);
  487. /* -------------------------------------------------------------------- */
  488. /* Set the linear units. */
  489. /* -------------------------------------------------------------------- */
  490. *projunits = G_create_key_value();
  491. if (OSRIsGeographic(hSRS)) {
  492. /* We assume degrees ... someday we will be wrong! */
  493. G_set_key_value("unit", "degree", *projunits);
  494. G_set_key_value("units", "degrees", *projunits);
  495. G_set_key_value("meters", "1.0", *projunits);
  496. }
  497. else {
  498. char szFormatBuf[256];
  499. char *pszUnitsName = NULL;
  500. double dfToMeters;
  501. char *pszUnitsPlural, *pszStringEnd;
  502. dfToMeters = OSRGetLinearUnits(hSRS, &pszUnitsName);
  503. /* Workaround for the most obvious case when unit name is unknown */
  504. if ((G_strcasecmp(pszUnitsName, "unknown") == 0) &&
  505. (dfToMeters == 1.))
  506. G_asprintf(&pszUnitsName, "meter");
  507. if ((G_strcasecmp(pszUnitsName, "metre") == 0))
  508. G_asprintf(&pszUnitsName, "meter");
  509. if ((G_strcasecmp(pszUnitsName, "kilometre") == 0))
  510. G_asprintf(&pszUnitsName, "kilometer");
  511. G_set_key_value("unit", pszUnitsName, *projunits);
  512. /* Attempt at plural formation (WKT format doesn't store plural
  513. * form of unit name) */
  514. pszUnitsPlural = G_malloc(strlen(pszUnitsName) + 3);
  515. strcpy(pszUnitsPlural, pszUnitsName);
  516. pszStringEnd = pszUnitsPlural + strlen(pszUnitsPlural) - 4;
  517. if (G_strcasecmp(pszStringEnd, "foot") == 0) {
  518. /* Special case for foot - change two o's to e's */
  519. pszStringEnd[1] = 'e';
  520. pszStringEnd[2] = 'e';
  521. }
  522. else if (G_strcasecmp(pszStringEnd, "inch") == 0) {
  523. /* Special case for inch - add es */
  524. pszStringEnd[4] = 'e';
  525. pszStringEnd[5] = 's';
  526. pszStringEnd[6] = '\0';
  527. }
  528. else {
  529. /* For anything else add an s at the end */
  530. pszStringEnd[4] = 's';
  531. pszStringEnd[5] = '\0';
  532. }
  533. G_set_key_value("units", pszUnitsPlural, *projunits);
  534. G_free(pszUnitsPlural);
  535. sprintf(szFormatBuf, "%.16g", dfToMeters);
  536. G_set_key_value("meters", szFormatBuf, *projunits);
  537. }
  538. return 2;
  539. /* -------------------------------------------------------------------- */
  540. /* Fallback to returning an ungeoreferenced definition. */
  541. /* -------------------------------------------------------------------- */
  542. default_to_xy:
  543. if (cellhd != NULL) {
  544. cellhd->proj = PROJECTION_XY;
  545. cellhd->zone = 0;
  546. }
  547. *projinfo = NULL;
  548. *projunits = NULL;
  549. return 1;
  550. }
  551. #endif
  552. /*!
  553. * \brief Converts a WKT projection description to a GRASS co-ordinate system.
  554. *
  555. * \param cellhd Pointer to a GRASS Cell_head structure that will have its
  556. * projection-related members populated with appropriate values
  557. * \param projinfo Pointer to a pointer which will have a GRASS Key_Value
  558. * structure allocated containing a set of GRASS PROJ_INFO values
  559. * \param projunits Pointer to a pointer which will have a GRASS Key_Value
  560. * structure allocated containing a set of GRASS PROJ_UNITS values
  561. * \param wkt Well-known Text (WKT) description of the co-ordinate
  562. * system to be converted
  563. * \param datumtrans Index number of datum parameter set to use, 0 to leave
  564. * unspecifed
  565. *
  566. * \return 2 if a projected or lat/long co-ordinate system has been
  567. * defined
  568. * \return 1 if an unreferenced XY co-ordinate system has
  569. * been defined
  570. * \return -1 on error
  571. */
  572. int GPJ_wkt_to_grass(struct Cell_head *cellhd, struct Key_Value **projinfo,
  573. struct Key_Value **projunits, const char *wkt,
  574. int datumtrans)
  575. {
  576. #ifdef HAVE_OGR
  577. int retval;
  578. if (wkt == NULL)
  579. retval =
  580. GPJ_osr_to_grass(cellhd, projinfo, projunits, NULL, datumtrans);
  581. else {
  582. OGRSpatialReferenceH hSRS;
  583. /* Set finder function for locating OGR csv co-ordinate system tables */
  584. SetCSVFilenameHook(GPJ_set_csv_loc);
  585. hSRS = OSRNewSpatialReference(wkt);
  586. retval =
  587. GPJ_osr_to_grass(cellhd, projinfo, projunits, hSRS, datumtrans);
  588. OSRDestroySpatialReference(hSRS);
  589. }
  590. return retval;
  591. #else
  592. return -1;
  593. #endif
  594. }
  595. #ifdef HAVE_OGR
  596. /* GPJ_set_csv_loc()
  597. * 'finder function' for use with OGR SetCSVFilenameHook() function */
  598. const char *GPJ_set_csv_loc(const char *name)
  599. {
  600. const char *gisbase = G_gisbase();
  601. static char *buf = NULL;
  602. if (buf != NULL)
  603. G_free(buf);
  604. G_asprintf(&buf, "%s%s/%s", gisbase, CSVDIR, name);
  605. return buf;
  606. }
  607. /* The list below is only for files that use a non-standard name for a
  608. * datum that is already supported in GRASS. The number of entries must be even;
  609. * they are all in pairs. The first one in the pair is the non-standard name;
  610. * the second is the GRASS name. If a name appears more than once (as for
  611. * European_Terrestrial_Reference_System_1989) then it means there was more
  612. * than one non-standard name for it that needs to be accounted for.
  613. *
  614. * N.B. The order of these pairs is different from that in
  615. * ogr/ogrfromepsg.cpp in the GDAL source tree! GRASS uses the EPSG
  616. * names in its WKT representation except WGS_1984 and WGS_1972 as
  617. * these shortened versions seem to be standard
  618. */
  619. static const char *papszDatumEquiv[] = {
  620. "Militar_Geographische_Institute",
  621. "Militar_Geographische_Institut",
  622. "World_Geodetic_System_1984",
  623. "WGS_1984",
  624. "World_Geodetic_System_1972",
  625. "WGS_1972",
  626. "European_Terrestrial_Reference_System_89",
  627. "European_Terrestrial_Reference_System_1989",
  628. "European_Reference_System_1989",
  629. "European_Terrestrial_Reference_System_1989",
  630. "ETRS_1989",
  631. "European_Terrestrial_Reference_System_1989",
  632. "ETRS89",
  633. "European_Terrestrial_Reference_System_1989",
  634. "ETRF_1989",
  635. "European_Terrestrial_Reference_System_1989",
  636. "NZGD_2000",
  637. "New_Zealand_Geodetic_Datum_2000",
  638. "Monte_Mario_Rome",
  639. "Monte_Mario",
  640. "MONTROME",
  641. "Monte_Mario",
  642. "Campo_Inchauspe_1969",
  643. "Campo_Inchauspe",
  644. "S_JTSK_Ferro",
  645. "Militar_Geographische_Institut",
  646. "Potsdam_Datum_83",
  647. "Deutsches_Hauptdreiecksnetz",
  648. "South_American_1969",
  649. "South_American_Datum_1969",
  650. "ITRF_1992",
  651. "ITRF92",
  652. NULL
  653. };
  654. /************************************************************************/
  655. /* OGREPSGDatumNameMassage() */
  656. /* */
  657. /* Massage an EPSG datum name into WMT format. Also transform */
  658. /* specific exception cases into WKT versions. */
  659. /************************************************************************/
  660. static void DatumNameMassage(char **ppszDatum)
  661. {
  662. int i, j;
  663. char *pszDatum = *ppszDatum;
  664. /* -------------------------------------------------------------------- */
  665. /* Translate non-alphanumeric values to underscores. */
  666. /* -------------------------------------------------------------------- */
  667. for (i = 0; pszDatum[i] != '\0'; i++) {
  668. if (!(pszDatum[i] >= 'A' && pszDatum[i] <= 'Z')
  669. && !(pszDatum[i] >= 'a' && pszDatum[i] <= 'z')
  670. && !(pszDatum[i] >= '0' && pszDatum[i] <= '9')) {
  671. pszDatum[i] = '_';
  672. }
  673. }
  674. /* -------------------------------------------------------------------- */
  675. /* Remove repeated and trailing underscores. */
  676. /* -------------------------------------------------------------------- */
  677. for (i = 1, j = 0; pszDatum[i] != '\0'; i++) {
  678. if (pszDatum[j] == '_' && pszDatum[i] == '_')
  679. continue;
  680. pszDatum[++j] = pszDatum[i];
  681. }
  682. if (pszDatum[j] == '_')
  683. pszDatum[j] = '\0';
  684. else
  685. pszDatum[j + 1] = '\0';
  686. /* -------------------------------------------------------------------- */
  687. /* Search for datum equivalences. Specific massaged names get */
  688. /* mapped to OpenGIS specified names. */
  689. /* -------------------------------------------------------------------- */
  690. for (i = 0; papszDatumEquiv[i] != NULL; i += 2) {
  691. if (EQUAL(*ppszDatum, papszDatumEquiv[i])) {
  692. G_free(*ppszDatum);
  693. *ppszDatum = G_store(papszDatumEquiv[i + 1]);
  694. break;
  695. }
  696. }
  697. }
  698. #endif /* HAVE_OGR */