Skip to content

Commit

Permalink
Add direct method to retrieve QgsFeatureSink from parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 5, 2017
1 parent f41eb41 commit ffce9c9
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 52 deletions.
20 changes: 20 additions & 0 deletions python/core/processing/qgsprocessingalgorithm.sip
Expand Up @@ -10,6 +10,7 @@




class QgsProcessingAlgorithm
{
%Docstring
Expand Down Expand Up @@ -306,6 +307,25 @@ class QgsProcessingAlgorithm
:rtype: bool
%End

QgsFeatureSink *parameterAsSink( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context,
const QString &encoding, const QgsFields &fields, QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem &crs,
QString &destinationIdentifier /Out/ ) const /Factory/;
%Docstring
Evaluates the parameter with matching ``name`` to a feature sink.

Sinks will either be taken from ``context``'s active project, or created from external
providers and stored temporarily in the ``context``.

The ``encoding``, ``fields``, ``geometryType`` and ``crs`` parameters dictate the properties
of the resulting feature sink.

The ``destinationIdentifier`` argument will be set to a string which can be used to retrieve the layer corresponding
to the sink, e.g. via calling QgsProcessingUtils.mapLayerFromString().

This function creates a new object and the caller takes responsibility for deleting the returned object.
:rtype: QgsFeatureSink
%End

QgsMapLayer *parameterAsLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
%Docstring
Evaluates the parameter with matching ``name`` to a map layer.
Expand Down
91 changes: 55 additions & 36 deletions python/core/processing/qgsprocessingparameters.sip
Expand Up @@ -12,6 +12,7 @@




class QgsProcessingParameterDefinition
{
%Docstring
Expand Down Expand Up @@ -222,124 +223,142 @@ class QgsProcessingParameters
:rtype: bool
%End

static QString parameterAsString( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context );
static QString parameterAsString( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context );
%Docstring
Evaluates the parameter with matching ``name`` to a static string value.
Evaluates the parameter with matching ``definition`` to a static string value.
:rtype: str
%End

static QString parameterAsExpression( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context );
static QString parameterAsExpression( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context );
%Docstring
Evaluates the parameter with matching ``name`` to an expression.
Evaluates the parameter with matching ``definition`` to an expression.
:rtype: str
%End

static double parameterAsDouble( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context );
static double parameterAsDouble( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context );
%Docstring
Evaluates the parameter with matching ``name`` to a static double value.
Evaluates the parameter with matching ``definition`` to a static double value.
:rtype: float
%End

static int parameterAsInt( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context );
static int parameterAsInt( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context );
%Docstring
Evaluates the parameter with matching ``name`` to a static integer value.
Evaluates the parameter with matching ``definition`` to a static integer value.
:rtype: int
%End

static int parameterAsEnum( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context );
static int parameterAsEnum( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context );
%Docstring
Evaluates the parameter with matching ``name`` to a enum value.
Evaluates the parameter with matching ``definition`` to a enum value.
:rtype: int
%End

static QList<int> parameterAsEnums( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context );
static QList<int> parameterAsEnums( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context );
%Docstring
Evaluates the parameter with matching ``name`` to list of enum values.
Evaluates the parameter with matching ``definition`` to list of enum values.
:rtype: list of int
%End

static bool parameterAsBool( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context );
static bool parameterAsBool( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context );
%Docstring
Evaluates the parameter with matching ``name`` to a static boolean value.
Evaluates the parameter with matching ``definition`` to a static boolean value.
:rtype: bool
%End

static QgsMapLayer *parameterAsLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
static QgsFeatureSink *parameterAsSink( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters,
const QString &encoding, const QgsFields &fields, QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem &crs,
QgsProcessingContext &context, QString &destinationIdentifier /Out/ ) /Factory/;
%Docstring
Evaluates the parameter with matching ``definition`` to a feature sink.

The ``encoding``, ``fields``, ``geometryType`` and ``crs`` parameters dictate the properties
of the resulting feature sink.

Sinks will either be taken from ``context``'s active project, or created from external
providers and stored temporarily in the ``context``. The ``destinationIdentifier``
argument will be set to a string which can be used to retrieve the layer corresponding
to the sink, e.g. via calling QgsProcessingUtils.mapLayerFromString().

This function creates a new object and the caller takes responsibility for deleting the returned object.
:rtype: QgsFeatureSink
%End

static QgsMapLayer *parameterAsLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
%Docstring
Evaluates the parameter with matching ``name`` to a map layer.
Evaluates the parameter with matching ``definition`` to a map layer.

Layers will either be taken from ``context``'s active project, or loaded from external
sources and stored temporarily in the ``context``. In either case, callers do not
need to handle deletion of the returned layer.
:rtype: QgsMapLayer
%End

static QgsRasterLayer *parameterAsRasterLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
static QgsRasterLayer *parameterAsRasterLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
%Docstring
Evaluates the parameter with matching ``name`` to a raster layer.
Evaluates the parameter with matching ``definition`` to a raster layer.

Layers will either be taken from ``context``'s active project, or loaded from external
sources and stored temporarily in the ``context``. In either case, callers do not
need to handle deletion of the returned layer.
:rtype: QgsRasterLayer
%End

static QgsVectorLayer *parameterAsVectorLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
static QgsVectorLayer *parameterAsVectorLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
%Docstring
Evaluates the parameter with matching ``name`` to a vector layer.
Evaluates the parameter with matching ``definition`` to a vector layer.

Layers will either be taken from ``context``'s active project, or loaded from external
sources and stored temporarily in the ``context``. In either case, callers do not
need to handle deletion of the returned layer.
:rtype: QgsVectorLayer
%End

static QgsCoordinateReferenceSystem parameterAsCrs( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
static QgsCoordinateReferenceSystem parameterAsCrs( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
%Docstring
Evaluates the parameter with matching ``name`` to a coordinate reference system.
Evaluates the parameter with matching ``definition`` to a coordinate reference system.
:rtype: QgsCoordinateReferenceSystem
%End

static QgsRectangle parameterAsExtent( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
static QgsRectangle parameterAsExtent( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
%Docstring
Evaluates the parameter with matching ``name`` to a rectangular extent.
Evaluates the parameter with matching ``definition`` to a rectangular extent.
:rtype: QgsRectangle
%End

static QgsPointXY parameterAsPoint( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
static QgsPointXY parameterAsPoint( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
%Docstring
Evaluates the parameter with matching ``name`` to a point.
Evaluates the parameter with matching ``definition`` to a point.
:rtype: QgsPointXY
%End

static QString parameterAsFile( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
static QString parameterAsFile( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
%Docstring
Evaluates the parameter with matching ``name`` to a file/folder name.
Evaluates the parameter with matching ``definition`` to a file/folder name.
:rtype: str
%End

static QVariantList parameterAsMatrix( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
static QVariantList parameterAsMatrix( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
%Docstring
Evaluates the parameter with matching ``name`` to a matrix/table of values.
Evaluates the parameter with matching ``definition`` to a matrix/table of values.
Tables are collapsed to a 1 dimensional list.
:rtype: QVariantList
%End

static QList< QgsMapLayer *> parameterAsLayerList( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
static QList< QgsMapLayer *> parameterAsLayerList( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
%Docstring
Evaluates the parameter with matching ``name`` to a list of map layers.
Evaluates the parameter with matching ``definition`` to a list of map layers.
:rtype: list of QgsMapLayer
%End

static QList<double> parameterAsRange( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
static QList<double> parameterAsRange( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
%Docstring
Evaluates the parameter with matching ``name`` to a range of values.
Evaluates the parameter with matching ``definition`` to a range of values.
:rtype: list of float
%End

static QStringList parameterAsFields( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
static QStringList parameterAsFields( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
%Docstring
Evaluates the parameter with matching ``name`` to a list of fields.
Evaluates the parameter with matching ``definition`` to a list of fields.
:rtype: list of str
%End

Expand Down
10 changes: 6 additions & 4 deletions python/plugins/processing/algs/qgis/Boundary.py
Expand Up @@ -82,13 +82,15 @@ def processAlgorithm(self, parameters, context, feedback):
if QgsWkbTypes.hasM(input_wkb):
output_wkb = QgsWkbTypes.addM(output_wkb)

dest = self.parameterAsString(parameters, self.OUTPUT_LAYER, context)
(writer, dest_layer) = QgsProcessingUtils.createFeatureSink(dest, '', layer.fields(), output_wkb, layer.crs(), context)
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT_LAYER, context, '',
layer.fields(), output_wkb, layer.crs())

features = QgsProcessingUtils.getFeatures(layer, context)
total = 100.0 / QgsProcessingUtils.featureCount(layer, context)

for current, input_feature in enumerate(features):
if feedback.isCanceled():
break
output_feature = input_feature
input_geometry = input_feature.geometry()
if input_geometry:
Expand All @@ -99,7 +101,7 @@ def processAlgorithm(self, parameters, context, feedback):

output_feature.setGeometry(output_geometry)

writer.addFeature(output_feature)
sink.addFeature(output_feature)
feedback.setProgress(int(current * total))

return {self.OUTPUT_LAYER: dest_layer}
return {self.OUTPUT_LAYER: dest_id}
16 changes: 8 additions & 8 deletions src/core/processing/qgsnativealgorithms.cpp
Expand Up @@ -78,12 +78,12 @@ QString QgsCentroidAlgorithm::shortHelpString() const

QVariantMap QgsCentroidAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const
{
QgsVectorLayer *layer = qobject_cast< QgsVectorLayer *>( parameterAsLayer( parameters, QStringLiteral( "INPUT" ), context ) );
QgsVectorLayer *layer = parameterAsVectorLayer( parameters, QStringLiteral( "INPUT" ), context );
if ( !layer )
return QVariantMap();

QString dest = parameterAsString( parameters, QStringLiteral( "OUTPUT_LAYER" ), context );
std::unique_ptr< QgsFeatureSink > writer( QgsProcessingUtils::createFeatureSink( dest, QString(), layer->fields(), QgsWkbTypes::Point, layer->crs(), context ) );
QString dest;
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT_LAYER" ), context, QString(), layer->fields(), QgsWkbTypes::Point, layer->crs(), dest ) );

long count = QgsProcessingUtils::featureCount( layer, context );
if ( count <= 0 )
Expand All @@ -110,7 +110,7 @@ QVariantMap QgsCentroidAlgorithm::processAlgorithm( const QVariantMap &parameter
QgsMessageLog::logMessage( QObject::tr( "Error calculating centroid for feature %1" ).arg( f.id() ), QObject::tr( "Processing" ), QgsMessageLog::WARNING );
}
}
writer->addFeature( out );
sink->addFeature( out );

feedback->setProgress( current * step );
current++;
Expand Down Expand Up @@ -151,12 +151,12 @@ QString QgsBufferAlgorithm::shortHelpString() const

QVariantMap QgsBufferAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const
{
QgsVectorLayer *layer = qobject_cast< QgsVectorLayer *>( parameterAsLayer( parameters, QStringLiteral( "INPUT" ), context ) );
QgsVectorLayer *layer = parameterAsVectorLayer( parameters, QStringLiteral( "INPUT" ), context );
if ( !layer )
return QVariantMap();

QString dest = parameterAsString( parameters, QStringLiteral( "OUTPUT_LAYER" ), context );
std::unique_ptr< QgsFeatureSink > writer( QgsProcessingUtils::createFeatureSink( dest, QString(), layer->fields(), QgsWkbTypes::Polygon, layer->crs(), context ) );
QString dest;
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT_LAYER" ), context, QString(), layer->fields(), QgsWkbTypes::Polygon, layer->crs(), dest ) );

// fixed parameters
//bool dissolve = QgsProcessingParameters::parameterAsBool( parameters, QStringLiteral( "DISSOLVE" ), context );
Expand Down Expand Up @@ -199,7 +199,7 @@ QVariantMap QgsBufferAlgorithm::processAlgorithm( const QVariantMap &parameters,
QgsMessageLog::logMessage( QObject::tr( "Error calculating buffer for feature %1" ).arg( f.id() ), QObject::tr( "Processing" ), QgsMessageLog::WARNING );
}
}
writer->addFeature( out );
sink->addFeature( out );

feedback->setProgress( current * step );
current++;
Expand Down
5 changes: 5 additions & 0 deletions src/core/processing/qgsprocessingalgorithm.cpp
Expand Up @@ -210,6 +210,11 @@ bool QgsProcessingAlgorithm::parameterAsBool( const QVariantMap &parameters, con
return QgsProcessingParameters::parameterAsBool( parameterDefinition( name ), parameters, context );
}

QgsFeatureSink *QgsProcessingAlgorithm::parameterAsSink( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, const QString &encoding, const QgsFields &fields, QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem &crs, QString &destinationIdentifier ) const
{
return QgsProcessingParameters::parameterAsSink( parameterDefinition( name ), parameters, encoding, fields, geometryType, crs, context, destinationIdentifier );
}

QgsMapLayer *QgsProcessingAlgorithm::parameterAsLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
{
return QgsProcessingParameters::parameterAsLayer( parameterDefinition( name ), parameters, context );
Expand Down
21 changes: 20 additions & 1 deletion src/core/processing/qgsprocessingalgorithm.h
Expand Up @@ -29,7 +29,8 @@
class QgsProcessingProvider;
class QgsProcessingContext;
class QgsProcessingFeedback;
class QWidget;
class QgsFeatureSink;


/**
* \class QgsProcessingAlgorithm
Expand Down Expand Up @@ -297,6 +298,24 @@ class CORE_EXPORT QgsProcessingAlgorithm
*/
bool parameterAsBool( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;

/**
* Evaluates the parameter with matching \a name to a feature sink.
*
* Sinks will either be taken from \a context's active project, or created from external
* providers and stored temporarily in the \a context.
*
* The \a encoding, \a fields, \a geometryType and \a crs parameters dictate the properties
* of the resulting feature sink.
*
* The \a destinationIdentifier argument will be set to a string which can be used to retrieve the layer corresponding
* to the sink, e.g. via calling QgsProcessingUtils::mapLayerFromString().
*
* This function creates a new object and the caller takes responsibility for deleting the returned object.
*/
QgsFeatureSink *parameterAsSink( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context,
const QString &encoding, const QgsFields &fields, QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem &crs,
QString &destinationIdentifier SIP_OUT ) const SIP_FACTORY;

/**
* Evaluates the parameter with matching \a name to a map layer.
*
Expand Down

0 comments on commit ffce9c9

Please sign in to comment.