Skip to content
Permalink
Browse files

Merge pull request #4761 from nyalldawson/algs2

Port more processing algs to new API
  • Loading branch information
nyalldawson committed Jun 21, 2017
2 parents 57bf9da + a65e171 commit b01cae0740710db848f7d153def323c4ffac604c
@@ -163,14 +163,16 @@ class QgsProcessingParameterDefinition
sipType = sipType_QgsProcessingParameterString;
else if ( sipCpp->type() == "expression" )
sipType = sipType_QgsProcessingParameterExpression;
else if ( sipCpp->type() == "table" )
sipType = sipType_QgsProcessingParameterTable;
else if ( sipCpp->type() == "vector" )
sipType = sipType_QgsProcessingParameterVectorLayer;
else if ( sipCpp->type() == "field" )
sipType = sipType_QgsProcessingParameterTableField;
sipType = sipType_QgsProcessingParameterField;
else if ( sipCpp->type() == "source" )
sipType = sipType_QgsProcessingParameterFeatureSource;
else if ( sipCpp->type() == "sink" )
sipType = sipType_QgsProcessingParameterFeatureSink;
else if ( sipCpp->type() == "vectorOut" )
sipType = sipType_QgsProcessingParameterVectorOutput;
else if ( sipCpp->type() == "rasterOut" )
sipType = sipType_QgsProcessingParameterRasterOutput;
else if ( sipCpp->type() == "fileOut" )
@@ -1153,10 +1155,11 @@ class QgsProcessingParameterExpression : QgsProcessingParameterDefinition

};

class QgsProcessingParameterTable : QgsProcessingParameterDefinition
class QgsProcessingParameterVectorLayer : QgsProcessingParameterDefinition
{
%Docstring
A table (i.e. vector layers with or without geometry) parameter for processing algorithms.
A vector layer (with or without geometry) parameter for processing algorithms. Consider using
the more versatile QgsProcessingParameterFeatureSource wherever possible.
.. versionadded:: 3.0
%End

@@ -1165,20 +1168,24 @@ class QgsProcessingParameterTable : QgsProcessingParameterDefinition
%End
public:

QgsProcessingParameterTable( const QString &name, const QString &description = QString(), const QVariant &defaultValue = QVariant(),
bool optional = false );
QgsProcessingParameterVectorLayer( const QString &name, const QString &description = QString(), const QVariant &defaultValue = QVariant(),
bool optional = false );
%Docstring
Constructor for QgsProcessingParameterTable.
Constructor for QgsProcessingParameterVectorLayer.
%End

virtual QString type() const;
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;

virtual QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const;


};

class QgsProcessingParameterTableField : QgsProcessingParameterDefinition
class QgsProcessingParameterField : QgsProcessingParameterDefinition
{
%Docstring
A table field parameter for processing algorithms.
A vector layer or feature source field parameter for processing algorithms.
.. versionadded:: 3.0
%End

@@ -1195,13 +1202,13 @@ class QgsProcessingParameterTableField : QgsProcessingParameterDefinition
DateTime
};

QgsProcessingParameterTableField( const QString &name, const QString &description = QString(), const QVariant &defaultValue = QVariant(),
const QString &parentLayerParameterName = QString(),
DataType type = Any,
bool allowMultiple = false,
bool optional = false );
QgsProcessingParameterField( const QString &name, const QString &description = QString(), const QVariant &defaultValue = QVariant(),
const QString &parentLayerParameterName = QString(),
DataType type = Any,
bool allowMultiple = false,
bool optional = false );
%Docstring
Constructor for QgsProcessingParameterTableField.
Constructor for QgsProcessingParameterField.
%End

virtual QString type() const;
@@ -1354,6 +1361,60 @@ class QgsProcessingParameterFeatureSink : QgsProcessingParameterDefinition
virtual bool fromVariantMap( const QVariantMap &map );


};


