Skip to content

Commit a27c22d

Browse files
committedJun 6, 2017
Add raster layer output parameters
1 parent e6a71ab commit a27c22d

13 files changed

+251
-13
lines changed
 

‎python/core/processing/qgsprocessingalgorithm.sip

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,12 @@ class QgsProcessingAlgorithm
368368
:rtype: QgsRasterLayer
369369
%End
370370

371+
QString parameterAsRasterOutputLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
372+
%Docstring
373+
Evaluates the parameter with matching ``name`` to a raster output layer destination.
374+
:rtype: str
375+
%End
376+
371377
QgsVectorLayer *parameterAsVectorLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
372378
%Docstring
373379
Evaluates the parameter with matching ``name`` to a vector layer.

‎python/core/processing/qgsprocessingoutputs.sip

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class QgsProcessingOutputDefinition
3030
%ConvertToSubClassCode
3131
if ( sipCpp->type() == "outputVector" )
3232
sipType = sipType_QgsProcessingOutputVectorLayer;
33+
else if ( sipCpp->type() == "outputRaster" )
34+
sipType = sipType_QgsProcessingOutputRasterLayer;
3335
%End
3436
public:
3537

@@ -118,6 +120,26 @@ class QgsProcessingOutputVectorLayer : QgsProcessingOutputDefinition
118120

119121
};
120122

123+
class QgsProcessingOutputRasterLayer : QgsProcessingOutputDefinition
124+
{
125+
%Docstring
126+
A raster layer output for processing algorithms.
127+
.. versionadded:: 3.0
128+
%End
129+
130+
%TypeHeaderCode
131+
#include "qgsprocessingoutputs.h"
132+
%End
133+
public:
134+
135+
QgsProcessingOutputRasterLayer( const QString &name, const QString &description = QString() );
136+
%Docstring
137+
Constructor for QgsProcessingOutputRasterLayer.
138+
%End
139+
140+
virtual QString type() const;
141+
};
142+
121143

122144

123145

‎python/core/processing/qgsprocessingparameters.sip

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ class QgsProcessingParameterDefinition
171171
sipType = sipType_QgsProcessingParameterFeatureSource;
172172
else if ( sipCpp->type() == "sink" )
173173
sipType = sipType_QgsProcessingParameterFeatureSink;
174+
else if ( sipCpp->type() == "rasterOut" )
175+
sipType = sipType_QgsProcessingParameterRasterOutput;
174176
%End
175177
public:
176178

@@ -419,6 +421,12 @@ class QgsProcessingParameters
419421
:rtype: QgsRasterLayer
420422
%End
421423

424+
static QString parameterAsRasterOutputLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
425+
%Docstring
426+
Evaluates the parameter with matching ``definition`` to a raster output layer destination.
427+
:rtype: str
428+
%End
429+
422430
static QgsVectorLayer *parameterAsVectorLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
423431
%Docstring
424432
Evaluates the parameter with matching ``definition`` to a vector layer.
@@ -1221,6 +1229,31 @@ class QgsProcessingParameterFeatureSink : QgsProcessingParameterDefinition
12211229

12221230
};
12231231

1232+
class QgsProcessingParameterRasterOutput : QgsProcessingParameterDefinition
1233+
{
1234+
%Docstring
1235+
A raster layer output parameter.
1236+
.. versionadded:: 3.0
1237+
%End
1238+
1239+
%TypeHeaderCode
1240+
#include "qgsprocessingparameters.h"
1241+
%End
1242+
public:
1243+
1244+
QgsProcessingParameterRasterOutput( const QString &name, const QString &description = QString(),
1245+
const QVariant &defaultValue = QVariant(),
1246+
bool optional = false );
1247+
%Docstring
1248+
Constructor for QgsProcessingParameterRasterOutput.
1249+
%End
1250+
1251+
virtual QString type() const;
1252+
virtual bool isDestination() const;
1253+
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;
1254+
1255+
};
1256+
12241257

12251258

