Skip to content

Commit

Permalink
Merge pull request #10846 from rouault/remove_use_OGRSFDriver_OGRData…
Browse files Browse the repository at this point in the history
…Source

[Lint] Remove most remaining uses of deprecated OGRDataSource and OGRSFDriver classes
  • Loading branch information
rouault authored Sep 27, 2024
2 parents 725739a + f1ef574 commit c32fbfe
Show file tree
Hide file tree
Showing 125 changed files with 630 additions and 1,804 deletions.
134 changes: 67 additions & 67 deletions frmts/mbtiles/mbtilesdataset.cpp

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions frmts/nitf/nitfdump.c
Original file line number Diff line number Diff line change
Expand Up @@ -640,13 +640,14 @@ int main(int nArgc, char **papszArgv)

if (NITFDESExtractShapefile(psDES, szRadix))
{
OGRDataSourceH hDS;
GDALDatasetH hDS;
snprintf(szFilename, sizeof(szFilename), "%s.SHP", szRadix);
hDS = OGROpen(szFilename, FALSE, NULL);
hDS = GDALOpenEx(szFilename, GDAL_OF_VECTOR, NULL, NULL,
NULL);
if (hDS)
{
int nGeom = 0;
OGRLayerH hLayer = OGR_DS_GetLayer(hDS, 0);
OGRLayerH hLayer = GDALDatasetGetLayer(hDS, 0);
if (hLayer)
{
OGRFeatureH hFeat;
Expand All @@ -668,7 +669,7 @@ int main(int nArgc, char **papszArgv)
OGR_F_Destroy(hFeat);
}
}
OGR_DS_Destroy(hDS);
GDALClose(hDS);
}
}