class QgsProcessingParameterVectorOutput : QgsProcessingParameterDefinition
{
%Docstring
A vector layer output parameter. Consider using the more flexible QgsProcessingParameterFeatureSink wherever
possible.
.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgsprocessingparameters.h"
%End
public:

QgsProcessingParameterVectorOutput( const QString &name, const QString &description = QString(), QgsProcessingParameterDefinition::LayerType type = QgsProcessingParameterDefinition::TypeVectorAny, const QVariant &defaultValue = QVariant(),
bool optional = false );
%Docstring
Constructor for QgsProcessingParameterVectorOutput.
%End

virtual QString type() const;
virtual bool isDestination() const;
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;

virtual QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const;


QgsProcessingParameterDefinition::LayerType dataType() const;
%Docstring
Returns the layer type for layers associated with the parameter.
.. seealso:: setDataType()
:rtype: QgsProcessingParameterDefinition.LayerType
%End

bool hasGeometry() const;
%Docstring
Returns true if the layer is likely to include geometries. In cases were presence of geometry
cannot be reliably determined in advance, this method will default to returning true.
:rtype: bool
%End

void setDataType( QgsProcessingParameterDefinition::LayerType type );
%Docstring
Sets the layer ``type`` for the layers associated with the parameter.
.. seealso:: dataType()
%End

virtual QVariantMap toVariantMap() const;

virtual bool fromVariantMap( const QVariantMap &map );


};

class QgsProcessingParameterRasterOutput : QgsProcessingParameterDefinition
@@ -32,7 +32,7 @@
from qgis.core import (QgsApplication,
QgsProcessingUtils,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterTableField,
QgsProcessingParameterField,
QgsProcessingParameterFileOutput,
QgsProcessingOutputHtml)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
@@ -60,12 +60,12 @@ def __init__(self):

self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
self.tr('Input layer')))
self.addParameter(QgsProcessingParameterTableField(self.NAME_FIELD,
self.tr('Category name field'),
None, self.INPUT, QgsProcessingParameterTableField.Any))
self.addParameter(QgsProcessingParameterTableField(self.VALUE_FIELD,
self.tr('Value field'),
None, self.INPUT, QgsProcessingParameterTableField.Numeric))
self.addParameter(QgsProcessingParameterField(self.NAME_FIELD,
self.tr('Category name field'),
None, self.INPUT, QgsProcessingParameterField.Any))
self.addParameter(QgsProcessingParameterField(self.VALUE_FIELD,
self.tr('Value field'),
None, self.INPUT, QgsProcessingParameterField.Numeric))

self.addParameter(QgsProcessingParameterFileOutput(self.OUTPUT, self.tr('Added'), self.tr('HTML files (*.html)')))

@@ -37,7 +37,7 @@
QgsFeatureRequest,
QgsProcessingUtils,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterTableField,
QgsProcessingParameterField,
QgsProcessingParameterFileOutput,
QgsProcessingOutputHtml,
QgsProcessingOutputNumber)
@@ -90,9 +90,9 @@ def __init__(self):
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT_LAYER,
self.tr('Input layer')))

self.addParameter(QgsProcessingParameterTableField(self.FIELD_NAME,
self.tr('Field to calculate statistics on'),
None, self.INPUT_LAYER, QgsProcessingParameterTableField.Any))
self.addParameter(QgsProcessingParameterField(self.FIELD_NAME,
self.tr('Field to calculate statistics on'),
None, self.INPUT_LAYER, QgsProcessingParameterField.Any))

self.addParameter(QgsProcessingParameterFileOutput(self.OUTPUT_HTML_FILE, self.tr('Statistics'), self.tr('HTML files (*.html)'), None, True))
self.addOutput(QgsProcessingOutputHtml(self.OUTPUT_HTML_FILE, self.tr('Statistics')))
@@ -28,12 +28,12 @@
from qgis.core import (QgsVectorDataProvider,
QgsFields,
QgsApplication,
QgsProcessingUtils)
QgsProcessingParameterVectorLayer,
QgsProcessingParameterField,
QgsProcessingParameterDefinition,
QgsProcessingOutputVectorLayer)

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.parameters import ParameterTable
from processing.core.parameters import ParameterTableField
from processing.core.outputs import OutputVector


class CreateAttributeIndex(QgisAlgorithm):
@@ -53,12 +53,11 @@ def group(self):

def __init__(self):
super().__init__()
self.addParameter(ParameterTable(self.INPUT,
self.tr('Input Layer')))
self.addParameter(ParameterTableField(self.FIELD,
self.tr('Attribute to index'), self.INPUT))
self.addOutput(OutputVector(self.OUTPUT,
self.tr('Indexed layer'), True))
self.addParameter(QgsProcessingParameterVectorLayer(self.INPUT,
self.tr('Input Layer')))
self.addParameter(QgsProcessingParameterField(self.FIELD,
self.tr('Attribute to index'), None, self.INPUT))
self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT, self.tr('Indexed layer')))

