Skip to content
Permalink
Browse files

[OGR provider] Use GDAL driver and dataset API

This is a preparation work to be able to provide open options to vector
drivers, which require using the GDALOpenEx() API, and thus requires to
use GDALDatasetH instead of OGRDataSourceH, GDALDriverH instead of OGRDriverH,
GDALOpenEx() instead of OGROpen(), GDALClose() instead of OGR_DS_Destroy(),
GDALDatasetXXXX instead of OGR_DS_XXXX, etc...
  • Loading branch information
rouault committed Sep 15, 2017
1 parent a081905 commit 78591b77bdf01cb98279e5b4e20ca95301efa931
@@ -18,13 +18,13 @@

#include "qgsconnectionpool.h"
#include "qgsogrprovider.h"
#include <ogr_api.h>
#include <gdal.h>


struct QgsOgrConn
{
QString path;
OGRDataSourceH ds;
GDALDatasetH ds;
bool valid;
};

@@ -37,14 +37,14 @@ inline void qgsConnectionPool_ConnectionCreate( const QString &connInfo, QgsOgrC
{
c = new QgsOgrConn;
QString filePath = connInfo.left( connInfo.indexOf( QLatin1String( "|" ) ) );
c->ds = OGROpen( filePath.toUtf8().constData(), false, nullptr );
c->ds = GDALOpenEx( filePath.toUtf8().constData(), GDAL_OF_VECTOR, nullptr, nullptr, nullptr );
c->path = connInfo;
c->valid = true;
}

inline void qgsConnectionPool_ConnectionDestroy( QgsOgrConn *c )
{
QgsOgrProviderUtils::OGRDestroyWrapper( c->ds );
QgsOgrProviderUtils::GDALCloseWrapper( c->ds );
delete c;
}

@@ -51,13 +51,13 @@ QgsOgrLayerItem::QgsOgrLayerItem( QgsDataItem *parent,
setState( Populated ); // children are not expected

OGRRegisterAll();
OGRSFDriverH hDriver;
OGRDataSourceH hDataSource = QgsOgrProviderUtils::OGROpenWrapper( mPath.toUtf8().constData(), true, &hDriver );
GDALDriverH hDriver;
GDALDatasetH hDataSource = QgsOgrProviderUtils::GDALOpenWrapper( mPath.toUtf8().constData(), true, &hDriver );

if ( hDataSource )
{
QString driverName = OGR_Dr_GetName( hDriver );
OGR_DS_Destroy( hDataSource );
QString driverName = GDALGetDriverShortName( hDriver );
GDALClose( hDataSource );

if ( driverName == QLatin1String( "ESRI Shapefile" ) )
mCapabilities |= SetCrs;
@@ -336,9 +336,9 @@ void QgsOgrLayerItem::deleteLayer()

// -------

static QgsOgrLayerItem *dataItemForLayer( QgsDataItem *parentItem, QString name, QString path, OGRDataSourceH hDataSource, int layerId, bool isSubLayer = false )
static QgsOgrLayerItem *dataItemForLayer( QgsDataItem *parentItem, QString name, QString path, GDALDatasetH hDataSource, int layerId, bool isSubLayer = false )
{
OGRLayerH hLayer = OGR_DS_GetLayer( hDataSource, layerId );
OGRLayerH hLayer = GDALDatasetGetLayer( hDataSource, layerId );
OGRFeatureDefnH hDef = OGR_L_GetLayerDefn( hLayer );

QgsLayerItem::LayerType layerType = QgsLayerItem::Vector;
@@ -404,11 +404,11 @@ QVector<QgsDataItem *> QgsOgrDataCollectionItem::createChildren()
{
QVector<QgsDataItem *> children;

OGRSFDriverH hDriver;
OGRDataSourceH hDataSource = QgsOgrProviderUtils::OGROpenWrapper( mPath.toUtf8().constData(), false, &hDriver );
GDALDriverH hDriver;
GDALDatasetH hDataSource = QgsOgrProviderUtils::GDALOpenWrapper( mPath.toUtf8().constData(), false, &hDriver );
if ( !hDataSource )
return children;
int numLayers = OGR_DS_GetLayerCount( hDataSource );
int numLayers = GDALDatasetGetLayerCount( hDataSource );

children.reserve( numLayers );
for ( int i = 0; i < numLayers; ++i )
@@ -417,7 +417,7 @@ QVector<QgsDataItem *> QgsOgrDataCollectionItem::createChildren()
children.append( item );
}

OGR_DS_Destroy( hDataSource );
GDALClose( hDataSource );

return children;
}
@@ -576,20 +576,21 @@ QGISEXTERN QgsDataItem *dataItem( QString path, QgsDataItem *parentItem )
// if this is a VRT file make sure it is vector VRT to avoid duplicates
if ( suffix == QLatin1String( "vrt" ) )
{
OGRSFDriverH hDriver = OGRGetDriverByName( "VRT" );
GDALDriverH hDriver = GDALGetDriverByName( "OGR_VRT" );
if ( hDriver )
{
// do not print errors, but write to debug
CPLPushErrorHandler( CPLQuietErrorHandler );
CPLErrorReset();
OGRDataSourceH hDataSource = OGR_Dr_Open( hDriver, path.toLocal8Bit().constData(), 0 );
GDALDatasetH hDataSource = GDALOpenEx(
path.toLocal8Bit().constData(), GDAL_OF_VECTOR, nullptr, nullptr, nullptr );
CPLPopErrorHandler();
if ( ! hDataSource )
{
QgsDebugMsgLevel( "Skipping VRT file because root is not a OGR VRT", 2 );
return nullptr;
}
OGR_DS_Destroy( hDataSource );
GDALClose( hDataSource );
}
}
// Handle collections
@@ -609,22 +610,22 @@ QGISEXTERN QgsDataItem *dataItem( QString path, QgsDataItem *parentItem )

// test that file is valid with OGR
OGRRegisterAll();
OGRSFDriverH hDriver;
GDALDriverH hDriver;
// do not print errors, but write to debug
CPLPushErrorHandler( CPLQuietErrorHandler );
CPLErrorReset();
OGRDataSourceH hDataSource = QgsOgrProviderUtils::OGROpenWrapper( path.toUtf8().constData(), false, &hDriver );
GDALDatasetH hDataSource = QgsOgrProviderUtils::GDALOpenWrapper( path.toUtf8().constData(), false, &hDriver );
CPLPopErrorHandler();

if ( ! hDataSource )
{
QgsDebugMsg( QString( "OGROpen error # %1 : %2 on %3" ).arg( CPLGetLastErrorNo() ).arg( CPLGetLastErrorMsg() ).arg( path ) );
QgsDebugMsg( QString( "GDALOpen error # %1 : %2 on %3" ).arg( CPLGetLastErrorNo() ).arg( CPLGetLastErrorMsg() ).arg( path ) );
return nullptr;
}

QgsDebugMsgLevel( QString( "OGR Driver : %1" ).arg( OGR_Dr_GetName( hDriver ) ), 2 );
QgsDebugMsgLevel( QString( "GDAL Driver : %1" ).arg( GDALGetDriverShortName( hDriver ) ), 2 );

int numLayers = OGR_DS_GetLayerCount( hDataSource );
int numLayers = GDALDatasetGetLayerCount( hDataSource );

QgsDataItem *item = nullptr;

@@ -639,6 +640,6 @@ QGISEXTERN QgsDataItem *dataItem( QString path, QgsDataItem *parentItem )
item = new QgsOgrDataCollectionItem( parentItem, name, path );
}

OGR_DS_Destroy( hDataSource );
GDALClose( hDataSource );
return item;
}
@@ -55,11 +55,11 @@ QgsOgrFeatureIterator::QgsOgrFeatureIterator( QgsOgrFeatureSource *source, bool

if ( mSource->mLayerName.isNull() )
{
ogrLayer = OGR_DS_GetLayer( mConn->ds, mSource->mLayerIndex );
ogrLayer = GDALDatasetGetLayer( mConn->ds, mSource->mLayerIndex );
}
else
{
ogrLayer = OGR_DS_GetLayerByName( mConn->ds, mSource->mLayerName.toUtf8().constData() );
ogrLayer = GDALDatasetGetLayerByName( mConn->ds, mSource->mLayerName.toUtf8().constData() );
}
if ( !ogrLayer )
{
@@ -306,7 +306,7 @@ bool QgsOgrFeatureIterator::close()

if ( mSubsetStringSet )
{
OGR_DS_ReleaseResultSet( mConn->ds, ogrLayer );
GDALDatasetReleaseResultSet( mConn->ds, ogrLayer );
}

if ( mConn )
@@ -420,7 +420,7 @@ QgsOgrFeatureSource::QgsOgrFeatureSource( const QgsOgrProvider *p )
, mFields( p->mAttributeFields )
, mFirstFieldIsFid( p->mFirstFieldIsFid )
, mOgrGeometryTypeFilter( QgsOgrProvider::ogrWkbSingleFlatten( p->mOgrGeometryTypeFilter ) )
, mDriverName( p->ogrDriverName )
, mDriverName( p->mGDALDriverName )
, mCrs( p->crs() )
, mWkbType( p->wkbType() )
{

0 comments on commit 78591b7

Please sign in to comment.
You can’t perform that action at this time.