Skip to content
Permalink
Browse files

Handle gpkg vectors and rasters with ogr data items

  • Loading branch information
elpaso committed Sep 18, 2017
1 parent 6bc1d1f commit 7cf26369c452f9265f7f51eafb65278098316c60
Showing with 47 additions and 15 deletions.
  1. +29 −0 src/providers/gdal/qgsgdaldataitems.cpp
  2. +18 −15 src/providers/ogr/qgsogrdataitems.cpp
@@ -241,12 +241,32 @@ QGISEXTERN QgsDataItem *dataItem( QString path, QgsDataItem *parentItem )
#endif
}

// Filters out the OGR/GDAL supported formats that can contain multiple layers
// and should be treated like a DB: GeoPackage and SQLite
// NOTE: this formats are scanned for rasters too and they are handled
// by the "ogr" provider. For this reason they must
// be skipped by "gdal" provider or the rasters will be listed
// twice. ogrSupportedDbLayersExtensions must be kept in sync
// with the companion variable (same name) in the ogr provider
// class
// TODO: add more OGR supported multiple layers formats here!
QStringList ogrSupportedDbLayersExtensions;
ogrSupportedDbLayersExtensions << QLatin1String( "gpkg" ) << QLatin1String( "sqlite" ) << QLatin1String( "db" );
QStringList ogrSupportedDbDriverNames;
ogrSupportedDbDriverNames << QLatin1String( "GPKG" ) << QLatin1String( "db" );

// return item without testing if:
// scanExtSetting
// or zipfile and scan zip == "Basic scan"
if ( scanExtSetting ||
( ( is_vsizip || is_vsitar ) && scanZipSetting == QLatin1String( "basic" ) ) )
{
// Skip this layer if it's handled by ogr:
if ( ogrSupportedDbLayersExtensions.contains( suffix ) )
{
return nullptr;
}

// if this is a VRT file make sure it is raster VRT to avoid duplicates
if ( suffix == QLatin1String( "vrt" ) )
{
@@ -283,6 +303,15 @@ QGISEXTERN QgsDataItem *dataItem( QString path, QgsDataItem *parentItem )
return nullptr;
}

GDALDriverH hDriver = GDALGetDatasetDriver( hDS );
QString ogrDriverName = GDALGetDriverShortName( hDriver );

// Skip this layer if it's handled by ogr:
if ( ogrSupportedDbDriverNames.contains( ogrDriverName ) )
{
return nullptr;
}

QStringList sublayers = QgsGdalProvider::subLayers( hDS );

GDALClose( hDS );
@@ -578,6 +578,8 @@ QGISEXTERN QgsDataItem *dataItem( QString path, QgsDataItem *parentItem )
// TODO: add more OGR supported multiple layers formats here!
QStringList ogrSupportedDbLayersExtensions;
ogrSupportedDbLayersExtensions << QLatin1String( "gpkg" ) << QLatin1String( "sqlite" ) << QLatin1String( "db" );
QStringList ogrSupportedDbDriverNames;
ogrSupportedDbDriverNames << QLatin1String( "GPKG" ) << QLatin1String( "db" );

// Fast track: return item without testing if:
// scanExtSetting or zipfile and scan zip == "Basic scan"
@@ -624,13 +626,16 @@ QGISEXTERN QgsDataItem *dataItem( QString path, QgsDataItem *parentItem )
return item;
}

// Slow track: scan file contents
QgsDataItem *item = nullptr;

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

if ( ! hDataSource )
@@ -639,26 +644,24 @@ QGISEXTERN QgsDataItem *dataItem( QString path, QgsDataItem *parentItem )
return nullptr;
}

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

QgsDataItem *item = nullptr;

QgsDebugMsgLevel( QString( "GDAL Driver : %1" ).arg( OGR_Dr_GetName( hDriver ) ), 2 );
QString ogrDriverName = OGR_Dr_GetName( hDriver );
int numLayers = OGR_DS_GetLayerCount( hDataSource );
if ( numLayers == 1 )

// GeoPackage needs a specialized data item, mainly because of raster deletion not
// yet implemented in GDAL (2.2.1)
if ( ogrDriverName == QLatin1String( "GPKG" ) )
{
QgsDebugMsgLevel( QString( "using name = %1" ).arg( name ), 2 );
item = dataItemForLayer( parentItem, name, path, hDataSource, 0 );
item = new QgsGeoPackageCollectionItem( parentItem, name, path );
}
else if ( numLayers > 1 )
else if ( numLayers > 1 || ogrSupportedDbDriverNames.contains( ogrDriverName ) )
{
QgsDebugMsgLevel( QString( "using name = %1" ).arg( name ), 2 );
item = new QgsOgrDataCollectionItem( parentItem, name, path );
}
else if ( suffix.compare( QLatin1String( "gpkg" ), Qt::CaseInsensitive ) == 0 )
else
{
item = new QgsGeoPackageCollectionItem( parentItem, name, path );
item = dataItemForLayer( parentItem, name, path, hDataSource, 0 );
}

GDALClose( hDataSource );
OGR_DS_Destroy( hDataSource );
return item;
}

0 comments on commit 7cf2636

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