12261259
/************************************************************************

‎python/plugins/processing/gui/AlgorithmDialog.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@
3434
QgsProcessingUtils,
3535
QgsMessageLog,
3636
QgsProcessingParameterDefinition,
37+
QgsProcessingOutputRasterLayer,
3738
QgsProcessingOutputVectorLayer,
3839
QgsProcessingOutputLayerDefinition,
3940
QgsProcessingParameterFeatureSink,
41+
QgsProcessingParameterRasterOutput,
4042
QgsProcessingAlgorithm)
4143
from qgis.gui import QgsMessageBar
4244
from qgis.utils import iface
@@ -56,8 +58,6 @@
5658
from processing.core.parameters import ParameterMultipleInput
5759
from processing.core.GeoAlgorithm import executeAlgorithm
5860

59-
from processing.core.outputs import OutputRaster
60-
from processing.core.outputs import OutputVector
6161
from processing.core.outputs import OutputTable
6262

6363
from processing.tools import dataobjects
@@ -113,7 +113,7 @@ def getParamValues(self):
113113
else:
114114
dest_project = None
115115
if not param.flags() & QgsProcessingParameterDefinition.FlagHidden and \
116-
isinstance(param, (OutputRaster, QgsProcessingParameterFeatureSink, OutputTable)):
116+
isinstance(param, (QgsProcessingParameterRasterOutput, QgsProcessingParameterFeatureSink, OutputTable)):
117117
if self.mainWidget.checkBoxes[param.name()].isChecked():
118118
dest_project = QgsProject.instance()
119119

‎python/plugins/processing/gui/ParameterGuiUtils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ def getFileFilter(param):
5656
for i in range(len(exts)):
5757
exts[i] = tr('{0} files (*.{1})', 'QgsProcessingParameterMultipleLayers').format(exts[i].upper(), exts[i].lower())
5858
return ';;'.join(exts)
59-
elif param.type() == 'raster':
59+
elif param.type() in ('raster', 'rasterOut'):
6060
exts = dataobjects.getSupportedOutputRasterLayerExtensions()
6161
for i in range(len(exts)):
62-
exts[i] = tr('{0} files (*.{1})', 'ParameterRaster').format(exts[i].upper(), exts[i].lower())
62+
exts[i] = tr('{0} files (*.{1})', 'QgsProcessingParameterRasterOutput').format(exts[i].upper(), exts[i].lower())
6363
return ';;'.join(exts)
6464
elif param.type() == 'table':
6565
exts = ['csv', 'dbf']

‎python/plugins/processing/gui/ParametersPanel.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
QgsProcessingParameterPoint,
3737
QgsProcessingParameterFeatureSource,
3838
QgsProcessingOutputVectorLayer,
39+
QgsProcessingOutputRasterLayer,
40+
QgsProcessingParameterRasterOutput,
3941
QgsProcessingParameterFeatureSink)
4042
from qgis.PyQt import uic
4143
from qgis.PyQt.QtCore import QCoreApplication
@@ -46,9 +48,7 @@
4648
from processing.gui.DestinationSelectionPanel import DestinationSelectionPanel
4749
from processing.gui.wrappers import WidgetWrapperFactory
4850
from processing.core.parameters import ParameterVector, ParameterExtent, ParameterPoint
49-
from processing.core.outputs import OutputRaster
5051
from processing.core.outputs import OutputTable
51-
from processing.core.outputs import OutputVector
5252

5353
pluginPath = os.path.split(os.path.dirname(__file__))[0]
5454
WIDGET, BASE = uic.loadUiType(
@@ -157,7 +157,7 @@ def initWidgets(self):
157157
widget = DestinationSelectionPanel(output, self.alg)
158158
self.layoutMain.insertWidget(self.layoutMain.count() - 1, label)
159159
self.layoutMain.insertWidget(self.layoutMain.count() - 1, widget)
160-
if isinstance(output, (OutputRaster, QgsProcessingParameterFeatureSink, OutputTable)):
160+
if isinstance(output, (QgsProcessingParameterRasterOutput, QgsProcessingParameterFeatureSink, OutputTable)):
161161
check = QCheckBox()
162162
check.setText(self.tr('Open output file after running algorithm'))
163163
check.setChecked(True)

‎src/core/processing/qgsprocessingalgorithm.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,11 @@ QgsRasterLayer *QgsProcessingAlgorithm::parameterAsRasterLayer( const QVariantMa
247247
return QgsProcessingParameters::parameterAsRasterLayer( parameterDefinition( name ), parameters, context );
248248
}
249249

250+
QString QgsProcessingAlgorithm::parameterAsRasterOutputLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
251+
{
252+
return QgsProcessingParameters::parameterAsRasterOutputLayer( parameterDefinition( name ), parameters, context );
253+
}
254+
250255
QgsVectorLayer *QgsProcessingAlgorithm::parameterAsVectorLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
251256
{
252257
return QgsProcessingParameters::parameterAsVectorLayer( parameterDefinition( name ), parameters, context );

‎src/core/processing/qgsprocessingalgorithm.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,11 @@ class CORE_EXPORT QgsProcessingAlgorithm
354354
*/
355355
QgsRasterLayer *parameterAsRasterLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
356356

