Skip to content

Commit 4e5888d

Browse files
committed
[processing] Fix layerName= suffix is incorrectly passed to SAGA algorithms
Fixes #21569
1 parent 5b57fc5 commit 4e5888d

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

python/plugins/processing/algs/gdal/GdalAlgorithm.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def getOgrCompatibleSource(self, parameter_name, parameters, context, feedback,
101101
ogr_data_path = 'path_to_data_file'
102102
ogr_layer_name = 'layer_name'
103103
elif input_layer.dataProvider().name() == 'ogr':
104-
if executing:
104+
if executing and isinstance(parameters[parameter_name], QgsProcessingFeatureSourceDefinition) and parameters[parameter_name].selectedFeaturesOnly:
105105
# parameter is a vector layer, with OGR data provider
106106
# so extract selection if required
107107
ogr_data_path = self.parameterAsCompatibleSourceLayerPath(parameters, parameter_name, context,
@@ -114,6 +114,7 @@ def getOgrCompatibleSource(self, parameter_name, parameters, context, feedback,
114114
else:
115115
ogr_layer_name = GdalUtils.ogrLayerName(ogr_data_path)
116116
else:
117+
#either not using the selection, or
117118
#not executing - don't worry about 'selected features only' handling. It has no meaning
118119
#for the command line preview since it has no meaning outside of a QGIS session!
119120
ogr_data_path = GdalUtils.ogrConnectionStringAndFormatFromLayer(input_layer)[0]

src/core/processing/qgsprocessingutils.cpp

+10-3
Original file line numberDiff line numberDiff line change
@@ -787,13 +787,20 @@ QString QgsProcessingUtils::convertToCompatibleFormat( const QgsVectorLayer *vl,
787787
requiresTranslation = requiresTranslation || !vl->subsetString().isEmpty();
788788

789789
// Check if layer is a disk based format and if so if the layer's path has a compatible filename suffix
790+
QString diskPath;
790791
if ( !requiresTranslation )
791792
{
792793
const QVariantMap parts = QgsProviderRegistry::instance()->decodeUri( vl->dataProvider()->name(), vl->source() );
793-
if ( parts.contains( QLatin1String( "path" ) ) )
794+
if ( parts.contains( QStringLiteral( "path" ) ) )
794795
{
795-
QFileInfo fi( parts.value( QLatin1String( "path" ) ).toString() );
796+
diskPath = parts.value( QStringLiteral( "path" ) ).toString();
797+
QFileInfo fi( diskPath );
796798
requiresTranslation = !compatibleFormats.contains( fi.suffix(), Qt::CaseInsensitive );
799+
800+
// if the layer name doesn't match the filename, we need to convert the layer. This method can only return
801+
// a filename, and cannot handle layernames as well as file paths
802+
const QString layerName = parts.value( QStringLiteral( "layerName" ) ).toString();
803+
requiresTranslation = requiresTranslation || ( !layerName.isEmpty() && layerName != fi.baseName() );
797804
}
798805
else
799806
{
@@ -824,7 +831,7 @@ QString QgsProcessingUtils::convertToCompatibleFormat( const QgsVectorLayer *vl,
824831
}
825832
else
826833
{
827-
return vl->source();
834+
return diskPath;
828835
}
829836
}
830837

tests/src/analysis/testqgsprocessing.cpp

+16-7
Original file line numberDiff line numberDiff line change
@@ -7457,6 +7457,14 @@ void TestQgsProcessing::convertCompatible()
74577457
// layer should be returned unchanged - underlying source is compatible
74587458
QCOMPARE( out, layer->source() );
74597459

7460+
// path with layer suffix
7461+
QString vectorWithLayer = testDataDir + "points.shp|layername=points";
7462+
QgsVectorLayer *layer2 = new QgsVectorLayer( vectorWithLayer, "vl" );
7463+
p.addMapLayer( layer2 );
7464+
out = QgsProcessingUtils::convertToCompatibleFormat( layer2, false, QStringLiteral( "test" ), QStringList() << "shp", QString( "shp" ), context, &feedback );
7465+
// layer should be returned unchanged - underlying source is compatible
7466+
QCOMPARE( out, vector );
7467+
74607468
// don't include shp as compatible type
74617469
out = QgsProcessingUtils::convertToCompatibleFormat( layer, false, QStringLiteral( "test" ), QStringList() << "tab", QString( "tab" ), context, &feedback );
74627470
QVERIFY( out != layer->source() );
@@ -7534,20 +7542,21 @@ void TestQgsProcessing::convertCompatible()
75347542
std::unique_ptr< QgsVectorLayer > gpkgLayer = qgis::make_unique< QgsVectorLayer >( gpkgPath, "vl" );
75357543
QVERIFY( gpkgLayer->isValid() );
75367544
out = QgsProcessingUtils::convertToCompatibleFormat( gpkgLayer.get(), false, QStringLiteral( "test" ), QStringList() << "gpkg" << "shp", QString( "shp" ), context, &feedback );
7537-
// layer should be returned unchanged - underlying source is compatible
7538-
QCOMPARE( out, gpkgLayer->source() );
7545+
// layer must be translated -- we do not know if external tool can handle picking the correct layer automatically
7546+
QCOMPARE( out, testDataDir + QStringLiteral( "points_gpkg.gpkg" ) );
75397547
gpkgPath = testDataDir + "points_gpkg.gpkg|layername=points_small";
75407548
gpkgLayer = qgis::make_unique< QgsVectorLayer >( gpkgPath, "vl" );
75417549
QVERIFY( gpkgLayer->isValid() );
75427550
out = QgsProcessingUtils::convertToCompatibleFormat( gpkgLayer.get(), false, QStringLiteral( "test" ), QStringList() << "gpkg" << "shp", QString( "shp" ), context, &feedback );
7543-
QCOMPARE( out, gpkgLayer->source() );
7551+
QVERIFY( out.endsWith( ".shp" ) );
7552+
QVERIFY( out.startsWith( QgsProcessingUtils::tempFolder() ) );
75447553

75457554
// also test evaluating parameter to compatible format
75467555
std::unique_ptr< QgsProcessingParameterDefinition > def( new QgsProcessingParameterFeatureSource( QStringLiteral( "source" ) ) );
75477556
QVariantMap params;
75487557
params.insert( QStringLiteral( "source" ), QgsProcessingFeatureSourceDefinition( layer->id(), false ) );
75497558
out = QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( def.get(), params, context, QStringList() << "shp", QString( "shp" ), &feedback );
7550-
QCOMPARE( out, layer->source() );
7559+
QCOMPARE( out, testDataDir + "points.shp" );
75517560

75527561
// incompatible format, will be converted
75537562
out = QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( def.get(), params, context, QStringList() << "tab", QString( "tab" ), &feedback );
@@ -7558,7 +7567,7 @@ void TestQgsProcessing::convertCompatible()
75587567
// layer as input
75597568
params.insert( QStringLiteral( "source" ), QVariant::fromValue( layer ) );
75607569
out = QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( def.get(), params, context, QStringList() << "shp", QString( "shp" ), &feedback );
7561-
QCOMPARE( out, layer->source() );
7570+
QCOMPARE( out, testDataDir + "points.shp" );
75627571

75637572
// incompatible format, will be converted
75647573
out = QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( def.get(), params, context, QStringList() << "tab", QString( "tab" ), &feedback );
@@ -7577,12 +7586,12 @@ void TestQgsProcessing::convertCompatible()
75777586
def.reset( new QgsProcessingParameterFeatureSource( QStringLiteral( "source" ), QString(), QList<int>(), QVariant::fromValue( layer ) ) );
75787587
params.remove( QStringLiteral( "source" ) );
75797588
out = QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( def.get(), params, context, QStringList() << "shp", QString( "shp" ), &feedback );
7580-
QCOMPARE( out, layer->source() );
7589+
QCOMPARE( out, testDataDir + "points.shp" );
75817590

75827591
// output layer as input - e.g. from a previous model child
75837592
params.insert( QStringLiteral( "source" ), QgsProcessingOutputLayerDefinition( layer->id() ) );
75847593
out = QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( def.get(), params, context, QStringList() << "shp", QString( "shp" ), &feedback );
7585-
QCOMPARE( out, layer->source() );
7594+
QCOMPARE( out, testDataDir + "points.shp" );
75867595
}
75877596

75887597
void TestQgsProcessing::create()

0 commit comments

Comments
 (0)