@@ -4441,6 +4441,68 @@ QString QgisApp::crsAndFormatAdjustedLayerUri( const QString &uri, const QString
4441
4441
return newuri;
4442
4442
}
4443
4443
4444
+ static QStringList splitSubLayerDef ( const QString &subLayerDef )
4445
+ {
4446
+ QStringList elements = subLayerDef.split ( QgsDataProvider::SUBLAYER_SEPARATOR );
4447
+ // merge back parts of the name that may have been split
4448
+ while ( elements.size () > 5 )
4449
+ {
4450
+ elements[1 ] += " :" + elements[2 ];
4451
+ elements.removeAt ( 2 );
4452
+ }
4453
+ return elements;
4454
+ }
4455
+
4456
+ static void setupVectorLayer ( const QString &vectorLayerPath,
4457
+ const QStringList &sublayers,
4458
+ QgsVectorLayer *&layer,
4459
+ const QString &providerKey,
4460
+ const QgsVectorLayer::LayerOptions &options )
4461
+ {
4462
+ // set friendly name for datasources with only one layer
4463
+ QgsSettings settings;
4464
+ QStringList elements = splitSubLayerDef ( sublayers.at ( 0 ) );
4465
+ QString rawLayerName = elements.size () >= 2 ? elements.at ( 1 ) : QString ();
4466
+ QString subLayerNameFormatted = rawLayerName;
4467
+ if ( settings.value ( QStringLiteral ( " qgis/formatLayerName" ), false ).toBool () )
4468
+ {
4469
+ subLayerNameFormatted = QgsMapLayer::formatLayerName ( subLayerNameFormatted );
4470
+ }
4471
+
4472
+ if ( elements.size () >= 4 && layer->name ().compare ( rawLayerName, Qt::CaseInsensitive ) != 0
4473
+ && layer->name ().compare ( subLayerNameFormatted, Qt::CaseInsensitive ) != 0 )
4474
+ {
4475
+ layer->setName ( QStringLiteral ( " %1 %2" ).arg ( layer->name (), rawLayerName ) );
4476
+ }
4477
+
4478
+ // Systematically add a layername= option to OGR datasets in case
4479
+ // the current single layer dataset becomes layer a multi-layer one.
4480
+ // Except for a few select extensions, known to be always single layer dataset.
4481
+ QFileInfo fi ( vectorLayerPath );
4482
+ QString ext = fi.suffix ().toLower ();
4483
+ if ( providerKey == QLatin1String ( " ogr" ) &&
4484
+ ext != QLatin1String ( " shp" ) &&
4485
+ ext != QLatin1String ( " mif" ) &&
4486
+ ext != QLatin1String ( " tab" ) &&
4487
+ ext != QLatin1String ( " csv" ) &&
4488
+ ext != QLatin1String ( " geojson" ) &&
4489
+ ! vectorLayerPath.contains ( QStringLiteral ( " layerid=" ) ) &&
4490
+ ! vectorLayerPath.contains ( QStringLiteral ( " layername=" ) ) )
4491
+ {
4492
+ auto uriParts = QgsProviderRegistry::instance ()->decodeUri (
4493
+ layer->providerType (), layer->dataProvider ()->dataSourceUri () );
4494
+ QString composedURI ( uriParts.value ( QLatin1String ( " path" ) ).toString () );
4495
+ composedURI += " |layername=" + rawLayerName;
4496
+
4497
+ auto newLayer = qgis::make_unique<QgsVectorLayer>( composedURI, layer->name (), QStringLiteral ( " ogr" ), options );
4498
+ if ( newLayer && newLayer->isValid () )
4499
+ {
4500
+ delete layer;
4501
+ layer = newLayer.release ();
4502
+ }
4503
+ }
4504
+ }
4505
+
4444
4506
bool QgisApp::addVectorLayers ( const QStringList &layerQStringList, const QString &enc, const QString &dataSourceType )
4445
4507
{
4446
4508
bool wasfrozen = mMapCanvas ->isFrozen ();
@@ -4523,19 +4585,8 @@ bool QgisApp::addVectorLayers( const QStringList &layerQStringList, const QStrin
4523
4585
}
4524
4586
else if ( !sublayers.isEmpty () ) // there is 1 layer of data available
4525
4587
{
4526
- // set friendly name for datasources with only one layer
4527
- QStringList elements = sublayers.at ( 0 ).split ( QgsDataProvider::SUBLAYER_SEPARATOR );
4528
- QString subLayerNameFormatted = elements.size () >= 2 ? elements.at ( 1 ) : QString ();
4529
- if ( settings.value ( QStringLiteral ( " qgis/formatLayerName" ), false ).toBool () )
4530
- {
4531
- subLayerNameFormatted = QgsMapLayer::formatLayerName ( subLayerNameFormatted );
4532
- }
4533
-
4534
- if ( elements.size () >= 4 && layer->name ().compare ( elements.at ( 1 ), Qt::CaseInsensitive ) != 0
4535
- && layer->name ().compare ( subLayerNameFormatted, Qt::CaseInsensitive ) != 0 )
4536
- {
4537
- layer->setName ( QStringLiteral ( " %1 %2" ).arg ( layer->name (), elements.at ( 1 ) ) );
4538
- }
4588
+ setupVectorLayer ( src, sublayers, layer,
4589
+ QStringLiteral ( " ogr" ), options );
4539
4590
4540
4591
myList << layer;
4541
4592
}
@@ -4938,14 +4989,7 @@ void QgisApp::askUserForOGRSublayers( QgsVectorLayer *layer )
4938
4989
// OGR provider returns items in this format:
4939
4990
// <layer_index>:<name>:<feature_count>:<geom_type>
4940
4991
4941
- QStringList elements = sublayer.split ( QgsDataProvider::SUBLAYER_SEPARATOR );
4942
- // merge back parts of the name that may have been split
4943
- while ( elements.size () > 5 )
4944
- {
4945
- elements[1 ] += " :" + elements[2 ];
4946
- elements.removeAt ( 2 );
4947
- }
4948
-
4992
+ QStringList elements = splitSubLayerDef ( sublayer );
4949
4993
if ( elements.count () >= 4 )
4950
4994
{
4951
4995
QgsSublayersDialog::LayerDefinition def;
@@ -4978,18 +5022,11 @@ void QgisApp::askUserForOGRSublayers( QgsVectorLayer *layer )
4978
5022
if ( !chooseSublayersDialog.exec () )
4979
5023
return ;
4980
5024
4981
- QString uri = layer->source ();
4982
5025
QString name = layer->name ();
4983
- // the separator char & was changed to | to be compatible
4984
- // with url for protocol drivers
4985
- if ( uri.contains ( ' |' , Qt::CaseSensitive ) )
4986
- {
4987
- // If we get here, there are some options added to the filename.
4988
- // A valid uri is of the form: filename&option1=value1&option2=value2,...
4989
- // We want only the filename here, so we get the first part of the split.
4990
- QStringList theURIParts = uri.split ( ' |' );
4991
- uri = theURIParts.at ( 0 );
4992
- }
5026
+
5027
+ auto uriParts = QgsProviderRegistry::instance ()->decodeUri (
5028
+ layer->providerType (), layer->dataProvider ()->dataSourceUri () );
5029
+ QString uri ( uriParts.value ( QLatin1String ( " path" ) ).toString () );
4993
5030
4994
5031
// The uri must contain the actual uri of the vectorLayer from which we are
4995
5032
// going to load the sublayers.
@@ -10679,18 +10716,8 @@ QgsVectorLayer *QgisApp::addVectorLayer( const QString &vectorLayerPath, const Q
10679
10716
QStringList sublayers = layer->dataProvider ()->subLayers ();
10680
10717
if ( !sublayers.isEmpty () )
10681
10718
{
10682
- QStringList elements = sublayers.at ( 0 ).split ( QgsDataProvider::SUBLAYER_SEPARATOR );
10683
- QString subLayerNameFormatted = elements.size () >= 2 ? elements.at ( 1 ) : QString ();
10684
- if ( settings.value ( QStringLiteral ( " qgis/formatLayerName" ), false ).toBool () )
10685
- {
10686
- subLayerNameFormatted = QgsMapLayer::formatLayerName ( subLayerNameFormatted );
10687
- }
10688
-
10689
- if ( elements.size () >= 4 && layer->name ().compare ( elements.at ( 1 ), Qt::CaseInsensitive ) != 0
10690
- && layer->name ().compare ( subLayerNameFormatted, Qt::CaseInsensitive ) != 0 )
10691
- {
10692
- layer->setName ( QStringLiteral ( " %1 %2" ).arg ( layer->name (), elements.at ( 1 ) ) );
10693
- }
10719
+ setupVectorLayer ( vectorLayerPath, sublayers, layer,
10720
+ providerKey, options );
10694
10721
}
10695
10722
10696
10723
myList << layer;
0 commit comments