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);
179 ellpslong =
G_store(estruct.longname);
180 DatumNameMassage(&ellpslong);
186 startmod = strstr(wkt,
"GEOGCS");
187 lastpart = strstr(wkt,
"PRIMEM");
188 len = strlen(wkt) - strlen(startmod);
190 if (haveparams == 2) {
193 char *paramkey, *paramvalue;
195 paramkey = strtok(params,
"=");
196 paramvalue = params + strlen(paramkey) + 1;
198 G_asprintf(&towgs84,
",TOWGS84[%s]", paramvalue);
206 sysname = OSRGetAttrValue(hSRS,
"PROJCS", 0);
207 if (sysname ==
NULL) {
213 if ((strcmp(sysname,
"unnamed") == 0) &&
220 osrunit = OSRGetAttrValue(hSRS,
"UNIT", 0);
221 osrunfact = OSRGetAttrValue(hSRS,
"UNIT", 1);
227 double unfactf = atof(unfact);
231 startmod = strstr(lastpart, buff);
232 len = strlen(lastpart) - strlen(startmod);
233 lastpart[len] =
'\0';
238 G_asprintf(&end,
",UNIT[\"%s\",%.16g]]", unit, unfactf);
242 OSRDestroySpatialReference(hSRS);
244 "%sGEOGCS[\"%s\",DATUM[\"%s\",SPHEROID[\"%s\",%.16g,%.16g]%s],%s%s",
245 start, ellps, datumlongname, ellpslong, a, rf, towgs84,
247 hSRS2 = OSRNewSpatialReference(modwkt);
281 struct Key_Value **projunits, OGRSpatialReferenceH hSRS,
284 struct Key_Value *temp_projinfo;
285 char *pszProj4 =
NULL, *pszRemaining;
286 char *pszProj =
NULL;
287 const char *pszProjCS =
NULL;
289 struct gpj_datum dstruct;
299 OSRMorphFromESRI(hSRS);
304 if (!OSRIsGeographic(hSRS) && !OSRIsProjected(hSRS))
310 if (OSRIsGeographic(hSRS)) {
311 cellhd->proj = PROJECTION_LL;
314 else if (OSRGetUTMZone(hSRS, &bNorth) != 0) {
315 cellhd->proj = PROJECTION_UTM;
316 cellhd->zone = OSRGetUTMZone(hSRS, &bNorth);
321 cellhd->proj = PROJECTION_OTHER;
329 if (OSRExportToProj4(hSRS, &pszProj4) != OGRERR_NONE)
340 pszRemaining =
G_store(pszProj4);
342 pszProj4 = pszRemaining;
343 while ((pszRemaining = strstr(pszRemaining,
"+")) !=
NULL) {
344 char *pszToken, *pszValue;
349 pszToken = pszRemaining;
350 while (*pszRemaining !=
' ' && *pszRemaining !=
'\0')
353 if (*pszRemaining ==
' ') {
354 *pszRemaining =
'\0';
359 if (strstr(pszToken,
"=") !=
NULL) {
360 pszValue = strstr(pszToken,
"=");
365 pszValue =
"defined";
393 G_warning(_(
"No projection name! Projection parameters likely to be meaningless."));
400 pszProjCS = OSRGetAttrValue(hSRS,
"PROJCS", 0);
402 pszProjCS = OSRGetAttrValue(hSRS,
"GEOGCS", 0);
413 sprintf(path,
"%s/etc/proj/projections",
G_gisbase());
428 const char *pszDatumNameConst = OSRGetAttrValue(hSRS,
"DATUM", 0);
429 struct datum_list *
list, *listhead;
430 char *dum1, *dum2, *pszDatumName;
434 if (pszDatumNameConst) {
438 pszDatumName =
G_store(pszDatumNameConst);
439 DatumNameMassage(&pszDatumName);
443 while (list !=
NULL) {
453 if (paramspresent < 2)
455 G_warning(_(
"Datum <%s> not recognised by GRASS and no parameters found"),
461 if (paramspresent < 2) {
464 char *params, *chosenparams =
NULL;
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) {
475 G_warning(_(
"Invalid transformation number %d; valid range is 1 to %d. " 476 "Leaving datum transform parameters unspecified."),
477 datumtrans, paramsets);
482 struct gpj_datum_transform_list *
list, *old;
488 if (list->count == datumtrans)
489 chosenparams =
G_store(list->params);
493 }
while (list !=
NULL);
497 if (chosenparams !=
NULL) {
498 char *paramkey, *paramvalue;
500 paramkey = strtok(chosenparams,
"=");
501 paramvalue = chosenparams + strlen(paramkey) + 1;
529 const char *pszSemiMajor = OSRGetAttrValue(hSRS,
"SPHEROID", 1);
530 const char *pszInvFlat = OSRGetAttrValue(hSRS,
"SPHEROID", 2);
532 if (pszSemiMajor !=
NULL && pszInvFlat !=
NULL) {
534 struct ellps_list *
list, *listhead;
535 double a = atof(pszSemiMajor), invflat = atof(pszInvFlat), flat;
545 es = flat * (2.0 - flat);
549 while (list !=
NULL) {
554 if ((a == list->a || fabs(a - list->a) < 0.1 || fabs(1 - a / list->a) < 0.0000001) && ((es == 0 && list->es == 0) ||
555 (invflat == list->rf || fabs(invflat - list->rf) < 0.0000001))) {
561 if (listhead !=
NULL)
571 sprintf(es_str,
"%.16g", es);
591 for (i = 0; i < temp_projinfo->nitems; i++)
593 temp_projinfo->value[i], *projinfo);
605 if (OSRIsGeographic(hSRS)) {
612 char szFormatBuf[256];
613 char *pszUnitsName =
NULL;
615 char *pszUnitsPlural, *pszStringEnd;
617 dfToMeters = OSRGetLinearUnits(hSRS, &pszUnitsName);
633 pszUnitsPlural = G_malloc(strlen(pszUnitsName) + 3);
634 strcpy(pszUnitsPlural, pszUnitsName);
635 pszStringEnd = pszUnitsPlural + strlen(pszUnitsPlural) - 4;
638 pszStringEnd[1] =
'e';
639 pszStringEnd[2] =
'e';
643 pszStringEnd[4] =
'e';
644 pszStringEnd[5] =
's';
645 pszStringEnd[6] =
'\0';
649 pszStringEnd[4] =
's';
650 pszStringEnd[5] =
'\0';
656 sprintf(szFormatBuf,
"%.16g", dfToMeters);
667 if (cellhd !=
NULL) {
668 cellhd->proj = PROJECTION_XY;
700 struct Key_Value **projunits,
const char *wkt,
710 OGRSpatialReferenceH hSRS;
715 hSRS = OSRNewSpatialReference(wkt);
718 OSRDestroySpatialReference(hSRS);
734 static char *buf =
NULL;
768 static const char *papszDatumEquiv[] = {
769 "Militar_Geographische_Institute",
770 "Militar_Geographische_Institut",
771 "World_Geodetic_System_1984",
773 "World_Geodetic_System_1972",
775 "European_Terrestrial_Reference_System_89",
776 "European_Terrestrial_Reference_System_1989",
777 "European_Reference_System_1989",
778 "European_Terrestrial_Reference_System_1989",
780 "European_Terrestrial_Reference_System_1989",
782 "European_Terrestrial_Reference_System_1989",
784 "European_Terrestrial_Reference_System_1989",
786 "New_Zealand_Geodetic_Datum_2000",
791 "Campo_Inchauspe_1969",
794 "System_Jednotne_Trigonometricke_Site_Katastralni",
796 "Militar_Geographische_Institut",
798 "Deutsches_Hauptdreiecksnetz",
799 "South_American_1969",
800 "South_American_Datum_1969",
814 static void DatumNameMassage(
char **ppszDatum)
817 char *pszDatum = *ppszDatum;
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')) {
833 for (i = 1, j = 0; pszDatum[i] !=
'\0'; i++) {
834 if (pszDatum[j] ==
'_' && pszDatum[i] ==
'_')
837 pszDatum[++j] = pszDatum[i];
839 if (pszDatum[j] ==
'_')
842 pszDatum[j + 1] =
'\0';
848 for (i = 0; papszDatumEquiv[i] !=
NULL; i += 2) {
849 if (
EQUAL(*ppszDatum, papszDatumEquiv[i])) {
851 *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)