16 #include <grass/config.h> 22 #include <grass/gis.h> 23 #include <grass/gprojects.h> 24 #include <grass/glocale.h> 28 #include "local_proto.h" 31 #define CSVDIR "/etc/proj/ogr_csv" 33 static void DatumNameMassage(
char **);
56 const struct Key_Value *proj_units,
57 int esri_style,
int prettify)
60 OGRSpatialReferenceH hSRS;
61 char *wkt, *local_wkt;
72 OSRExportToPrettyWkt(hSRS, &wkt, 0);
74 OSRExportToWkt(hSRS, &wkt);
78 OSRDestroySpatialReference(hSRS);
82 G_warning(_(
"GRASS is not compiled with OGR support"));
98 const struct Key_Value * proj_units)
100 struct pj_info pjinfo;
101 char *proj4, *proj4mod, *wkt, *modwkt, *startmod, *lastpart;
102 OGRSpatialReferenceH hSRS, hSRS2;
104 struct gpj_datum dstruct;
105 struct gpj_ellps estruct;
107 const char *ellpskv, *unit, *unfact;
108 char *ellps, *ellpslong, *datum, *params, *towgs84, *datumlongname,
110 const char *sysname, *osrunit, *osrunfact;
114 if ((proj_info ==
NULL) || (proj_units ==
NULL))
117 hSRS = OSRNewSpatialReference(
NULL);
119 if (
pj_get_kv(&pjinfo, proj_info, proj_units) < 0) {
120 G_warning(_(
"Unable parse GRASS PROJ_INFO file"));
124 if ((proj4 = pj_get_def(pjinfo.pj, 0)) ==
NULL) {
125 G_warning(_(
"Unable get PROJ.4-style parameter string"));
132 if (unfact !=
NULL && (strcmp(pjinfo.proj,
"ll") != 0))
133 G_asprintf(&proj4mod,
"%s +to_meter=%s", proj4, unfact);
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);
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);
167 datumlongname =
G_store(
"unknown");
172 datumlongname =
G_store(dstruct.longname);
174 ellps =
G_store(dstruct.ellps);
177 G_debug(3,
"GPJ_grass_to_osr: datum: <%s>", datum);
180 ellpslong =
G_store(estruct.longname);
181 DatumNameMassage(&ellpslong);
187 startmod = strstr(wkt,
"GEOGCS");
188 lastpart = strstr(wkt,
"PRIMEM");
189 len = strlen(wkt) - strlen(startmod);
191 if (haveparams == 2) {
194 char *paramkey, *paramvalue;
196 paramkey = strtok(params,
"=");
197 paramvalue = params + strlen(paramkey) + 1;
199 G_asprintf(&towgs84,
",TOWGS84[%s]", paramvalue);
207 sysname = OSRGetAttrValue(hSRS,
"PROJCS", 0);
208 if (sysname ==
NULL) {
214 if ((strcmp(sysname,
"unnamed") == 0) &&
221 osrunit = OSRGetAttrValue(hSRS,
"UNIT", 0);
222 osrunfact = OSRGetAttrValue(hSRS,
"UNIT", 1);
228 double unfactf = atof(unfact);
232 startmod = strstr(lastpart, buff);
233 len = strlen(lastpart) - strlen(startmod);
234 lastpart[len] =
'\0';
239 G_asprintf(&end,
",UNIT[\"%s\",%.16g]]", unit, unfactf);
243 OSRDestroySpatialReference(hSRS);
245 "%sGEOGCS[\"%s\",DATUM[\"%s\",SPHEROID[\"%s\",%.16g,%.16g]%s],%s%s",
246 start, ellps, datumlongname, ellpslong, a, rf, towgs84,
248 hSRS2 = OSRNewSpatialReference(modwkt);
282 struct Key_Value **projunits, OGRSpatialReferenceH hSRS,
285 struct Key_Value *temp_projinfo;
286 char *pszProj4 =
NULL, *pszRemaining;
287 char *pszProj =
NULL;
288 const char *pszProjCS =
NULL;
290 struct gpj_datum dstruct;
300 OSRMorphFromESRI(hSRS);
305 if (!OSRIsGeographic(hSRS) && !OSRIsProjected(hSRS))
311 if (OSRIsGeographic(hSRS)) {
312 cellhd->proj = PROJECTION_LL;
315 else if (OSRGetUTMZone(hSRS, &bNorth) != 0) {
316 cellhd->proj = PROJECTION_UTM;
317 cellhd->zone = OSRGetUTMZone(hSRS, &bNorth);
322 cellhd->proj = PROJECTION_OTHER;
330 if (OSRExportToProj4(hSRS, &pszProj4) != OGRERR_NONE)
341 pszRemaining =
G_store(pszProj4);
343 pszProj4 = pszRemaining;
344 while ((pszRemaining = strstr(pszRemaining,
"+")) !=
NULL) {
345 char *pszToken, *pszValue;
350 pszToken = pszRemaining;
351 while (*pszRemaining !=
' ' && *pszRemaining !=
'\0')
354 if (*pszRemaining ==
' ') {
355 *pszRemaining =
'\0';
360 if (strstr(pszToken,
"=") !=
NULL) {
361 pszValue = strstr(pszToken,
"=");
366 pszValue =
"defined";
394 G_warning(_(
"No projection name! Projection parameters likely to be meaningless."));
401 pszProjCS = OSRGetAttrValue(hSRS,
"PROJCS", 0);
403 pszProjCS = OSRGetAttrValue(hSRS,
"GEOGCS", 0);
414 sprintf(path,
"%s/etc/proj/projections",
G_gisbase());
429 const char *pszDatumNameConst = OSRGetAttrValue(hSRS,
"DATUM", 0);
430 struct datum_list *
list, *listhead;
431 char *dum1, *dum2, *pszDatumName;
435 if (pszDatumNameConst) {
439 pszDatumName =
G_store(pszDatumNameConst);
440 DatumNameMassage(&pszDatumName);
441 G_debug(3,
"GPJ_osr_to_grass: pszDatumNameConst: <%s>", pszDatumName);
445 while (list !=
NULL) {
455 if (paramspresent < 2)
457 G_warning(_(
"Datum <%s> not recognised by GRASS and no parameters found"),
463 if (paramspresent < 2) {
466 char *params, *chosenparams =
NULL;
473 G_warning(_(
"Datum <%s> apparently recognised by GRASS but no parameters found. " 474 "You may want to look into this."), datum);
475 else if (datumtrans > paramsets) {
477 G_warning(_(
"Invalid transformation number %d; valid range is 1 to %d. " 478 "Leaving datum transform parameters unspecified."),
479 datumtrans, paramsets);
484 struct gpj_datum_transform_list *
list, *old;
490 if (list->count == datumtrans)
491 chosenparams =
G_store(list->params);
495 }
while (list !=
NULL);
499 if (chosenparams !=
NULL) {
500 char *paramkey, *paramvalue;
502 paramkey = strtok(chosenparams,
"=");
503 paramvalue = chosenparams + strlen(paramkey) + 1;
531 const char *pszSemiMajor = OSRGetAttrValue(hSRS,
"SPHEROID", 1);
532 const char *pszInvFlat = OSRGetAttrValue(hSRS,
"SPHEROID", 2);
534 if (pszSemiMajor !=
NULL && pszInvFlat !=
NULL) {
536 struct ellps_list *
list, *listhead;
537 double a = atof(pszSemiMajor), invflat = atof(pszInvFlat), flat;
547 es = flat * (2.0 - flat);
551 while (list !=
NULL) {
556 if ((a == list->a || fabs(a - list->a) < 0.1 || fabs(1 - a / list->a) < 0.0000001) && ((es == 0 && list->es == 0) ||
557 (invflat == list->rf || fabs(invflat - list->rf) < 0.0000001))) {
563 if (listhead !=
NULL)
573 sprintf(es_str,
"%.16g", es);
593 for (i = 0; i < temp_projinfo->nitems; i++)
595 temp_projinfo->value[i], *projinfo);
607 if (OSRIsGeographic(hSRS)) {
614 char szFormatBuf[256];
615 char *pszUnitsName =
NULL;
617 char *pszUnitsPlural, *pszStringEnd;
619 dfToMeters = OSRGetLinearUnits(hSRS, &pszUnitsName);
635 pszUnitsPlural = G_malloc(strlen(pszUnitsName) + 3);
636 strcpy(pszUnitsPlural, pszUnitsName);
637 pszStringEnd = pszUnitsPlural + strlen(pszUnitsPlural) - 4;
640 pszStringEnd[1] =
'e';
641 pszStringEnd[2] =
'e';
645 pszStringEnd[4] =
'e';
646 pszStringEnd[5] =
's';
647 pszStringEnd[6] =
'\0';
651 pszStringEnd[4] =
's';
652 pszStringEnd[5] =
'\0';
658 sprintf(szFormatBuf,
"%.16g", dfToMeters);
669 if (cellhd !=
NULL) {
670 cellhd->proj = PROJECTION_XY;
702 struct Key_Value **projunits,
const char *wkt,
712 OGRSpatialReferenceH hSRS;
717 hSRS = OSRNewSpatialReference(wkt);
720 OSRDestroySpatialReference(hSRS);
736 static char *buf =
NULL;
770 static const char *papszDatumEquiv[] = {
771 "Militar_Geographische_Institute",
772 "Militar_Geographische_Institut",
773 "World_Geodetic_System_1984",
775 "World_Geodetic_System_1972",
777 "European_Terrestrial_Reference_System_89",
778 "European_Terrestrial_Reference_System_1989",
779 "European_Reference_System_1989",
780 "European_Terrestrial_Reference_System_1989",
782 "European_Terrestrial_Reference_System_1989",
784 "European_Terrestrial_Reference_System_1989",
786 "European_Terrestrial_Reference_System_1989",
788 "New_Zealand_Geodetic_Datum_2000",
793 "Campo_Inchauspe_1969",
796 "System_Jednotne_Trigonometricke_Site_Katastralni",
798 "Militar_Geographische_Institut",
800 "Deutsches_Hauptdreiecksnetz",
801 "South_American_1969",
802 "South_American_Datum_1969",
816 static void DatumNameMassage(
char **ppszDatum)
819 char *pszDatum = *ppszDatum;
821 G_debug(3,
"DatumNameMassage: Raw string found <%s>", (
char *)pszDatum);
825 for (i = 0; pszDatum[i] !=
'\0'; i++) {
826 if (!(pszDatum[i] >=
'A' && pszDatum[i] <=
'Z')
827 && !(pszDatum[i] >=
'a' && pszDatum[i] <=
'z')
828 && !(pszDatum[i] >=
'0' && pszDatum[i] <=
'9')) {
836 for (i = 1, j = 0; pszDatum[i] !=
'\0'; i++) {
837 if (pszDatum[j] ==
'_' && pszDatum[i] ==
'_')
840 pszDatum[++j] = pszDatum[i];
842 if (pszDatum[j] ==
'_')
845 pszDatum[j + 1] =
'\0';
851 G_debug(3,
"DatumNameMassage: Search for datum equivalences of <%s>", (
char *)pszDatum);
852 for (i = 0; papszDatumEquiv[i] !=
NULL; i += 2) {
853 if (
EQUAL(*ppszDatum, papszDatumEquiv[i])) {
855 *ppszDatum =
G_store(papszDatumEquiv[i + 1]);
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.
int G_strcasecmp(const char *x, const char *y)
String compare ignoring case (upper or lower)
const char * G_find_key_value(const char *key, const struct Key_Value *kv)
Find given key (case sensitive)
void GPJ_free_datum(struct gpj_datum *dstruct)
Free the memory used for the strings in a gpj_datum struct.
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...
void GPJ_free_datum_transform(struct gpj_datum_transform_list *item)
Free the memory used by a gpj_datum_transform_list struct.
struct ellps_list * read_ellipsoid_table(int fatal)
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.
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.
char * G_store(const char *s)
Copy string to allocated memory.
int G_asprintf(char **out, const char *fmt,...)
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...
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...
void G_free_key_value(struct Key_Value *kv)
Free allocated Key_Value structure.
int GPJ__get_ellipsoid_params(const struct Key_Value *proj_keys, double *a, double *e2, double *rf)
Get the ellipsoid parameters from proj keys structure.
int G_lookup_key_value_from_file(const char *file, const char *key, char value[], int n)
Look up for key in file.
void free_datum_list(struct datum_list *dstruct)
Free the memory used by a datum_list linked list structure.
const char * GPJ_set_csv_loc(const char *name)
int G_debug(int level, const char *msg,...)
Print debugging message.
void GPJ_free_ellps(struct gpj_ellps *estruct)
Free ellipsoid data structure.
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.
struct datum_list * read_datum_table(void)
Read the current GRASS datum.table from disk and store in memory.
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-...
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...
void G_set_key_value(const char *key, const char *value, struct Key_Value *kv)
Set value for given key.
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.
void G_free(void *buf)
Free allocated memory.
const char * G_gisbase(void)
Get full path name of the top level module directory.
void G_warning(const char *msg,...)
Print a warning message to stderr.
struct Key_Value * G_create_key_value(void)
Allocate and initialize Key_Value structure.
void free_ellps_list(struct ellps_list *elist)