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);
145 if ((errcode = OSRExportToWkt(hSRS, &wkt)) != OGRERR_NONE) {
146 G_warning(_(
"OGR can't get WKT-style parameter string " 147 "(OGR Error code was %d)"), errcode);
161 datumlongname =
G_store(
"unknown");
166 datumlongname =
G_store(dstruct.longname);
168 ellps =
G_store(dstruct.ellps);
173 ellpslong =
G_store(estruct.longname);
174 DatumNameMassage(&ellpslong);
180 startmod = strstr(wkt,
"GEOGCS");
181 lastpart = strstr(wkt,
"PRIMEM");
182 len = strlen(wkt) - strlen(startmod);
184 if (haveparams == 2) {
187 char *paramkey, *paramvalue;
189 paramkey = strtok(params,
"=");
190 paramvalue = params + strlen(paramkey) + 1;
192 G_asprintf(&towgs84,
",TOWGS84[%s]", paramvalue);
200 sysname = OSRGetAttrValue(hSRS,
"PROJCS", 0);
201 if (sysname ==
NULL) {
207 if ((strcmp(sysname,
"unnamed") == 0) &&
214 osrunit = OSRGetAttrValue(hSRS,
"UNIT", 0);
215 osrunfact = OSRGetAttrValue(hSRS,
"UNIT", 1);
221 double unfactf = atof(unfact);
225 startmod = strstr(lastpart, buff);
226 len = strlen(lastpart) - strlen(startmod);
227 lastpart[len] =
'\0';
232 G_asprintf(&end,
",UNIT[\"%s\",%.16g]]", unit, unfactf);
236 OSRDestroySpatialReference(hSRS);
238 "%sGEOGCS[\"%s\",DATUM[\"%s\",SPHEROID[\"%s\",%.16g,%.16g]%s],%s%s",
239 start, ellps, datumlongname, ellpslong, a, rf, towgs84,
241 hSRS2 = OSRNewSpatialReference(modwkt);
275 struct Key_Value **projunits, OGRSpatialReferenceH hSRS,
278 struct Key_Value *temp_projinfo;
279 char *pszProj4 =
NULL, *pszRemaining;
280 char *pszProj =
NULL;
282 struct gpj_datum dstruct;
292 OSRMorphFromESRI(hSRS);
297 if (!OSRIsGeographic(hSRS) && !OSRIsProjected(hSRS))
303 if (OSRIsGeographic(hSRS)) {
304 cellhd->proj = PROJECTION_LL;
307 else if (OSRGetUTMZone(hSRS, &bNorth) != 0) {
308 cellhd->proj = PROJECTION_UTM;
309 cellhd->zone = OSRGetUTMZone(hSRS, &bNorth);
314 cellhd->proj = PROJECTION_OTHER;
322 if (OSRExportToProj4(hSRS, &pszProj4) != OGRERR_NONE)
333 pszRemaining =
G_store(pszProj4);
335 pszProj4 = pszRemaining;
336 while ((pszRemaining = strstr(pszRemaining,
"+")) !=
NULL) {
337 char *pszToken, *pszValue;
342 pszToken = pszRemaining;
343 while (*pszRemaining !=
' ' && *pszRemaining !=
'\0')
346 if (*pszRemaining ==
' ') {
347 *pszRemaining =
'\0';
352 if (strstr(pszToken,
"=") !=
NULL) {
353 pszValue = strstr(pszToken,
"=");
358 pszValue =
"defined";
396 sprintf(path,
"%s/etc/proj/projections",
G_gisbase());
406 G_warning(_(
"No projection name! Projection parameters likely to be meaningless."));
415 const char *pszDatumNameConst = OSRGetAttrValue(hSRS,
"DATUM", 0);
416 struct datum_list *
list, *listhead;
417 char *dum1, *dum2, *pszDatumName;
421 if (pszDatumNameConst) {
425 pszDatumName =
G_store(pszDatumNameConst);
426 DatumNameMassage(&pszDatumName);
430 while (list !=
NULL) {
440 if (paramspresent < 2)
442 G_warning(_(
"Datum <%s> not recognised by GRASS and no parameters found"),
448 if (paramspresent < 2) {
451 char *params, *chosenparams =
NULL;
458 G_warning(_(
"Datum <%s> apparently recognised by GRASS but no parameters found. " 459 "You may want to look into this."), datum);
460 else if (datumtrans > paramsets) {
462 G_warning(_(
"Invalid transformation number %d; valid range is 1 to %d. " 463 "Leaving datum transform parameters unspecified."),
464 datumtrans, paramsets);
469 struct gpj_datum_transform_list *
list, *old;
475 if (list->count == datumtrans)
476 chosenparams =
G_store(list->params);
480 }
while (list !=
NULL);
484 if (chosenparams !=
NULL) {
485 char *paramkey, *paramvalue;
487 paramkey = strtok(chosenparams,
"=");
488 paramvalue = chosenparams + strlen(paramkey) + 1;
516 const char *pszSemiMajor = OSRGetAttrValue(hSRS,
"SPHEROID", 1);
517 const char *pszInvFlat = OSRGetAttrValue(hSRS,
"SPHEROID", 2);
519 if (pszSemiMajor !=
NULL && pszInvFlat !=
NULL) {
521 struct ellps_list *
list, *listhead;
522 double a = atof(pszSemiMajor), invflat = atof(pszInvFlat), flat;
532 es = flat * (2.0 - flat);
536 while (list !=
NULL) {
541 if ((a == list->a || fabs(a - list->a) < 0.1 || fabs(1 - a / list->a) < 0.0000001) && ((es == 0 && list->es == 0) ||
542 (invflat == list->rf || fabs(invflat - list->rf) < 0.0000001))) {
548 if (listhead !=
NULL)
558 sprintf(es_str,
"%.16g", es);
578 for (i = 0; i < temp_projinfo->nitems; i++)
580 temp_projinfo->value[i], *projinfo);
592 if (OSRIsGeographic(hSRS)) {
599 char szFormatBuf[256];
600 char *pszUnitsName =
NULL;
602 char *pszUnitsPlural, *pszStringEnd;
604 dfToMeters = OSRGetLinearUnits(hSRS, &pszUnitsName);
620 pszUnitsPlural = G_malloc(strlen(pszUnitsName) + 3);
621 strcpy(pszUnitsPlural, pszUnitsName);
622 pszStringEnd = pszUnitsPlural + strlen(pszUnitsPlural) - 4;
625 pszStringEnd[1] =
'e';
626 pszStringEnd[2] =
'e';
630 pszStringEnd[4] =
'e';
631 pszStringEnd[5] =
's';
632 pszStringEnd[6] =
'\0';
636 pszStringEnd[4] =
's';
637 pszStringEnd[5] =
'\0';
643 sprintf(szFormatBuf,
"%.16g", dfToMeters);
654 if (cellhd !=
NULL) {
655 cellhd->proj = PROJECTION_XY;
687 struct Key_Value **projunits,
const char *wkt,
697 OGRSpatialReferenceH hSRS;
702 hSRS = OSRNewSpatialReference(wkt);
705 OSRDestroySpatialReference(hSRS);
721 static char *buf =
NULL;
745 static const char *papszDatumEquiv[] = {
746 "Militar_Geographische_Institute",
747 "Militar_Geographische_Institut",
748 "World_Geodetic_System_1984",
750 "World_Geodetic_System_1972",
752 "European_Terrestrial_Reference_System_89",
753 "European_Terrestrial_Reference_System_1989",
754 "European_Reference_System_1989",
755 "European_Terrestrial_Reference_System_1989",
757 "European_Terrestrial_Reference_System_1989",
759 "European_Terrestrial_Reference_System_1989",
761 "European_Terrestrial_Reference_System_1989",
763 "New_Zealand_Geodetic_Datum_2000",
768 "Campo_Inchauspe_1969",
771 "Militar_Geographische_Institut",
773 "Deutsches_Hauptdreiecksnetz",
774 "South_American_1969",
775 "South_American_Datum_1969",
789 static void DatumNameMassage(
char **ppszDatum)
792 char *pszDatum = *ppszDatum;
797 for (i = 0; pszDatum[i] !=
'\0'; i++) {
798 if (!(pszDatum[i] >=
'A' && pszDatum[i] <=
'Z')
799 && !(pszDatum[i] >=
'a' && pszDatum[i] <=
'z')
800 && !(pszDatum[i] >=
'0' && pszDatum[i] <=
'9')) {
808 for (i = 1, j = 0; pszDatum[i] !=
'\0'; i++) {
809 if (pszDatum[j] ==
'_' && pszDatum[i] ==
'_')
812 pszDatum[++j] = pszDatum[i];
814 if (pszDatum[j] ==
'_')
817 pszDatum[j + 1] =
'\0';
823 for (i = 0; papszDatumEquiv[i] !=
NULL; i += 2) {
824 if (
EQUAL(*ppszDatum, papszDatumEquiv[i])) {
826 *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)
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)
void GPJ_free_ellps(struct gpj_ellps *estruct)
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)