357+
/**
358+
* Evaluates the parameter with matching \a name to a raster output layer destination.
359+
*/
360+
QString parameterAsRasterOutputLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
361+
357362
/**
358363
* Evaluates the parameter with matching \a name to a vector layer.
359364
*

‎src/core/processing/qgsprocessingoutputs.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,7 @@ void QgsProcessingOutputVectorLayer::setDataType( QgsProcessingParameterDefiniti
3838
{
3939
mDataType = type;
4040
}
41+
42+
QgsProcessingOutputRasterLayer::QgsProcessingOutputRasterLayer( const QString &name, const QString &description )
43+
: QgsProcessingOutputDefinition( name, description )
44+
{}

‎src/core/processing/qgsprocessingoutputs.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class CORE_EXPORT QgsProcessingOutputDefinition
4545
SIP_CONVERT_TO_SUBCLASS_CODE
4646
if ( sipCpp->type() == "outputVector" )
4747
sipType = sipType_QgsProcessingOutputVectorLayer;
48+
else if ( sipCpp->type() == "outputRaster" )
49+
sipType = sipType_QgsProcessingOutputRasterLayer;
4850
SIP_END
4951
#endif
5052

@@ -137,6 +139,24 @@ class CORE_EXPORT QgsProcessingOutputVectorLayer : public QgsProcessingOutputDef
137139
QgsProcessingParameterDefinition::LayerType mDataType = QgsProcessingParameterDefinition::TypeVectorAny;
138140
};
139141

142+
/**
143+
* \class QgsProcessingOutputRasterLayer
144+
* \ingroup core
145+
* A raster layer output for processing algorithms.
146+
* \since QGIS 3.0
147+
*/
148+
class CORE_EXPORT QgsProcessingOutputRasterLayer : public QgsProcessingOutputDefinition
149+
{
150+
public:
151+
152+
/**
153+
* Constructor for QgsProcessingOutputRasterLayer.
154+
*/
155+
QgsProcessingOutputRasterLayer( const QString &name, const QString &description = QString() );
156+
157+
QString type() const override { return QStringLiteral( "outputRaster" ); }
158+
};
159+
140160

141161
#endif // QGSPROCESSINGOUTPUTS_H
142162

‎src/core/processing/qgsprocessingparameters.cpp

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,46 @@ QgsRasterLayer *QgsProcessingParameters::parameterAsRasterLayer( const QgsProces
338338
return qobject_cast< QgsRasterLayer *>( parameterAsLayer( definition, parameters, context ) );
339339
}
340340

341+
QString QgsProcessingParameters::parameterAsRasterOutputLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
342+
{
343+
QVariant val;
344+
if ( definition )
345+
{
346+
val = parameters.value( definition->name() );
347+
}
348+
349+
QgsProject *destinationProject = nullptr;
350+
QVariantMap createOptions;
351+
if ( val.canConvert<QgsProcessingOutputLayerDefinition>() )
352+
{
353+
// input is a QgsProcessingOutputLayerDefinition - get extra properties from it
354+
QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( val );
355+
destinationProject = fromVar.destinationProject;
356+
createOptions = fromVar.createOptions;
357+
val = fromVar.sink;
358+
}
359+
360+
QString dest;
361+
if ( val.canConvert<QgsProperty>() )
362+
{
363+
dest = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
364+
}
365+
else if ( !val.isValid() || val.toString().isEmpty() )
366+
{
367+
// fall back to default
368+
dest = definition->defaultValue().toString();
369+
}
370+
else
371+
{
372+
dest = val.toString();
373+
}
374+
375+
if ( destinationProject )
376+
context.addLayerToLoadOnCompletion( dest, QgsProcessingContext::LayerDetails( definition ? definition->description() : QString(), destinationProject ) );
377+
378+
return dest;
379+
}
380+
341381
QgsVectorLayer *QgsProcessingParameters::parameterAsVectorLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
342382
{
343383
return qobject_cast< QgsVectorLayer *>( parameterAsLayer( definition, parameters, context ) );
@@ -681,6 +721,11 @@ bool QgsProcessingParameterMapLayer::checkValueIsAcceptable( const QVariant &inp
681721
return true;
682722
}
683723

724+
if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( input ) ) )
725+
{
726+
return true;
727+
}
728+
684729
if ( input.type() != QVariant::String || input.toString().isEmpty() )
685730
return mFlags & FlagOptional;
686731

