mirror of
https://github.com/qgis/QGIS.git
synced 2025-10-07 00:15:48 -04:00
[processing] Fix layerName= suffix is incorrectly passed to SAGA algorithms
Fixes #21569
This commit is contained in:
parent
bdbb622bac
commit
a38e9e66fc
@ -101,7 +101,7 @@ class GdalAlgorithm(QgsProcessingAlgorithm):
|
||||
ogr_data_path = 'path_to_data_file'
|
||||
ogr_layer_name = 'layer_name'
|
||||
elif input_layer.dataProvider().name() == 'ogr':
|
||||
if executing:
|
||||
if executing and isinstance(parameters[parameter_name], QgsProcessingFeatureSourceDefinition) and parameters[parameter_name].selectedFeaturesOnly:
|
||||
# parameter is a vector layer, with OGR data provider
|
||||
# so extract selection if required
|
||||
ogr_data_path = self.parameterAsCompatibleSourceLayerPath(parameters, parameter_name, context,
|
||||
@ -114,6 +114,7 @@ class GdalAlgorithm(QgsProcessingAlgorithm):
|
||||
else:
|
||||
ogr_layer_name = GdalUtils.ogrLayerName(ogr_data_path)
|
||||
else:
|
||||
#either not using the selection, or
|
||||
#not executing - don't worry about 'selected features only' handling. It has no meaning
|
||||
#for the command line preview since it has no meaning outside of a QGIS session!
|
||||
ogr_data_path = GdalUtils.ogrConnectionStringAndFormatFromLayer(input_layer)[0]
|
||||
|
@ -787,13 +787,20 @@ QString QgsProcessingUtils::convertToCompatibleFormat( const QgsVectorLayer *vl,
|
||||
requiresTranslation = requiresTranslation || !vl->subsetString().isEmpty();
|
||||
|
||||
// Check if layer is a disk based format and if so if the layer's path has a compatible filename suffix
|
||||
QString diskPath;
|
||||
if ( !requiresTranslation )
|
||||
{
|
||||
const QVariantMap parts = QgsProviderRegistry::instance()->decodeUri( vl->dataProvider()->name(), vl->source() );
|
||||
if ( parts.contains( QLatin1String( "path" ) ) )
|
||||
if ( parts.contains( QStringLiteral( "path" ) ) )
|
||||
{
|
||||
QFileInfo fi( parts.value( QLatin1String( "path" ) ).toString() );
|
||||
diskPath = parts.value( QStringLiteral( "path" ) ).toString();
|
||||
QFileInfo fi( diskPath );
|
||||
requiresTranslation = !compatibleFormats.contains( fi.suffix(), Qt::CaseInsensitive );
|
||||
|
||||
// if the layer name doesn't match the filename, we need to convert the layer. This method can only return
|
||||
// a filename, and cannot handle layernames as well as file paths
|
||||
const QString layerName = parts.value( QStringLiteral( "layerName" ) ).toString();
|
||||
requiresTranslation = requiresTranslation || ( !layerName.isEmpty() && layerName != fi.baseName() );
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -824,7 +831,7 @@ QString QgsProcessingUtils::convertToCompatibleFormat( const QgsVectorLayer *vl,
|
||||
}
|
||||
else
|
||||
{
|
||||
return vl->source();
|
||||
return diskPath;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -7857,6 +7857,14 @@ void TestQgsProcessing::convertCompatible()
|
||||
// layer should be returned unchanged - underlying source is compatible
|
||||
QCOMPARE( out, layer->source() );
|
||||
|
||||
// path with layer suffix
|
||||
QString vectorWithLayer = testDataDir + "points.shp|layername=points";
|
||||
QgsVectorLayer *layer2 = new QgsVectorLayer( vectorWithLayer, "vl" );
|
||||
p.addMapLayer( layer2 );
|
||||
out = QgsProcessingUtils::convertToCompatibleFormat( layer2, false, QStringLiteral( "test" ), QStringList() << "shp", QString( "shp" ), context, &feedback );
|
||||
// layer should be returned unchanged - underlying source is compatible
|
||||
QCOMPARE( out, vector );
|
||||
|
||||
// don't include shp as compatible type
|
||||
out = QgsProcessingUtils::convertToCompatibleFormat( layer, false, QStringLiteral( "test" ), QStringList() << "tab", QString( "tab" ), context, &feedback );
|
||||
QVERIFY( out != layer->source() );
|
||||
@ -7934,20 +7942,21 @@ void TestQgsProcessing::convertCompatible()
|
||||
std::unique_ptr< QgsVectorLayer > gpkgLayer = qgis::make_unique< QgsVectorLayer >( gpkgPath, "vl" );
|
||||
QVERIFY( gpkgLayer->isValid() );
|
||||
out = QgsProcessingUtils::convertToCompatibleFormat( gpkgLayer.get(), false, QStringLiteral( "test" ), QStringList() << "gpkg" << "shp", QString( "shp" ), context, &feedback );
|
||||
// layer should be returned unchanged - underlying source is compatible
|
||||
QCOMPARE( out, gpkgLayer->source() );
|
||||
// layer must be translated -- we do not know if external tool can handle picking the correct layer automatically
|
||||
QCOMPARE( out, testDataDir + QStringLiteral( "points_gpkg.gpkg" ) );
|
||||
gpkgPath = testDataDir + "points_gpkg.gpkg|layername=points_small";
|
||||
gpkgLayer = qgis::make_unique< QgsVectorLayer >( gpkgPath, "vl" );
|
||||
QVERIFY( gpkgLayer->isValid() );
|
||||
out = QgsProcessingUtils::convertToCompatibleFormat( gpkgLayer.get(), false, QStringLiteral( "test" ), QStringList() << "gpkg" << "shp", QString( "shp" ), context, &feedback );
|
||||
QCOMPARE( out, gpkgLayer->source() );
|
||||
QVERIFY( out.endsWith( ".shp" ) );
|
||||
QVERIFY( out.startsWith( QgsProcessingUtils::tempFolder() ) );
|
||||
|
||||
// also test evaluating parameter to compatible format
|
||||
std::unique_ptr< QgsProcessingParameterDefinition > def( new QgsProcessingParameterFeatureSource( QStringLiteral( "source" ) ) );
|
||||
QVariantMap params;
|
||||
params.insert( QStringLiteral( "source" ), QgsProcessingFeatureSourceDefinition( layer->id(), false ) );
|
||||
out = QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( def.get(), params, context, QStringList() << "shp", QString( "shp" ), &feedback );
|
||||
QCOMPARE( out, layer->source() );
|
||||
QCOMPARE( out, testDataDir + "points.shp" );
|
||||
|
||||
// incompatible format, will be converted
|
||||
out = QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( def.get(), params, context, QStringList() << "tab", QString( "tab" ), &feedback );
|
||||
@ -7958,7 +7967,7 @@ void TestQgsProcessing::convertCompatible()
|
||||
// layer as input
|
||||
params.insert( QStringLiteral( "source" ), QVariant::fromValue( layer ) );
|
||||
out = QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( def.get(), params, context, QStringList() << "shp", QString( "shp" ), &feedback );
|
||||
QCOMPARE( out, layer->source() );
|
||||
QCOMPARE( out, testDataDir + "points.shp" );
|
||||
|
||||
// incompatible format, will be converted
|
||||
out = QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( def.get(), params, context, QStringList() << "tab", QString( "tab" ), &feedback );
|
||||
@ -7977,12 +7986,12 @@ void TestQgsProcessing::convertCompatible()
|
||||
def.reset( new QgsProcessingParameterFeatureSource( QStringLiteral( "source" ), QString(), QList<int>(), QVariant::fromValue( layer ) ) );
|
||||
params.remove( QStringLiteral( "source" ) );
|
||||
out = QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( def.get(), params, context, QStringList() << "shp", QString( "shp" ), &feedback );
|
||||
QCOMPARE( out, layer->source() );
|
||||
QCOMPARE( out, testDataDir + "points.shp" );
|
||||
|
||||
// output layer as input - e.g. from a previous model child
|
||||
params.insert( QStringLiteral( "source" ), QgsProcessingOutputLayerDefinition( layer->id() ) );
|
||||
out = QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( def.get(), params, context, QStringList() << "shp", QString( "shp" ), &feedback );
|
||||
QCOMPARE( out, layer->source() );
|
||||
QCOMPARE( out, testDataDir + "points.shp" );
|
||||
}
|
||||
|
||||
void TestQgsProcessing::create()
|
||||
|
Loading…
x
Reference in New Issue
Block a user