Skip to content

Commit 2f74037

Browse files
committed
fix QgsOgrProvider::subLayers() to return only one sublayer when a specific layer id or name was requested in uri
1 parent 5f72747 commit 2f74037

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

‎src/providers/ogr/qgsogrprovider.cpp‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ QgsOgrProvider::QgsOgrProvider( QString const & uri )
261261
, extent_( 0 )
262262
, ogrLayer( 0 )
263263
, ogrOrigLayer( 0 )
264+
, mLayerIndex( 0 )
265+
, mIsSubLayer( false )
264266
, mOgrGeometryTypeFilter( wkbUnknown )
265267
, ogrDriver( 0 )
266268
, valid( false )
@@ -276,7 +278,7 @@ QgsOgrProvider::QgsOgrProvider( QString const & uri )
276278

277279
// make connection to the data source
278280

279-
QgsDebugMsg( "Data source uri is " + uri );
281+
QgsDebugMsg( "Data source uri is [" + uri + "]" );
280282

281283
// try to open for update, but disable error messages to avoid a
282284
// message if the file is read only, because we cope with that
@@ -315,10 +317,15 @@ QgsOgrProvider::QgsOgrProvider( QString const & uri )
315317
{
316318
mLayerIndex = -1;
317319
}
320+
else
321+
{
322+
mIsSubLayer = true;
323+
}
318324
}
319325
else if ( field == "layername" )
320326
{
321327
mLayerName = value;
328+
mIsSubLayer = true;
322329
}
323330

324331
if ( field == "subset" )
@@ -397,7 +404,14 @@ QgsOgrProvider::QgsOgrProvider( QString const & uri )
397404
setEncoding( encoding() );
398405

399406
valid = setSubsetString( mSubsetString );
400-
QgsDebugMsg( "Data source is valid" );
407+
if ( valid )
408+
{
409+
QgsDebugMsg( "Data source is valid" );
410+
}
411+
else
412+
{
413+
QgsMessageLog::logMessage( tr( "Data source is invalid (%1)" ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ), tr( "OGR" ) );
414+
}
401415
}
402416
else
403417
{
@@ -594,7 +608,16 @@ QStringList QgsOgrProvider::subLayers() const
594608
QString theLayerName = FROM8( OGR_FD_GetName( fdef ) );
595609
OGRwkbGeometryType layerGeomType = OGR_FD_GetGeomType( fdef );
596610

597-
QgsDebugMsg( QString( "layerGeomType = %1" ).arg( layerGeomType ) );
611+
// ignore this layer if a sublayer was requested and it is not this one
612+
if ( mIsSubLayer &&
613+
(( !mLayerName.isNull() && theLayerName != mLayerName ) ||
614+
( mLayerName.isNull() && mLayerIndex >= 0 && i != ( unsigned int )mLayerIndex ) ) )
615+
{
616+
QgsDebugMsg( QString( "subLayers() ignoring layer #%1 (%2)" ).arg( i ).arg( theLayerName ) );
617+
continue;
618+
}
619+
620+
QgsDebugMsg( QString( "id = %1 name = %2 layerGeomType = %3" ).arg( i ).arg( theLayerName ).arg( layerGeomType ) );
598621

599622
if ( layerGeomType != wkbUnknown )
600623
{

‎src/providers/ogr/qgsogrprovider.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,9 @@ class QgsOgrProvider : public QgsVectorDataProvider
303303
//! layer index
304304
int mLayerIndex;
305305

306+
//! was a sub layer requested?
307+
bool mIsSubLayer;
308+
306309
/** Optional geometry type for layers with multiple geometries,
307310
* otherwise wkbUnknown. This type is always flatten (2D) and single, it means
308311
* that 2D, 25D, single and multi types are mixed in one sublayer */

0 commit comments

Comments
 (0)