mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-05 00:05:32 -04:00
Merge pull request #5415 from rouault/use_layername
[OGR provider/askUserForOGRSublayers] Use layername= instead of layerid= when no ambiguity
This commit is contained in:
commit
e3fc73f41e
@ -4500,6 +4500,8 @@ void QgisApp::askUserForOGRSublayers( QgsVectorLayer *layer )
|
|||||||
QStringList sublayers = layer->dataProvider()->subLayers();
|
QStringList sublayers = layer->dataProvider()->subLayers();
|
||||||
|
|
||||||
QgsSublayersDialog::LayerDefinitionList list;
|
QgsSublayersDialog::LayerDefinitionList list;
|
||||||
|
QMap< QString, int > mapLayerNameToCount;
|
||||||
|
bool uniqueNames = true;
|
||||||
Q_FOREACH ( const QString &sublayer, sublayers )
|
Q_FOREACH ( const QString &sublayer, sublayers )
|
||||||
{
|
{
|
||||||
// OGR provider returns items in this format:
|
// OGR provider returns items in this format:
|
||||||
@ -4520,6 +4522,9 @@ void QgisApp::askUserForOGRSublayers( QgsVectorLayer *layer )
|
|||||||
def.layerName = elements[1];
|
def.layerName = elements[1];
|
||||||
def.count = elements[2].toInt();
|
def.count = elements[2].toInt();
|
||||||
def.type = elements[3];
|
def.type = elements[3];
|
||||||
|
int count = ++mapLayerNameToCount[def.layerName];
|
||||||
|
if ( count > 1 || def.layerName.isEmpty() )
|
||||||
|
uniqueNames = false;
|
||||||
list << def;
|
list << def;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -4558,7 +4563,16 @@ void QgisApp::askUserForOGRSublayers( QgsVectorLayer *layer )
|
|||||||
Q_FOREACH ( const QgsSublayersDialog::LayerDefinition &def, chooseSublayersDialog.selection() )
|
Q_FOREACH ( const QgsSublayersDialog::LayerDefinition &def, chooseSublayersDialog.selection() )
|
||||||
{
|
{
|
||||||
QString layerGeometryType = def.type;
|
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() )
|
if ( !layerGeometryType.isEmpty() )
|
||||||
{
|
{
|
||||||
@ -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
|
// 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.
|
// 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 );
|
askUserForOGRSublayers( layer );
|
||||||
|
|
||||||
|
@ -157,12 +157,24 @@ QList<QgsOgrDbLayerInfo *> QgsOgrLayerItem::subLayers( const QString &path, cons
|
|||||||
// Collect mixed-geom layers
|
// Collect mixed-geom layers
|
||||||
QMultiMap<int, QStringList> subLayersMap;
|
QMultiMap<int, QStringList> subLayersMap;
|
||||||
const QStringList subLayersList( layer.dataProvider()->subLayers( ) );
|
const QStringList subLayersList( layer.dataProvider()->subLayers( ) );
|
||||||
|
QMap< QString, int > mapLayerNameToCount;
|
||||||
|
bool uniqueNames = true;
|
||||||
|
int prevIdx = -1;
|
||||||
for ( const QString &descriptor : subLayersList )
|
for ( const QString &descriptor : subLayersList )
|
||||||
{
|
{
|
||||||
QStringList pieces = descriptor.split( ':' );
|
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( );
|
const auto subLayerKeys = subLayersMap.keys( );
|
||||||
for ( const int &idx : subLayerKeys )
|
for ( const int &idx : subLayerKeys )
|
||||||
{
|
{
|
||||||
@ -194,13 +206,13 @@ QList<QgsOgrDbLayerInfo *> QgsOgrLayerItem::subLayers( const QString &path, cons
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
if ( uniqueNames )
|
||||||
|
uri = QStringLiteral( "%1|layername=%2" ).arg( path, name );
|
||||||
|
else
|
||||||
|
uri = QStringLiteral( "%1|layerid=%2" ).arg( path, layerId );
|
||||||
if ( values.size() > 1 )
|
if ( values.size() > 1 )
|
||||||
{
|
{
|
||||||
uri = QStringLiteral( "%1|layerid=%2|geometrytype=%3" ).arg( path, layerId, geometryType );
|
uri += QStringLiteral( "|geometrytype=" ) + geometryType;
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
uri = QStringLiteral( "%1|layerid=%2" ).arg( path, layerId );
|
|
||||||
}
|
}
|
||||||
QgsDebugMsgLevel( QStringLiteral( "Adding %1 Vector item %2 %3 %4" ).arg( driver, name, uri, geometryType ), 3 );
|
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 ) );
|
children.append( new QgsOgrDbLayerInfo( path, uri, name, geometryColumn, geometryType, layerType ) );
|
||||||
|
Loading…
x
Reference in New Issue
Block a user