/src/gdal/ogr/ogrsf_frmts/dgn/ogrdgndatasource.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Project: OpenGIS Simple Features Reference Implementation |
4 | | * Purpose: Implements OGRPGDataSource class. |
5 | | * Author: Frank Warmerdam, warmerdam@pobox.com |
6 | | * |
7 | | ****************************************************************************** |
8 | | * Copyright (c) 2000, Frank Warmerdam (warmerdam@pobox.com) |
9 | | * |
10 | | * SPDX-License-Identifier: MIT |
11 | | ****************************************************************************/ |
12 | | |
13 | | #include "ogr_dgn.h" |
14 | | #include "cpl_conv.h" |
15 | | #include "cpl_string.h" |
16 | | |
17 | | #ifdef EMBED_RESOURCE_FILES |
18 | | #include "embedded_resources.h" |
19 | | #endif |
20 | | |
21 | | /************************************************************************/ |
22 | | /* OGRDGNDataSource() */ |
23 | | /************************************************************************/ |
24 | | |
25 | 2.40k | OGRDGNDataSource::OGRDGNDataSource() = default; |
26 | | |
27 | | /************************************************************************/ |
28 | | /* ~OGRDGNDataSource() */ |
29 | | /************************************************************************/ |
30 | | |
31 | | OGRDGNDataSource::~OGRDGNDataSource() |
32 | | |
33 | 2.40k | { |
34 | 4.80k | for (int i = 0; i < nLayers; i++) |
35 | 2.40k | delete papoLayers[i]; |
36 | | |
37 | 2.40k | CPLFree(papoLayers); |
38 | 2.40k | CSLDestroy(papszOptions); |
39 | | |
40 | 2.40k | if (hDGN != nullptr) |
41 | 2.40k | DGNClose(hDGN); |
42 | 2.40k | } |
43 | | |
44 | | /************************************************************************/ |
45 | | /* Open() */ |
46 | | /************************************************************************/ |
47 | | |
48 | | bool OGRDGNDataSource::Open(GDALOpenInfo *poOpenInfo) |
49 | | |
50 | 2.40k | { |
51 | 2.40k | m_osEncoding = |
52 | 2.40k | CSLFetchNameValueDef(poOpenInfo->papszOpenOptions, "ENCODING", ""); |
53 | | |
54 | 2.40k | CPLAssert(nLayers == 0); |
55 | | |
56 | | /* -------------------------------------------------------------------- */ |
57 | | /* Try to open the file as a DGN file. */ |
58 | | /* -------------------------------------------------------------------- */ |
59 | 2.40k | const bool bUpdate = (poOpenInfo->eAccess == GA_Update); |
60 | 2.40k | hDGN = DGNOpen(poOpenInfo->pszFilename, bUpdate); |
61 | 2.40k | if (hDGN == nullptr) |
62 | 0 | { |
63 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
64 | 0 | "Unable to open %s as a Microstation .dgn file.", |
65 | 0 | poOpenInfo->pszFilename); |
66 | 0 | return false; |
67 | 0 | } |
68 | | |
69 | | /* -------------------------------------------------------------------- */ |
70 | | /* Create the layer object. */ |
71 | | /* -------------------------------------------------------------------- */ |
72 | 2.40k | OGRDGNLayer *poLayer = new OGRDGNLayer(this, "elements", hDGN, bUpdate); |
73 | | |
74 | | /* -------------------------------------------------------------------- */ |
75 | | /* Add layer to data source layer list. */ |
76 | | /* -------------------------------------------------------------------- */ |
77 | 2.40k | papoLayers = static_cast<OGRDGNLayer **>( |
78 | 2.40k | CPLRealloc(papoLayers, sizeof(OGRDGNLayer *) * (nLayers + 1))); |
79 | 2.40k | papoLayers[nLayers++] = poLayer; |
80 | | |
81 | 2.40k | return true; |
82 | 2.40k | } |
83 | | |
84 | | /************************************************************************/ |
85 | | /* TestCapability() */ |
86 | | /************************************************************************/ |
87 | | |
88 | | int OGRDGNDataSource::TestCapability(const char *pszCap) |
89 | | |
90 | 0 | { |
91 | 0 | if (EQUAL(pszCap, ODsCCreateLayer)) |
92 | 0 | return TRUE; |
93 | 0 | else if (EQUAL(pszCap, ODsCZGeometries)) |
94 | 0 | return TRUE; |
95 | | |
96 | 0 | return FALSE; |
97 | 0 | } |
98 | | |
99 | | /************************************************************************/ |
100 | | /* GetLayer() */ |
101 | | /************************************************************************/ |
102 | | |
103 | | OGRLayer *OGRDGNDataSource::GetLayer(int iLayer) |
104 | | |
105 | 2.40k | { |
106 | 2.40k | if (iLayer < 0 || iLayer >= nLayers) |
107 | 0 | return nullptr; |
108 | | |
109 | 2.40k | return papoLayers[iLayer]; |
110 | 2.40k | } |
111 | | |
112 | | /************************************************************************/ |
113 | | /* PreCreate() */ |
114 | | /* */ |
115 | | /* Called by OGRDGNDriver::Create() method to setup a stub */ |
116 | | /* OGRDataSource object without the associated file created */ |
117 | | /* yet. It will be created by the ICreateLayer() call. */ |
118 | | /************************************************************************/ |
119 | | |
120 | | void OGRDGNDataSource::PreCreate(CSLConstList papszOptionsIn) |
121 | | |
122 | 0 | { |
123 | 0 | papszOptions = CSLDuplicate(papszOptionsIn); |
124 | |
|
125 | 0 | m_osEncoding = CSLFetchNameValueDef(papszOptionsIn, "ENCODING", ""); |
126 | 0 | } |
127 | | |
128 | | /************************************************************************/ |
129 | | /* ICreateLayer() */ |
130 | | /************************************************************************/ |
131 | | |
132 | | OGRLayer * |
133 | | OGRDGNDataSource::ICreateLayer(const char *pszLayerName, |
134 | | const OGRGeomFieldDefn *poGeomFieldDefn, |
135 | | CSLConstList papszExtraOptions) |
136 | | |
137 | 0 | { |
138 | | /* -------------------------------------------------------------------- */ |
139 | | /* Ensure only one layer gets created. */ |
140 | | /* -------------------------------------------------------------------- */ |
141 | 0 | if (nLayers > 0) |
142 | 0 | { |
143 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
144 | 0 | "DGN driver only supports one layer with all the elements " |
145 | 0 | "in it."); |
146 | 0 | return nullptr; |
147 | 0 | } |
148 | | |
149 | 0 | const auto eGeomType = |
150 | 0 | poGeomFieldDefn ? poGeomFieldDefn->GetType() : wkbNone; |
151 | 0 | const auto poSRS = |
152 | 0 | poGeomFieldDefn ? poGeomFieldDefn->GetSpatialRef() : nullptr; |
153 | | |
154 | | /* -------------------------------------------------------------------- */ |
155 | | /* If the coordinate system is geographic, we should use a */ |
156 | | /* localized default origin and resolution. */ |
157 | | /* -------------------------------------------------------------------- */ |
158 | 0 | const char *pszMasterUnit = "m"; |
159 | 0 | const char *pszSubUnit = "cm"; |
160 | |
|
161 | 0 | int nUORPerSU = 1; |
162 | 0 | int nSUPerMU = 100; |
163 | |
|
164 | 0 | double dfOriginX = -21474836.0; // Default origin centered on zero |
165 | 0 | double dfOriginY = -21474836.0; // with two decimals of precision. |
166 | 0 | double dfOriginZ = -21474836.0; |
167 | |
|
168 | 0 | if (poSRS != nullptr && poSRS->IsGeographic()) |
169 | 0 | { |
170 | 0 | dfOriginX = -200.0; |
171 | 0 | dfOriginY = -200.0; |
172 | |
|
173 | 0 | pszMasterUnit = "d"; |
174 | 0 | pszSubUnit = "s"; |
175 | 0 | nSUPerMU = 3600; |
176 | 0 | nUORPerSU = 1000; |
177 | 0 | } |
178 | | |
179 | | /* -------------------------------------------------------------------- */ |
180 | | /* Parse out various creation options. */ |
181 | | /* -------------------------------------------------------------------- */ |
182 | 0 | papszOptions = CSLInsertStrings(papszOptions, 0, papszExtraOptions); |
183 | |
|
184 | 0 | const bool b3DRequested = |
185 | 0 | CPLFetchBool(papszOptions, "3D", wkbHasZ(eGeomType)); |
186 | |
|
187 | 0 | const char *pszRequestSeed = CSLFetchNameValue(papszOptions, "SEED"); |
188 | 0 | const char *pszSeed = pszRequestSeed; |
189 | 0 | int nCreationFlags = 0; |
190 | | #ifdef EMBED_RESOURCE_FILES |
191 | | std::string osTmpSeedFilename; |
192 | | #endif |
193 | 0 | if (pszSeed) |
194 | 0 | nCreationFlags |= DGNCF_USE_SEED_ORIGIN | DGNCF_USE_SEED_UNITS; |
195 | 0 | else |
196 | 0 | { |
197 | 0 | pszRequestSeed = b3DRequested ? "seed_3d.dgn" : "seed_2d.dgn"; |
198 | | #ifdef EMBED_RESOURCE_FILES |
199 | | CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); |
200 | | #endif |
201 | 0 | pszSeed = CPLFindFile("gdal", pszRequestSeed); |
202 | | #ifdef EMBED_RESOURCE_FILES |
203 | | if (!pszSeed) |
204 | | { |
205 | | if (b3DRequested) |
206 | | { |
207 | | static const bool bOnce [[maybe_unused]] = []() |
208 | | { |
209 | | CPLDebug("DGN", "Using embedded seed_3d"); |
210 | | return true; |
211 | | }(); |
212 | | } |
213 | | else |
214 | | { |
215 | | static const bool bOnce [[maybe_unused]] = []() |
216 | | { |
217 | | CPLDebug("DGN", "Using embedded seed_2d"); |
218 | | return true; |
219 | | }(); |
220 | | } |
221 | | unsigned nSize = 0; |
222 | | const unsigned char *pabyData = |
223 | | b3DRequested ? DGNGetSeed3D(&nSize) : DGNGetSeed2D(&nSize); |
224 | | osTmpSeedFilename = VSIMemGenerateHiddenFilename(pszRequestSeed); |
225 | | pszSeed = osTmpSeedFilename.c_str(); |
226 | | VSIFCloseL(VSIFileFromMemBuffer(osTmpSeedFilename.c_str(), |
227 | | const_cast<GByte *>(pabyData), |
228 | | static_cast<int>(nSize), |
229 | | /* bTakeOwnership = */ false)); |
230 | | } |
231 | | #endif |
232 | 0 | } |
233 | |
|
234 | 0 | if (pszSeed == nullptr) |
235 | 0 | { |
236 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
237 | 0 | "No seed file provided, and unable to find %s.", |
238 | 0 | pszRequestSeed); |
239 | 0 | return nullptr; |
240 | 0 | } |
241 | | |
242 | 0 | if (CPLFetchBool(papszOptions, "COPY_WHOLE_SEED_FILE", true)) |
243 | 0 | nCreationFlags |= DGNCF_COPY_WHOLE_SEED_FILE; |
244 | 0 | if (CPLFetchBool(papszOptions, "COPY_SEED_FILE_COLOR_TABLE", true)) |
245 | 0 | nCreationFlags |= DGNCF_COPY_SEED_FILE_COLOR_TABLE; |
246 | |
|
247 | 0 | const char *pszValue = CSLFetchNameValue(papszOptions, "MASTER_UNIT_NAME"); |
248 | 0 | if (pszValue != nullptr) |
249 | 0 | { |
250 | 0 | nCreationFlags &= ~DGNCF_USE_SEED_UNITS; |
251 | 0 | pszMasterUnit = pszValue; |
252 | 0 | } |
253 | |
|
254 | 0 | pszValue = CSLFetchNameValue(papszOptions, "SUB_UNIT_NAME"); |
255 | 0 | if (pszValue != nullptr) |
256 | 0 | { |
257 | 0 | nCreationFlags &= ~DGNCF_USE_SEED_UNITS; |
258 | 0 | pszSubUnit = pszValue; |
259 | 0 | } |
260 | |
|
261 | 0 | pszValue = CSLFetchNameValue(papszOptions, "SUB_UNITS_PER_MASTER_UNIT"); |
262 | 0 | if (pszValue != nullptr) |
263 | 0 | { |
264 | 0 | nCreationFlags &= ~DGNCF_USE_SEED_UNITS; |
265 | 0 | nSUPerMU = atoi(pszValue); |
266 | 0 | } |
267 | |
|
268 | 0 | pszValue = CSLFetchNameValue(papszOptions, "UOR_PER_SUB_UNIT"); |
269 | 0 | if (pszValue != nullptr) |
270 | 0 | { |
271 | 0 | nCreationFlags &= ~DGNCF_USE_SEED_UNITS; |
272 | 0 | nUORPerSU = atoi(pszValue); |
273 | 0 | } |
274 | |
|
275 | 0 | pszValue = CSLFetchNameValue(papszOptions, "ORIGIN"); |
276 | 0 | if (pszValue != nullptr) |
277 | 0 | { |
278 | 0 | char **papszTuple = |
279 | 0 | CSLTokenizeStringComplex(pszValue, " ,", FALSE, FALSE); |
280 | |
|
281 | 0 | nCreationFlags &= ~DGNCF_USE_SEED_ORIGIN; |
282 | 0 | if (CSLCount(papszTuple) == 3) |
283 | 0 | { |
284 | 0 | dfOriginX = CPLAtof(papszTuple[0]); |
285 | 0 | dfOriginY = CPLAtof(papszTuple[1]); |
286 | 0 | dfOriginZ = CPLAtof(papszTuple[2]); |
287 | 0 | } |
288 | 0 | else if (CSLCount(papszTuple) == 2) |
289 | 0 | { |
290 | 0 | dfOriginX = CPLAtof(papszTuple[0]); |
291 | 0 | dfOriginY = CPLAtof(papszTuple[1]); |
292 | 0 | dfOriginZ = 0.0; |
293 | 0 | } |
294 | 0 | else |
295 | 0 | { |
296 | 0 | CSLDestroy(papszTuple); |
297 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
298 | 0 | "ORIGIN is not a valid 2d or 3d tuple.\n" |
299 | 0 | "Separate tuple values with comma."); |
300 | | #ifdef EMBED_RESOURCE_FILES |
301 | | if (!osTmpSeedFilename.empty()) |
302 | | VSIUnlink(osTmpSeedFilename.c_str()); |
303 | | #endif |
304 | 0 | return nullptr; |
305 | 0 | } |
306 | 0 | CSLDestroy(papszTuple); |
307 | 0 | } |
308 | | |
309 | | /* -------------------------------------------------------------------- */ |
310 | | /* Try creating the base file. */ |
311 | | /* -------------------------------------------------------------------- */ |
312 | 0 | hDGN = DGNCreate(GetDescription(), pszSeed, nCreationFlags, dfOriginX, |
313 | 0 | dfOriginY, dfOriginZ, nSUPerMU, nUORPerSU, pszMasterUnit, |
314 | 0 | pszSubUnit); |
315 | | #ifdef EMBED_RESOURCE_FILES |
316 | | if (!osTmpSeedFilename.empty()) |
317 | | VSIUnlink(osTmpSeedFilename.c_str()); |
318 | | #endif |
319 | |
|
320 | 0 | if (hDGN == nullptr) |
321 | 0 | return nullptr; |
322 | | |
323 | | /* -------------------------------------------------------------------- */ |
324 | | /* Create the layer object. */ |
325 | | /* -------------------------------------------------------------------- */ |
326 | 0 | OGRDGNLayer *poLayer = new OGRDGNLayer(this, pszLayerName, hDGN, TRUE); |
327 | | |
328 | | /* -------------------------------------------------------------------- */ |
329 | | /* Add layer to data source layer list. */ |
330 | | /* -------------------------------------------------------------------- */ |
331 | 0 | papoLayers = static_cast<OGRDGNLayer **>( |
332 | 0 | CPLRealloc(papoLayers, sizeof(OGRDGNLayer *) * (nLayers + 1))); |
333 | 0 | papoLayers[nLayers++] = poLayer; |
334 | |
|
335 | 0 | return poLayer; |
336 | 0 | } |