Skip to content
Permalink
Browse files

[processing] Fix layerName= suffix is incorrectly passed to SAGA algo…

…rithms

Fixes #21569
  • Loading branch information
nyalldawson committed Mar 18, 2019
1 parent 5b57fc5 commit 4e5888d27d7cb003f85c7c7dabc53d291961c951
@@ -101,7 +101,7 @@ def getOgrCompatibleSource(self, parameter_name, parameters, context, feedback,
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 @@ def getOgrCompatibleSource(self, parameter_name, parameters, context, feedback,
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;
}
}

@@ -7457,6 +7457,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() );
@@ -7534,20 +7542,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 );
@@ -7558,7 +7567,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 );
@@ -7577,12 +7586,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()

0 comments on commit 4e5888d

Please sign in to comment.
You can’t perform that action at this time.