Skip to content

Commit a38e9e6

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

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 10 additions & 3 deletions
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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7857,6 +7857,14 @@ void TestQgsProcessing::convertCompatible()
78577857
// layer should be returned unchanged - underlying source is compatible
78587858
QCOMPARE( out, layer->source() );
78597859

7860+
// path with layer suffix
7861+
QString vectorWithLayer = testDataDir + "points.shp|layername=points";
7862+
QgsVectorLayer *layer2 = new QgsVectorLayer( vectorWithLayer, "vl" );
7863+
p.addMapLayer( layer2 );
7864+
out = QgsProcessingUtils::convertToCompatibleFormat( layer2, false, QStringLiteral( "test" ), QStringList() << "shp", QString( "shp" ), context, &feedback );
7865+
// layer should be returned unchanged - underlying source is compatible
7866+
QCOMPARE( out, vector );
7867+
78607868
// don't include shp as compatible type
78617869
out = QgsProcessingUtils::convertToCompatibleFormat( layer, false, QStringLiteral( "test" ), QStringList() << "tab", QString( "tab" ), context, &feedback );
78627870
QVERIFY( out != layer->source() );
@@ -7934,20 +7942,21 @@ void TestQgsProcessing::convertCompatible()
79347942
std::unique_ptr< QgsVectorLayer > gpkgLayer = qgis::make_unique< QgsVectorLayer >( gpkgPath, "vl" );
79357943
QVERIFY( gpkgLayer->isValid() );
79367944
out = QgsProcessingUtils::convertToCompatibleFormat( gpkgLayer.get(), false, QStringLiteral( "test" ), QStringList() << "gpkg" << "shp", QString( "shp" ), context, &feedback );
7937-
// layer should be returned unchanged - underlying source is compatible
7938-
QCOMPARE( out, gpkgLayer->source() );
7945+
// layer must be translated -- we do not know if external tool can handle picking the correct layer automatically
7946+
QCOMPARE( out, testDataDir + QStringLiteral( "points_gpkg.gpkg" ) );
79397947
gpkgPath = testDataDir + "points_gpkg.gpkg|layername=points_small";
79407948
gpkgLayer = qgis::make_unique< QgsVectorLayer >( gpkgPath, "vl" );
79417949
QVERIFY( gpkgLayer->isValid() );
79427950
out = QgsProcessingUtils::convertToCompatibleFormat( gpkgLayer.get(), false, QStringLiteral( "test" ), QStringList() << "gpkg" << "shp", QString( "shp" ), context, &feedback );
7943-
QCOMPARE( out, gpkgLayer->source() );
7951+
QVERIFY( out.endsWith( ".shp" ) );
7952+
QVERIFY( out.startsWith( QgsProcessingUtils::tempFolder() ) );
79447953

79457954
// also test evaluating parameter to compatible format
79467955
std::unique_ptr< QgsProcessingParameterDefinition > def( new QgsProcessingParameterFeatureSource( QStringLiteral( "source" ) ) );
79477956
QVariantMap params;
79487957
params.insert( QStringLiteral( "source" ), QgsProcessingFeatureSourceDefinition( layer->id(), false ) );
79497958
out = QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( def.get(), params, context, QStringList() << "shp", QString( "shp" ), &feedback );
7950-
QCOMPARE( out, layer->source() );
7959+
QCOMPARE( out, testDataDir + "points.shp" );
79517960

79527961
// incompatible format, will be converted
79537962
out = QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( def.get(), params, context, QStringList() << "tab", QString( "tab" ), &feedback );
@@ -7958,7 +7967,7 @@ void TestQgsProcessing::convertCompatible()
79587967
// layer as input
79597968
params.insert( QStringLiteral( "source" ), QVariant::fromValue( layer ) );
79607969
out = QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( def.get(), params, context, QStringList() << "shp", QString( "shp" ), &feedback );
7961-
QCOMPARE( out, layer->source() );
7970+
QCOMPARE( out, testDataDir + "points.shp" );
79627971

79637972
// incompatible format, will be converted
79647973
out = QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( def.get(), params, context, QStringList() << "tab", QString( "tab" ), &feedback );
@@ -7977,12 +7986,12 @@ void TestQgsProcessing::convertCompatible()
79777986
def.reset( new QgsProcessingParameterFeatureSource( QStringLiteral( "source" ), QString(), QList<int>(), QVariant::fromValue( layer ) ) );
79787987
params.remove( QStringLiteral( "source" ) );
79797988
out = QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( def.get(), params, context, QStringList() << "shp", QString( "shp" ), &feedback );
7980-
QCOMPARE( out, layer->source() );
7989+
QCOMPARE( out, testDataDir + "points.shp" );
79817990

79827991
// output layer as input - e.g. from a previous model child
79837992
params.insert( QStringLiteral( "source" ), QgsProcessingOutputLayerDefinition( layer->id() ) );
79847993
out = QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( def.get(), params, context, QStringList() << "shp", QString( "shp" ), &feedback );
7985-
QCOMPARE( out, layer->source() );
7994+
QCOMPARE( out, testDataDir + "points.shp" );
79867995
}
79877996

79887997
void TestQgsProcessing::create()

0 commit comments

Comments
 (0)