Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[OGR provider] Workaround GDAL 1.11.X (X<=4) crashing bug on GPKG dat…
…abases with layers with curve geometries
  • Loading branch information
rouault committed Jun 27, 2016
1 parent 1b60b08 commit e02661c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/providers/ogr/qgsogrdataitems.cpp
Expand Up @@ -40,7 +40,7 @@ QgsOgrLayerItem::QgsOgrLayerItem( QgsDataItem* parent,

OGRRegisterAll();
OGRSFDriverH hDriver;
OGRDataSourceH hDataSource = OGROpen( TO8F( mPath ), true, &hDriver );
OGRDataSourceH hDataSource = QgsOgrProviderUtils::OGROpenWrapper( TO8F( mPath ), true, &hDriver );

if ( hDataSource )
{
Expand Down Expand Up @@ -195,7 +195,7 @@ QVector<QgsDataItem*> QgsOgrDataCollectionItem::createChildren()
QVector<QgsDataItem*> children;

OGRSFDriverH hDriver;
OGRDataSourceH hDataSource = OGROpen( TO8F( mPath ), false, &hDriver );
OGRDataSourceH hDataSource = QgsOgrProviderUtils::OGROpenWrapper( TO8F( mPath ), false, &hDriver );
if ( !hDataSource )
return children;
int numLayers = OGR_DS_GetLayerCount( hDataSource );
Expand Down Expand Up @@ -363,7 +363,7 @@ QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
// do not print errors, but write to debug
CPLPushErrorHandler( CPLQuietErrorHandler );
CPLErrorReset();
OGRDataSourceH hDataSource = OGROpen( TO8F( thePath ), false, &hDriver );
OGRDataSourceH hDataSource = QgsOgrProviderUtils::OGROpenWrapper( TO8F( thePath ), false, &hDriver );
CPLPopErrorHandler();

if ( ! hDataSource )
Expand Down
35 changes: 31 additions & 4 deletions src/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -159,7 +159,7 @@ void QgsOgrProvider::repack()
OGR_DS_Destroy( ogrDataSource );
ogrLayer = ogrOrigLayer = nullptr;
ogrDataSource = OGROpen( TO8F( mFilePath ), true, nullptr );
ogrDataSource = QgsOgrProviderUtils::OGROpenWrapper( TO8F( mFilePath ), true, nullptr );
if ( ogrDataSource )
{
if ( mLayerName.isNull() )
Expand Down Expand Up @@ -2853,6 +2853,33 @@ void QgsOgrProvider::forceReload()
QgsOgrConnPool::instance()->invalidateConnections( dataSourceUri() );
}

OGRDataSourceH QgsOgrProviderUtils::OGROpenWrapper( const char* pszPath, bool bUpdate, OGRSFDriverH *phDriver )
{
CPLErrorReset();
OGRSFDriverH hDriver = nullptr;
OGRDataSourceH hDS = OGROpen( pszPath, bUpdate, &hDriver );
if ( phDriver )
*phDriver = hDriver;
if ( !hDS )
return nullptr;
// GDAL < 1.11.5 has a crashing bug with GeoPackage databases with curve geometry
// types (https://trac.osgeo.org/gdal/ticket/6558)
#if GDAL_VERSION_MAJOR == 1 && GDAL_VERSION_MINOR == 11 && GDAL_VERSION_MACRO < 5
const char* pszLastErrorMsg = CPLGetLastErrorMsg();
if ( hDriver == OGRGetDriverByName( "GPKG" ) &&
strstr( pszLastErrorMsg, "geometry column" ) &&
strstr( pszLastErrorMsg, "of type" ) &&
strstr( pszLastErrorMsg, "ignored" ) )
{
QgsDebugMsg( QString( "Ignoring %1 that is a GeoPackage DB with curve geometries" ).arg( pszPath ) );
OGR_DS_Destroy( hDS );
hDS = nullptr;
}
#endif
return hDS;
}


QByteArray QgsOgrProviderUtils::quotedIdentifier( QByteArray field, const QString& ogrDriverName )
{
if ( ogrDriverName == "MySQL" )
Expand Down Expand Up @@ -3087,7 +3114,7 @@ void QgsOgrProvider::open( OpenMode mode )

// first try to open in update mode (unless specified otherwise)
if ( !openReadOnly )
ogrDataSource = OGROpen( TO8F( mFilePath ), true, &ogrDriver );
ogrDataSource = QgsOgrProviderUtils::OGROpenWrapper( TO8F( mFilePath ), true, &ogrDriver );

mValid = false;
if ( ogrDataSource )
Expand All @@ -3104,7 +3131,7 @@ void QgsOgrProvider::open( OpenMode mode )
}

// try to open read-only
ogrDataSource = OGROpen( TO8F( mFilePath ), false, &ogrDriver );
ogrDataSource = QgsOgrProviderUtils::OGROpenWrapper( TO8F( mFilePath ), false, &ogrDriver );
}

if ( ogrDataSource )
Expand Down Expand Up @@ -3165,7 +3192,7 @@ void QgsOgrProvider::open( OpenMode mode )
ogrLayer = ogrOrigLayer = nullptr;
mValid = false;

ogrDataSource = OGROpen( TO8F( mFilePath ), false, &ogrDriver );
ogrDataSource = QgsOgrProviderUtils::OGROpenWrapper( TO8F( mFilePath ), false, &ogrDriver );

mWriteAccess = false;

Expand Down
2 changes: 2 additions & 0 deletions src/providers/ogr/qgsogrprovider.h
Expand Up @@ -391,4 +391,6 @@ class QgsOgrProviderUtils
/** Quote a value for placement in a SQL string.
*/
static QString quotedValue( const QVariant& value );

static OGRDataSourceH OGROpenWrapper( const char* pszPath, bool bUpdate, OGRSFDriverH *phDriver );
};

0 comments on commit e02661c

Please sign in to comment.