Skip to content

Commit

Permalink
Merge pull request #5206 from rouault/ogrprovider_use_gdaldriver_and_…
Browse files Browse the repository at this point in the history
…gdaldataset_api

[OGR provider] USE GDAL driver and dataset API, and pass FORCE_SRS_DETECTION=YES for GML
  • Loading branch information
rouault committed Sep 18, 2017
2 parents 938f261 + 0ee9d66 commit 7de9999
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 183 deletions.
8 changes: 4 additions & 4 deletions src/providers/ogr/qgsogrconnpool.h
Expand Up @@ -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;
};

Expand All @@ -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;
}

Expand Down
39 changes: 20 additions & 19 deletions src/providers/ogr/qgsogrdataitems.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 )
Expand All @@ -417,7 +417,7 @@ QVector<QgsDataItem *> QgsOgrDataCollectionItem::createChildren()
children.append( item );
}

OGR_DS_Destroy( hDataSource );
GDALClose( hDataSource );

return children;
}
Expand Down Expand Up @@ -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
Expand All @@ -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;

Expand All @@ -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;
}
8 changes: 4 additions & 4 deletions src/providers/ogr/qgsogrfeatureiterator.cpp
Expand Up @@ -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 )
{
Expand Down Expand Up @@ -306,7 +306,7 @@ bool QgsOgrFeatureIterator::close()

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

if ( mConn )
Expand Down Expand Up @@ -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() )
{
Expand Down

0 comments on commit 7de9999

Please sign in to comment.