Skip to content

Commit

Permalink
[processing] port add table field algorithm to C++
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Dec 9, 2019
1 parent ac958de commit eeaffb9
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 121 deletions.
12 changes: 0 additions & 12 deletions python/plugins/processing/algs/help/qgis.yaml
@@ -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.

Expand Down
104 changes: 0 additions & 104 deletions python/plugins/processing/algs/qgis/AddTableField.py

This file was deleted.

4 changes: 1 addition & 3 deletions python/plugins/processing/algs/qgis/QgisAlgorithmProvider.py
Expand Up @@ -28,7 +28,6 @@

from PyQt5.QtCore import QCoreApplication

from .AddTableField import AddTableField
from .Aggregate import Aggregate
from .BarPlot import BarPlot
from .BasicStatistics import BasicStatisticsForField
Expand Down Expand Up @@ -124,8 +123,7 @@ def __init__(self):
self.externalAlgs = []

def getAlgs(self):
algs = [AddTableField(),
Aggregate(),
algs = [Aggregate(),
BarPlot(),
BasicStatisticsForField(),
BoxPlot(),
Expand Down
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/analysis/CMakeLists.txt
Expand Up @@ -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
Expand Down
134 changes: 134 additions & 0 deletions src/analysis/processing/qgsalgorithmaddtablefield.cpp
@@ -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
64 changes: 64 additions & 0 deletions src/analysis/processing/qgsalgorithmaddtablefield.h
@@ -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
2 changes: 2 additions & 0 deletions src/analysis/processing/qgsnativealgorithms.cpp
Expand Up @@ -17,6 +17,7 @@

#include "qgsnativealgorithms.h"
#include "qgsalgorithmaddincrementalfield.h"
#include "qgsalgorithmaddtablefield.h"
#include "qgsalgorithmaddxyfields.h"
#include "qgsalgorithmarraytranslatedfeatures.h"
#include "qgsalgorithmaspect.h"
Expand Down Expand Up @@ -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() );
Expand Down

0 comments on commit eeaffb9

Please sign in to comment.