28 #include "local_proto.h" 31 #define CSVDIR "/etc/proj/ogr_csv" 33 static void DatumNameMassage(
char **);
46 {
"km",
"1000.",
"Kilometer", 1000.0},
47 {
"m",
"1.",
"Meter", 1.0},
48 {
"dm",
"1/10",
"Decimeter", 0.1},
49 {
"cm",
"1/100",
"Centimeter", 0.01},
50 {
"mm",
"1/1000",
"Millimeter", 0.001},
51 {
"kmi",
"1852.0",
"International Nautical Mile", 1852.0},
52 {
"in",
"0.0254",
"International Inch", 0.0254},
53 {
"ft",
"0.3048",
"International Foot", 0.3048},
54 {
"yd",
"0.9144",
"International Yard", 0.9144},
55 {
"mi",
"1609.344",
"International Statute Mile", 1609.344},
56 {
"fath",
"1.8288",
"International Fathom", 1.8288},
57 {
"ch",
"20.1168",
"International Chain", 20.1168},
58 {
"link",
"0.201168",
"International Link", 0.201168},
59 {
"us-in",
"1./39.37",
"U.S. Surveyor's Inch", 0.0254},
60 {
"us-ft",
"0.304800609601219",
"U.S. Surveyor's Foot", 0.304800609601219},
61 {
"us-yd",
"0.914401828803658",
"U.S. Surveyor's Yard", 0.914401828803658},
62 {
"us-ch",
"20.11684023368047",
"U.S. Surveyor's Chain", 20.11684023368047},
63 {
"us-mi",
"1609.347218694437",
"U.S. Surveyor's Statute Mile", 1609.347218694437},
64 {
"ind-yd",
"0.91439523",
"Indian Yard", 0.91439523},
65 {
"ind-ft",
"0.30479841",
"Indian Foot", 0.30479841},
66 {
"ind-ch",
"20.11669506",
"Indian Chain", 20.11669506},
70 static char *grass_to_wkt(
const struct Key_Value *proj_info,
73 int esri_style,
int prettify)
76 OGRSpatialReferenceH hSRS;
77 char *wkt, *local_wkt;
88 OSRExportToPrettyWkt(hSRS, &wkt, 0);
90 OSRExportToWkt(hSRS, &wkt);
94 OSRDestroySpatialReference(hSRS);
98 G_warning(
_(
"GRASS is not compiled with OGR support"));
123 int esri_style,
int prettify)
125 return grass_to_wkt(proj_info, proj_units,
NULL, esri_style, prettify);
155 int esri_style,
int prettify)
157 return grass_to_wkt(proj_info, proj_units, proj_epsg, esri_style, prettify);
174 char *proj4, *proj4mod, *
wkt, *modwkt, *startmod, *lastpart;
175 OGRSpatialReferenceH hSRS, hSRS2;
180 const char *ellpskv, *unit, *unfact;
181 char *ellps, *ellpslong, *datum, *params, *towgs84, *datumlongname,
183 const char *sysname, *osrunit, *osrunfact;
187 if ((proj_info ==
NULL) || (proj_units ==
NULL))
190 hSRS = OSRNewSpatialReference(
NULL);
193 if (
pj_get_kv(&pjinfo, proj_info, proj_units) < 0) {
194 G_warning(
_(
"Unable parse GRASS PROJ_INFO file"));
200 if ((proj4 = pjinfo.
def) ==
NULL) {
201 G_warning(
_(
"Unable get PROJ.4-style parameter string"));
205 proj_destroy(pjinfo.
pj);
212 if (unfact !=
NULL && (strcmp(pjinfo.
proj,
"ll") != 0))
213 G_asprintf(&proj4mod,
"%s +to_meter=%s", proj4, unfact);
218 if ((errcode = OSRImportFromProj4(hSRS, proj4mod)) != OGRERR_NONE) {
219 G_warning(
_(
"OGR can't parse PROJ.4-style parameter string: " 220 "%s (OGR Error code was %d)"), proj4mod, errcode);
231 if ((errcode = OSRExportToWkt(hSRS, &wkt)) != OGRERR_NONE) {
232 G_warning(
_(
"OGR can't get WKT-style parameter string " 233 "(OGR Error code was %d)"), errcode);
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);
302 osrunfact = OSRGetAttrValue(hSRS,
"UNIT", 1);
308 double unfactf = atof(unfact);
312 startmod = strstr(lastpart, buff);
313 len = strlen(lastpart) - strlen(startmod);
314 lastpart[len] =
'\0';
319 G_asprintf(&end,
",UNIT[\"%s\",%.16g]]", unit, unfactf);
323 OSRDestroySpatialReference(hSRS);
325 "%sGEOGCS[\"%s\",DATUM[\"%s\",SPHEROID[\"%s\",%.16g,%.16g]%s],%s%s",
326 start, ellps, datumlongname, ellpslong, a, rf, towgs84,
328 hSRS2 = OSRNewSpatialReference(modwkt);
370 epsgcode = atoi(epsgstr);
375 OGRSpatialReferenceH hSRS;
377 hSRS = OSRNewSpatialReference(
NULL);
379 OSRImportFromEPSG(hSRS, epsgcode);
386 double df[] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
391 df[i] = atof(tokens[i]);
394 OSRSetTOWGS84(hSRS, df[0], df[1], df[2], df[3], df[4], df[5], df[6]);
423 struct Key_Value **projunits, OGRSpatialReferenceH hSRS1,
426 struct Key_Value *temp_projinfo, *temp_projinfo_ext;
427 char *pszProj4 =
NULL, *pszRemaining;
428 char *pszProj =
NULL;
429 const char *pszProjCS =
NULL;
431 char *proj4_unit =
NULL;
434 OGRSpatialReferenceH hSRS;
435 int use_proj_extension;
450 OSRMorphFromESRI(hSRS);
453 use_proj_extension = 0;
456 ograttr = OSRGetAttrValue(hSRS,
"EXTENSION", 0);
457 if (ograttr && *ograttr && strcmp(ograttr,
"PROJ4") == 0) {
458 ograttr = OSRGetAttrValue(hSRS,
"EXTENSION", 1);
459 G_debug(3,
"proj4 extension:");
462 if (ograttr && *ograttr) {
464 OGRSpatialReferenceH hSRS2;
466 hSRS2 = OSRNewSpatialReference(
NULL);
470 if (OSRImportFromProj4(hSRS2, proj4ext) != OGRERR_NONE) {
471 G_warning(
_(
"Updating spatial reference with embedded proj4 definition failed. " 472 "Proj4 definition: <%s>"), proj4ext);
473 OSRDestroySpatialReference(hSRS2);
478 G_warning(
_(
"Updating spatial reference with embedded proj4 definition"));
483 pszProjCS = OSRGetAttrValue(hSRS,
"PROJCS", 0);
485 pszProjCS = OSRGetAttrValue(hSRS,
"GEOGCS", 0);
496 sprintf(path,
"%s/etc/proj/projections",
G_gisbase());
506 use_proj_extension = 1;
515 if (!OSRIsGeographic(hSRS) && !OSRIsProjected(hSRS))
521 if (OSRIsGeographic(hSRS)) {
525 else if (OSRGetUTMZone(hSRS, &bNorth) != 0) {
527 cellhd->
zone = OSRGetUTMZone(hSRS, &bNorth);
540 if (OSRExportToProj4(hSRS, &pszProj4) != OGRERR_NONE)
552 pszRemaining =
G_store(pszProj4);
554 pszProj4 = pszRemaining;
555 while ((pszRemaining = strstr(pszRemaining,
"+")) !=
NULL) {
556 char *pszToken, *pszValue;
561 pszToken = pszRemaining;
562 while (*pszRemaining !=
' ' && *pszRemaining !=
'\0')
565 if (*pszRemaining ==
' ') {
566 *pszRemaining =
'\0';
571 if (strstr(pszToken,
"=") !=
NULL) {
572 pszValue = strstr(pszToken,
"=");
577 pszValue =
"defined";
604 proj4_unit =
G_store(pszValue);
611 G_warning(
_(
"No projection name! Projection parameters likely to be meaningless."));
617 pszProjCS = OSRGetAttrValue(hSRS,
"PROJCS", 0);
619 pszProjCS = OSRGetAttrValue(hSRS,
"GEOGCS", 0);
630 sprintf(path,
"%s/etc/proj/projections",
G_gisbase());
645 const char *pszDatumNameConst;
646 struct datum_list *
list, *listhead;
647 char *dum1, *dum2, *pszDatumName;
651 if (!use_proj_extension)
652 pszDatumNameConst = OSRGetAttrValue(hSRS,
"DATUM", 0);
656 if (pszDatumNameConst) {
660 pszDatumName =
G_store(pszDatumNameConst);
661 DatumNameMassage(&pszDatumName);
662 G_debug(3,
"GPJ_osr_to_grass: pszDatumNameConst: <%s>", pszDatumName);
666 while (list !=
NULL) {
676 if (paramspresent < 2)
678 G_debug(1,
"Datum <%s> not recognised by GRASS and no parameters found",
684 if (paramspresent < 2) {
687 char *params, *chosenparams =
NULL;
694 G_debug(1,
"Datum <%s> apparently recognised by GRASS but no parameters found. " 695 "You may want to look into this.", datum);
696 else if (datumtrans > paramsets) {
698 G_debug(1,
"Invalid transformation number %d; valid range is 1 to %d. " 699 "Leaving datum transform parameters unspecified.",
700 datumtrans, paramsets);
711 if (tlist->
count == datumtrans)
716 }
while (tlist !=
NULL);
720 if (chosenparams !=
NULL) {
721 char *paramkey, *paramvalue;
723 paramkey = strtok(chosenparams,
"=");
724 paramvalue = chosenparams + strlen(paramkey) + 1;
749 else if (!use_proj_extension) {
752 const char *pszSemiMajor = OSRGetAttrValue(hSRS,
"SPHEROID", 1);
753 const char *pszInvFlat = OSRGetAttrValue(hSRS,
"SPHEROID", 2);
755 if (pszSemiMajor !=
NULL && pszInvFlat !=
NULL) {
757 struct ellps_list *
list, *listhead;
758 double a = atof(pszSemiMajor), invflat = atof(pszInvFlat), flat;
768 es = flat * (2.0 - flat);
772 while (list !=
NULL) {
777 if ((a == list->a || fabs(a - list->a) < 0.1 || fabs(1 - a / list->a) < 0.0000001) &&
778 ((es == 0 && list->es == 0) ||
780 (invflat == list->rf || fabs(invflat - list->rf) < 0.0000001))) {
786 if (listhead !=
NULL)
796 sprintf(es_str,
"%.16g", es);
808 else if (use_proj_extension) {
814 sprintf(parmstr,
"%.16g", a);
816 sprintf(parmstr,
"%.16g", es);
828 for (i = 0; i < temp_projinfo->
nitems; i++)
830 temp_projinfo->
value[i], *projinfo);
843 if (OSRIsGeographic(hSRS)) {
850 char szFormatBuf[256];
851 char *pszUnitsName =
NULL;
853 char *pszUnitsPlural, *pszStringEnd;
855 dfToMeters = OSRGetLinearUnits(hSRS, &pszUnitsName);
874 if (dfToMeters != 1. && proj4_unit) {
878 while (gpj_units[i].
id !=
NULL) {
879 if (strcmp(proj4_unit, gpj_units[i].
id) == 0) {
891 pszUnitsPlural =
G_malloc(strlen(pszUnitsName) + 3);
892 strcpy(pszUnitsPlural, pszUnitsName);
893 pszStringEnd = pszUnitsPlural + strlen(pszUnitsPlural) - 4;
896 pszStringEnd[1] =
'e';
897 pszStringEnd[2] =
'e';
901 pszStringEnd[4] =
'e';
902 pszStringEnd[5] =
's';
903 pszStringEnd[6] =
'\0';
907 pszStringEnd[4] =
's';
908 pszStringEnd[5] =
'\0';
914 sprintf(szFormatBuf,
"%.16g", dfToMeters);
920 OSRDestroySpatialReference(hSRS);
928 if (cellhd !=
NULL) {
938 if (hSRS !=
NULL && hSRS != hSRS1)
939 OSRDestroySpatialReference(hSRS);
966 struct Key_Value **projunits,
const char *wkt,
976 OGRSpatialReferenceH hSRS;
981 hSRS = OSRNewSpatialReference(wkt);
984 OSRDestroySpatialReference(hSRS);
1000 static char *buf =
NULL;
1034 static const char *papszDatumEquiv[] = {
1035 "Militar_Geographische_Institute",
1036 "Militar_Geographische_Institut",
1037 "World_Geodetic_System_1984",
1039 "World_Geodetic_System_1972",
1041 "European_Terrestrial_Reference_System_89",
1042 "European_Terrestrial_Reference_System_1989",
1043 "European_Reference_System_1989",
1044 "European_Terrestrial_Reference_System_1989",
1046 "European_Terrestrial_Reference_System_1989",
1048 "European_Terrestrial_Reference_System_1989",
1050 "European_Terrestrial_Reference_System_1989",
1052 "New_Zealand_Geodetic_Datum_2000",
1057 "Campo_Inchauspe_1969",
1060 "System_Jednotne_Trigonometricke_Site_Katastralni",
1062 "Militar_Geographische_Institut",
1064 "Deutsches_Hauptdreiecksnetz",
1065 "Rauenberg_Datum_83",
1066 "Deutsches_Hauptdreiecksnetz",
1067 "South_American_1969",
1068 "South_American_Datum_1969",
1069 "International_Terrestrial_Reference_Frame_1992",
1084 static void DatumNameMassage(
char **ppszDatum)
1087 char *pszDatum = *ppszDatum;
1089 G_debug(3,
"DatumNameMassage: Raw string found <%s>", (
char *)pszDatum);
1093 for (i = 0; pszDatum[i] !=
'\0'; i++) {
1094 if (!(pszDatum[i] >=
'A' && pszDatum[i] <=
'Z')
1095 && !(pszDatum[i] >=
'a' && pszDatum[i] <=
'z')
1096 && !(pszDatum[i] >=
'0' && pszDatum[i] <=
'9')) {
1104 for (i = 1, j = 0; pszDatum[i] !=
'\0'; i++) {
1105 if (pszDatum[j] ==
'_' && pszDatum[i] ==
'_')
1108 pszDatum[++j] = pszDatum[i];
1110 if (pszDatum[j] ==
'_')
1113 pszDatum[j + 1] =
'\0';
1119 G_debug(3,
"DatumNameMassage: Search for datum equivalences of <%s>", (
char *)pszDatum);
1120 for (i = 0; papszDatumEquiv[i] !=
NULL; i += 2) {
1121 if (
EQUAL(*ppszDatum, papszDatumEquiv[i])) {
1123 *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.
2D/3D raster map header (used also for region)
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-...
struct ellps_list * read_ellipsoid_table(int fatal)
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.
#define PROJECTION_XY
Projection code - XY coordinate system (unreferenced data)
char ** G_tokenize(const char *, const char *)
Tokenize string.
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...
int G_lookup_key_value_from_file(const char *, const char *, char[], int)
Look up for key in file.
int GPJ__get_ellipsoid_params(const struct Key_Value *, double *, double *, double *)
Get the ellipsoid parameters from proj keys structure.
struct gpj_units gpj_units[]
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...
#define PROJECTION_UTM
Projection code - UTM.
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...
int int G_strcasecmp(const char *, const char *)
String compare ignoring case (upper or lower)
#define PROJECTION_OTHER
Projection code - other projection (other then noted above)
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...
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_datum_transform(struct gpj_datum_transform_list *)
Free the memory used by a gpj_datum_transform_list struct.
int G_number_of_tokens(char **)
Return number of tokens.
int zone
Projection zone (UTM)
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.
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...
void G_set_key_value(const char *, const char *, struct Key_Value *)
Set value for given key.
#define PROJECTION_LL
Projection code - Latitude-Longitude.
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.
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.
void GPJ_free_ellps(struct gpj_ellps *)
Free ellipsoid data structure.
struct datum_list * read_datum_table(void)
Read the current GRASS datum.table from disk and store in memory.
const char * G_gisbase(void)
Get full path name of the top level module directory.
void G_free_tokens(char **)
Free memory allocated to tokens.
void G_warning(const char *,...) __attribute__((format(printf
char * G_store(const char *)
Copy string to allocated memory.
struct Key_Value * G_create_key_value(void)
Allocate and initialize Key_Value structure.
int G_asprintf(char **, const char *,...) __attribute__((format(printf
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 G_debug(int, const char *,...) __attribute__((format(printf
void G_free_key_value(struct Key_Value *)
Free allocated Key_Value structure.
const char * G_find_key_value(const char *, const struct Key_Value *)
Find given key (case sensitive)
void free_ellps_list(struct ellps_list *elist)