[processing] port add table field algorithm to C++

This commit is contained in:
Alexander Bruy 2019-11-27 16:35:46 +02:00
parent ac958dec89
commit eeaffb9d79
8 changed files with 203 additions and 121 deletions

View File

@ -1,15 +1,3 @@
qgis:addfieldtoattributestable: >
This algorithm adds a new attribute to a vector layer.
The name and characteristics of the attribute are defined as parameters.
The new attribute is not added to the input layer but a new layer is generated instead.
qgis:adduniquevalueindexfield: >
This algorithm takes a vector layer and an attribute and adds a new numeric field. Values in this field correspond to values in the specified attribute, so features with the same value for the attribute will have the same value in the new numeric field. This creates a numeric equivalent of the specified attribute, which defines the same classes.
The new attribute is not added to the input layer but a new layer is generated instead.
qgis:advancedpythonfieldcalculator: >
This algorithm adds a new attribute to a vector layer, with values resulting from applying an expression to each feature. The expression is defined as a Python function.

View File

@ -1,104 +0,0 @@
# -*- coding: utf-8 -*-
"""
***************************************************************************
AddTableField.py
---------------------
Date : August 2012
Copyright : (C) 2012 by Victor Olaya
Email : volayaf 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. *
* *
***************************************************************************
"""
__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
from qgis.PyQt.QtCore import QVariant
from qgis.core import (QgsField,
QgsProcessing,
QgsProcessingAlgorithm,
QgsProcessingParameterString,
QgsProcessingParameterNumber,
QgsProcessingParameterEnum,
QgsProcessingFeatureSource)
from processing.algs.qgis.QgisAlgorithm import QgisFeatureBasedAlgorithm
class AddTableField(QgisFeatureBasedAlgorithm):
FIELD_NAME = 'FIELD_NAME'
FIELD_TYPE = 'FIELD_TYPE'
FIELD_LENGTH = 'FIELD_LENGTH'
FIELD_PRECISION = 'FIELD_PRECISION'
TYPES = [QVariant.Int, QVariant.Double, QVariant.String]
def group(self):
return self.tr('Vector table')
def groupId(self):
return 'vectortable'
def __init__(self):
super().__init__()
self.type_names = [self.tr('Integer'),
self.tr('Float'),
self.tr('String')]
self.field = None
def flags(self):
return super().flags() & ~QgsProcessingAlgorithm.FlagSupportsInPlaceEdits
def initParameters(self, config=None):
self.addParameter(QgsProcessingParameterString(self.FIELD_NAME,
self.tr('Field name')))
self.addParameter(QgsProcessingParameterEnum(self.FIELD_TYPE,
self.tr('Field type'), self.type_names))
self.addParameter(QgsProcessingParameterNumber(self.FIELD_LENGTH,
self.tr('Field length'), QgsProcessingParameterNumber.Integer,
10, False, 1, 255))
self.addParameter(QgsProcessingParameterNumber(self.FIELD_PRECISION,
self.tr('Field precision'), QgsProcessingParameterNumber.Integer, 0, False, 0, 10))
def name(self):
return 'addfieldtoattributestable'
def displayName(self):
return self.tr('Add field to attributes table')
def outputName(self):
return self.tr('Added')
def inputLayerTypes(self):
return [QgsProcessing.TypeVector]
def prepareAlgorithm(self, parameters, context, feedback):
field_type = self.parameterAsEnum(parameters, self.FIELD_TYPE, context)
field_name = self.parameterAsString(parameters, self.FIELD_NAME, context)
field_length = self.parameterAsInt(parameters, self.FIELD_LENGTH, context)
field_precision = self.parameterAsInt(parameters, self.FIELD_PRECISION, context)
self.field = QgsField(field_name, self.TYPES[field_type], '',
field_length, field_precision)
return True
def outputFields(self, inputFields):
inputFields.append(self.field)
return inputFields
def sourceFlags(self):
return QgsProcessingFeatureSource.FlagSkipGeometryValidityChecks
def processFeature(self, feature, context, feedback):
attributes = feature.attributes()
attributes.append(None)
feature.setAttributes(attributes)
return [feature]

