Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[processing] Fix layerName= suffix is incorrectly passed to SAGA algo…
…rithms

Fixes #21569
  • Loading branch information
nyalldawson committed Mar 18, 2019
1 parent bdbb622 commit a38e9e6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
3 changes: 2 additions & 1 deletion python/plugins/processing/algs/gdal/GdalAlgorithm.py
Expand Up @@ -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,
Expand All @@ -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]
Expand Down
13 changes: 10 additions & 3 deletions src/core/processing/qgsprocessingutils.cpp
Expand Up @@ -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
{
Expand Down Expand Up @@ -824,7 +831,7 @@ QString QgsProcessingUtils::convertToCompatibleFormat( const QgsVectorLayer *vl,
}
else
{
return vl->source();
return diskPath;
}
}

Expand Down
23 changes: 16 additions & 7 deletions tests/src/analysis/testqgsprocessing.cpp
Expand Up @@ -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() );
Expand Down Expand Up @@ -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 );
Expand All @@ -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 );
Expand All @@ -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()
Expand Down

0 comments on commit a38e9e6

Please sign in to comment.