Skip to content

Commit e3fc73f

Browse files
authoredOct 23, 2017
Merge pull request #5415 from rouault/use_layername
[OGR provider/askUserForOGRSublayers] Use layername= instead of layerid= when no ambiguity
2 parents a6964b5 + 1822b76 commit e3fc73f

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
@@ -4500,6 +4500,8 @@ void QgisApp::askUserForOGRSublayers( QgsVectorLayer *layer )
45004500
QStringList sublayers = layer->dataProvider()->subLayers();
45014501

45024502
QgsSublayersDialog::LayerDefinitionList list;
4503+
QMap< QString, int > mapLayerNameToCount;
4504+
bool uniqueNames = true;
45034505
Q_FOREACH ( const QString &sublayer, sublayers )
45044506
{
45054507
// OGR provider returns items in this format:
@@ -4520,6 +4522,9 @@ void QgisApp::askUserForOGRSublayers( QgsVectorLayer *layer )
45204522
def.layerName = elements[1];
45214523
def.count = elements[2].toInt();
45224524
def.type = elements[3];
4525+
int count = ++mapLayerNameToCount[def.layerName];
4526+
if ( count > 1 || def.layerName.isEmpty() )
4527+
uniqueNames = false;
45234528
list << def;
45244529
}
45254530
else
@@ -4558,7 +4563,16 @@ void QgisApp::askUserForOGRSublayers( QgsVectorLayer *layer )
45584563
Q_FOREACH ( const QgsSublayersDialog::LayerDefinition &def, chooseSublayersDialog.selection() )
45594564
{
45604565
QString layerGeometryType = def.type;
4561-
QString composedURI = uri + "|layerid=" + QString::number( def.layerId );
4566+
QString composedURI = uri;
4567+
if ( uniqueNames )
4568+
{
4569+
composedURI += "|layername=" + def.layerName;
4570+
}
4571+
else
4572+
{
4573+
// Only use layerId if there are ambiguities with names
4574+
composedURI += "|layerid=" + QString::number( def.layerId );
4575+
}
45624576

45634577
if ( !layerGeometryType.isEmpty() )
45644578
{
@@ -9900,7 +9914,9 @@ QgsVectorLayer *QgisApp::addVectorLayer( const QString &vectorLayerPath, const Q
99009914

99019915
// If the newly created layer has more than 1 layer of data available, we show the
99029916
// sublayers selection dialog so the user can select the sublayers to actually load.
9903-
if ( sublayers.count() > 1 && ! vectorLayerPath.contains( QStringLiteral( "layerid=" ) ) )
9917+
if ( sublayers.count() > 1 &&
9918+
! vectorLayerPath.contains( QStringLiteral( "layerid=" ) ) &&
9919+
! vectorLayerPath.contains( QStringLiteral( "layername=" ) ) )
99049920
{
99059921
askUserForOGRSublayers( layer );
99069922

‎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.