Coverage Report

Created: 2026-11-12 08:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/ogr/ogrsf_frmts/dgn/ogrdgndatasource.cpp
Line
Count
Source
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
4.30k
OGRDGNDataSource::OGRDGNDataSource() = default;
26
27
/************************************************************************/
28
/*                         ~OGRDGNDataSource()                          */
29
/************************************************************************/
30
31
OGRDGNDataSource::~OGRDGNDataSource()
32
33
4.30k
{
34
8.61k
    for (int i = 0; i < nLayers; i++)
35
4.30k
        delete papoLayers[i];
36
37
4.30k
    CPLFree(papoLayers);
38
4.30k
    CSLDestroy(papszOptions);
39
40
4.30k
    if (hDGN != nullptr)
41
4.30k
        DGNClose(hDGN);
42
4.30k
}
43
44
/************************************************************************/
45
/*                                Open()                                */
46
/************************************************************************/
47
48
bool OGRDGNDataSource::Open(GDALOpenInfo *poOpenInfo)
49
50
4.30k
{
51
4.30k
    m_osEncoding =
52
4.30k
        CSLFetchNameValueDef(poOpenInfo->papszOpenOptions, "ENCODING", "");
53
54
4.30k
    CPLAssert(nLayers == 0);
55
56
    /* -------------------------------------------------------------------- */
57
    /*      Try to open the file as a DGN file.                             */
58
    /* -------------------------------------------------------------------- */
59
4.30k
    const bool bUpdate = (poOpenInfo->eAccess == GA_Update);
60
4.30k
    hDGN = DGNOpen(poOpenInfo->pszFilename, bUpdate);
61
4.30k
    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
4.30k
    OGRDGNLayer *poLayer = new OGRDGNLayer(this, "elements", hDGN, bUpdate);
73
74
    /* -------------------------------------------------------------------- */
75
    /*      Add layer to data source layer list.                            */
76
    /* -------------------------------------------------------------------- */
77
4.30k
    papoLayers = static_cast<OGRDGNLayer **>(
78
4.30k
        CPLRealloc(papoLayers, sizeof(OGRDGNLayer *) * (nLayers + 1)));
79
4.30k
    papoLayers[nLayers++] = poLayer;
80
81
4.30k
    return true;
82
4.30k
}
83
84
/************************************************************************/
85
/*                           TestCapability()                           */
86
/************************************************************************/
87
88
bool OGRDGNDataSource::TestCapability(const char *pszCap) const
89
90
10
{
91
10
    if (EQUAL(pszCap, ODsCCreateLayer))
92
3
        return TRUE;
93
7
    else if (EQUAL(pszCap, ODsCZGeometries))
94
0
        return TRUE;
95
96
7
    return FALSE;
97
10
}
98
99
/************************************************************************/
100
/*                              GetLayer()                              */
101
/************************************************************************/
102
103
const OGRLayer *OGRDGNDataSource::GetLayer(int iLayer) const
104
105
4.30k
{
106
4.30k
    if (iLayer < 0 || iLayer >= nLayers)
107
0
        return nullptr;
108
109
4.30k
    return papoLayers[iLayer];
110
4.30k
}
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
4
{
123
4
    papszOptions = CSLDuplicate(papszOptionsIn);
124
125
4
    m_osEncoding = CSLFetchNameValueDef(papszOptionsIn, "ENCODING", "");
126
4
}
127
128
/************************************************************************/
129
/*                            ICreateLayer()                            */
130
/************************************************************************/
131
132
OGRLayer *
133
OGRDGNDataSource::ICreateLayer(const char *pszLayerName,
134
                               const OGRGeomFieldDefn *poGeomFieldDefn,
135
                               CSLConstList papszExtraOptions)
