Skip to content

Commit

Permalink
[gdal][api] GDALDriverHasOpenOption (OSGeo#11517)
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso authored Dec 20, 2024
1 parent e7b6c09 commit 7041096
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
18 changes: 18 additions & 0 deletions autotest/gcore/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,24 @@ def test_misc_general_cmd_line_processor(tmp_path):
assert processed == ["program", "2", str(tmp_path / "a_path"), "a_string"]


###############################################################################
# Test GDALDriverHasOpenOption()


@pytest.mark.require_driver("GTiff")
@pytest.mark.parametrize(
"driver_name,open_option,expected",
[
("GTiff", "XXXX", False),
("GTiff", "GEOTIFF_KEYS_FLAVOR", True),
],
)
def test_misc_gdal_driver_has_open_option(driver_name, open_option, expected):
driver = gdal.GetDriverByName(driver_name)
assert driver is not None
assert driver.HasOpenOption(open_option) == expected


###############################################################################


Expand Down
3 changes: 3 additions & 0 deletions gcore/gdal.h
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,9 @@ char CPL_DLL **GDALGetOutputDriversForDatasetName(const char *pszDestFilename,
bool bSingleMatch,
bool bEmitWarning);

bool CPL_DLL GDALDriverHasOpenOption(GDALDriverH,
const char *pszOpenOptionName);

/* The following are deprecated */
const char CPL_DLL *CPL_STDCALL GDALGetDriverShortName(GDALDriverH);
const char CPL_DLL *CPL_STDCALL GDALGetDriverLongName(GDALDriverH);
Expand Down
8 changes: 8 additions & 0 deletions gcore/gdal_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -2157,6 +2157,14 @@ class CPL_DLL GDALDriver : public GDALMajorObject
CSLConstList papszVectorTranslateArguments,
char ***ppapszFailureReasons);

/**
* \brief Returns TRUE if the given open option is supported by the driver.
* @param pszOpenOptionName name of the open option to be checked
* @return TRUE if the driver supports the open option
* @since GDAL 3.11
*/
bool HasOpenOption(const char *pszOpenOptionName) const;

GDALDataset *
VectorTranslateFrom(const char *pszDestName, GDALDataset *poSourceDS,
CSLConstList papszVectorTranslateArguments,
Expand Down
39 changes: 39 additions & 0 deletions gcore/gdaldriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,28 @@ bool GDALDriver::CanVectorTranslateFrom(
return bRet;
}

bool GDALDriver::HasOpenOption(const char *pszOpenOptionName) const
{
if (pszOpenOptionName == nullptr)
return false;

// Const cast is safe here since we are only reading the metadata
auto pszOOMd{const_cast<GDALDriver *>(this)->GetMetadataItem(
GDAL_DMD_OPENOPTIONLIST)};
if (pszOOMd == nullptr)
return false;

const CPLXMLTreeCloser oXml{CPLParseXMLString(pszOOMd)};
for (CPLXMLNode *option = oXml->psChild; option != nullptr;
option = option->psNext)
{
if (EQUAL(CPLGetXMLValue(CPLGetXMLNode(option, "name"), nullptr, ""),
pszOpenOptionName))
return true;
}
return false;
}

/************************************************************************/
/* VectorTranslateFrom() */
/************************************************************************/
Expand Down Expand Up @@ -1985,6 +2007,23 @@ CPLErr CPL_STDCALL GDALCopyDatasetFiles(GDALDriverH hDriver,
return GDALDriver::FromHandle(hDriver)->CopyFiles(pszNewName, pszOldName);
}

/************************************************************************/
/* GDALDriverHasOpenOption() */
/************************************************************************/

/**
* \brief Returns TRUE if the given open option is supported by the driver.
* @param hDriver the handle of the driver
* @param pszOpenOptionName name of the open option to be checked
* @return TRUE if the driver supports the open option
* @since GDAL 3.11
*/
bool GDALDriverHasOpenOption(GDALDriverH hDriver, const char *pszOpenOptionName)
{
VALIDATE_POINTER1(hDriver, "GDALDriverHasOpenOption", false);
return GDALDriver::FromHandle(hDriver)->HasOpenOption(pszOpenOptionName);
}

/************************************************************************/
/* GDALGetDriverShortName() */
/************************************************************************/
Expand Down
4 changes: 4 additions & 0 deletions swig/include/Driver.i
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ public:
return GDALCopyDatasetFiles( self, newName, oldName );
}

bool HasOpenOption( const char *openOptionName ) {
return GDALDriverHasOpenOption( self, openOptionName );
}

#ifdef SWIGPYTHON
bool TestCapability(const char* cap) {
// TODO: should this also check DCAP entries in driver metadata?
Expand Down

0 comments on commit 7041096

Please sign in to comment.