View File

@ -28,7 +28,6 @@ from qgis.core import (QgsApplication,
from PyQt5.QtCore import QCoreApplication
from .AddTableField import AddTableField
from .Aggregate import Aggregate
from .BarPlot import BarPlot
from .BasicStatistics import BasicStatisticsForField
@ -124,8 +123,7 @@ class QgisAlgorithmProvider(QgsProcessingProvider):
self.externalAlgs = []
def getAlgs(self):
algs = [AddTableField(),
Aggregate(),
algs = [Aggregate(),
BarPlot(),
BasicStatisticsForField(),
BoxPlot(),

View File

@ -541,8 +541,7 @@ tests:
name: expected/extract_by_location_touches.gml
type: vector
- algorithm: qgis:addfieldtoattributestable
- algorithm: native:addfieldtoattributestable
name: add float field
params:
FIELD_LENGTH: 10

View File

@ -22,6 +22,7 @@ SET(QGIS_ANALYSIS_SRCS
interpolation/Vector3D.cpp
processing/qgsalgorithmaddincrementalfield.cpp
processing/qgsalgorithmaddtablefield.cpp
processing/qgsalgorithmaddxyfields.cpp
processing/qgsalgorithmarraytranslatedfeatures.cpp
processing/qgsalgorithmaspect.cpp

View File

@ -0,0 +1,134 @@
/***************************************************************************
qgsalgorithmaddtablefield.cpp
-----------------------------------
begin : November 2019
copyright : (C) 2019 by Alexander Bruy
email : alexander dot bruy 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 "qgsalgorithmaddtablefield.h"
///@cond PRIVATE
QString QgsAddTableFieldAlgorithm::name() const
{
return QStringLiteral( "addfieldtoattributestable" );
}
QString QgsAddTableFieldAlgorithm::displayName() const
{
return QObject::tr( "Add field to attributes table" );
}
QString QgsAddTableFieldAlgorithm::shortHelpString() const
{
return QObject::tr( "This algorithm adds a new attribute to a vector layer.\n\n"
"The name and characteristics of the attribute are defined as parameters. The new attribute "
"is not added to the input layer but a new layer is generated instead.\n\n" );
}
QStringList QgsAddTableFieldAlgorithm::tags() const
{
return QObject::tr( "add,create,new,attribute,fields" ).split( ',' );
}
QString QgsAddTableFieldAlgorithm::group() const
{
return QObject::tr( "Vector table" );
}
QString QgsAddTableFieldAlgorithm::groupId() const
{
return QStringLiteral( "vectortable" );
}
QString QgsAddTableFieldAlgorithm::outputName() const
{
return QObject::tr( "Added" );
}
QList<int> QgsAddTableFieldAlgorithm::inputLayerTypes() const
{
return QList<int>() << QgsProcessing::TypeVector;
}
QgsProcessingFeatureSource::Flag QgsAddTableFieldAlgorithm::sourceFlags() const
{
return QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks;
}
QgsAddTableFieldAlgorithm *QgsAddTableFieldAlgorithm::createInstance() const
{
return new QgsAddTableFieldAlgorithm();
}
void QgsAddTableFieldAlgorithm::initParameters( const QVariantMap & )
{
addParameter( new QgsProcessingParameterString( QStringLiteral( "FIELD_NAME" ), QObject::tr( "Field name" ) ) );
addParameter( new QgsProcessingParameterEnum( QStringLiteral( "FIELD_TYPE" ), QObject::tr( "Field type" ),
QStringList() << QObject::tr( "Integer" ) << QObject::tr( "Float" ) << QObject::tr( "String" ), false, 0 ) );
addParameter( new QgsProcessingParameterNumber( QStringLiteral( "FIELD_LENGTH" ), QObject::tr( "Field length" ),
QgsProcessingParameterNumber::Integer, 10, false, 1, 255 ) );
addParameter( new QgsProcessingParameterNumber( QStringLiteral( "FIELD_PRECISION" ), QObject::tr( "Field precision" ),
QgsProcessingParameterNumber::Integer, 0, false, 0, 10 ) );
}
QgsFields QgsAddTableFieldAlgorithm::outputFields( const QgsFields &inputFields ) const
{
QgsFields outFields = inputFields;
outFields.append( QgsField( mField ) );
return outFields;
}
bool QgsAddTableFieldAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
{
QString name = parameterAsString( parameters, QStringLiteral( "FIELD_NAME" ), context );
int type = parameterAsInt( parameters, QStringLiteral( "FIELD_TYPE" ), context );
int length = parameterAsInt( parameters, QStringLiteral( "FIELD_LENGTH" ), context );
int precision = parameterAsInt( parameters, QStringLiteral( "FIELD_PRECISION" ), context );
mField.setName( name );
mField.setLength( length );
mField.setPrecision( precision );
switch ( type )
{
case 0:
mField.setType( QVariant::Int );
break;
case 1:
mField.setType( QVariant::Double );
break;
case 2:
mField.setType( QVariant::String );
break;
}
return true;
}
QgsFeatureList QgsAddTableFieldAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
{
QgsFeature f = feature;
QgsAttributes attributes = f.attributes();
attributes.append( QVariant() );
f.setAttributes( attributes );
return QgsFeatureList() << f;
}
bool QgsAddTableFieldAlgorithm::supportInPlaceEdit( const QgsMapLayer *layer ) const
{
Q_UNUSED( layer )
return false;
}
///@endcond

View File

@ -0,0 +1,64 @@
/***************************************************************************
qgsalgorithmaddtablefield.h
---------------------------------
begin : November 2019
copyright : (C) 2019 by Alexander Bruy
email : alexander dot bruy 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 QGSALGORITHMADDTABLEFIELD_H
#define QGSALGORITHMADDTABLEFIELD_H
#define SIP_NO_FILE
#include "qgis_sip.h"
#include "qgsprocessingalgorithm.h"
///@cond PRIVATE
/**
* Native add table field algorithm.
*/
class QgsAddTableFieldAlgorithm : public QgsProcessingFeatureBasedAlgorithm
{
public:
QgsAddTableFieldAlgorithm() = default;
QString name() const override;
QString displayName() const override;
QStringList tags() const override;
QString group() const override;
QString groupId() const override;
QString shortHelpString() const override;
QList<int> inputLayerTypes() const override;
QgsAddTableFieldAlgorithm *createInstance() const override SIP_FACTORY;
protected:
void initParameters( const QVariantMap &configuration = QVariantMap() ) override;
QString outputName() const override;
QgsFields outputFields( const QgsFields &inputFields ) const override;
QgsProcessingFeatureSource::Flag sourceFlags() const override;
bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
QgsFeatureList processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
bool supportInPlaceEdit( const QgsMapLayer *layer ) const override;
private:
QgsField mField;
};
///@endcond PRIVATE
#endif // QGSALGORITHMADDTABLEFIELD_H

View File

@ -17,6 +17,7 @@
#include "qgsnativealgorithms.h"
#include "qgsalgorithmaddincrementalfield.h"
#include "qgsalgorithmaddtablefield.h"
#include "qgsalgorithmaddxyfields.h"
#include "qgsalgorithmarraytranslatedfeatures.h"
#include "qgsalgorithmaspect.h"
@ -180,6 +181,7 @@ bool QgsNativeAlgorithms::supportsNonFileBasedOutput() const
void QgsNativeAlgorithms::loadAlgorithms()
{
addAlgorithm( new QgsAddIncrementalFieldAlgorithm() );
addAlgorithm( new QgsAddTableFieldAlgorithm() );
addAlgorithm( new QgsAddXYFieldsAlgorithm() );
addAlgorithm( new QgsAddUniqueValueIndexAlgorithm() );
addAlgorithm( new QgsArrayTranslatedFeaturesAlgorithm() );