136
137
3
{
138
    /* -------------------------------------------------------------------- */
139
    /*      Ensure only one layer gets created.                             */
140
    /* -------------------------------------------------------------------- */
141
3
    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
3
    const auto eGeomType =
150
3
        poGeomFieldDefn ? poGeomFieldDefn->GetType() : wkbNone;
151
3
    const auto poSRS =
152
3
        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
3
    const char *pszMasterUnit = "m";
159
3
    const char *pszSubUnit = "cm";
160
161
3
    int nUORPerSU = 1;
162
3
    int nSUPerMU = 100;
163
164
3
    double dfOriginX = -21474836.0;  // Default origin centered on zero
165
3
    double dfOriginY = -21474836.0;  // with two decimals of precision.
166
3
    double dfOriginZ = -21474836.0;
167
168
3
    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
3
    papszOptions = CSLInsertStrings(papszOptions, 0, papszExtraOptions);
183
184
3
    const bool b3DRequested =
185
3
        CPLFetchBool(papszOptions, "3D", wkbHasZ(eGeomType));
186
187
3
    const char *pszRequestSeed = CSLFetchNameValue(papszOptions, "SEED");
188
3
    const char *pszSeed = pszRequestSeed;
189
3
    int nCreationFlags = 0;
190
3
#ifdef EMBED_RESOURCE_FILES
191
3
    std::string osTmpSeedFilename;
192
3
#endif
193
3
    if (pszSeed)
194
0
        nCreationFlags |= DGNCF_USE_SEED_ORIGIN | DGNCF_USE_SEED_UNITS;
195
3
    else
196
3
    {
197
3
        pszRequestSeed = b3DRequested ? "seed_3d.dgn" : "seed_2d.dgn";
198
3
#ifdef EMBED_RESOURCE_FILES
199
3
        CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler);
200
3
#endif
201
3
        pszSeed = CPLFindFile("gdal", pszRequestSeed);
202
3
#ifdef EMBED_RESOURCE_FILES
203
3
        if (!pszSeed)
204
3
        {
205
3
            if (b3DRequested)
206
0
            {
207
0
                static const bool bOnce [[maybe_unused]] = []()
208
0
                {
209
0
                    CPLDebug("DGN", "Using embedded seed_3d");
210
0
                    return true;
211
0
                }();
212
0
            }
213
3
            else
214
3
            {
215
3
                static const bool bOnce [[maybe_unused]] = []()
216
3
                {
217
1
                    CPLDebug("DGN", "Using embedded seed_2d");
218
1
                    return true;
219
1
                }();
220
3
            }
221
3
            unsigned nSize = 0;
222
3
            const unsigned char *pabyData =
223
3
                b3DRequested ? DGNGetSeed3D(&nSize) : DGNGetSeed2D(&nSize);
224
3
            osTmpSeedFilename = VSIMemGenerateHiddenFilename(pszRequestSeed);
225
3
            pszSeed = osTmpSeedFilename.c_str();
226
3
            VSIFCloseL(VSIFileFromMemBuffer(osTmpSeedFilename.c_str(),
227
3
                                            const_cast<GByte *>(pabyData),
228
3
                                            static_cast<int>(nSize),
229
3
                                            /* bTakeOwnership = */ false));
230
3
        }
231
3
#endif
232
3
    }
233
234
3
    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
3
    if (CPLFetchBool(papszOptions, "COPY_WHOLE_SEED_FILE", true))
243
3
        nCreationFlags |= DGNCF_COPY_WHOLE_SEED_FILE;
244
3
    if (CPLFetchBool(papszOptions, "COPY_SEED_FILE_COLOR_TABLE", true))
245
3
        nCreationFlags |= DGNCF_COPY_SEED_FILE_COLOR_TABLE;
246
247
3
    const char *pszValue = CSLFetchNameValue(papszOptions, "MASTER_UNIT_NAME");
248
3
    if (pszValue != nullptr)
249
0
    {
250
0
        nCreationFlags &= ~DGNCF_USE_SEED_UNITS;
251
0
        pszMasterUnit = pszValue;
252
0
    }
253
254
3
    pszValue = CSLFetchNameValue(papszOptions, "SUB_UNIT_NAME");
255
3
    if (pszValue != nullptr)
256
0
    {
257
0
        nCreationFlags &= ~DGNCF_USE_SEED_UNITS;
258
0
        pszSubUnit = pszValue;
259
0
    }
260
261
3
    pszValue = CSLFetchNameValue(papszOptions, "SUB_UNITS_PER_MASTER_UNIT");
262
3
    if (pszValue != nullptr)
263
0
    {
264
0
        nCreationFlags &= ~DGNCF_USE_SEED_UNITS;
265
0
        nSUPerMU = atoi(pszValue);
266
0
    }
267
268
3
    pszValue = CSLFetchNameValue(papszOptions, "UOR_PER_SUB_UNIT");
269
3
    if (pszValue != nullptr)
270
0
    {
271
0
        nCreationFlags &= ~DGNCF_USE_SEED_UNITS;
272
0
        nUORPerSU = atoi(pszValue);
273
0
    }
274
275
3
    pszValue = CSLFetchNameValue(papszOptions, "ORIGIN");
276
3
    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
0
#ifdef EMBED_RESOURCE_FILES
301
0
            if (!osTmpSeedFilename.empty())
302
0
                VSIUnlink(osTmpSeedFilename.c_str());
303
0
#endif
304
0
            return nullptr;
305
0
        }
306
0
        CSLDestroy(papszTuple);
307
0
    }
308
309
    /* -------------------------------------------------------------------- */
310
    /*      Try creating the base file.                                     */
311
    /* -------------------------------------------------------------------- */
312
3
    hDGN = DGNCreate(GetDescription(), pszSeed, nCreationFlags, dfOriginX,
313
3
                     dfOriginY, dfOriginZ, nSUPerMU, nUORPerSU, pszMasterUnit,
314
3
                     pszSubUnit);
315
3
#ifdef EMBED_RESOURCE_FILES
316
3
    if (!osTmpSeedFilename.empty())
317
3
        VSIUnlink(osTmpSeedFilename.c_str());
318
3
#endif
319
320
3
    if (hDGN == nullptr)
321
0
        return nullptr;
322
323
    /* -------------------------------------------------------------------- */
324
    /*      Create the layer object.                                        */
325
    /* -------------------------------------------------------------------- */
326
3
    OGRDGNLayer *poLayer = new OGRDGNLayer(this, pszLayerName, hDGN, true);
327
328
    /* -------------------------------------------------------------------- */
329
    /*      Add layer to data source layer list.                            */
330
    /* -------------------------------------------------------------------- */
331
3
    papoLayers = static_cast<OGRDGNLayer **>(
332
3
        CPLRealloc(papoLayers, sizeof(OGRDGNLayer *) * (nLayers + 1)));
333
3
    papoLayers[nLayers++] = poLayer;
334
335
3
    return poLayer;
336
3
}