Commit a3cb08d1 authored by Julien Michel's avatar Julien Michel

ENH: Remove the TestCapability() method from the proxy namespace, has it is...

ENH: Remove the TestCapability() method from the proxy namespace, has it is only needed for the Delete() method and does not exist anymore in gdal 2.x. Improved Delete() method instead, and simplified OGRDataSourceWrapper and OGRVectorDataIO code accordingly.
parent b869f570
......@@ -108,12 +108,9 @@ namespace version_proxy
* Calls OGRDataSource::DeleteDataSource for gdal 1.x implementation
* and GDALDriver::Delete for gdal 2.x implementation.
*
* \param driver Pointer to the driver used for creation. Will not
* be checked for null pointer.
*
* \param name Name of the dataset to destroy.
*/
bool Delete(GDALDriverType * driver, const char * name);
bool Delete(const char * name);
/**
* This function returns a pointer to the driver from its name.
......@@ -129,24 +126,6 @@ namespace version_proxy
*/
GDALDriverType * GetDriverByName(const char * name);
/**
* Test driver and dataset for a given capability.
*
* Calls OGRSFDriver::TestCapability for gdal 1.x implementation and
* GDALDataset::TestCapability for gdal 2.x implementation.
*
* \param driver Pointer to the driver to test. Will not
* be checked for null pointer.
*
* \param dataset Pointer to the dataset to test. Will not be
* checked for null pointer.
*
* \param capability Name of the capability to test.
*
* \return True if the capability is supported.
*/
bool TestCapability(const GDALDriverType * driver, const GDALDatasetType * dataset, const char * capability);
/**
* Sync dataset to disk.
*
......
......@@ -175,32 +175,12 @@ otb::ogr::DataSource::Pointer otb::ogr::DataSource::OpenDataSource(std::string c
void DeleteDataSource(std::string const& datasourceName)
{
// Attempt to delete the datasource if it already exists
otb::ogr::version_proxy::GDALDatasetType * poDS = otb::ogr::version_proxy::Open(datasourceName.c_str(),false);
if (poDS != NULL)
bool ret = otb::ogr::version_proxy::Delete(datasourceName.c_str());
if (!ret)
{
otb::ogr::version_proxy::GDALDriverType * ogrDriver = poDS->GetDriver();
//Erase the data if possible
if (otb::ogr::version_proxy::TestCapability(ogrDriver,poDS,ODrCDeleteDataSource))
{
//Delete datasource
bool ret = otb::ogr::version_proxy::Delete(ogrDriver,datasourceName.c_str());
if (!ret)
{
otb::ogr::version_proxy::Close(poDS);
itkGenericExceptionMacro(<< "Deletion of data source " << datasourceName
<< " failed: " << CPLGetLastErrorMsg());
}
}
else
{
otb::ogr::version_proxy::Close(poDS);
itkGenericExceptionMacro(<< "Cannot delete data source " << datasourceName);
}
otb::ogr::version_proxy::Close(poDS);
} // if (poDS != NULL)
itkGenericExceptionMacro(<< "Deletion of data source " << datasourceName
<< " failed: " << CPLGetLastErrorMsg());
}
}
otb::ogr::DataSource::Pointer
......@@ -213,7 +193,14 @@ otb::ogr::DataSource::New(std::string const& datasourceName, Modes::type mode)
Drivers::Init();
if (mode == Modes::Overwrite)
ogr::version_proxy::GDALDatasetType * ds = ogr::version_proxy::Open(datasourceName.c_str(),true);
bool ds_exists = (ds!=NULL);
ogr::version_proxy::Close(ds);
if (ds_exists && mode == Modes::Overwrite)
{
DeleteDataSource(datasourceName);
}
......@@ -693,7 +680,7 @@ void otb::ogr::DataSource::PrintSelf(
bool otb::ogr::DataSource::HasCapability(std::string const& capabilityName) const
{
assert(m_DataSource && "Datasource not initialized");
return otb::ogr::version_proxy::TestCapability(m_DataSource->GetDriver(),m_DataSource,capabilityName.c_str());
return m_DataSource->TestCapability(capabilityName.c_str());
}
void otb::ogr::DataSource::SyncToDisk()
......
......@@ -53,11 +53,25 @@ GDALDatasetType * Create(GDALDriverType * driver, const char * name)
return ds;
}
bool Delete(GDALDriverType * driver, const char * name)
bool Delete(const char * name)
{
OGRErr ret = driver->DeleteDataSource(name);
// Open dataset
GDALDatasetType * poDS = Open(name,false);
GDALDriverType * poDriver = NULL;
if(poDS)
{
poDriver = poDS->GetDriver();
Close(poDS);
}
if(poDriver && poDriver->TestCapability(ODrCDeleteDataSource))
{
OGRErr ret = poDriver->DeleteDataSource(name);
return (ret == OGRERR_NONE);
}
return (ret == OGRERR_NONE);
return false;
}
GDALDriverType * GetDriverByName(const char * name)
......@@ -65,12 +79,6 @@ GDALDriverType * GetDriverByName(const char * name)
return OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(name);
}
bool TestCapability(const GDALDriverType * driver, const GDALDatasetType *, const char * capability)
{
// Class OGRDataSource is not const correct
return const_cast<GDALDriverType * >(driver)->TestCapability(capability);
}
std::string GetDatasetClassName()
{
return std::string("OGRDataSource");
......
......@@ -26,6 +26,7 @@
#include "gdal_priv.h"
#endif
namespace otb
{
namespace ogr
......@@ -48,11 +49,24 @@ GDALDatasetType * Create(GDALDriverType * driver, const char * name)
return driver->Create(name,0,0,0,GDT_Unknown,NULL);
}
bool Delete(GDALDriverType * driver, const char * name)
bool Delete(const char * name)
{
OGRErr ret = driver->Delete(name);
// Open dataset
GDALDatasetType * poDS = otb::ogr::version_proxy::Open(name,false);
GDALDriverType * poDriver = NULL;
if(poDS)
{
poDriver = poDS->GetDriver();
Close(poDS);
}
if(poDriver)
{
OGRErr ret = poDriver->Delete(name);
return (ret == OGRERR_NONE);
return (ret == OGRERR_NONE);
}
return false;
}
GDALDriverType * GetDriverByName(const char * name)
......@@ -60,12 +74,6 @@ GDALDriverType * GetDriverByName(const char * name)
return GetGDALDriverManager()->GetDriverByName(name);
}
bool TestCapability(const GDALDriverType *, const GDALDatasetType * dataset, const char * capability)
{
// Const correctness problem in GDALDataset class
return const_cast<GDALDatasetType *>(dataset)->TestCapability(capability);
}
std::string GetDatasetClassName()
{
return std::string("GDALDataset");
......
......@@ -225,18 +225,7 @@ void OGRVectorDataIO::Write(const itk::DataObject* datag, char ** /** unused */)
}
// Erase the dataSource if already exist
//TODO investigate the possibility of giving the option OVERWRITE=YES to the CreateDataSource method
ogr::version_proxy::GDALDatasetType * poDS = ogr::version_proxy::Open(this->m_FileName.c_str(),false);
if (poDS != NULL)
{
//Erase the data if possible
if (ogr::version_proxy::TestCapability(ogrDriver,poDS,ODrCDeleteDataSource))
{
//Delete datasource
ogr::version_proxy::Delete(ogrDriver,m_FileName.c_str());
}
}
ogr::version_proxy::Close(poDS);
ogr::version_proxy::Delete(this->m_FileName.c_str());
// m_DataSource = OGRSFDriverRegistrar::Open(this->m_FileName.c_str(), TRUE);
m_DataSource = ogr::version_proxy::Create(ogrDriver,this->m_FileName.c_str());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment