| 8022 | #endif |
| 8023 | |
| 8024 | char *gmt_importproj4 (struct GMT_CTRL *GMT, char *pStr, int *scale_pos, char *epsg2proj) { |
| 8025 | /* Take a PROJ.4 projection string or EPSG code and try to find the equivalent -J syntax |
| 8026 | scale_pos is position on the return string where starts the scale sub-string. |
| 8027 | The pStr pointer is changed when in input it contains an EPSG code. On return it will |
| 8028 | have the proj4 string corresponding to the EPSG code. That pointer can later be freed with free() |
| 8029 | */ |
| 8030 | unsigned int pos = 0; |
| 8031 | //bool got_lonlat = false; |
| 8032 | char opt_J[GMT_LEN256] = {""}, szProj4[GMT_LEN256] = {""}, prjcode[16] = {""}; |
| 8033 | char token[GMT_LEN256] = {""}, scale_c[GMT_LEN32] = {""}, *pch = NULL, *pStrOut = NULL; |
| 8034 | char lon_0[32] = {""}, lat_0[32] = {""}, lat_1[32] = {""}, lat_2[32] = {""}, lat_ts[32] = {""}; |
| 8035 | |
| 8036 | snprintf(szProj4, GMT_LEN256-1, "%s", pStr); |
| 8037 | |
| 8038 | if ((pch = strchr(szProj4, '/')) != NULL) { /* Get the scale factor and chop it out of proj string */ |
| 8039 | strncpy(scale_c, &pch[1], GMT_LEN32-1); |
| 8040 | pch[0] = '\0'; |
| 8041 | } |
| 8042 | else { |
| 8043 | GMT->current.ps.active ? sprintf(scale_c, "15c") : sprintf(scale_c, "1:1"); |
| 8044 | } |
| 8045 | |
| 8046 | if (isdigit(szProj4[1])) { /* A EPSG code. By looking at 2nd char instead of 1st both +epsg and epsg work */ |
| 8047 | int EPSGID; |
| 8048 | char *pszResult = NULL; |
| 8049 | OGRSpatialReferenceH hSRS; |
| 8050 | OGRErr eErr = OGRERR_NONE; |
| 8051 | #if 0 |
| 8052 | bool found = false; |
| 8053 | char buffer [GMT_LEN256] = {""}; |
| 8054 | FILE *fp = NULL; |
| 8055 | #endif |
| 8056 | |
| 8057 | if ((pch = strstr(szProj4, "+width=")) != NULL || (pch = strstr(szProj4, "+scale=")) != NULL) { |
| 8058 | snprintf (scale_c, GMT_LEN32-1, "%s", pch); |
| 8059 | pch[0] = '\0'; /* Strip it */ |
| 8060 | } |
| 8061 | |
| 8062 | if (szProj4[0] == '+') /* Let it both work: -J+epsg or -Jepsg */ |
| 8063 | EPSGID = atoi(&szProj4[1]); |
| 8064 | else |
| 8065 | EPSGID = atoi(szProj4); |
| 8066 | |
| 8067 | /* Rely on GDAL to tell us the proj4 string of this EPSG code */ |
| 8068 | hSRS = OSRNewSpatialReference(NULL); |
| 8069 | if ((eErr = OSRImportFromEPSG(hSRS, EPSGID)) != OGRERR_NONE) { |
| 8070 | GMT_Report (GMT->parent, GMT_MSG_WARNING, "Did not get the SRS from input EPSG %d\n", EPSGID); |
| 8071 | return (pStrOut); |
| 8072 | } |
| 8073 | if ((eErr = OSRExportToProj4(hSRS, &pszResult)) != OGRERR_NONE) { |
| 8074 | GMT_Report (GMT->parent, GMT_MSG_WARNING, "Failed to convert the SRS to proj4 syntax\n"); |
| 8075 | return (pStrOut); |
| 8076 | } |
| 8077 | snprintf(szProj4, GMT_LEN256-1, "%s", pszResult); |
| 8078 | snprintf(epsg2proj, GMT_LEN256-1, "%s", pszResult); /* This one now has the proj4 conversion of the EPSG code. */ |
| 8079 | if (scale_c[0] != '\0') strcat (szProj4, scale_c); /* Add the width/scale found above */ |
| 8080 | CPLFree(pszResult); |
| 8081 | OSRDestroySpatialReference(hSRS); |
no test coverage detected