Skip to content

Commit

Permalink
Merge pull request OSGeo#9005 from rouault/fix_9004
Browse files Browse the repository at this point in the history
DGN (v7) driver: emit explicit error when attempting to open a DGNv8 …
  • Loading branch information
rouault authored Jan 11, 2024
2 parents 17a36ae + 8f7e6db commit 4f0607d
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 4 deletions.
22 changes: 21 additions & 1 deletion autotest/ogr/ogr_dgn.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import ogrtest
import pytest

from osgeo import ogr
from osgeo import gdal, ogr

pytestmark = pytest.mark.require_driver("DGN")

Expand Down Expand Up @@ -293,3 +293,23 @@ def test_ogr_dgn_online_1():
wkt = "LINESTRING (82.9999500717185 23.2084166997284,83.0007450788903 23.2084495986816,83.00081490524 23.2068095339824,82.9999503769036 23.2067737968078)"

ogrtest.check_feature_geometry(feat, wkt)


###############################################################################
# Test opening a (not supported by this driver) DGNv8 file


def test_ogr_dgn_open_dgnv8_not_supported():

dgnv8_drv = gdal.GetDriverByName("DGNv8")
if dgnv8_drv:
dgnv8_drv.Deregister()
try:
with pytest.raises(
Exception,
match="recognized as a DGNv8 dataset, but the DGNv8 driver is not available in this GDAL build",
):
ogr.Open("data/dgnv8/test_dgnv8.dgn")
finally:
if dgnv8_drv:
dgnv8_drv.Register()
6 changes: 6 additions & 0 deletions gcore/gdal.h
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,12 @@ GDALDatasetH CPL_DLL CPL_STDCALL GDALOpenShared(const char *, GDALAccess)
#define GDAL_OF_BLOCK_ACCESS_MASK 0x300
#endif

#ifndef DOXYGEN_SKIP
/** Set by GDALOpenEx() to indicate to Identify() method that they are called
* from it */
#define GDAL_OF_FROM_GDALOPEN 0x400
#endif

GDALDatasetH CPL_DLL CPL_STDCALL GDALOpenEx(
const char *pszFilename, unsigned int nOpenFlags,
const char *const *papszAllowedDrivers, const char *const *papszOpenOptions,
Expand Down
1 change: 1 addition & 0 deletions gcore/gdaldataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3622,6 +3622,7 @@ GDALDatasetH CPL_STDCALL GDALOpenEx(const char *pszFilename,
}

oOpenInfo.papszOpenOptions = papszOpenOptionsCleaned;
oOpenInfo.nOpenFlags |= GDAL_OF_FROM_GDALOPEN;

#ifdef OGRAPISPY_ENABLED
const bool bUpdate = (nOpenFlags & GDAL_OF_UPDATE) != 0;
Expand Down
2 changes: 1 addition & 1 deletion gcore/gdaldriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ GDALDataset *GDALDriver::Open(GDALOpenInfo *poOpenInfo, bool bSetOpenOptions)

if (poDS)
{
poDS->nOpenFlags = poOpenInfo->nOpenFlags;
poDS->nOpenFlags = poOpenInfo->nOpenFlags & ~GDAL_OF_FROM_GDALOPEN;

if (strlen(poDS->GetDescription()) == 0)
poDS->SetDescription(poOpenInfo->pszFilename);
Expand Down
28 changes: 26 additions & 2 deletions ogr/ogrsf_frmts/dgn/ogrdgndriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,32 @@
static int OGRDGNDriverIdentify(GDALOpenInfo *poOpenInfo)

{
return poOpenInfo->fpL != nullptr && poOpenInfo->nHeaderBytes >= 512 &&
DGNTestOpen(poOpenInfo->pabyHeader, poOpenInfo->nHeaderBytes);
if (poOpenInfo->fpL != nullptr && poOpenInfo->nHeaderBytes >= 512 &&
DGNTestOpen(poOpenInfo->pabyHeader, poOpenInfo->nHeaderBytes))
{
return TRUE;
}

// Is this is a DGNv8 file ? If so, and if the DGNV8 driver is not
// available, and we are called from GDALError(), emit an explicit
// error.
VSIStatBuf sStat;
if ((poOpenInfo->nOpenFlags & GDAL_OF_FROM_GDALOPEN) != 0 &&
poOpenInfo->papszAllowedDrivers == nullptr &&
poOpenInfo->fpL != nullptr && poOpenInfo->nHeaderBytes >= 512 &&
memcmp(poOpenInfo->pabyHeader, "\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1", 8) ==
0 &&
EQUAL(CPLGetExtension(poOpenInfo->pszFilename), "DGN") &&
VSIStat(poOpenInfo->pszFilename, &sStat) == 0 &&
GDALGetDriverByName("DGNV8") == nullptr)
{
CPLError(CE_Failure, CPLE_AppDefined,
"`%s' recognized as a DGNv8 dataset, but the DGNv8 driver is "
"not available in this GDAL build. Consult "
"https://gdal.org/drivers/vector/dgnv8.html",
poOpenInfo->pszFilename);
}
return FALSE;
}

/************************************************************************/
Expand Down

0 comments on commit 4f0607d

Please sign in to comment.