Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #5415 from rouault/use_layername
[OGR provider/askUserForOGRSublayers] Use layername= instead of layerid= when no ambiguity
  • Loading branch information
rouault committed Oct 23, 2017
2 parents a6964b5 + 1822b76 commit e3fc73f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
20 changes: 18 additions & 2 deletions src/app/qgisapp.cpp
Expand Up @@ -4500,6 +4500,8 @@ void QgisApp::askUserForOGRSublayers( QgsVectorLayer *layer )
QStringList sublayers = layer->dataProvider()->subLayers();

QgsSublayersDialog::LayerDefinitionList list;
QMap< QString, int > mapLayerNameToCount;
bool uniqueNames = true;
Q_FOREACH ( const QString &sublayer, sublayers )
{
// OGR provider returns items in this format:
Expand All @@ -4520,6 +4522,9 @@ void QgisApp::askUserForOGRSublayers( QgsVectorLayer *layer )
def.layerName = elements[1];
def.count = elements[2].toInt();
def.type = elements[3];
int count = ++mapLayerNameToCount[def.layerName];
if ( count > 1 || def.layerName.isEmpty() )
uniqueNames = false;
list << def;
}
else
Expand Down Expand Up @@ -4558,7 +4563,16 @@ void QgisApp::askUserForOGRSublayers( QgsVectorLayer *layer )
Q_FOREACH ( const QgsSublayersDialog::LayerDefinition &def, chooseSublayersDialog.selection() )
{
QString layerGeometryType = def.type;
QString composedURI = uri + "|layerid=" + QString::number( def.layerId );
QString composedURI = uri;
if ( uniqueNames )
{
composedURI += "|layername=" + def.layerName;
}
else
{
// Only use layerId if there are ambiguities with names
composedURI += "|layerid=" + QString::number( def.layerId );
}

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

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

Expand Down
26 changes: 19 additions & 7 deletions src/providers/ogr/qgsogrdataitems.cpp
Expand Up @@ -157,12 +157,24 @@ QList<QgsOgrDbLayerInfo *> QgsOgrLayerItem::subLayers( const QString &path, cons
// Collect mixed-geom layers
QMultiMap<int, QStringList> subLayersMap;
const QStringList subLayersList( layer.dataProvider()->subLayers( ) );
QMap< QString, int > mapLayerNameToCount;
bool uniqueNames = true;
int prevIdx = -1;
for ( const QString &descriptor : subLayersList )
{
QStringList pieces = descriptor.split( ':' );
subLayersMap.insert( pieces[0].toInt(), pieces );
int idx = pieces[0].toInt();
subLayersMap.insert( idx, pieces );
if ( pieces.count() >= 4 && idx != prevIdx )
{
QString layerName = pieces[1];
int count = ++mapLayerNameToCount[layerName];
if ( count > 1 || layerName.isEmpty() )
uniqueNames = false;
}
prevIdx = idx;
}
int prevIdx = -1;
prevIdx = -1;
const auto subLayerKeys = subLayersMap.keys( );
for ( const int &idx : subLayerKeys )
{
Expand Down Expand Up @@ -194,13 +206,13 @@ QList<QgsOgrDbLayerInfo *> QgsOgrLayerItem::subLayers( const QString &path, cons
}
else
{
if ( values.size() > 1 )
{
uri = QStringLiteral( "%1|layerid=%2|geometrytype=%3" ).arg( path, layerId, geometryType );
}
if ( uniqueNames )
uri = QStringLiteral( "%1|layername=%2" ).arg( path, name );
else
{
uri = QStringLiteral( "%1|layerid=%2" ).arg( path, layerId );
if ( values.size() > 1 )
{
uri += QStringLiteral( "|geometrytype=" ) + geometryType;
}
QgsDebugMsgLevel( QStringLiteral( "Adding %1 Vector item %2 %3 %4" ).arg( driver, name, uri, geometryType ), 3 );
children.append( new QgsOgrDbLayerInfo( path, uri, name, geometryColumn, geometryType, layerType ) );
Expand Down

0 comments on commit e3fc73f

Please sign in to comment.