Skip to content

Commit 1822b76

Browse files
committedOct 23, 2017
[OGR provider/askUserForOGRSublayers] Use layername= instead of layerid= when no ambiguity (#16135)
When composing URI, use layer names if layer names are unique. This will be more stable in case the datasource get layers added/removed (for example for GeoPackage)
1 parent cc96f51 commit 1822b76

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed
 

‎src/app/qgisapp.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4367,6 +4367,8 @@ void QgisApp::askUserForOGRSublayers( QgsVectorLayer *layer )
43674367
QStringList sublayers = layer->dataProvider()->subLayers();
43684368

43694369
QgsSublayersDialog::LayerDefinitionList list;
4370+
QMap< QString, int > mapLayerNameToCount;
4371+
bool uniqueNames = true;
43704372
Q_FOREACH ( const QString &sublayer, sublayers )
43714373
{
43724374
// OGR provider returns items in this format:
@@ -4387,6 +4389,9 @@ void QgisApp::askUserForOGRSublayers( QgsVectorLayer *layer )
43874389
def.layerName = elements[1];
43884390
def.count = elements[2].toInt();
43894391
def.type = elements[3];
4392+
int count = ++mapLayerNameToCount[def.layerName];
4393+
if ( count > 1 || def.layerName.isEmpty() )
4394+
uniqueNames = false;
43904395
list << def;
43914396
}
43924397
else
@@ -4425,7 +4430,16 @@ void QgisApp::askUserForOGRSublayers( QgsVectorLayer *layer )
44254430
Q_FOREACH ( const QgsSublayersDialog::LayerDefinition &def, chooseSublayersDialog.selection() )
44264431
{
44274432
QString layerGeometryType = def.type;
4428-
QString composedURI = uri + "|layerid=" + QString::number( def.layerId );
4433+
QString composedURI = uri;
4434+
if ( uniqueNames )
4435+
{
4436+
composedURI += "|layername=" + def.layerName;
4437+
}
4438+
else
4439+
{
4440+
// Only use layerId if there are ambiguities with names
4441+
composedURI += "|layerid=" + QString::number( def.layerId );
4442+
}
44294443

44304444
if ( !layerGeometryType.isEmpty() )
44314445
{
@@ -9772,7 +9786,9 @@ QgsVectorLayer *QgisApp::addVectorLayer( const QString &vectorLayerPath, const Q
97729786

97739787
// If the newly created layer has more than 1 layer of data available, we show the
97749788
// sublayers selection dialog so the user can select the sublayers to actually load.
9775-
if ( sublayers.count() > 1 && ! vectorLayerPath.contains( QStringLiteral( "layerid=" ) ) )
9789+
if ( sublayers.count() > 1 &&
9790+
! vectorLayerPath.contains( QStringLiteral( "layerid=" ) ) &&
9791+
! vectorLayerPath.contains( QStringLiteral( "layername=" ) ) )
97769792
{
97779793
askUserForOGRSublayers( layer );
97789794

‎src/providers/ogr/qgsogrdataitems.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,24 @@ QList<QgsOgrDbLayerInfo *> QgsOgrLayerItem::subLayers( const QString &path, cons
157157
// Collect mixed-geom layers
158158
QMultiMap<int, QStringList> subLayersMap;
159159
const QStringList subLayersList( layer.dataProvider()->subLayers( ) );
160+
QMap< QString, int > mapLayerNameToCount;
161+
bool uniqueNames = true;
162+
int prevIdx = -1;
160163
for ( const QString &descriptor : subLayersList )
161164
{
162165
QStringList pieces = descriptor.split( ':' );
163-
subLayersMap.insert( pieces[0].toInt(), pieces );
166+
int idx = pieces[0].toInt();
167+
subLayersMap.insert( idx, pieces );
168+
if ( pieces.count() >= 4 && idx != prevIdx )
169+
{
170+
QString layerName = pieces[1];
171+
int count = ++mapLayerNameToCount[layerName];
172+
if ( count > 1 || layerName.isEmpty() )
173+
uniqueNames = false;
174+
}
175+
prevIdx = idx;
164176
}
165-
int prevIdx = -1;
177+
prevIdx = -1;
166178
const auto subLayerKeys = subLayersMap.keys( );
167179
for ( const int &idx : subLayerKeys )
168180
{
@@ -194,13 +206,13 @@ QList<QgsOgrDbLayerInfo *> QgsOgrLayerItem::subLayers( const QString &path, cons
194206
}
195207
else
196208
{
197-
if ( values.size() > 1 )
198-
{
199-
uri = QStringLiteral( "%1|layerid=%2|geometrytype=%3" ).arg( path, layerId, geometryType );
200-
}
209+
if ( uniqueNames )
210+
uri = QStringLiteral( "%1|layername=%2" ).arg( path, name );
201211
else
202-
{
203212
uri = QStringLiteral( "%1|layerid=%2" ).arg( path, layerId );
213+
if ( values.size() > 1 )
214+
{
215+
uri += QStringLiteral( "|geometrytype=" ) + geometryType;
204216
}
205217
QgsDebugMsgLevel( QStringLiteral( "Adding %1 Vector item %2 %3 %4" ).arg( driver, name, uri, geometryType ), 3 );
206218
children.append( new QgsOgrDbLayerInfo( path, uri, name, geometryColumn, geometryType, layerType ) );

0 commit comments

Comments
 (0)
Please sign in to comment.