@@ -713,11 +758,6 @@ bool QgsProcessingParameterExtent::checkValueIsAcceptable( const QVariant &input
713758
return true;
714759
}
715760

716-
if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( input ) ) )
717-
{
718-
return true;
719-
}
720-
721761
if ( input.type() != QVariant::String || input.toString().isEmpty() )
722762
return mFlags & FlagOptional;
723763

@@ -1442,3 +1482,34 @@ void QgsProcessingParameterFeatureSink::setDataType( QgsProcessingParameterDefin
14421482
{
14431483
mDataType = type;
14441484
}
1485+
1486+
QgsProcessingParameterRasterOutput::QgsProcessingParameterRasterOutput( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
1487+
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
1488+
{}
1489+
1490+
bool QgsProcessingParameterRasterOutput::checkValueIsAcceptable( const QVariant &input, QgsProcessingContext * ) const
1491+
{
1492+
QVariant var = input;
1493+
if ( !var.isValid() )
1494+
return mFlags & FlagOptional;
1495+
1496+
if ( var.canConvert<QgsProcessingOutputLayerDefinition>() )
1497+
{
1498+
QgsProcessingOutputLayerDefinition fromVar = qvariant_cast<QgsProcessingOutputLayerDefinition>( var );
1499+
var = fromVar.sink;
1500+
}
1501+
1502+
if ( var.canConvert<QgsProperty>() )
1503+
{
1504+
return true;
1505+
}
1506+
1507+
if ( var.type() != QVariant::String )
1508+
return false;
1509+
1510+
if ( var.toString().isEmpty() )
1511+
return mFlags & FlagOptional;
1512+
1513+
return true;
1514+
}
1515+

‎src/core/processing/qgsprocessingparameters.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ class CORE_EXPORT QgsProcessingParameterDefinition
206206
sipType = sipType_QgsProcessingParameterFeatureSource;
207207
else if ( sipCpp->type() == "sink" )
208208
sipType = sipType_QgsProcessingParameterFeatureSink;
209+
else if ( sipCpp->type() == "rasterOut" )
210+
sipType = sipType_QgsProcessingParameterRasterOutput;
209211
SIP_END
210212
#endif
211213

@@ -445,6 +447,11 @@ class CORE_EXPORT QgsProcessingParameters
445447
*/
446448
static QgsRasterLayer *parameterAsRasterLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
447449

450+
/**
451+
* Evaluates the parameter with matching \a definition to a raster output layer destination.
452+
*/
453+
static QString parameterAsRasterOutputLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
454+
448455
/**
449456
* Evaluates the parameter with matching \a definition to a vector layer.
450457
*
@@ -1219,6 +1226,28 @@ class CORE_EXPORT QgsProcessingParameterFeatureSink : public QgsProcessingParame
12191226
QgsProcessingParameterDefinition::LayerType mDataType = QgsProcessingParameterDefinition::TypeVectorAny;
12201227
};
12211228

1229+
/**
1230+
* \class QgsProcessingParameterRasterOutput
1231+
* \ingroup core
1232+
* A raster layer output parameter.
1233+
* \since QGIS 3.0
1234+
*/
1235+
class CORE_EXPORT QgsProcessingParameterRasterOutput : public QgsProcessingParameterDefinition
1236+
{
1237+
public:
1238+
1239+
/**
1240+
* Constructor for QgsProcessingParameterRasterOutput.
1241+
*/
1242+
QgsProcessingParameterRasterOutput( const QString &name, const QString &description = QString(),
1243+
const QVariant &defaultValue = QVariant(),
1244+
bool optional = false );
1245+
1246+
QString type() const override { return QStringLiteral( "rasterOut" ); }
1247+
bool isDestination() const override { return true; }
1248+
bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = nullptr ) const override;
1249+
};
1250+
12221251
#endif // QGSPROCESSINGPARAMETERS_H
12231252

12241253

‎tests/src/core/testqgsprocessing.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ class TestQgsProcessing: public QObject
216216
void parameterField();
217217
void parameterFeatureSource();
218218
void parameterFeatureSink();
219+
void parameterRasterOut();
219220
void checkParamValues();
220221
void combineLayerExtent();
221222
void processingFeatureSource();
@@ -2357,6 +2358,48 @@ void TestQgsProcessing::parameterFeatureSink()
23572358

23582359
}
23592360

