// returned WKT string should be free by OGRFree or CPLFree static char* get_wkt_of(int epsg_code) { OGRSpatialReferenceH srs = OSRNewSpatialReference(NULL); OSRImportFromEPSG(srs, epsg_code); char* srs_wkt; OSRExportToWkt(srs, &srs_wkt); return srs_wkt; }
char *msProjectionObj2OGCWKT( projectionObj *projection ) { #if !defined(USE_GDAL) && !defined(USE_OGR) msSetError(MS_OGRERR, "Not implemented since neither OGR nor GDAL is enabled.", "msProjectionObj2OGCWKT()"); return NULL; #else /* defined USE_GDAL or USE_OGR */ OGRSpatialReferenceH hSRS; char *pszWKT=NULL, *pszProj4, *pszInitEpsg=NULL; int nLength = 0, i; OGRErr eErr; if( projection->proj == NULL ) return NULL; hSRS = OSRNewSpatialReference( NULL ); /* -------------------------------------------------------------------- */ /* Look for an EPSG-like projection argument */ /* -------------------------------------------------------------------- */ if( projection->numargs == 1 && (pszInitEpsg = strcasestr(projection->args[0],"init=epsg:"))) { int nEpsgCode = atoi(pszInitEpsg + strlen("init=epsg:")); eErr = OSRImportFromEPSG(hSRS, nEpsgCode); } else { /* -------------------------------------------------------------------- */ /* Form arguments into a full Proj.4 definition string. */ /* -------------------------------------------------------------------- */ for( i = 0; i < projection->numargs; i++ ) nLength += strlen(projection->args[i]) + 2; pszProj4 = (char *) CPLMalloc(nLength+2); pszProj4[0] = '\0'; for( i = 0; i < projection->numargs; i++ ) { strcat( pszProj4, "+" ); strcat( pszProj4, projection->args[i] ); strcat( pszProj4, " " ); } /* -------------------------------------------------------------------- */ /* Ingest the string into OGRSpatialReference. */ /* -------------------------------------------------------------------- */ eErr = OSRImportFromProj4( hSRS, pszProj4 ); CPLFree( pszProj4 ); } /* -------------------------------------------------------------------- */ /* Export as a WKT string. */ /* -------------------------------------------------------------------- */ if( eErr == OGRERR_NONE ) eErr = OSRExportToWkt( hSRS, &pszWKT ); OSRDestroySpatialReference( hSRS ); if( pszWKT ) { char *pszWKT2 = msStrdup(pszWKT); CPLFree( pszWKT ); return pszWKT2; } else return NULL; #endif /* defined USE_GDAL or USE_OGR */ }