Expand Down
14 changes: 8 additions & 6 deletions frmts/pdf/pdfcreatecopy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2178,13 +2178,15 @@ bool GDALPDFWriter::WriteOGRDataSource(const char *pszOGRDataSource,
const char *pszOGRLinkField,
int bWriteOGRAttributes)
{
OGRDataSourceH hDS = OGROpen(pszOGRDataSource, 0, nullptr);
GDALDatasetH hDS =
GDALOpenEx(pszOGRDataSource, GDAL_OF_VECTOR | GDAL_OF_VERBOSE_ERROR,
nullptr, nullptr, nullptr);
if (hDS == nullptr)
return false;

int iObj = 0;

int nLayers = OGR_DS_GetLayerCount(hDS);
int nLayers = GDALDatasetGetLayerCount(hDS);

char **papszLayerNames =
CSLTokenizeString2(pszOGRDisplayLayerNames, ",", 0);
Expand All @@ -2193,15 +2195,15 @@ bool GDALPDFWriter::WriteOGRDataSource(const char *pszOGRDataSource,
{
CPLString osLayerName;
if (CSLCount(papszLayerNames) < nLayers)
osLayerName = OGR_L_GetName(OGR_DS_GetLayer(hDS, iLayer));
osLayerName = OGR_L_GetName(GDALDatasetGetLayer(hDS, iLayer));
else
osLayerName = papszLayerNames[iLayer];

WriteOGRLayer(hDS, iLayer, pszOGRDisplayField, pszOGRLinkField,
osLayerName, bWriteOGRAttributes, iObj);
}

OGRReleaseDataSource(hDS);
GDALClose(hDS);

CSLDestroy(papszLayerNames);

Expand Down Expand Up @@ -2273,7 +2275,7 @@ void GDALPDFWriter::EndOGRLayer(GDALPDFLayerDesc &osVectorDesc)
/* WriteOGRLayer() */
/************************************************************************/

int GDALPDFWriter::WriteOGRLayer(OGRDataSourceH hDS, int iLayer,
int GDALPDFWriter::WriteOGRLayer(GDALDatasetH hDS, int iLayer,
const char *pszOGRDisplayField,
const char *pszOGRLinkField,
const std::string &osLayerName,
Expand All @@ -2286,7 +2288,7 @@ int GDALPDFWriter::WriteOGRLayer(OGRDataSourceH hDS, int iLayer,

GDALPDFLayerDesc osVectorDesc =
StartOGRLayer(osLayerName, bWriteOGRAttributes);
OGRLayerH hLyr = OGR_DS_GetLayer(hDS, iLayer);
OGRLayerH hLyr = GDALDatasetGetLayer(hDS, iLayer);

const auto poLayerDefn = OGRLayer::FromHandle(hLyr)->GetLayerDefn();
for (int i = 0; i < poLayerDefn->GetFieldCount(); i++)
Expand Down
2 changes: 1 addition & 1 deletion frmts/pdf/pdfcreatecopy.h
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ class GDALPDFWriter final : public GDALPDFBaseWriter
int bWriteOGRAttributes);
void EndOGRLayer(GDALPDFLayerDesc &osVectorDesc);

int WriteOGRLayer(OGRDataSourceH hDS, int iLayer,
int WriteOGRLayer(GDALDatasetH hDS, int iLayer,
const char *pszOGRDisplayField,
const char *pszOGRLinkField,
const std::string &osLayerName, int bWriteOGRAttributes,
Expand Down
6 changes: 3 additions & 3 deletions frmts/pdf/pdfwritabledataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,9 @@ OGRErr PDFWritableVectorDataset::SyncToDisk()
else
osLayerName = papszLayerNames[i];

oWriter.WriteOGRLayer((OGRDataSourceH)this, i, pszOGRDisplayField,
pszOGRLinkField, osLayerName, bWriteOGRAttributes,
iObj);
oWriter.WriteOGRLayer(GDALDataset::ToHandle(this), i,
pszOGRDisplayField, pszOGRLinkField, osLayerName,
bWriteOGRAttributes, iObj);
}

CSLDestroy(papszLayerNames);
Expand Down
87 changes: 44 additions & 43 deletions frmts/rasterlite/rasterlitecreatecopy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ char **RasterliteGetTileDriverOptions(CSLConstList papszOptions)
/* RasterliteInsertSRID () */
/************************************************************************/

static int RasterliteInsertSRID(OGRDataSourceH hDS, const char *pszWKT)
static int RasterliteInsertSRID(GDALDatasetH hDS, const char *pszWKT)
{
int nAuthorityCode = 0;
CPLString osAuthorityName, osProjCS, osProj4;
Expand Down Expand Up @@ -143,7 +143,7 @@ static int RasterliteInsertSRID(OGRDataSourceH hDS, const char *pszWKT)
osSQL.Printf("SELECT srid FROM spatial_ref_sys WHERE auth_srid = %d",
nAuthorityCode);
OGRLayerH hLyr =
OGR_DS_ExecuteSQL(hDS, osSQL.c_str(), nullptr, nullptr);
GDALDatasetExecuteSQL(hDS, osSQL.c_str(), nullptr, nullptr);
if (hLyr == nullptr)
{
nSRSId = nAuthorityCode;
Expand All @@ -162,7 +162,7 @@ static int RasterliteInsertSRID(OGRDataSourceH hDS, const char *pszWKT)
nSRSId, osAuthorityName.c_str(), nAuthorityCode,
osProj4.c_str());

OGR_DS_ExecuteSQL(hDS, osSQL.c_str(), nullptr, nullptr);
GDALDatasetExecuteSQL(hDS, osSQL.c_str(), nullptr, nullptr);
}
else
{
Expand All @@ -172,7 +172,7 @@ static int RasterliteInsertSRID(OGRDataSourceH hDS, const char *pszWKT)
nSRSId = OGR_F_GetFieldAsInteger(hFeat, 0);
OGR_F_Destroy(hFeat);
}
OGR_DS_ReleaseResultSet(hDS, hLyr);
GDALDatasetReleaseResultSet(hDS, hLyr);
}
}

Expand All @@ -183,13 +183,13 @@ static int RasterliteInsertSRID(OGRDataSourceH hDS, const char *pszWKT)
/* RasterliteCreateTables () */
/************************************************************************/

static OGRDataSourceH RasterliteCreateTables(OGRDataSourceH hDS,
const char *pszTableName,
int nSRSId, int bWipeExistingData)
static GDALDatasetH RasterliteCreateTables(GDALDatasetH hDS,
const char *pszTableName, int nSRSId,
int bWipeExistingData)
{
CPLString osSQL;

const CPLString osDBName = OGR_DS_GetName(hDS);
const CPLString osDBName = GDALGetDescription(hDS);

CPLString osRasterLayer;
osRasterLayer.Printf("%s_rasters", pszTableName);
Expand All @@ -199,7 +199,7 @@ static OGRDataSourceH RasterliteCreateTables(OGRDataSourceH hDS,

OGRLayerH hLyr;

if (OGR_DS_GetLayerByName(hDS, osRasterLayer.c_str()) == nullptr)
if (GDALDatasetGetLayerByName(hDS, osRasterLayer.c_str()) == nullptr)
{
/* --------------------------------------------------------------------
*/
Expand All @@ -212,7 +212,7 @@ static OGRDataSourceH RasterliteCreateTables(OGRDataSourceH hDS,
"id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,"
"raster BLOB NOT NULL)",
osRasterLayer.c_str());
OGR_DS_ExecuteSQL(hDS, osSQL.c_str(), nullptr, nullptr);
GDALDatasetExecuteSQL(hDS, osSQL.c_str(), nullptr, nullptr);

/* Create _metadata table */
osSQL.Printf("CREATE TABLE \"%s\" ("
Expand All @@ -224,42 +224,42 @@ static OGRDataSourceH RasterliteCreateTables(OGRDataSourceH hDS,
"pixel_x_size DOUBLE NOT NULL,"
"pixel_y_size DOUBLE NOT NULL)",
osMetadataLayer.c_str());
OGR_DS_ExecuteSQL(hDS, osSQL.c_str(), nullptr, nullptr);
GDALDatasetExecuteSQL(hDS, osSQL.c_str(), nullptr, nullptr);

/* Add geometry column to _metadata table */
osSQL.Printf(
"SELECT AddGeometryColumn('%s', 'geometry', %d, 'POLYGON', 2)",
osMetadataLayer.c_str(), nSRSId);
if ((hLyr = OGR_DS_ExecuteSQL(hDS, osSQL.c_str(), nullptr, nullptr)) ==
nullptr)
if ((hLyr = GDALDatasetExecuteSQL(hDS, osSQL.c_str(), nullptr,
nullptr)) == nullptr)
{
CPLError(CE_Failure, CPLE_AppDefined,
"Check that the OGR SQLite driver has Spatialite support");
OGRReleaseDataSource(hDS);
GDALClose(hDS);
return nullptr;
}
OGR_DS_ReleaseResultSet(hDS, hLyr);
GDALDatasetReleaseResultSet(hDS, hLyr);

/* Create spatial index on _metadata table */
osSQL.Printf("SELECT CreateSpatialIndex('%s', 'geometry')",
osMetadataLayer.c_str());
if ((hLyr = OGR_DS_ExecuteSQL(hDS, osSQL.c_str(), nullptr, nullptr)) ==
nullptr)
if ((hLyr = GDALDatasetExecuteSQL(hDS, osSQL.c_str(), nullptr,
nullptr)) == nullptr)
{
OGRReleaseDataSource(hDS);
GDALClose(hDS);
return nullptr;
}
OGR_DS_ReleaseResultSet(hDS, hLyr);
GDALDatasetReleaseResultSet(hDS, hLyr);

/* Create statistics tables */
osSQL.Printf("SELECT UpdateLayerStatistics()");
CPLPushErrorHandler(CPLQuietErrorHandler);
hLyr = OGR_DS_ExecuteSQL(hDS, osSQL.c_str(), nullptr, nullptr);
hLyr = GDALDatasetExecuteSQL(hDS, osSQL.c_str(), nullptr, nullptr);
CPLPopErrorHandler();
OGR_DS_ReleaseResultSet(hDS, hLyr);
GDALDatasetReleaseResultSet(hDS, hLyr);

/* Re-open the DB to take into account the new tables*/
OGRReleaseDataSource(hDS);
GDALClose(hDS);

hDS = RasterliteOpenSQLiteDB(osDBName.c_str(), GA_Update);
}
Expand All @@ -270,7 +270,7 @@ static OGRDataSourceH RasterliteCreateTables(OGRDataSourceH hDS,
osSQL.Printf(
"SELECT srid FROM geometry_columns WHERE f_table_name = '%s'",
osMetadataLayer.c_str());
hLyr = OGR_DS_ExecuteSQL(hDS, osSQL.c_str(), nullptr, nullptr);
hLyr = GDALDatasetExecuteSQL(hDS, osSQL.c_str(), nullptr, nullptr);
if (hLyr)
{
int nExistingSRID = -1;
Expand All @@ -280,7 +280,7 @@ static OGRDataSourceH RasterliteCreateTables(OGRDataSourceH hDS,
nExistingSRID = OGR_F_GetFieldAsInteger(hFeat, 0);
OGR_F_Destroy(hFeat);
}
OGR_DS_ReleaseResultSet(hDS, hLyr);
GDALDatasetReleaseResultSet(hDS, hLyr);

if (nExistingSRID != nSRSId)
{
Expand All @@ -289,18 +289,18 @@ static OGRDataSourceH RasterliteCreateTables(OGRDataSourceH hDS,
osSQL.Printf("UPDATE geometry_columns SET srid = %d "
"WHERE f_table_name = \"%s\"",
nSRSId, osMetadataLayer.c_str());
OGR_DS_ExecuteSQL(hDS, osSQL.c_str(), nullptr, nullptr);
GDALDatasetExecuteSQL(hDS, osSQL.c_str(), nullptr, nullptr);

/* Re-open the DB to take into account the change of SRS */
OGRReleaseDataSource(hDS);
GDALClose(hDS);

hDS = RasterliteOpenSQLiteDB(osDBName.c_str(), GA_Update);
}
else
{
CPLError(CE_Failure, CPLE_NotSupported,
"New data has not the same SRS as existing data");
OGRReleaseDataSource(hDS);
GDALClose(hDS);
return nullptr;
}
}
Expand All @@ -309,10 +309,10 @@ static OGRDataSourceH RasterliteCreateTables(OGRDataSourceH hDS,
if (bWipeExistingData)
{
osSQL.Printf("DELETE FROM \"%s\"", osRasterLayer.c_str());
OGR_DS_ExecuteSQL(hDS, osSQL.c_str(), nullptr, nullptr);
GDALDatasetExecuteSQL(hDS, osSQL.c_str(), nullptr, nullptr);

osSQL.Printf("DELETE FROM \"%s\"", osMetadataLayer.c_str());
OGR_DS_ExecuteSQL(hDS, osSQL.c_str(), nullptr, nullptr);
GDALDatasetExecuteSQL(hDS, osSQL.c_str(), nullptr, nullptr);
}
}

Expand Down Expand Up @@ -468,20 +468,20 @@ GDALDataset *RasterliteCreateCopy(const char *pszFilename, GDALDataset *poSrcDS,
/* Create or open the SQLite DB */
/* -------------------------------------------------------------------- */

OGRSFDriverH hSQLiteDriver = OGRGetDriverByName("SQLite");
GDALDriverH hSQLiteDriver = GDALGetDriverByName("SQLite");
if (hSQLiteDriver == nullptr)
{
CPLError(CE_Failure, CPLE_AppDefined, "Cannot load OGR SQLite driver");
return nullptr;
}

OGRDataSourceH hDS;
GDALDatasetH hDS;

if (!bExists)
{
char **papszOGROptions = CSLAddString(nullptr, "SPATIALITE=YES");
hDS = OGR_Dr_CreateDataSource(hSQLiteDriver, osDBName.c_str(),
papszOGROptions);
hDS = GDALCreate(hSQLiteDriver, osDBName.c_str(), 0, 0, 0, GDT_Unknown,
papszOGROptions);
CSLDestroy(papszOGROptions);
}
else
Expand Down Expand Up @@ -514,14 +514,15 @@ GDALDataset *RasterliteCreateCopy(const char *pszFilename, GDALDataset *poSrcDS,
if (hDS == nullptr)
return nullptr;

OGRLayerH hRasterLayer = OGR_DS_GetLayerByName(hDS, osRasterLayer.c_str());
OGRLayerH hRasterLayer =
GDALDatasetGetLayerByName(hDS, osRasterLayer.c_str());
OGRLayerH hMetadataLayer =
OGR_DS_GetLayerByName(hDS, osMetadataLayer.c_str());
GDALDatasetGetLayerByName(hDS, osMetadataLayer.c_str());
if (hRasterLayer == nullptr || hMetadataLayer == nullptr)
{
CPLError(CE_Failure, CPLE_AppDefined,
"Cannot find metadata and/or raster tables");
OGRReleaseDataSource(hDS);
GDALClose(hDS);
return nullptr;
}

Expand All @@ -545,7 +546,7 @@ GDALDataset *RasterliteCreateCopy(const char *pszFilename, GDALDataset *poSrcDS,

int nOverlappingGeoms = 0;
OGRLayerH hCountLyr =
OGR_DS_ExecuteSQL(hDS, osSQL.c_str(), nullptr, nullptr);
GDALDatasetExecuteSQL(hDS, osSQL.c_str(), nullptr, nullptr);
if (hCountLyr)
{
OGRFeatureH hFeat = OGR_L_GetNextFeature(hCountLyr);
Expand All @@ -554,7 +555,7 @@ GDALDataset *RasterliteCreateCopy(const char *pszFilename, GDALDataset *poSrcDS,
nOverlappingGeoms = OGR_F_GetFieldAsInteger(hFeat, 0);
OGR_F_Destroy(hFeat);
}
OGR_DS_ReleaseResultSet(hDS, hCountLyr);
GDALDatasetReleaseResultSet(hDS, hCountLyr);
}

if (nOverlappingGeoms != 0)
Expand All @@ -577,7 +578,7 @@ GDALDataset *RasterliteCreateCopy(const char *pszFilename, GDALDataset *poSrcDS,
nBlockXSize, nBlockYSize, cpl::fits_on<int>(nBands * nDataTypeSize)));
if (pabyMEMDSBuffer == nullptr)
{
OGRReleaseDataSource(hDS);
GDALClose(hDS);
return nullptr;
}

Expand All @@ -591,7 +592,7 @@ GDALDataset *RasterliteCreateCopy(const char *pszFilename, GDALDataset *poSrcDS,
char **papszTileDriverOptions =
RasterliteGetTileDriverOptions(papszOptions);

OGR_DS_ExecuteSQL(hDS, "BEGIN", nullptr, nullptr);
GDALDatasetExecuteSQL(hDS, "BEGIN", nullptr, nullptr);

CPLErr eErr = CE_None;
for (int nBlockYOff = 0; eErr == CE_None && nBlockYOff < nYBlocks;
Expand Down Expand Up @@ -718,15 +719,15 @@ GDALDataset *RasterliteCreateCopy(const char *pszFilename, GDALDataset *poSrcDS,
VSIUnlink((osTempFileName + ".aux.xml").c_str());

if (eErr == CE_None)
OGR_DS_ExecuteSQL(hDS, "COMMIT", nullptr, nullptr);
GDALDatasetExecuteSQL(hDS, "COMMIT", nullptr, nullptr);
else
OGR_DS_ExecuteSQL(hDS, "ROLLBACK", nullptr, nullptr);
GDALDatasetExecuteSQL(hDS, "ROLLBACK", nullptr, nullptr);

CSLDestroy(papszTileDriverOptions);

VSIFree(pabyMEMDSBuffer);

OGRReleaseDataSource(hDS);
GDALClose(hDS);

if (eErr == CE_Failure)
return nullptr;
Expand Down
Loading

0 comments on commit c32fbfe

Please sign in to comment.