/src/gdal/ogr/ogr_srs_dict.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | OGRErr OGRSpatialReference::importFromDict(const char *pszDictFile, |
53 | | const char *pszCode) |
54 | | |
55 | 0 | { |
56 | 0 | CPLString osWKT(lookupInDict(pszDictFile, pszCode)); |
57 | 0 | if (osWKT.empty()) |
58 | 0 | return OGRERR_UNSUPPORTED_SRS; |
59 | | |
60 | 0 | OGRErr eErr = importFromWkt(osWKT); |
61 | 0 | if (eErr == OGRERR_NONE && strstr(pszDictFile, "esri_") == nullptr) |
62 | 0 | { |
63 | 0 | morphFromESRI(); |
64 | 0 | } |
65 | |
|
66 | 0 | return eErr; |
67 | 0 | } |
68 | | |
69 | | /************************************************************************/ |
70 | | /* lookupInDict() */ |
71 | | /************************************************************************/ |
72 | | |
73 | | CPLString OGRSpatialReference::lookupInDict(const char *pszDictFile, |
74 | | const char *pszCode) |
75 | | |
76 | 0 | { |
77 | | /* -------------------------------------------------------------------- */ |
78 | | /* Find and open file. */ |
79 | | /* -------------------------------------------------------------------- */ |
80 | 0 | CPLString osDictFile(pszDictFile); |
81 | 0 | const char *pszFilename = CPLFindFile("gdal", pszDictFile); |
82 | 0 | if (pszFilename == nullptr) |
83 | 0 | return CPLString(); |
84 | | |
85 | 0 | VSILFILE *fp = VSIFOpenL(pszFilename, "rb"); |
86 | 0 | if (fp == nullptr) |
87 | 0 | return CPLString(); |
88 | | |
89 | | /* -------------------------------------------------------------------- */ |
90 | | /* Process lines. */ |
91 | | /* -------------------------------------------------------------------- */ |
92 | 0 | CPLString osWKT; |
93 | 0 | const char *pszLine = nullptr; |
94 | |
|
95 | 0 | while ((pszLine = CPLReadLineL(fp)) != nullptr) |
96 | | |
97 | 0 | { |
98 | 0 | if (pszLine[0] == '#') |
99 | 0 | continue; |
100 | | |
101 | 0 | if (STARTS_WITH_CI(pszLine, "include ")) |
102 | 0 | { |
103 | 0 | osWKT = lookupInDict(pszLine + 8, pszCode); |
104 | 0 | if (!osWKT.empty()) |
105 | 0 | break; |
106 | 0 | continue; |
107 | 0 | } |
108 | | |
109 | 0 | if (strstr(pszLine, ",") == nullptr) |
110 | 0 | continue; |
111 | | |
112 | 0 | if (EQUALN(pszLine, pszCode, strlen(pszCode)) && |
113 | 0 | pszLine[strlen(pszCode)] == ',') |
114 | 0 | { |
115 | 0 | osWKT = pszLine + strlen(pszCode) + 1; |
116 | 0 | break; |
117 | 0 | } |
118 | 0 | } |
119 | | |
120 | | /* -------------------------------------------------------------------- */ |
121 | | /* Cleanup */ |
122 | | /* -------------------------------------------------------------------- */ |
123 | 0 | VSIFCloseL(fp); |
124 | |
|
125 | 0 | return osWKT; |
126 | 0 | } |
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 | | OGRErr OSRImportFromDict(OGRSpatialReferenceH hSRS, const char *pszDictFile, |
158 | | const char *pszCode) |
159 | | |
160 | 0 | { |
161 | 0 | VALIDATE_POINTER1(hSRS, "OSRImportFromDict", OGRERR_FAILURE); |
162 | | |
163 | 0 | return reinterpret_cast<OGRSpatialReference *>(hSRS)->importFromDict( |
164 | 0 | pszDictFile, pszCode); |
165 | 0 | } |