27 #include "local_proto.h"
30 #define CSVDIR "/etc/proj/ogr_csv"
32 static void DatumNameMassage(
char **);
44 {
"km",
"1000.",
"Kilometer", 1000.0},
45 {
"m",
"1.",
"Meter", 1.0},
46 {
"dm",
"1/10",
"Decimeter", 0.1},
47 {
"cm",
"1/100",
"Centimeter", 0.01},
48 {
"mm",
"1/1000",
"Millimeter", 0.001},
49 {
"kmi",
"1852.0",
"International Nautical Mile", 1852.0},
50 {
"in",
"0.0254",
"International Inch", 0.0254},
51 {
"ft",
"0.3048",
"International Foot", 0.3048},
52 {
"yd",
"0.9144",
"International Yard", 0.9144},
53 {
"mi",
"1609.344",
"International Statute Mile", 1609.344},
54 {
"fath",
"1.8288",
"International Fathom", 1.8288},
55 {
"ch",
"20.1168",
"International Chain", 20.1168},
56 {
"link",
"0.201168",
"International Link", 0.201168},
57 {
"us-in",
"1./39.37",
"U.S. Surveyor's Inch", 0.0254},
58 {
"us-ft",
"0.304800609601219",
"U.S. Surveyor's Foot", 0.304800609601219},
59 {
"us-yd",
"0.914401828803658",
"U.S. Surveyor's Yard", 0.914401828803658},
60 {
"us-ch",
"20.11684023368047",
"U.S. Surveyor's Chain", 20.11684023368047},
61 {
"us-mi",
"1609.347218694437",
"U.S. Surveyor's Statute Mile",
63 {
"ind-yd",
"0.91439523",
"Indian Yard", 0.91439523},
64 {
"ind-ft",
"0.30479841",
"Indian Foot", 0.30479841},
65 {
"ind-ch",
"20.11669506",
"Indian Chain", 20.11669506},
68 static char *grass_to_wkt(
const struct Key_Value *proj_info,
70 const struct Key_Value *proj_epsg,
int esri_style,
74 OGRSpatialReferenceH hSRS;
75 char *wkt, *local_wkt;
86 OSRExportToPrettyWkt(hSRS, &wkt, 0);
88 OSRExportToWkt(hSRS, &wkt);
92 OSRDestroySpatialReference(hSRS);
96 G_warning(
_(
"GRASS is not compiled with OGR support"));
120 const struct Key_Value *proj_units,
int esri_style,
123 return grass_to_wkt(proj_info, proj_units,
NULL, esri_style, prettify);
152 const struct Key_Value *proj_epsg,
int esri_style,
155 return grass_to_wkt(proj_info, proj_units, proj_epsg, esri_style, prettify);
172 char *proj4, *proj4mod, *
wkt, *modwkt, *startmod, *lastpart;
173 OGRSpatialReferenceH hSRS, hSRS2;
178 const char *ellpskv, *unit, *unfact;
179 char *ellps, *ellpslong, *datum, *params, *towgs84, *datumlongname, *start,
181 const char *sysname, *osrunit;
185 if ((proj_info ==
NULL) || (proj_units ==
NULL))
188 hSRS = OSRNewSpatialReference(
NULL);
191 if (
pj_get_kv(&pjinfo, proj_info, proj_units) < 0) {
192 G_warning(
_(
"Unable parse GRASS PROJ_INFO file"));
198 if ((proj4 = pjinfo.
def) ==
NULL) {
199 G_warning(
_(
"Unable get PROJ.4-style parameter string"));
203 proj_destroy(pjinfo.
pj);
210 if (unfact !=
NULL && (strcmp(pjinfo.
proj,
"ll") != 0))
211 G_asprintf(&proj4mod,
"%s +to_meter=%s", proj4, unfact);
216 if ((errcode = OSRImportFromProj4(hSRS, proj4mod)) != OGRERR_NONE) {
217 G_warning(
_(
"OGR can't parse PROJ.4-style parameter string: "
218 "%s (OGR Error code was %d)"),
230 if ((errcode = OSRExportToWkt(hSRS, &wkt)) != OGRERR_NONE) {
231 G_warning(
_(
"OGR can't get WKT-style parameter string "
232 "(OGR Error code was %d)"),
247 datumlongname =
G_store(
"unknown");
257 G_debug(3,
"GPJ_grass_to_osr: datum: <%s>", datum);
261 DatumNameMassage(&ellpslong);
267 startmod = strstr(wkt,
"GEOGCS");
268 lastpart = strstr(wkt,
"PRIMEM");
269 len = strlen(wkt) - strlen(startmod);
271 if (haveparams == 2) {
274 char *paramkey, *paramvalue;
276 paramkey = strtok(params,
"=");
277 paramvalue = params + strlen(paramkey) + 1;
279 G_asprintf(&towgs84,
",TOWGS84[%s]", paramvalue);
287 sysname = OSRGetAttrValue(hSRS,
"PROJCS", 0);
288 if (sysname ==
NULL) {
294 if ((strcmp(sysname,
"unnamed") == 0) &&
301 osrunit = OSRGetAttrValue(hSRS,
"UNIT", 0);
307 double unfactf = atof(unfact);
311 startmod = strstr(lastpart, buff);
312 len = strlen(lastpart) - strlen(startmod);
313 lastpart[len] =
'\0';
318 G_asprintf(&end,
",UNIT[\"%s\",%.16g]]", unit, unfactf);
321 OSRDestroySpatialReference(hSRS);
324 "%sGEOGCS[\"%s\",DATUM[\"%s\",SPHEROID[\"%s\",%.16g,%.16g]%s],%s%s",
325 start, ellps, datumlongname, ellpslong,
a,
rf, towgs84, lastpart, end);
326 hSRS2 = OSRNewSpatialReference(modwkt);
369 epsgcode = atoi(epsgstr);
374 OGRSpatialReferenceH hSRS;
376 hSRS = OSRNewSpatialReference(
NULL);
378 OSRImportFromEPSG(hSRS, epsgcode);
385 double df[] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
390 df[i] = atof(tokens[i]);
393 OSRSetTOWGS84(hSRS, df[0], df[1], df[2], df[3], df[4], df[5],
422 struct Key_Value **projunits, OGRSpatialReferenceH hSRS1,
425 struct Key_Value *temp_projinfo, *temp_projinfo_ext;
426 char *pszProj4 =
NULL, *pszRemaining;
427 char *pszProj =
NULL;
428 const char *pszProjCS =
NULL;
430 char *proj4_unit =
NULL;
433 OGRSpatialReferenceH hSRS;
434 int use_proj_extension;
449 OSRMorphFromESRI(hSRS);
452 use_proj_extension = 0;
455 ograttr = OSRGetAttrValue(hSRS,
"EXTENSION", 0);
456 if (ograttr && *ograttr && strcmp(ograttr,
"PROJ4") == 0) {
457 ograttr = OSRGetAttrValue(hSRS,
"EXTENSION", 1);
458 G_debug(3,
"proj4 extension:");
461 if (ograttr && *ograttr) {
463 OGRSpatialReferenceH hSRS2;
465 hSRS2 = OSRNewSpatialReference(
NULL);
469 if (OSRImportFromProj4(hSRS2, proj4ext) != OGRERR_NONE) {
470 G_warning(
_(
"Updating spatial reference with embedded proj4 "
471 "definition failed. "
472 "Proj4 definition: <%s>"),
474 OSRDestroySpatialReference(hSRS2);
481 G_warning(
_(
"Updating spatial reference with embedded proj4 "
489 pszProjCS = OSRGetAttrValue(hSRS,
"PROJCS", 0);
491 pszProjCS = OSRGetAttrValue(hSRS,
"GEOGCS", 0);
513 use_proj_extension = 1;
522 if (!OSRIsGeographic(hSRS) && !OSRIsProjected(hSRS))
528 if (OSRIsGeographic(hSRS)) {
532 else if (OSRGetUTMZone(hSRS, &bNorth) != 0) {
534 cellhd->
zone = OSRGetUTMZone(hSRS, &bNorth);
547 if (OSRExportToProj4(hSRS, &pszProj4) != OGRERR_NONE)
559 pszRemaining =
G_store(pszProj4);
561 pszProj4 = pszRemaining;
562 while ((pszRemaining = strstr(pszRemaining,
"+")) !=
NULL) {
563 char *pszToken, *pszValue;
568 pszToken = pszRemaining;
569 while (*pszRemaining !=
' ' && *pszRemaining !=
'\0')
572 if (*pszRemaining ==
' ') {
573 *pszRemaining =
'\0';
578 if (strstr(pszToken,
"=") !=
NULL) {
579 pszValue = strstr(pszToken,
"=");
584 pszValue =
"defined";
611 proj4_unit =
G_store(pszValue);
618 G_warning(
_(
"No projection name! Projection parameters likely to be "
625 pszProjCS = OSRGetAttrValue(hSRS,
"PROJCS", 0);
627 pszProjCS = OSRGetAttrValue(hSRS,
"GEOGCS", 0);
653 const char *pszDatumNameConst;
654 struct datum_list *
list, *listhead;
655 char *dum1, *dum2, *pszDatumName;
658 if (!use_proj_extension)
659 pszDatumNameConst = OSRGetAttrValue(hSRS,
"DATUM", 0);
663 if (pszDatumNameConst) {
667 pszDatumName =
G_store(pszDatumNameConst);
668 DatumNameMassage(&pszDatumName);
669 G_debug(3,
"GPJ_osr_to_grass: pszDatumNameConst: <%s>",
684 if (paramspresent < 2)
687 "Datum <%s> not recognised by GRASS and no "
694 if (paramspresent < 2) {
698 char *params, *chosenparams =
NULL;
706 "Datum <%s> apparently recognised by GRASS but "
707 "no parameters found. "
708 "You may want to look into this.",
710 else if (datumtrans > paramsets) {
714 "Invalid transformation number %d; valid range is "
716 "Leaving datum transform parameters unspecified.",
717 datumtrans, paramsets);
728 if (tlist->
count == datumtrans)
733 }
while (tlist !=
NULL);
737 if (chosenparams !=
NULL) {
738 char *paramkey, *paramvalue;
740 paramkey = strtok(chosenparams,
"=");
741 paramvalue = chosenparams + strlen(paramkey) + 1;
765 else if (!use_proj_extension) {
768 const char *pszSemiMajor = OSRGetAttrValue(hSRS,
"SPHEROID", 1);
769 const char *pszInvFlat = OSRGetAttrValue(hSRS,
"SPHEROID", 2);
771 if (pszSemiMajor !=
NULL && pszInvFlat !=
NULL) {
773 struct ellps_list *
list, *listhead;
774 double a = atof(pszSemiMajor), invflat = atof(pszInvFlat), flat;
784 es = flat * (2.0 - flat);
793 if ((a ==
list->a || fabs(a -
list->a) < 0.1 ||
794 fabs(1 - a /
list->a) < 0.0000001) &&
795 ((es == 0 &&
list->es == 0) ||
797 (invflat ==
list->rf ||
798 fabs(invflat -
list->rf) < 0.0000001))) {
804 if (listhead !=
NULL)
814 sprintf(es_str,
"%.16g", es);
824 else if (use_proj_extension) {
830 sprintf(parmstr,
"%.16g", a);
832 sprintf(parmstr,
"%.16g", es);
844 for (i = 0; i < temp_projinfo->
nitems; i++)
859 if (OSRIsGeographic(hSRS)) {
866 char szFormatBuf[256];
867 char *pszUnitsName =
NULL;
869 char *pszUnitsPlural, *pszStringEnd;
871 dfToMeters = OSRGetLinearUnits(hSRS, &pszUnitsName);
881 if ((
G_strcasecmp(pszUnitsName,
"unknown") == 0) && (dfToMeters == 1.))
889 if (dfToMeters != 1. && proj4_unit) {
894 if (strcmp(proj4_unit,
gpj_units[i].
id) == 0) {
906 pszUnitsPlural =
G_malloc(strlen(pszUnitsName) + 3);
907 strcpy(pszUnitsPlural, pszUnitsName);
908 pszStringEnd = pszUnitsPlural + strlen(pszUnitsPlural) - 4;
911 pszStringEnd[1] =
'e';
912 pszStringEnd[2] =
'e';
916 pszStringEnd[4] =
'e';
917 pszStringEnd[5] =
's';
918 pszStringEnd[6] =
'\0';
922 pszStringEnd[4] =
's';
923 pszStringEnd[5] =
'\0';
929 sprintf(szFormatBuf,
"%.16g", dfToMeters);
934 OSRDestroySpatialReference(hSRS);
942 if (cellhd !=
NULL) {
952 if (hSRS !=
NULL && hSRS != hSRS1)
953 OSRDestroySpatialReference(hSRS);
979 struct Key_Value **projunits,
const char *wkt,
989 OGRSpatialReferenceH hSRS;
994 hSRS = OSRNewSpatialReference(wkt);
997 OSRDestroySpatialReference(hSRS);
1013 static char *buf =
NULL;
1046 static const char *papszDatumEquiv[] = {
1047 "Militar_Geographische_Institute",
1048 "Militar_Geographische_Institut",
1049 "World_Geodetic_System_1984",
1051 "World_Geodetic_System_1972",
1053 "European_Terrestrial_Reference_System_89",
1054 "European_Terrestrial_Reference_System_1989",
1055 "European_Reference_System_1989",
1056 "European_Terrestrial_Reference_System_1989",
1058 "European_Terrestrial_Reference_System_1989",
1060 "European_Terrestrial_Reference_System_1989",
1062 "European_Terrestrial_Reference_System_1989",
1064 "New_Zealand_Geodetic_Datum_2000",
1069 "Campo_Inchauspe_1969",
1072 "System_Jednotne_Trigonometricke_Site_Katastralni",
1074 "Militar_Geographische_Institut",
1076 "Deutsches_Hauptdreiecksnetz",
1077 "Rauenberg_Datum_83",
1078 "Deutsches_Hauptdreiecksnetz",
1079 "South_American_1969",
1080 "South_American_Datum_1969",
1081 "International_Terrestrial_Reference_Frame_1992",
1095 static void DatumNameMassage(
char **ppszDatum)
1098 char *pszDatum = *ppszDatum;
1100 G_debug(3,
"DatumNameMassage: Raw string found <%s>", (
char *)pszDatum);
1104 for (i = 0; pszDatum[i] !=
'\0'; i++) {
1105 if (!(pszDatum[i] >=
'A' && pszDatum[i] <=
'Z') &&
1106 !(pszDatum[i] >=
'a' && pszDatum[i] <=
'z') &&
1107 !(pszDatum[i] >=
'0' && pszDatum[i] <=
'9')) {
1115 for (i = 1, j = 0; pszDatum[i] !=
'\0'; i++) {
1116 if (pszDatum[j] ==
'_' && pszDatum[i] ==
'_')
1119 pszDatum[++j] = pszDatum[i];
1121 if (pszDatum[j] ==
'_')
1124 pszDatum[j + 1] =
'\0';
1130 G_debug(3,
"DatumNameMassage: Search for datum equivalences of <%s>",
1132 for (i = 0; papszDatumEquiv[i] !=
NULL; i += 2) {
1133 if (
EQUAL(*ppszDatum, papszDatumEquiv[i])) {
1135 *ppszDatum =
G_store(papszDatumEquiv[i + 1]);
struct gpj_units gpj_units[]
OGRSpatialReferenceH GPJ_grass_to_osr2(const struct Key_Value *proj_info, const struct Key_Value *proj_units, const struct Key_Value *proj_epsg)
Converts a GRASS co-ordinate system to an OGRSpatialReferenceH object. EPSG code is preferred if avai...
const char * GPJ_set_csv_loc(const char *name)
int GPJ_osr_to_grass(struct Cell_head *cellhd, struct Key_Value **projinfo, struct Key_Value **projunits, OGRSpatialReferenceH hSRS1, int datumtrans)
Converts an OGRSpatialReferenceH object to a GRASS co-ordinate system.
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.
char * GPJ_grass_to_wkt2(const struct Key_Value *proj_info, const struct Key_Value *proj_units, const struct Key_Value *proj_epsg, int esri_style, int prettify)
Converts a GRASS co-ordinate system representation to WKT style. EPSG code is preferred if available.
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.
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.
void G_free(void *)
Free allocated memory.
void G_warning(const char *,...) __attribute__((format(printf
const char * G_find_key_value(const char *, const struct Key_Value *)
Find given key (case sensitive)
void G_free_key_value(struct Key_Value *)
Free allocated Key_Value structure.
void G_set_key_value(const char *, const char *, struct Key_Value *)
Set value for given key.
int G_lookup_key_value_from_file(const char *, const char *, char[], int)
Look up for key in file.
void G_free_tokens(char **)
Free memory allocated to tokens.
int G_asprintf(char **, const char *,...) __attribute__((format(printf
int G_number_of_tokens(char **)
Return number of tokens.
int int G_strcasecmp(const char *, const char *)
String compare ignoring case (upper or lower)
const char * G_gisbase(void)
Get full path name of the top level module directory.
struct Key_Value * G_create_key_value(void)
Allocate and initialize Key_Value structure.
int G_debug(int, const char *,...) __attribute__((format(printf
char * G_store(const char *)
Copy string to allocated memory.
char ** G_tokenize(const char *, const char *)
Tokenize string.
struct gpj_datum_transform_list * GPJ_get_datum_transform_by_name(const char *)
Internal function to find all possible sets of transformation parameters for a particular datum.
void GPJ_free_datum(struct gpj_datum *)
Free the memory used for the strings in a gpj_datum struct.
int GPJ_get_ellipsoid_by_name(const char *, struct gpj_ellps *)
Looks up ellipsoid in ellipsoid table and returns the a, e2 parameters for the ellipsoid.
int GPJ_get_datum_by_name(const char *, struct gpj_datum *)
Look up a string in datum.table file to see if it is a valid datum name and if so place its informati...
void GPJ_free_ellps(struct gpj_ellps *)
Free ellipsoid data structure.
int GPJ_get_default_datum_params_by_name(const char *, char **)
"Last resort" function to retrieve a "default" set of datum parameters for a datum (N....
int GPJ__get_datum_params(const struct Key_Value *, char **, char **)
Extract the datum transformation-related parameters from a set of general PROJ_INFO parameters.
int pj_get_kv(struct pj_info *, const struct Key_Value *, const struct Key_Value *)
Create a pj_info struct Co-ordinate System definition from a set of PROJ_INFO / PROJ_UNITS-style key-...
int GPJ__get_ellipsoid_params(const struct Key_Value *, double *, double *, double *)
Get the ellipsoid parameters from proj keys structure.
void GPJ_free_datum_transform(struct gpj_datum_transform_list *)
Free the memory used by a gpj_datum_transform_list struct.
struct ellps_list * read_ellipsoid_table(int fatal)
void free_ellps_list(struct ellps_list *elist)
#define PROJECTION_OTHER
Projection code - other projection (other then noted above)
#define PROJECTION_XY
Projection code - XY coordinate system (unreferenced data)
#define PROJECTION_UTM
Projection code - UTM.
#define PROJECTION_LL
Projection code - Latitude-Longitude.
void free_datum_list(struct datum_list *dstruct)
Free the memory used by a datum_list linked list structure.
struct datum_list * read_datum_table(void)
Read the current GRASS datum.table from disk and store in memory.
2D/3D raster map header (used also for region)
int zone
Projection zone (UTM)