Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: OpenGIS Simple Features Reference Implementation 4 : * Purpose: Implement importFromDict() method to read a WKT SRS from a 5 : * coordinate system dictionary in a simple text format. 6 : * Author: Frank Warmerdam, warmerdam@pobox.com 7 : * 8 : ****************************************************************************** 9 : * Copyright (c) 2004, Frank Warmerdam 10 : * 11 : * SPDX-License-Identifier: MIT 12 : ****************************************************************************/ 13 : 14 : #include "cpl_port.h" 15 : #include "ogr_spatialref.h" 16 : 17 : #include <cstring> 18 : 19 : #include "cpl_conv.h" 20 : #include "cpl_error.h" 21 : #include "cpl_string.h" 22 : #include "cpl_vsi.h" 23 : #include "ogr_core.h" 24 : #include "ogr_srs_api.h" 25 : 26 : /************************************************************************/ 27 : /* importFromDict() */ 28 : /************************************************************************/ 29 : 30 : /** 31 : * Read SRS from WKT dictionary. 32 : * 33 : * This method will attempt to find the indicated coordinate system identity 34 : * in the indicated dictionary file. If found, the WKT representation is 35 : * imported and used to initialize this OGRSpatialReference. 36 : * 37 : * More complete information on the format of the dictionary files can 38 : * be found in the epsg.wkt file in the GDAL data tree. The dictionary 39 : * files are searched for in the "GDAL" domain using CPLFindFile(). Normally 40 : * this results in searching /usr/local/share/gdal or somewhere similar. 41 : * 42 : * This method is the same as the C function OSRImportFromDict(). 43 : * 44 : * @param pszDictFile the name of the dictionary file to load. 45 : * 46 : * @param pszCode the code to lookup in the dictionary. 47 : * 48 : * @return OGRERR_NONE on success, or OGRERR_SRS_UNSUPPORTED if the code isn't 49 : * found, and OGRERR_SRS_FAILURE if something more dramatic goes wrong. 50 : */ 51 : 52 3 : OGRErr OGRSpatialReference::importFromDict(const char *pszDictFile, 53 : const char *pszCode) 54 : 55 : { 56 6 : CPLString osWKT(lookupInDict(pszDictFile, pszCode)); 57 3 : if (osWKT.empty()) 58 0 : return OGRERR_UNSUPPORTED_SRS; 59 : 60 3 : OGRErr eErr = importFromWkt(osWKT); 61 3 : if (eErr == OGRERR_NONE && strstr(pszDictFile, "esri_") == nullptr) 62 : { 63 0 : morphFromESRI(); 64 : } 65 : 66 3 : return eErr; 67 : } 68 : 69 : /************************************************************************/ 70 : /* lookupInDict() */ 71 : /************************************************************************/ 72 : 73 116 : CPLString OGRSpatialReference::lookupInDict(const char *pszDictFile, 74 : const char *pszCode) 75 : 76 : { 77 : /* -------------------------------------------------------------------- */ 78 : /* Find and open file. */ 79 : /* -------------------------------------------------------------------- */ 80 232 : CPLString osDictFile(pszDictFile); 81 116 : const char *pszFilename = CPLFindFile("gdal", pszDictFile); 82 116 : if (pszFilename == nullptr) 83 0 : return CPLString(); 84 : 85 116 : VSILFILE *fp = VSIFOpenL(pszFilename, "rb"); 86 116 : if (fp == nullptr) 87 0 : return CPLString(); 88 : 89 : /* -------------------------------------------------------------------- */ 90 : /* Process lines. */ 91 : /* -------------------------------------------------------------------- */ 92 232 : CPLString osWKT; 93 116 : const char *pszLine = nullptr; 94 : 95 136995 : while ((pszLine = CPLReadLineL(fp)) != nullptr) 96 : 97 : { 98 136940 : if (pszLine[0] == '#') 99 0 : continue; 100 : 101 136940 : if (STARTS_WITH_CI(pszLine, "include ")) 102 : { 103 0 : osWKT = lookupInDict(pszLine + 8, pszCode); 104 0 : if (!osWKT.empty()) 105 0 : break; 106 0 : continue; 107 : } 108 : 109 136940 : if (strstr(pszLine, ",") == nullptr) 110 0 : continue; 111 : 112 136940 : if (EQUALN(pszLine, pszCode, strlen(pszCode)) && 113 61 : pszLine[strlen(pszCode)] == ',') 114 : { 115 61 : osWKT = pszLine + strlen(pszCode) + 1; 116 61 : break; 117 : } 118 : } 119 : 120 : /* -------------------------------------------------------------------- */ 121 : /* Cleanup */ 122 : /* -------------------------------------------------------------------- */ 123 116 : VSIFCloseL(fp); 124 : 125 116 : return osWKT; 126 : } 127 : 128 : /************************************************************************/ 129 : /* OSRImportFromDict() */ 130 : /************************************************************************/ 131 : 132 : /** 133 : * Read SRS from WKT dictionary. 134 : * 135 : * This method will attempt to find the indicated coordinate system identity 136 : * in the indicated dictionary file. If found, the WKT representation is 137 : * imported and used to initialize this OGRSpatialReference. 138 : * 139 : * More complete information on the format of the dictionary files can 140 : * be found in the epsg.wkt file in the GDAL data tree. The dictionary 141 : * files are searched for in the "GDAL" domain using CPLFindFile(). Normally 142 : * this results in searching /usr/local/share/gdal or somewhere similar. 143 : * 144 : * This method is the same as the C++ method 145 : * OGRSpatialReference::importFromDict(). 146 : * 147 : * @param hSRS spatial reference system handle. 148 : * 149 : * @param pszDictFile the name of the dictionary file to load. 150 : * 151 : * @param pszCode the code to lookup in the dictionary. 152 : * 153 : * @return OGRERR_NONE on success, or OGRERR_SRS_UNSUPPORTED if the code isn't 154 : * found, and OGRERR_SRS_FAILURE if something more dramatic goes wrong. 155 : */ 156 : 157 0 : OGRErr OSRImportFromDict(OGRSpatialReferenceH hSRS, const char *pszDictFile, 158 : const char *pszCode) 159 : 160 : { 161 0 : VALIDATE_POINTER1(hSRS, "OSRImportFromDict", OGRERR_FAILURE); 162 : 163 0 : return reinterpret_cast<OGRSpatialReference *>(hSRS)->importFromDict( 164 0 : pszDictFile, pszCode); 165 : }