2361+
void TestQgsProcessing::parameterRasterOut()
2362+
{
2363+
// setup a context
2364+
QgsProject p;
2365+
p.setCrs( QgsCoordinateReferenceSystem::fromEpsgId( 28353 ) );
2366+
QgsProcessingContext context;
2367+
context.setProject( &p );
2368+
2369+
// not optional!
2370+
std::unique_ptr< QgsProcessingParameterRasterOutput > def( new QgsProcessingParameterRasterOutput( "non_optional", QString(), QString(), false ) );
2371+
QVERIFY( !def->checkValueIsAcceptable( false ) );
2372+
QVERIFY( !def->checkValueIsAcceptable( true ) );
2373+
QVERIFY( !def->checkValueIsAcceptable( 5 ) );
2374+
QVERIFY( def->checkValueIsAcceptable( "layer12312312" ) );
2375+
QVERIFY( !def->checkValueIsAcceptable( "" ) );
2376+
QVERIFY( !def->checkValueIsAcceptable( QVariant() ) );
2377+
QVERIFY( def->checkValueIsAcceptable( QgsProcessingOutputLayerDefinition( "layer1231123" ) ) );
2378+
2379+
// should be OK with or without context - it's an output layer!
2380+
QVERIFY( def->checkValueIsAcceptable( "c:/Users/admin/Desktop/roads_clipped_transformed_v1_reprojected_final_clipped_aAAA.tif" ) );
2381+
QVERIFY( def->checkValueIsAcceptable( "c:/Users/admin/Desktop/roads_clipped_transformed_v1_reprojected_final_clipped_aAAA.tif", &context ) );
2382+
2383+
QVariantMap params;
2384+
params.insert( "non_optional", "test.tif" );
2385+
QCOMPARE( QgsProcessingParameters::parameterAsRasterOutputLayer( def.get(), params, context ), QStringLiteral( "test.tif" ) );
2386+
2387+
// optional
2388+
def.reset( new QgsProcessingParameterRasterOutput( "optional", QString(), QString( "default.tif" ), true ) );
2389+
QVERIFY( !def->checkValueIsAcceptable( false ) );
2390+
QVERIFY( !def->checkValueIsAcceptable( true ) );
2391+
QVERIFY( !def->checkValueIsAcceptable( 5 ) );
2392+
QVERIFY( def->checkValueIsAcceptable( "layer12312312" ) );
2393+
QVERIFY( def->checkValueIsAcceptable( "c:/Users/admin/Desktop/roads_clipped_transformed_v1_reprojected_final_clipped_aAAA.tif" ) );
2394+
QVERIFY( def->checkValueIsAcceptable( "" ) );
2395+
QVERIFY( def->checkValueIsAcceptable( QVariant() ) );
2396+
QVERIFY( def->checkValueIsAcceptable( QgsProcessingOutputLayerDefinition( "layer1231123" ) ) );
2397+
2398+
params.insert( "optional", QVariant() );
2399+
QCOMPARE( QgsProcessingParameters::parameterAsRasterOutputLayer( def.get(), params, context ), QStringLiteral( "default.tif" ) );
2400+
2401+
}
2402+
23602403
void TestQgsProcessing::checkParamValues()
23612404
{
23622405
DummyAlgorithm a( "asd" );

0 commit comments

Comments
 (0)
Please sign in to comment.