def name(self):
return 'createattributeindex'
@@ -67,9 +66,8 @@ def displayName(self):
return self.tr('Create attribute index')

def processAlgorithm(self, parameters, context, feedback):
file_name = self.getParameterValue(self.INPUT)
layer = QgsProcessingUtils.mapLayerFromString(file_name, context)
field = self.getParameterValue(self.FIELD)
layer = self.parameterAsVectorLayer(parameters, self.INPUT, context)
field = self.parameterAsString(parameters, self.FIELD, context)
provider = layer.dataProvider()

field_index = layer.fields().lookupField(field)
@@ -84,4 +82,4 @@ def processAlgorithm(self, parameters, context, feedback):
feedback.pushInfo(self.tr("Layer's data provider does not support "
"creating attribute indexes"))

self.setOutputValue(self.OUTPUT, file_name)
return {self.OUTPUT: layer.id()}
@@ -29,7 +29,7 @@
QgsProcessingUtils,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterTableField,
QgsProcessingParameterField,
QgsProcessingOutputVectorLayer)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm

@@ -56,9 +56,9 @@ def __init__(self):
super().__init__()

self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT, self.tr('Input layer')))
self.addParameter(QgsProcessingParameterTableField(self.COLUMNS,
self.tr('Fields to delete'),
None, self.INPUT, QgsProcessingParameterTableField.Any, True))
self.addParameter(QgsProcessingParameterField(self.COLUMNS,
self.tr('Fields to delete'),
None, self.INPUT, QgsProcessingParameterField.Any, True))

self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Output layer')))
self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT, self.tr("Output layer")))
@@ -25,12 +25,13 @@
__revision__ = '$Format:%H$'

from qgis.core import (QgsApplication,
QgsProcessingUtils)
QgsProcessingUtils,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterNumber,
QgsProcessingParameterFeatureSink,
QgsProcessingOutputVectorLayer,
QgsProcessingParameterDefinition)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.parameters import (ParameterVector,
ParameterNumber)
from processing.core.outputs import OutputVector
from processing.tools import dataobjects


class DeleteHoles(QgisAlgorithm):
@@ -53,12 +54,14 @@ def group(self):

def __init__(self):
super().__init__()
self.addParameter(ParameterVector(self.INPUT,
self.tr('Input layer'), [dataobjects.TYPE_VECTOR_POLYGON]))
self.addParameter(ParameterNumber(self.MIN_AREA,
self.tr('Remove holes with area less than'), 0, 10000000.0, default=0.0, optional=True))
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
self.tr('Input layer'), [QgsProcessingParameterDefinition.TypeVectorPolygon]))
self.addParameter(QgsProcessingParameterNumber(self.MIN_AREA,
self.tr('Remove holes with area less than'), QgsProcessingParameterNumber.Double,
0, True, 0.0, 10000000.0))

self.addOutput(OutputVector(self.OUTPUT, self.tr('Cleaned'), datatype=[dataobjects.TYPE_VECTOR_POLYGON]))
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Cleaned'), QgsProcessingParameterDefinition.TypeVectorPolygon))
self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT, self.tr('Cleaned'), QgsProcessingParameterDefinition.TypeVectorPolygon))

def name(self):
return 'deleteholes'
@@ -67,29 +70,24 @@ def displayName(self):
return self.tr('Delete holes')

def processAlgorithm(self, parameters, context, feedback):
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
min_area = self.getParameterValue(self.MIN_AREA)
if min_area is not None:
try:
min_area = float(min_area)
except:
pass
source = self.parameterAsSource(parameters, self.INPUT, context)
min_area = self.parameterAsDouble(parameters, self.MIN_AREA, context)
if min_area == 0.0:
min_area = -1.0

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(layer.fields(), layer.wkbType(), layer.crs(),
context)
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
source.fields(), source.wkbType(), source.sourceCrs())

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

for current, f in enumerate(features):
if feedback.isCanceled():
break

if f.hasGeometry():
if min_area is not None:
f.setGeometry(f.geometry().removeInteriorRings(min_area))
else:
f.setGeometry(f.geometry().removeInteriorRings())
writer.addFeature(f)
f.setGeometry(f.geometry().removeInteriorRings(min_area))
sink.addFeature(f)
feedback.setProgress(int(current * total))

del writer
return {self.OUTPUT: dest_id}

0 comments on commit b01cae0

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