GRASS GIS 7 Programmer's Manual  7.0.4(2016)-r00000
convert.c
Go to the documentation of this file.
1 
16 #include <grass/config.h>
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <math.h>
22 #include <grass/gis.h>
23 #include <grass/gprojects.h>
24 #include <grass/glocale.h>
25 
26 #ifdef HAVE_OGR
27 #include <cpl_csv.h>
28 #include "local_proto.h"
29 
30 /* GRASS relative location of OGR co-ordinate system lookup tables */
31 #define CSVDIR "/etc/proj/ogr_csv"
32 
33 static void DatumNameMassage(char **);
34 #endif
35 
55 char *GPJ_grass_to_wkt(const struct Key_Value *proj_info,
56  const struct Key_Value *proj_units,
57  int esri_style, int prettify)
58 {
59 #ifdef HAVE_OGR
60  OGRSpatialReferenceH hSRS;
61  char *wkt, *local_wkt;
62 
63  hSRS = GPJ_grass_to_osr(proj_info, proj_units);
64 
65  if (hSRS == NULL)
66  return NULL;
67 
68  if (esri_style)
69  OSRMorphToESRI(hSRS);
70 
71  if (prettify)
72  OSRExportToPrettyWkt(hSRS, &wkt, 0);
73  else
74  OSRExportToWkt(hSRS, &wkt);
75 
76  local_wkt = G_store(wkt);
77  CPLFree(wkt);
78  OSRDestroySpatialReference(hSRS);
79 
80  return local_wkt;
81 #else
82  G_warning(_("GRASS is not compiled with OGR support"));
83  return NULL;
84 #endif
85 }
86 
87 #ifdef HAVE_OGR
88 
97 OGRSpatialReferenceH GPJ_grass_to_osr(const struct Key_Value * proj_info,
98  const struct Key_Value * proj_units)
99 {
100  struct pj_info pjinfo;
101  char *proj4, *proj4mod, *wkt, *modwkt, *startmod, *lastpart;
102  OGRSpatialReferenceH hSRS, hSRS2;
103  OGRErr errcode;
104  struct gpj_datum dstruct;
105  struct gpj_ellps estruct;
106  size_t len;
107  const char *ellpskv, *unit, *unfact;
108  char *ellps, *ellpslong, *datum, *params, *towgs84, *datumlongname,
109  *start, *end;
110  const char *sysname, *osrunit, *osrunfact;
111  double a, es, rf;
112  int haveparams = 0;
113 
114  if ((proj_info == NULL) || (proj_units == NULL))
115  return NULL;
116 
117  hSRS = OSRNewSpatialReference(NULL);
118 
119  if (pj_get_kv(&pjinfo, proj_info, proj_units) < 0) {
120  G_warning(_("Unable parse GRASS PROJ_INFO file"));
121  return NULL;
122  }
123 
124  if ((proj4 = pj_get_def(pjinfo.pj, 0)) == NULL) {
125  G_warning(_("Unable get PROJ.4-style parameter string"));
126  return NULL;
127  }
128  pj_free(pjinfo.pj);
129 
130  unit = G_find_key_value("unit", proj_units);
131  unfact = G_find_key_value("meters", proj_units);
132  if (unfact != NULL && (strcmp(pjinfo.proj, "ll") != 0))
133  G_asprintf(&proj4mod, "%s +to_meter=%s", proj4, unfact);
134  else
135  proj4mod = G_store(proj4);
136  pj_dalloc(proj4);
137 
138  if ((errcode = OSRImportFromProj4(hSRS, proj4mod)) != OGRERR_NONE) {
139  G_warning(_("OGR can't parse PROJ.4-style parameter string: "
140  "%s (OGR Error code was %d)"), proj4mod, errcode);
141  return NULL;
142  }
143  G_free(proj4mod);
144 
145  /* this messes up PROJCS versus GEOGCS!
146  sysname = G_find_key_value("name", proj_info);
147  if (sysname)
148  OSRSetProjCS(hSRS, sysname);
149  */
150 
151  if ((errcode = OSRExportToWkt(hSRS, &wkt)) != OGRERR_NONE) {
152  G_warning(_("OGR can't get WKT-style parameter string "
153  "(OGR Error code was %d)"), errcode);
154  return NULL;
155  }
156 
157  ellpskv = G_find_key_value("ellps", proj_info);
158  GPJ__get_ellipsoid_params(proj_info, &a, &es, &rf);
159  haveparams = GPJ__get_datum_params(proj_info, &datum, &params);
160 
161  if (ellpskv != NULL)
162  ellps = G_store(ellpskv);
163  else
164  ellps = NULL;
165 
166  if ((datum == NULL) || (GPJ_get_datum_by_name(datum, &dstruct) < 0)) {
167  datumlongname = G_store("unknown");
168  if (ellps == NULL)
169  ellps = G_store("unnamed");
170  }
171  else {
172  datumlongname = G_store(dstruct.longname);
173  if (ellps == NULL)
174  ellps = G_store(dstruct.ellps);
175  GPJ_free_datum(&dstruct);
176  }
177  G_free(datum);
178  if (GPJ_get_ellipsoid_by_name(ellps, &estruct) > 0) {
179  ellpslong = G_store(estruct.longname);
180  DatumNameMassage(&ellpslong);
181  GPJ_free_ellps(&estruct);
182  }
183  else
184  ellpslong = G_store(ellps);
185 
186  startmod = strstr(wkt, "GEOGCS");
187  lastpart = strstr(wkt, "PRIMEM");
188  len = strlen(wkt) - strlen(startmod);
189  wkt[len] = '\0';
190  if (haveparams == 2) {
191  /* Only put datum params into the WKT if they were specifically
192  * specified in PROJ_INFO */
193  char *paramkey, *paramvalue;
194 
195  paramkey = strtok(params, "=");
196  paramvalue = params + strlen(paramkey) + 1;
197  if (G_strcasecmp(paramkey, "towgs84") == 0)
198  G_asprintf(&towgs84, ",TOWGS84[%s]", paramvalue);
199  else
200  towgs84 = G_store("");
201  G_free(params);
202  }
203  else
204  towgs84 = G_store("");
205 
206  sysname = OSRGetAttrValue(hSRS, "PROJCS", 0);
207  if (sysname == NULL) {
208  /* Not a projected co-ordinate system */
209  start = G_store("");
210  end = G_store("");
211  }
212  else {
213  if ((strcmp(sysname, "unnamed") == 0) &&
214  (G_find_key_value("name", proj_info) != NULL))
215  G_asprintf(&start, "PROJCS[\"%s\",",
216  G_find_key_value("name", proj_info));
217  else
218  start = G_store(wkt);
219 
220  osrunit = OSRGetAttrValue(hSRS, "UNIT", 0);
221  osrunfact = OSRGetAttrValue(hSRS, "UNIT", 1);
222 
223  if ((unfact == NULL) || (G_strcasecmp(osrunit, "unknown") != 0))
224  end = G_store("");
225  else {
226  char *buff;
227  double unfactf = atof(unfact);
228 
229  G_asprintf(&buff, ",UNIT[\"%s\",", osrunit);
230 
231  startmod = strstr(lastpart, buff);
232  len = strlen(lastpart) - strlen(startmod);
233  lastpart[len] = '\0';
234  G_free(buff);
235 
236  if (unit == NULL)
237  unit = "unknown";
238  G_asprintf(&end, ",UNIT[\"%s\",%.16g]]", unit, unfactf);
239  }
240 
241  }
242  OSRDestroySpatialReference(hSRS);
243  G_asprintf(&modwkt,
244  "%sGEOGCS[\"%s\",DATUM[\"%s\",SPHEROID[\"%s\",%.16g,%.16g]%s],%s%s",
245  start, ellps, datumlongname, ellpslong, a, rf, towgs84,
246  lastpart, end);
247  hSRS2 = OSRNewSpatialReference(modwkt);
248  G_free(modwkt);
249 
250  CPLFree(wkt);
251  G_free(start);
252  G_free(ellps);
253  G_free(datumlongname);
254  G_free(ellpslong);
255  G_free(towgs84);
256  G_free(end);
257 
258  return hSRS2;
259 }
260 
280 int GPJ_osr_to_grass(struct Cell_head *cellhd, struct Key_Value **projinfo,
281  struct Key_Value **projunits, OGRSpatialReferenceH hSRS,
282  int datumtrans)
283 {
284  struct Key_Value *temp_projinfo;
285  char *pszProj4 = NULL, *pszRemaining;
286  char *pszProj = NULL;
287  const char *pszProjCS = NULL;
288  char *datum = NULL;
289  struct gpj_datum dstruct;
290 
291  if (hSRS == NULL)
292  goto default_to_xy;
293 
294  /* Set finder function for locating OGR csv co-ordinate system tables */
295  /* SetCSVFilenameHook(GPJ_set_csv_loc); */
296 
297  /* Hopefully this doesn't do any harm if it wasn't in ESRI format
298  * to start with... */
299  OSRMorphFromESRI(hSRS);
300 
301  /* -------------------------------------------------------------------- */
302  /* Set cellhd for well known coordinate systems. */
303  /* -------------------------------------------------------------------- */
304  if (!OSRIsGeographic(hSRS) && !OSRIsProjected(hSRS))
305  goto default_to_xy;
306 
307  if (cellhd) {
308  int bNorth;
309 
310  if (OSRIsGeographic(hSRS)) {
311  cellhd->proj = PROJECTION_LL;
312  cellhd->zone = 0;
313  }
314  else if (OSRGetUTMZone(hSRS, &bNorth) != 0) {
315  cellhd->proj = PROJECTION_UTM;
316  cellhd->zone = OSRGetUTMZone(hSRS, &bNorth);
317  if (!bNorth)
318  cellhd->zone *= -1;
319  }
320  else {
321  cellhd->proj = PROJECTION_OTHER;
322  cellhd->zone = 0;
323  }
324  }
325 
326  /* -------------------------------------------------------------------- */
327  /* Get the coordinate system definition in PROJ.4 format. */
328  /* -------------------------------------------------------------------- */
329  if (OSRExportToProj4(hSRS, &pszProj4) != OGRERR_NONE)
330  goto default_to_xy;
331 
332  /* -------------------------------------------------------------------- */
333  /* Parse the PROJ.4 string into key/value pairs. Do a bit of */
334  /* extra work to "GRASSify" the result. */
335  /* -------------------------------------------------------------------- */
336  temp_projinfo = G_create_key_value();
337 
338  /* Create "local" copy of proj4 string so we can modify and free it
339  * using GRASS functions */
340  pszRemaining = G_store(pszProj4);
341  CPLFree(pszProj4);
342  pszProj4 = pszRemaining;
343  while ((pszRemaining = strstr(pszRemaining, "+")) != NULL) {
344  char *pszToken, *pszValue;
345 
346  pszRemaining++;
347 
348  /* Advance pszRemaining to end of this token[=value] pair */
349  pszToken = pszRemaining;
350  while (*pszRemaining != ' ' && *pszRemaining != '\0')
351  pszRemaining++;
352 
353  if (*pszRemaining == ' ') {
354  *pszRemaining = '\0';
355  pszRemaining++;
356  }
357 
358  /* parse token, and value */
359  if (strstr(pszToken, "=") != NULL) {
360  pszValue = strstr(pszToken, "=");
361  *pszValue = '\0';
362  pszValue++;
363  }
364  else
365  pszValue = "defined";
366 
367  /* projection name */
368  if (G_strcasecmp(pszToken, "proj") == 0) {
369  /* The ll projection is known as longlat in PROJ.4 */
370  if (G_strcasecmp(pszValue, "longlat") == 0)
371  pszValue = "ll";
372 
373  pszProj = pszValue;
374  }
375 
376  /* Ellipsoid and datum handled separately below */
377  if (G_strcasecmp(pszToken, "ellps") == 0
378  || G_strcasecmp(pszToken, "a") == 0
379  || G_strcasecmp(pszToken, "b") == 0
380  || G_strcasecmp(pszToken, "es") == 0
381  || G_strcasecmp(pszToken, "rf") == 0
382  || G_strcasecmp(pszToken, "datum") == 0)
383  continue;
384 
385  /* We will handle units separately */
386  if (G_strcasecmp(pszToken, "to_meter") == 0
387  || G_strcasecmp(pszToken, "units") == 0)
388  continue;
389 
390  G_set_key_value(pszToken, pszValue, temp_projinfo);
391  }
392  if (!pszProj)
393  G_warning(_("No projection name! Projection parameters likely to be meaningless."));
394 
395  *projinfo = G_create_key_value();
396 
397  /* -------------------------------------------------------------------- */
398  /* Derive the user name for the coordinate system. */
399  /* -------------------------------------------------------------------- */
400  pszProjCS = OSRGetAttrValue(hSRS, "PROJCS", 0);
401  if (!pszProjCS)
402  pszProjCS = OSRGetAttrValue(hSRS, "GEOGCS", 0);
403 
404  if (pszProjCS) {
405  G_set_key_value("name", pszProjCS, *projinfo);
406  }
407  else if (pszProj) {
408  char path[4095];
409  char name[80];
410 
411  /* use name of the projection as name for the coordinate system */
412 
413  sprintf(path, "%s/etc/proj/projections", G_gisbase());
414  if (G_lookup_key_value_from_file(path, pszProj, name, sizeof(name)) >
415  0)
416  G_set_key_value("name", name, *projinfo);
417  else
418  G_set_key_value("name", pszProj, *projinfo);
419  }
420 
421 
422  /* -------------------------------------------------------------------- */
423  /* Find the GRASS datum name and choose parameters either */
424  /* interactively or not. */
425  /* -------------------------------------------------------------------- */
426 
427  {
428  const char *pszDatumNameConst = OSRGetAttrValue(hSRS, "DATUM", 0);
429  struct datum_list *list, *listhead;
430  char *dum1, *dum2, *pszDatumName;
431  int paramspresent =
432  GPJ__get_datum_params(temp_projinfo, &dum1, &dum2);
433 
434  if (pszDatumNameConst) {
435  /* Need to make a new copy of the string so we don't mess
436  * around with the memory inside the OGRSpatialReferenceH? */
437 
438  pszDatumName = G_store(pszDatumNameConst);
439  DatumNameMassage(&pszDatumName);
440 
441  list = listhead = read_datum_table();
442 
443  while (list != NULL) {
444  if (G_strcasecmp(pszDatumName, list->longname) == 0) {
445  datum = G_store(list->name);
446  break;
447  }
448  list = list->next;
449  }
450  free_datum_list(listhead);
451 
452  if (datum == NULL) {
453  if (paramspresent < 2)
454  /* Only give warning if no parameters present */
455  G_warning(_("Datum <%s> not recognised by GRASS and no parameters found"),
456  pszDatumName);
457  }
458  else {
459  G_set_key_value("datum", datum, *projinfo);
460 
461  if (paramspresent < 2) {
462  /* If no datum parameters were imported from the OSR
463  * object then we should use the set specified by datumtrans */
464  char *params, *chosenparams = NULL;
465  int paramsets;
466 
467  paramsets =
468  GPJ_get_default_datum_params_by_name(datum, &params);
469 
470  if (paramsets < 0)
471  G_warning(_("Datum <%s> apparently recognised by GRASS but no parameters found. "
472  "You may want to look into this."), datum);
473  else if (datumtrans > paramsets) {
474 
475  G_warning(_("Invalid transformation number %d; valid range is 1 to %d. "
476  "Leaving datum transform parameters unspecified."),
477  datumtrans, paramsets);
478  datumtrans = 0;
479  }
480 
481  if (paramsets > 0) {
482  struct gpj_datum_transform_list *list, *old;
483 
484  list = GPJ_get_datum_transform_by_name(datum);
485 
486  if (list != NULL) {
487  do {
488  if (list->count == datumtrans)
489  chosenparams = G_store(list->params);
490  old = list;
491  list = list->next;
493  } while (list != NULL);
494  }
495  }
496 
497  if (chosenparams != NULL) {
498  char *paramkey, *paramvalue;
499 
500  paramkey = strtok(chosenparams, "=");
501  paramvalue = chosenparams + strlen(paramkey) + 1;
502  G_set_key_value(paramkey, paramvalue, *projinfo);
503  G_free(chosenparams);
504  }
505 
506  if (paramsets > 0)
507  G_free(params);
508  }
509 
510  }
511  G_free(pszDatumName);
512  }
513  }
514 
515  /* -------------------------------------------------------------------- */
516  /* Determine an appropriate GRASS ellipsoid name if possible, or */
517  /* else just put a and es values into PROJ_INFO */
518  /* -------------------------------------------------------------------- */
519 
520  if ((datum != NULL) && (GPJ_get_datum_by_name(datum, &dstruct) > 0)) {
521  /* Use ellps name associated with datum */
522  G_set_key_value("ellps", dstruct.ellps, *projinfo);
523  GPJ_free_datum(&dstruct);
524  G_free(datum);
525  }
526  else {
527  /* If we can't determine the ellipsoid from the datum, derive it
528  * directly from "SPHEROID" parameters in WKT */
529  const char *pszSemiMajor = OSRGetAttrValue(hSRS, "SPHEROID", 1);
530  const char *pszInvFlat = OSRGetAttrValue(hSRS, "SPHEROID", 2);
531 
532  if (pszSemiMajor != NULL && pszInvFlat != NULL) {
533  char *ellps = NULL;
534  struct ellps_list *list, *listhead;
535  double a = atof(pszSemiMajor), invflat = atof(pszInvFlat), flat;
536  double es;
537 
538  /* Allow for incorrect WKT describing a sphere where InvFlat
539  * is given as 0 rather than inf */
540  if (invflat > 0)
541  flat = 1 / invflat;
542  else
543  flat = 0;
544 
545  es = flat * (2.0 - flat);
546 
547  list = listhead = read_ellipsoid_table(0);
548 
549  while (list != NULL) {
550  /* Try and match a and es against GRASS defined ellipsoids;
551  * accept first one that matches. These numbers were found
552  * by trial and error and could be fine-tuned, or possibly
553  * a direct comparison of IEEE floating point values used. */
554  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 */
555  (invflat == list->rf || fabs(invflat - list->rf) < 0.0000001))) {
556  ellps = G_store(list->name);
557  break;
558  }
559  list = list->next;
560  }
561  if (listhead != NULL)
562  free_ellps_list(listhead);
563 
564  if (ellps == NULL) {
565  /* If we weren't able to find a matching ellps name, set
566  * a and es values directly from WKT-derived data */
567  char es_str[100];
568 
569  G_set_key_value("a", (char *)pszSemiMajor, *projinfo);
570 
571  sprintf(es_str, "%.16g", es);
572  G_set_key_value("es", es_str, *projinfo);
573  }
574  else {
575  /* else specify the GRASS ellps name for readability */
576  G_set_key_value("ellps", ellps, *projinfo);
577  G_free(ellps);
578  }
579 
580  }
581 
582  }
583 
584  /* -------------------------------------------------------------------- */
585  /* Finally append the detailed projection parameters to the end */
586  /* -------------------------------------------------------------------- */
587 
588  {
589  int i;
590 
591  for (i = 0; i < temp_projinfo->nitems; i++)
592  G_set_key_value(temp_projinfo->key[i],
593  temp_projinfo->value[i], *projinfo);
594 
595  G_free_key_value(temp_projinfo);
596  }
597 
598  G_free(pszProj4);
599 
600  /* -------------------------------------------------------------------- */
601  /* Set the linear units. */
602  /* -------------------------------------------------------------------- */
603  *projunits = G_create_key_value();
604 
605  if (OSRIsGeographic(hSRS)) {
606  /* We assume degrees ... someday we will be wrong! */
607  G_set_key_value("unit", "degree", *projunits);
608  G_set_key_value("units", "degrees", *projunits);
609  G_set_key_value("meters", "1.0", *projunits);
610  }
611  else {
612  char szFormatBuf[256];
613  char *pszUnitsName = NULL;
614  double dfToMeters;
615  char *pszUnitsPlural, *pszStringEnd;
616 
617  dfToMeters = OSRGetLinearUnits(hSRS, &pszUnitsName);
618 
619  /* Workaround for the most obvious case when unit name is unknown */
620  if ((G_strcasecmp(pszUnitsName, "unknown") == 0) &&
621  (dfToMeters == 1.))
622  G_asprintf(&pszUnitsName, "meter");
623 
624  if ((G_strcasecmp(pszUnitsName, "metre") == 0))
625  G_asprintf(&pszUnitsName, "meter");
626  if ((G_strcasecmp(pszUnitsName, "kilometre") == 0))
627  G_asprintf(&pszUnitsName, "kilometer");
628 
629  G_set_key_value("unit", pszUnitsName, *projunits);
630 
631  /* Attempt at plural formation (WKT format doesn't store plural
632  * form of unit name) */
633  pszUnitsPlural = G_malloc(strlen(pszUnitsName) + 3);
634  strcpy(pszUnitsPlural, pszUnitsName);
635  pszStringEnd = pszUnitsPlural + strlen(pszUnitsPlural) - 4;
636  if (G_strcasecmp(pszStringEnd, "foot") == 0) {
637  /* Special case for foot - change two o's to e's */
638  pszStringEnd[1] = 'e';
639  pszStringEnd[2] = 'e';
640  }
641  else if (G_strcasecmp(pszStringEnd, "inch") == 0) {
642  /* Special case for inch - add es */
643  pszStringEnd[4] = 'e';
644  pszStringEnd[5] = 's';
645  pszStringEnd[6] = '\0';
646  }
647  else {
648  /* For anything else add an s at the end */
649  pszStringEnd[4] = 's';
650  pszStringEnd[5] = '\0';
651  }
652 
653  G_set_key_value("units", pszUnitsPlural, *projunits);
654  G_free(pszUnitsPlural);
655 
656  sprintf(szFormatBuf, "%.16g", dfToMeters);
657  G_set_key_value("meters", szFormatBuf, *projunits);
658 
659  }
660 
661  return 2;
662 
663  /* -------------------------------------------------------------------- */
664  /* Fallback to returning an ungeoreferenced definition. */
665  /* -------------------------------------------------------------------- */
666  default_to_xy:
667  if (cellhd != NULL) {
668  cellhd->proj = PROJECTION_XY;
669  cellhd->zone = 0;
670  }
671 
672  *projinfo = NULL;
673  *projunits = NULL;
674 
675  return 1;
676 }
677 #endif
678 
699 int GPJ_wkt_to_grass(struct Cell_head *cellhd, struct Key_Value **projinfo,
700  struct Key_Value **projunits, const char *wkt,
701  int datumtrans)
702 {
703 #ifdef HAVE_OGR
704  int retval;
705 
706  if (wkt == NULL)
707  retval =
708  GPJ_osr_to_grass(cellhd, projinfo, projunits, NULL, datumtrans);
709  else {
710  OGRSpatialReferenceH hSRS;
711 
712  /* Set finder function for locating OGR csv co-ordinate system tables */
713  /* SetCSVFilenameHook(GPJ_set_csv_loc); */
714 
715  hSRS = OSRNewSpatialReference(wkt);
716  retval =
717  GPJ_osr_to_grass(cellhd, projinfo, projunits, hSRS, datumtrans);
718  OSRDestroySpatialReference(hSRS);
719  }
720 
721  return retval;
722 #else
723  return -1;
724 #endif
725 }
726 
727 #ifdef HAVE_OGR
728 /* GPJ_set_csv_loc()
729  * 'finder function' for use with OGR SetCSVFilenameHook() function */
730 
731 const char *GPJ_set_csv_loc(const char *name)
732 {
733  const char *gisbase = G_gisbase();
734  static char *buf = NULL;
735 
736  if (buf != NULL)
737  G_free(buf);
738 
739  G_asprintf(&buf, "%s%s/%s", gisbase, CSVDIR, name);
740 
741  return buf;
742 }
743 
744 
745 /* The list below is only for files that use a non-standard name for a
746  * datum that is already supported in GRASS. The number of entries must be even;
747  * they are all in pairs. The first one in the pair is the non-standard name;
748  * the second is the GRASS/GDAL name. If a name appears more than once (as for
749  * European_Terrestrial_Reference_System_1989) then it means there was more
750  * than one non-standard name for it that needs to be accounted for.
751  *
752  * N.B. The order of these pairs is different from that in
753  * ogr/ogrfromepsg.cpp in the GDAL source tree! GRASS uses the EPSG
754  * names in its WKT representation except WGS_1984 and WGS_1972 as
755  * these shortened versions seem to be standard.
756  * Below order:
757  * the equivalent name comes first in the pair, and
758  * the EPSG name (as used in the GRASS datum.table file) comes second.
759  *
760  * The datum parameters are stored in
761  * ../gis/datum.table # 3 parameters
762  * ../gis/datumtransform.table # 7 parameters (requires entry in datum.table)
763  *
764  * Hint: use GDAL's "testepsg" to identify the canonical name, e.g.
765  * testepsg epsg:4674
766  */
767 
768 static const char *papszDatumEquiv[] = {
769  "Militar_Geographische_Institute",
770  "Militar_Geographische_Institut",
771  "World_Geodetic_System_1984",
772  "WGS_1984",
773  "World_Geodetic_System_1972",
774  "WGS_1972",
775  "European_Terrestrial_Reference_System_89",
776  "European_Terrestrial_Reference_System_1989",
777  "European_Reference_System_1989",
778  "European_Terrestrial_Reference_System_1989",
779  "ETRS_1989",
780  "European_Terrestrial_Reference_System_1989",
781  "ETRS89",
782  "European_Terrestrial_Reference_System_1989",
783  "ETRF_1989",
784  "European_Terrestrial_Reference_System_1989",
785  "NZGD_2000",
786  "New_Zealand_Geodetic_Datum_2000",
787  "Monte_Mario_Rome",
788  "Monte_Mario",
789  "MONTROME",
790  "Monte_Mario",
791  "Campo_Inchauspe_1969",
792  "Campo_Inchauspe",
793  "S_JTSK",
794  "System_Jednotne_Trigonometricke_Site_Katastralni",
795  "S_JTSK_Ferro",
796  "Militar_Geographische_Institut",
797  "Potsdam_Datum_83",
798  "Deutsches_Hauptdreiecksnetz",
799  "South_American_1969",
800  "South_American_Datum_1969",
801  "ITRF_1992",
802  "ITRF92",
803  NULL
804 };
805 
806 /************************************************************************/
807 /* OGREPSGDatumNameMassage() */
808 /* */
809 /* Massage an EPSG datum name into WMT format. Also transform */
810 /* specific exception cases into WKT versions. */
811 
812 /************************************************************************/
813 
814 static void DatumNameMassage(char **ppszDatum)
815 {
816  int i, j;
817  char *pszDatum = *ppszDatum;
818 
819  /* -------------------------------------------------------------------- */
820  /* Translate non-alphanumeric values to underscores. */
821  /* -------------------------------------------------------------------- */
822  for (i = 0; pszDatum[i] != '\0'; i++) {
823  if (!(pszDatum[i] >= 'A' && pszDatum[i] <= 'Z')
824  && !(pszDatum[i] >= 'a' && pszDatum[i] <= 'z')
825  && !(pszDatum[i] >= '0' && pszDatum[i] <= '9')) {
826  pszDatum[i] = '_';
827  }
828  }
829 
830  /* -------------------------------------------------------------------- */
831  /* Remove repeated and trailing underscores. */
832  /* -------------------------------------------------------------------- */
833  for (i = 1, j = 0; pszDatum[i] != '\0'; i++) {
834  if (pszDatum[j] == '_' && pszDatum[i] == '_')
835  continue;
836 
837  pszDatum[++j] = pszDatum[i];
838  }
839  if (pszDatum[j] == '_')
840  pszDatum[j] = '\0';
841  else
842  pszDatum[j + 1] = '\0';
843 
844  /* -------------------------------------------------------------------- */
845  /* Search for datum equivalences. Specific massaged names get */
846  /* mapped to OpenGIS specified names. */
847  /* -------------------------------------------------------------------- */
848  for (i = 0; papszDatumEquiv[i] != NULL; i += 2) {
849  if (EQUAL(*ppszDatum, papszDatumEquiv[i])) {
850  G_free(*ppszDatum);
851  *ppszDatum = G_store(papszDatumEquiv[i + 1]);
852  break;
853  }
854  }
855 }
856 
857 #endif /* HAVE_OGR */
int GPJ_wkt_to_grass(struct Cell_head *cellhd, struct Key_Value **projinfo, struct Key_Value **projunits, const char *wkt, int datumtrans)
Converts a WKT projection description to a GRASS co-ordinate system.
Definition: convert.c:699
int G_strcasecmp(const char *x, const char *y)
String compare ignoring case (upper or lower)
Definition: strings.c:46
const char * G_find_key_value(const char *key, const struct Key_Value *kv)
Find given key (case sensitive)
Definition: key_value1.c:84
void GPJ_free_datum(struct gpj_datum *dstruct)
Free the memory used for the strings in a gpj_datum struct.
Definition: proj/datum.c:395
int GPJ__get_datum_params(const struct Key_Value *projinfo, char **datumname, char **params)
Extract the datum transformation-related parameters from a set of general PROJ_INFO parameters...
Definition: proj/datum.c:173
void GPJ_free_datum_transform(struct gpj_datum_transform_list *item)
Free the memory used by a gpj_datum_transform_list struct.
Definition: proj/datum.c:318
struct ellps_list * read_ellipsoid_table(int fatal)
Definition: ellipse.c:201
int GPJ_osr_to_grass(struct Cell_head *cellhd, struct Key_Value **projinfo, struct Key_Value **projunits, OGRSpatialReferenceH hSRS, int datumtrans)
Converts an OGRSpatialReferenceH object to a GRASS co-ordinate system.
Definition: convert.c:280
char * GPJ_grass_to_wkt(const struct Key_Value *proj_info, const struct Key_Value *proj_units, int esri_style, int prettify)
Converts a GRASS co-ordinate system representation to WKT style.
Definition: convert.c:55
char * G_store(const char *s)
Copy string to allocated memory.
Definition: strings.c:86
int G_asprintf(char **out, const char *fmt,...)
Definition: asprintf.c:70
#define NULL
Definition: ccmath.h:32
struct gpj_datum_transform_list * GPJ_get_datum_transform_by_name(const char *inputname)
Internal function to find all possible sets of transformation parameters for a particular datum...
Definition: proj/datum.c:229
int GPJ_get_datum_by_name(const char *name, struct gpj_datum *dstruct)
Look up a string in datum.table file to see if it is a valid datum name and if so place its informati...
Definition: proj/datum.c:37
void G_free_key_value(struct Key_Value *kv)
Free allocated Key_Value structure.
Definition: key_value1.c:103
int GPJ__get_ellipsoid_params(const struct Key_Value *proj_keys, double *a, double *e2, double *rf)
Definition: ellipse.c:52
int G_lookup_key_value_from_file(const char *file, const char *key, char value[], int n)
Look up for key in file.
Definition: key_value4.c:48
#define EQUAL(a, b)
Definition: gsdrape.c:64
void free_datum_list(struct datum_list *dstruct)
Free the memory used by a datum_list linked list structure.
Definition: proj/datum.c:409
struct list * list
Definition: read_list.c:24
#define CSVDIR
Definition: convert.c:31
const char * GPJ_set_csv_loc(const char *name)
Definition: convert.c:731
void GPJ_free_ellps(struct gpj_ellps *estruct)
Definition: ellipse.c:281
OGRSpatialReferenceH GPJ_grass_to_osr(const struct Key_Value *proj_info, const struct Key_Value *proj_units)
Converts a GRASS co-ordinate system to an OGRSpatialReferenceH object.
Definition: convert.c:97
struct datum_list * read_datum_table(void)
Read the current GRASS datum.table from disk and store in memory.
Definition: proj/datum.c:337
int pj_get_kv(struct pj_info *info, const struct Key_Value *in_proj_keys, const struct Key_Value *in_units_keys)
Create a pj_info struct Co-ordinate System definition from a set of PROJ_INFO / PROJ_UNITS-style key-...
Definition: get_proj.c:59
int GPJ_get_default_datum_params_by_name(const char *name, char **params)
"Last resort" function to retrieve a "default" set of datum parameters for a datum (N...
Definition: proj/datum.c:85
Definition: path.h:16
void G_set_key_value(const char *key, const char *value, struct Key_Value *kv)
Set value for given key.
Definition: key_value1.c:38
int GPJ_get_ellipsoid_by_name(const char *name, struct gpj_ellps *estruct)
looks up ellipsoid in ellipsoid table and returns the a, e2 parameters for the ellipsoid ...
Definition: ellipse.c:136
const char * name
Definition: named_colr.c:7
void G_free(void *buf)
Free allocated memory.
Definition: alloc.c:149
const char * G_gisbase(void)
Get full path name of the top level module directory.
Definition: gisbase.c:41
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition: gis/error.c:203
struct Key_Value * G_create_key_value(void)
Allocate and initialize Key_Value structure.
Definition: key_value1.c:23
void free_ellps_list(struct ellps_list *elist)
Definition: ellipse.c:288