Skip to content

Commit b46c9c3

Browse files
committed
Geopackge bugfix: also show single raster layers
The previous implementation only showed subdatasets while single raster were skipped. Now the single rasters are also showed and the name is retrieved from GDAL metadata IDENTIFIER. The gpkg table name is used to delete the table.
1 parent f999897 commit b46c9c3

File tree

1 file changed

+46
-8
lines changed

1 file changed

+46
-8
lines changed

src/providers/ogr/qgsgeopackagedataitems.cpp

+46-8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "qgsnewgeopackagelayerdialog.h"
2727
#include "qgsmessageoutput.h"
2828
#include "qgsvectorlayerexporter.h"
29+
#include "gdal.h"
2930

3031
#include <QAction>
3132
#include <QMessageBox>
@@ -231,14 +232,51 @@ QVector<QgsDataItem *> QgsGeoPackageConnectionItem::createChildren()
231232
}
232233
// Raster layers
233234
QgsRasterLayer rlayer( mPath, QStringLiteral( "gdal_tmp" ), QStringLiteral( "gdal" ), false );
234-
Q_FOREACH ( const QString &uri, rlayer.dataProvider()->subLayers( ) )
235+
if ( rlayer.dataProvider()->subLayers( ).size() > 0 )
235236
{
236-
QStringList pieces = uri.split( ':' );
237-
QString name = pieces.value( pieces.length() - 1 );
238-
QgsDebugMsgLevel( QStringLiteral( "Adding GeoPackage Raster item %1 %2 %3" ).arg( name, uri ), 3 );
239-
QgsGeoPackageRasterLayerItem *item = new QgsGeoPackageRasterLayerItem( this, name, mPath, uri );
240-
children.append( item );
237+
Q_FOREACH ( const QString &uri, rlayer.dataProvider()->subLayers( ) )
238+
{
239+
QStringList pieces = uri.split( ':' );
240+
QString name = pieces.value( pieces.length() - 1 );
241+
QgsDebugMsgLevel( QStringLiteral( "Adding GeoPackage Raster item %1 %2 %3" ).arg( name, uri ), 3 );
242+
QgsGeoPackageRasterLayerItem *item = new QgsGeoPackageRasterLayerItem( this, name, mPath, uri );
243+
children.append( item );
244+
}
245+
}
246+
else if ( rlayer.isValid( ) )
247+
{
248+
// Get the identifier
249+
GDALAllRegister();
250+
// do not print errors, but write to debug
251+
CPLPushErrorHandler( CPLQuietErrorHandler );
252+
CPLErrorReset();
253+
GDALDatasetH hDS = GDALOpen( mPath.toUtf8().constData(), GA_ReadOnly );
254+
CPLPopErrorHandler();
255+
256+
if ( ! hDS )
257+
{
258+
QgsDebugMsg( QString( "GDALOpen error # %1 : %2 " ).arg( CPLGetLastErrorNo() ).arg( CPLGetLastErrorMsg() ) );
241259

260+
}
261+
else
262+
{
263+
QString uri( QStringLiteral( "GPKG:%1" ).arg( mPath ) );
264+
QString name = GDALGetMetadataItem( hDS, "IDENTIFIER", NULL );
265+
GDALClose( hDS );
266+
// Fallback: will not be able to delete the table
267+
if ( name.isEmpty() )
268+
{
269+
name = QFileInfo( mPath ).fileName();
270+
}
271+
else
272+
{
273+
uri += QStringLiteral( ":%1" ).arg( name );
274+
}
275+
276+
QgsDebugMsgLevel( QStringLiteral( "Adding GeoPackage Raster item %1 %2 %3" ).arg( name, mPath ), 3 );
277+
QgsGeoPackageRasterLayerItem *item = new QgsGeoPackageRasterLayerItem( this, name, mPath, uri );
278+
children.append( item );
279+
}
242280
}
243281
return children;
244282

@@ -531,7 +569,7 @@ bool QgsGeoPackageRasterLayerItem::executeDeleteLayer( QString &errCause )
531569
QString baseUri = pieces.at( 1 );
532570
QString layerName = pieces.at( 2 );
533571
sqlite3 *handle;
534-
int status = sqlite3_open_v2( baseUri.toLocal8Bit().data(), &handle, SQLITE_OPEN_READWRITE, NULL );
572+
int status = sqlite3_open_v2( baseUri.toUtf8().constData(), &handle, SQLITE_OPEN_READWRITE, NULL );
535573
if ( status != SQLITE_OK )
536574
{
537575
errCause = sqlite3_errmsg( handle );
@@ -547,7 +585,7 @@ bool QgsGeoPackageRasterLayerItem::executeDeleteLayer( QString &errCause )
547585
"DELETE FROM gpkg_tile_matrix_set WHERE table_name = '%1';" ).arg( layerName );
548586
status = sqlite3_exec(
549587
handle, /* An open database */
550-
sql.toLocal8Bit().data(), /* SQL to be evaluated */
588+
sql.toUtf8().constData(), /* SQL to be evaluated */
551589
NULL, /* Callback function */
552590
NULL, /* 1st argument to callback */
553591
&errmsg /* Error msg written here */

0 commit comments

Comments
 (0)