Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
[processing] c++ framework for parameters and running algorithms (WIP)
This commit adds the virtual method for running processing algs to the base c++ class, and adds the initial framework for c++ algorithm parameters. When running an algorithm, a QVariantMap is passed as the algorithm parameters. The high level API provided by QgsProcessingParameters should be used to retrieve strings/layers/doubles/etc from this QVariantMap. This allows advanced use cases, such as passing QgsProperty with the QVariantMap for "dynamic" parameters, where the value should be evaluated for every feature processed. E.g. if the buffer algorithm uses a dynamic property for distance, then the distance could be bound to either a field value or to a custom expression. This gets evaluated before buffering each feature to allow for advanced variable buffering. Support for dynamic parameters will be "opt in", and non default. So algorithms will need to specifically add support for dynamic properties as required.
- Loading branch information
1 parent
8423aaf
commit 3706d88
Showing
12 changed files
with
572 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/core/processing/qgsprocessingparameters.h * | ||
* * | ||
* Do not edit manually ! Edit header and run scripts/sipify.pl again * | ||
************************************************************************/ | ||
|
||
|
||
|
||
|
||
|
||
|
||
class QgsProcessingParameters | ||
{ | ||
%Docstring | ||
|
||
A collection of utilities for working with parameters when running a processing algorithm. | ||
|
||
Parameters are stored in a QVariantMap and referenced by a unique string key. | ||
The QVariants in parameters are not usually accessed | ||
directly, and instead the high level API provided through QgsProcessingParameters | ||
parameterAsString(), parameterAsDouble() are used instead. | ||
|
||
Parameters are evaluated using a provided QgsProcessingContext, allowing | ||
the evaluation to understand available map layers and expression contexts | ||
(for expression based parameters). | ||
|
||
.. versionadded:: 3.0 | ||
%End | ||
|
||
%TypeHeaderCode | ||
#include "qgsprocessingparameters.h" | ||
%End | ||
public: | ||
|
||
static bool isDynamic( const QVariantMap ¶meters, const QString &name ); | ||
%Docstring | ||
Returns true if the parameter with matching ``name`` is a dynamic parameter, and must | ||
be evaluated once for every input feature processed. | ||
:rtype: bool | ||
%End | ||
|
||
static QString parameterAsString( const QVariantMap ¶meters, const QString &name, const QgsProcessingContext &context ); | ||
%Docstring | ||
Evaluates the parameter with matching ``name`` to a static string value. | ||
:rtype: str | ||
%End | ||
|
||
static double parameterAsDouble( const QVariantMap ¶meters, const QString &name, const QgsProcessingContext &context ); | ||
%Docstring | ||
Evaluates the parameter with matching ``name`` to a static double value. | ||
:rtype: float | ||
%End | ||
|
||
static int parameterAsInt( const QVariantMap ¶meters, const QString &name, const QgsProcessingContext &context ); | ||
%Docstring | ||
Evaluates the parameter with matching ``name`` to a static integer value. | ||
:rtype: int | ||
%End | ||
|
||
static bool parameterAsBool( const QVariantMap ¶meters, const QString &name, const QgsProcessingContext &context ); | ||
%Docstring | ||
Evaluates the parameter with matching ``name`` to a static boolean value. | ||
:rtype: bool | ||
%End | ||
|
||
static QgsMapLayer *parameterAsLayer( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context ); | ||
%Docstring | ||
Evaluates the parameter with matching ``name`` 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 | ||
|
||
}; | ||
|
||
|
||
|
||
|
||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/core/processing/qgsprocessingparameters.h * | ||
* * | ||
* Do not edit manually ! Edit header and run scripts/sipify.pl again * | ||
************************************************************************/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/*************************************************************************** | ||
qgsnativealgorithms.cpp | ||
--------------------- | ||
begin : April 2017 | ||
copyright : (C) 2017 by Nyall Dawson | ||
email : nyall dot dawson at gmail dot com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgsnativealgorithms.h" | ||
#include "qgsfeatureiterator.h" | ||
#include "qgsprocessingcontext.h" | ||
#include "qgsprocessingfeedback.h" | ||
#include "qgsprocessingutils.h" | ||
#include "qgsvectorlayer.h" | ||
#include "qgsgeometry.h" | ||
#include "qgswkbtypes.h" | ||
|
||
bool QgsCentroidAlgorithm::run( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback, QVariantMap &outputs ) const | ||
{ | ||
Q_UNUSED( outputs ); | ||
QgsVectorLayer *layer = qobject_cast< QgsVectorLayer *>( QgsProcessingParameters::parameterAsLayer( parameters, QStringLiteral( "INPUT_LAYER" ), context ) ); | ||
if ( !layer ) | ||
return false; | ||
|
||
QString dest; | ||
QgsVectorLayer *outputLayer = nullptr; | ||
std::unique_ptr< QgsFeatureSink > writer( QgsProcessingUtils::createFeatureSink( dest, QString(), layer->fields(), QgsWkbTypes::Point, layer->crs(), context, outputLayer ) ); | ||
|
||
QgsFeature f; | ||
QgsFeatureIterator it = QgsProcessingUtils::getFeatures( layer, context ); | ||
|
||
double step = 100.0 / QgsProcessingUtils::featureCount( layer, context ); | ||
int current = 0; | ||
while ( it.nextFeature( f ) ) | ||
{ | ||
if ( feedback->isCanceled() ) | ||
{ | ||
break; | ||
} | ||
|
||
QgsFeature out = f; | ||
if ( out.hasGeometry() ) | ||
{ | ||
out.setGeometry( f.geometry().centroid() ); | ||
if ( !out.geometry() ) | ||
{ | ||
QgsMessageLog::logMessage( QObject::tr( "Error calculating centroid for feature %1" ).arg( f.id() ), QObject::tr( "Processing" ), QgsMessageLog::WARNING ); | ||
} | ||
} | ||
writer->addFeature( out ); | ||
|
||
feedback->setProgress( current * step ); | ||
current++; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
bool QgsBufferAlgorithm::run( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback, QVariantMap &outputs ) const | ||
{ | ||
Q_UNUSED( outputs ); | ||
|
||
QgsVectorLayer *layer = qobject_cast< QgsVectorLayer *>( QgsProcessingParameters::parameterAsLayer( parameters, QStringLiteral( "INPUT" ), context ) ); | ||
if ( !layer ) | ||
return false; | ||
|
||
QString dest; | ||
QgsVectorLayer *outputLayer = nullptr; | ||
std::unique_ptr< QgsFeatureSink > writer( QgsProcessingUtils::createFeatureSink( dest, QString(), layer->fields(), QgsWkbTypes::Point, layer->crs(), context, outputLayer ) ); | ||
|
||
// fixed parameters | ||
bool dissolve = QgsProcessingParameters::parameterAsBool( parameters, QStringLiteral( "DISSOLVE" ), context ); | ||
int segments = QgsProcessingParameters::parameterAsInt( parameters, QStringLiteral( "DISSOLVE" ), context ); | ||
QgsGeometry::EndCapStyle endCapStyle = static_cast< QgsGeometry::EndCapStyle >( QgsProcessingParameters::parameterAsInt( parameters, QStringLiteral( "END_CAP_STYLE" ), context ) ); | ||
QgsGeometry::JoinStyle joinStyle = static_cast< QgsGeometry::JoinStyle>( QgsProcessingParameters::parameterAsInt( parameters, QStringLiteral( "JOIN_STYLE" ), context ) ); | ||
double miterLimit = QgsProcessingParameters::parameterAsDouble( parameters, QStringLiteral( "MITRE_LIMIT" ), context ); | ||
double bufferDistance = QgsProcessingParameters::parameterAsDouble( parameters, QStringLiteral( "DISTANCE" ), context ); | ||
bool dynamicBuffer = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DISTANCE" ) ); | ||
|
||
QgsFeature f; | ||
QgsFeatureIterator it = QgsProcessingUtils::getFeatures( layer, context ); | ||
|
||
double step = 100.0 / QgsProcessingUtils::featureCount( layer, context ); | ||
int current = 0; | ||
while ( it.nextFeature( f ) ) | ||
{ | ||
if ( feedback->isCanceled() ) | ||
{ | ||
break; | ||
} | ||
|
||
QgsFeature out = f; | ||
if ( out.hasGeometry() ) | ||
{ | ||
if ( dynamicBuffer ) | ||
{ | ||
context.expressionContext().setFeature( f ); | ||
bufferDistance = QgsProcessingParameters::parameterAsDouble( parameters, QStringLiteral( "DISTANCE" ), context ); | ||
} | ||
|
||
out.setGeometry( f.geometry().buffer( bufferDistance, segments, endCapStyle, joinStyle, miterLimit ) ); | ||
if ( !out.geometry() ) | ||
{ | ||
QgsMessageLog::logMessage( QObject::tr( "Error calculating buffer for feature %1" ).arg( f.id() ), QObject::tr( "Processing" ), QgsMessageLog::WARNING ); | ||
} | ||
} | ||
writer->addFeature( out ); | ||
|
||
feedback->setProgress( current * step ); | ||
current++; | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/*************************************************************************** | ||
qgsnativealgorithms.h | ||
--------------------- | ||
begin : April 2017 | ||
copyright : (C) 2017 by Nyall Dawson | ||
email : nyall dot dawson at gmail dot com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#ifndef QGSNATIVEALGORITHMS_H | ||
#define QGSNATIVEALGORITHMS_H | ||
|
||
#include "qgis_core.h" | ||
#include "qgis.h" | ||
#include "qgsprocessingalgorithm.h" | ||
|
||
///@cond PRIVATE | ||
|
||
/** | ||
* Native centroid algorithm. | ||
*/ | ||
class QgsCentroidAlgorithm : public QgsProcessingAlgorithm | ||
{ | ||
|
||
public: | ||
|
||
QgsCentroidAlgorithm() = default; | ||
|
||
QString name() const override { return QStringLiteral( "centroids" ); } | ||
QString displayName() const override { return QObject::tr( "Centroids" ); } | ||
virtual QStringList tags() const override { return QObject::tr( "centroid,center,average,point,middle" ).split( ',' ); } | ||
QString group() const override { return QObject::tr( "Vector geometry tools" ); } | ||
|
||
virtual bool run( const QVariantMap ¶meters, | ||
QgsProcessingContext &context, QgsProcessingFeedback *feedback, QVariantMap &outputs ) const override; | ||
|
||
}; | ||
|
||
/** | ||
* Native buffer algorithm. | ||
*/ | ||
class QgsBufferAlgorithm : public QgsProcessingAlgorithm | ||
{ | ||
|
||
public: | ||
|
||
QgsBufferAlgorithm() = default; | ||
|
||
QString name() const override { return QStringLiteral( "fixeddistancebuffer" ); } | ||
QString displayName() const override { return QObject::tr( "Buffer" ); } | ||
virtual QStringList tags() const override { return QObject::tr( "buffer,grow" ).split( ',' ); } | ||
QString group() const override { return QObject::tr( "Vector geometry tools" ); } | ||
|
||
virtual bool run( const QVariantMap ¶meters, | ||
QgsProcessingContext &context, QgsProcessingFeedback *feedback, QVariantMap &outputs ) const override; | ||
|
||
}; | ||
|
||
///@endcond PRIVATE | ||
|
||
#endif // QGSNATIVEALGORITHMS_H | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.