Commit 8511ebdc authored by Cédric Traizet's avatar Cédric Traizet

ENH: removed version_proxy functions Open and Create

parent 4ef17d6e
......@@ -64,38 +64,6 @@ namespace version_proxy
* See function documentation for details.
*/
/**
* This function opens a file, possibly in read-only mode, and returns
* a dataset.
*
* Calls OGRSFDriverRegistrar::Open for gdal 1.x implementation and GDALopenEx for
* gdal 2.x implementation.
* \param filename Filename of the file to open
* \param readOnly: If true, dataset is open in read-only mode.
* \return NULL if file could not be open.
*/
OTBGdalAdapters_EXPORT
GDALDataset * Open(const char * filename, bool readOnly = true , std::vector< std::string > const & options = std::vector< std::string >() );
/**
* This function creates a new dataset.
*
* Calls OGRSFDriver::CreateDataSource for gdal 1.x implementation
* and GDALDriver::Create with (0,0) raster size 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 create.
*
* \return NULL if dataset could not be created.
*/
OTBGdalAdapters_EXPORT
GDALDataset * Create(GDALDriver * driver, const char * name , std::vector< std::string > const & options = std::vector< std::string >() );
/**
* This function physically deletes an existing dataset.
*
......
......@@ -129,7 +129,13 @@ otb::ogr::DataSource::DataSource()
GDALDriver * d =
GetGDALDriverManager()->GetDriverByName("Memory");
assert(d && "OGR Memory driver not found");
m_DataSource = ogr::version_proxy::Create(d,"in-memory");
m_DataSource = d->Create( "in-memory" ,
0 ,
0 ,
0 ,
GDT_Unknown ,
0 );
if (!m_DataSource) {
itkExceptionMacro(<< "Failed to create OGRMemDataSource: "
<< CPLGetLastErrorMsg());
......@@ -155,10 +161,12 @@ otb::ogr::DataSource::Pointer otb::ogr::DataSource::OpenDataSource(std::string c
std::string simpleFileName = fileNameHelper->GetSimpleFileName();
bool update = (mode != Modes::Read);
GDALDataset * source =
ogr::version_proxy::Open( simpleFileName.c_str() ,
!update ,
fileNameHelper->GetGDALOpenOptions() );
GDALDataset * source = (GDALDataset *)GDALOpenEx(
simpleFileName.c_str(),
(update? GDAL_OF_UPDATE: GDAL_OF_READONLY) | GDAL_OF_VECTOR,
NULL,
otb::ogr::StringListConverter( fileNameHelper->GetGDALOpenOptions() ).to_ogr(),
NULL);
if (!source)
{
// In read mode, this is a failure
......@@ -187,10 +195,13 @@ otb::ogr::DataSource::Pointer otb::ogr::DataSource::OpenDataSource(std::string c
<< ", check your OGR configuration for available drivers." );
}
source = ogr::version_proxy::Create(
d ,
simpleFileName.c_str() ,
fileNameHelper->GetGDALCreationOptions() );
source = d->Create( simpleFileName.c_str() ,
0 ,
0 ,
0 ,
GDT_Unknown ,
otb::ogr::StringListConverter(
fileNameHelper->GetGDALCreationOptions() ).to_ogr() );
if (!source) {
itkGenericExceptionMacro(<< "Failed to create GDALDataset <"
<< simpleFileName << "> (driver name: <" << driverName
......@@ -228,9 +239,12 @@ otb::ogr::DataSource::New(std::string const& datasourceName, Modes::type mode)
}
Drivers::Init();
GDALDataset * ds =
ogr::version_proxy::Open( simpleFileName.c_str() , true );
GDALDataset * ds = (GDALDataset *)GDALOpenEx(
simpleFileName.c_str(),
GDAL_OF_READONLY | GDAL_OF_VECTOR,
NULL,
NULL,
NULL);
bool ds_exists = (ds!=nullptr);
GDALClose(ds);
......
......@@ -49,45 +49,16 @@ namespace ogr
namespace version_proxy
{
GDALDataset * Open(const char * filename, bool readOnly , std::vector< std::string > const & options )
bool Delete(const char * name)
{
#if GDAL_VERSION_NUM<2000000
(void)options;
return OGRSFDriverRegistrar::Open(filename,!readOnly);
#else
return (GDALDataset *)GDALOpenEx(
filename,
(readOnly? GDAL_OF_READONLY : GDAL_OF_UPDATE) | GDAL_OF_VECTOR,
// Open dataset
GDALDataset * poDS = (GDALDataset *)GDALOpenEx(
name,
GDAL_OF_UPDATE | GDAL_OF_VECTOR,
NULL,
NULL,
otb::ogr::StringListConverter( options ).to_ogr(),
NULL);
#endif
}
GDALDataset * Create(GDALDriver * driver, const char * name , std::vector< std::string > const & options )
{
#if GDAL_VERSION_NUM<2000000
(void)options;
GDALDataset * ds = driver->CreateDataSource(name);
if(ds)
ds->SetDriver(driver);
return ds;
#else
return driver->Create( name ,
0 ,
0 ,
0 ,
GDT_Unknown ,
otb::ogr::StringListConverter( options ).to_ogr() );
#endif
}
bool Delete(const char * name)
{
// Open dataset
GDALDataset * poDS = otb::ogr::version_proxy::Open(name,false);
GDALDriver * poDriver = NULL;
if(poDS)
{
......
......@@ -1037,7 +1037,12 @@ std::vector<OGRLayer*> OGRIOHelper
{
const char * driverName = "Memory";
GDALDriver * ogrDriver = GetGDALDriverManager()->GetDriverByName(driverName);
inMemoryDataSource = ogr::version_proxy::Create(ogrDriver,"tempDataSource");
inMemoryDataSource = ogrDriver->Create( "tempDataSource",
0,
0,
0,
GDT_Unknown,
0);
}
std::vector<OGRLayer*> ogrLayerVector;
......
......@@ -54,8 +54,12 @@ OGRVectorDataIO::~OGRVectorDataIO()
bool
OGRVectorDataIO::CanReadFile(const char* filename) const
{
GDALDataset * poDS = ogr::version_proxy::Open(filename, true);
GDALDataset * poDS = (GDALDataset *)GDALOpenEx(
filename,
GDAL_OF_READONLY | GDAL_OF_VECTOR,
NULL,
NULL,
NULL);
if (poDS == nullptr)
{
std::cerr<<"Can not read file "<<filename<<" with GDALOpen"<<std::endl;
......@@ -93,8 +97,12 @@ OGRVectorDataIO
this->CloseInternalDataSource();
}
m_DataSource = ogr::version_proxy::Open(this->m_FileName.c_str(),true);
m_DataSource = (GDALDataset *)GDALOpenEx(
this->m_FileName.c_str(),
GDAL_OF_READONLY | GDAL_OF_VECTOR,
NULL,
NULL,
NULL);
if (m_DataSource == nullptr)
{
itkExceptionMacro(<< "Failed to open data file " << this->m_FileName);
......@@ -232,9 +240,12 @@ void OGRVectorDataIO::Write(const itk::DataObject* datag, char ** /** unused */)
// Erase the dataSource if already exist
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());
m_DataSource = ogrDriver->Create( this->m_FileName.c_str(),
0,
0,
0,
GDT_Unknown,
0);
// check the created data source
if (m_DataSource == nullptr)
{
......
......@@ -1734,21 +1734,41 @@ int TestHelper::RegressionTestOgrFile(const char *testOgrFilename, const char *b
GDALDriver * test_poDriver = nullptr;
//OGRGeometry * test_poSpatialFilter = NULL;
ref_poDS = otb::ogr::version_proxy::Open(ref_pszDataSource, false);
ref_poDS = (GDALDataset *)GDALOpenEx(
ref_pszDataSource,
GDAL_OF_UPDATE | GDAL_OF_VECTOR,
NULL,
NULL,
NULL);
if (ref_poDS == nullptr && !bReadOnly)
{
ref_poDS = otb::ogr::version_proxy::Open(ref_pszDataSource, true);
ref_poDS = (GDALDataset *)GDALOpenEx(
ref_pszDataSource,
GDAL_OF_READONLY | GDAL_OF_VECTOR,
NULL,
NULL,
NULL);
bReadOnly = TRUE;
if (ref_poDS != nullptr && m_ReportErrors)
{
std::cout << "Had to open REF data source read-only."<<std::endl;
}
}
test_poDS = otb::ogr::version_proxy::Open(ref_pszDataSource, bReadOnly);
test_poDS = (GDALDataset *)GDALOpenEx(
ref_pszDataSource,
(bReadOnly? GDAL_OF_READONLY : GDAL_OF_UPDATE) | GDAL_OF_VECTOR,
NULL,
NULL,
NULL);
if (test_poDS == nullptr && !bReadOnly)
{
test_poDS = otb::ogr::version_proxy::Open(ref_pszDataSource, bReadOnly);
test_poDS = (GDALDataset *)GDALOpenEx(
ref_pszDataSource,
(bReadOnly? GDAL_OF_READONLY : GDAL_OF_UPDATE) | GDAL_OF_VECTOR,
NULL,
NULL,
NULL);
bReadOnly = TRUE;
if (test_poDS != nullptr && m_ReportErrors)
......
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