Skip to content

Commit 5f23b8f

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

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
@@ -653,13 +653,20 @@ QString QgsProcessingUtils::convertToCompatibleFormat( const QgsVectorLayer *vl,
653653
requiresTranslation = requiresTranslation || !vl->subsetString().isEmpty();
654654

655655
// Check if layer is a disk based format and if so if the layer's path has a compatible filename suffix
656+
QString diskPath;
656657
if ( !requiresTranslation )
657658
{
658659
const QVariantMap parts = QgsProviderRegistry::instance()->decodeUri( vl->dataProvider()->name(), vl->source() );
659-
if ( parts.contains( QLatin1String( "path" ) ) )
660+
if ( parts.contains( QStringLiteral( "path" ) ) )
660661
{
661-
QFileInfo fi( parts.value( QLatin1String( "path" ) ).toString() );
662+
diskPath = parts.value( QStringLiteral( "path" ) ).toString();
663+
QFileInfo fi( diskPath );
662664
requiresTranslation = !compatibleFormats.contains( fi.suffix(), Qt::CaseInsensitive );
665+
666+
// if the layer name doesn't match the filename, we need to convert the layer. This method can only return
667+
// a filename, and cannot handle layernames as well as file paths
668+
const QString layerName = parts.value( QStringLiteral( "layerName" ) ).toString();
669+
requiresTranslation = requiresTranslation || ( !layerName.isEmpty() && layerName != fi.baseName() );
663670
}
664671
else
665672
{
@@ -690,7 +697,7 @@ QString QgsProcessingUtils::convertToCompatibleFormat( const QgsVectorLayer *vl,
690697
}
691698
else
692699
{
693-
return vl->source();
700+
return diskPath;
694701
}
695702
}
696703

tests/src/analysis/testqgsprocessing.cpp

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

6825+
// path with layer suffix
6826+
QString vectorWithLayer = testDataDir + "points.shp|layername=points";
6827+
QgsVectorLayer *layer2 = new QgsVectorLayer( vectorWithLayer, "vl" );
6828+
p.addMapLayer( layer2 );
6829+
out = QgsProcessingUtils::convertToCompatibleFormat( layer2, false, QStringLiteral( "test" ), QStringList() << "shp", QString( "shp" ), context, &feedback );
6830+
// layer should be returned unchanged - underlying source is compatible
6831+
QCOMPARE( out, vector );
6832+
68256833
// don't include shp as compatible type
68266834
out = QgsProcessingUtils::convertToCompatibleFormat( layer, false, QStringLiteral( "test" ), QStringList() << "tab", QString( "tab" ), context, &feedback );
68276835
QVERIFY( out != layer->source() );
@@ -6899,20 +6907,21 @@ void TestQgsProcessing::convertCompatible()
68996907
std::unique_ptr< QgsVectorLayer > gpkgLayer = qgis::make_unique< QgsVectorLayer >( gpkgPath, "vl" );
69006908
QVERIFY( gpkgLayer->isValid() );
69016909
out = QgsProcessingUtils::convertToCompatibleFormat( gpkgLayer.get(), false, QStringLiteral( "test" ), QStringList() << "gpkg" << "shp", QString( "shp" ), context, &feedback );
6902-
// layer should be returned unchanged - underlying source is compatible
6903-
QCOMPARE( out, gpkgLayer->source() );
6910+
// layer must be translated -- we do not know if external tool can handle picking the correct layer automatically
6911+
QCOMPARE( out, testDataDir + QStringLiteral( "points_gpkg.gpkg" ) );
69046912
gpkgPath = testDataDir + "points_gpkg.gpkg|layername=points_small";
69056913
gpkgLayer = qgis::make_unique< QgsVectorLayer >( gpkgPath, "vl" );
69066914
QVERIFY( gpkgLayer->isValid() );
69076915
out = QgsProcessingUtils::convertToCompatibleFormat( gpkgLayer.get(), false, QStringLiteral( "test" ), QStringList() << "gpkg" << "shp", QString( "shp" ), context, &feedback );
6908-
QCOMPARE( out, gpkgLayer->source() );
6916+
QVERIFY( out.endsWith( ".shp" ) );
6917+
QVERIFY( out.startsWith( QgsProcessingUtils::tempFolder() ) );
69096918

69106919
// also test evaluating parameter to compatible format
69116920
std::unique_ptr< QgsProcessingParameterDefinition > def( new QgsProcessingParameterFeatureSource( QStringLiteral( "source" ) ) );
69126921
QVariantMap params;
69136922
params.insert( QStringLiteral( "source" ), QgsProcessingFeatureSourceDefinition( layer->id(), false ) );
69146923
out = QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( def.get(), params, context, QStringList() << "shp", QString( "shp" ), &feedback );
6915-
QCOMPARE( out, layer->source() );
6924+
QCOMPARE( out, testDataDir + "points.shp" );
69166925

69176926
// incompatible format, will be converted
69186927
out = QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( def.get(), params, context, QStringList() << "tab", QString( "tab" ), &feedback );
@@ -6923,7 +6932,7 @@ void TestQgsProcessing::convertCompatible()
69236932
// layer as input
69246933
params.insert( QStringLiteral( "source" ), QVariant::fromValue( layer ) );
69256934
out = QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( def.get(), params, context, QStringList() << "shp", QString( "shp" ), &feedback );
6926-
QCOMPARE( out, layer->source() );
6935+
QCOMPARE( out, testDataDir + "points.shp" );
69276936

69286937
// incompatible format, will be converted
69296938
out = QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( def.get(), params, context, QStringList() << "tab", QString( "tab" ), &feedback );
@@ -6942,12 +6951,12 @@ void TestQgsProcessing::convertCompatible()
69426951
def.reset( new QgsProcessingParameterFeatureSource( QStringLiteral( "source" ), QString(), QList<int>(), QVariant::fromValue( layer ) ) );
69436952
params.remove( QStringLiteral( "source" ) );
69446953
out = QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( def.get(), params, context, QStringList() << "shp", QString( "shp" ), &feedback );
6945-
QCOMPARE( out, layer->source() );
6954+
QCOMPARE( out, testDataDir + "points.shp" );
69466955

69476956
// output layer as input - e.g. from a previous model child
69486957
params.insert( QStringLiteral( "source" ), QgsProcessingOutputLayerDefinition( layer->id() ) );
69496958
out = QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( def.get(), params, context, QStringList() << "shp", QString( "shp" ), &feedback );
6950-
QCOMPARE( out, layer->source() );
6959+
QCOMPARE( out, testDataDir + "points.shp" );
69516960
}
69526961

69536962
void TestQgsProcessing::create()

0 commit comments

Comments
 (0)