23QString QgsRefactorFieldsAlgorithm::name()
const
25 return QStringLiteral(
"refactorfields" );
28QString QgsRefactorFieldsAlgorithm::displayName()
const
30 return QObject::tr(
"Refactor fields" );
33QString QgsRefactorFieldsAlgorithm::shortHelpString()
const
35 return QObject::tr(
"This algorithm allows editing the structure of the attributes table of a vector layer. Fields can be modified "
36 "in their type and name, using a fields mapping.\n\n"
37 "The original layer is not modified. A new layer is generated, which contains a modified attribute table, according "
38 "to the provided fields mapping.\n\n"
39 "Rows in orange have constraints in the template layer from which these fields were loaded. Treat this information "
40 "as a hint during configuration. No constraints will be added on an output layer nor will they be checked or "
41 "enforced by the algorithm." );
44QStringList QgsRefactorFieldsAlgorithm::tags()
const
46 return QObject::tr(
"attributes,table" ).split(
',' );
49QString QgsRefactorFieldsAlgorithm::group()
const
51 return QObject::tr(
"Vector table" );
54QString QgsRefactorFieldsAlgorithm::groupId()
const
56 return QStringLiteral(
"vectortable" );
59QString QgsRefactorFieldsAlgorithm::outputName()
const
61 return QObject::tr(
"Refactored" );
64QList<int> QgsRefactorFieldsAlgorithm::inputLayerTypes()
const
74QgsRefactorFieldsAlgorithm *QgsRefactorFieldsAlgorithm::createInstance()
const
76 return new QgsRefactorFieldsAlgorithm();
79void QgsRefactorFieldsAlgorithm::initParameters(
const QVariantMap & )
81 std::unique_ptr< QgsProcessingParameterFieldMapping > param = std::make_unique< QgsProcessingParameterFieldMapping> ( QStringLiteral(
"FIELDS_MAPPING" ), QObject::tr(
"Fields mapping" ), QStringLiteral(
"INPUT" ) );
82 addParameter( param.release() );
92 std::unique_ptr< QgsProcessingFeatureSource > source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
99 mExpressionContext = createExpressionContext( parameters, context, source.get() );
101 const QVariantList mapping = parameters.value( QStringLiteral(
"FIELDS_MAPPING" ) ).toList();
102 for (
const QVariant &map : mapping )
104 const QVariantMap fieldDef = map.toMap();
105 const QString name = fieldDef.value( QStringLiteral(
"name" ) ).toString();
106 if ( name.isEmpty() )
109 const QVariant::Type type =
static_cast< QVariant::Type
>( fieldDef.value( QStringLiteral(
"type" ) ).toInt() );
110 const QString
typeName = fieldDef.value( QStringLiteral(
"sub_name" ) ).toString();
111 const QVariant::Type subType =
static_cast< QVariant::Type
>( fieldDef.value( QStringLiteral(
"sub_type" ) ).toInt() );
113 const int length = fieldDef.value( QStringLiteral(
"length" ), 0 ).toInt();
114 const int precision = fieldDef.value( QStringLiteral(
"precision" ), 0 ).toInt();
118 const QString expressionString = fieldDef.value( QStringLiteral(
"expression" ) ).toString();
119 if ( !expressionString.isEmpty() )
122 expression.setGeomCalculator( &mDa );
124 expression.setAreaUnits( context.
areaUnit() );
125 if ( expression.hasParserError() )
131 expression.parserErrorString() ) );
133 mExpressions.append( expression );
146 if ( !mExpressionsPrepared )
148 for (
auto it = mExpressions.begin(); it != mExpressions.end(); ++it )
151 it->prepare( &mExpressionContext );
156 attributes.reserve( mExpressions.size() );
157 for (
auto it = mExpressions.begin(); it != mExpressions.end(); ++it )
161 mExpressionContext.setFeature( feature );
162 mExpressionContext.lastScope()->setVariable( QStringLiteral(
"row_number" ), mRowNumber );
163 const QVariant value = it->evaluate( &mExpressionContext );
164 if ( it->hasEvalError() )
166 throw QgsProcessingException( QObject::tr(
"Evaluation error in expression \"%1\": %2" ).arg( it->expression(), it->evalErrorString() ) );
168 attributes.append( value );
172 attributes.append( QVariant() );
182bool QgsRefactorFieldsAlgorithm::supportInPlaceEdit(
const QgsMapLayer *layer )
const
Class for parsing and evaluation of expressions (formerly called "search strings").
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
Encapsulate a field in an attribute table or data source.
Container of fields for a vector layer.
Base class for all map layer types.
Contains information about the context in which a processing algorithm is executed.
QgsUnitTypes::AreaUnit areaUnit() const
Returns the area unit to use for area calculations.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
QgsUnitTypes::DistanceUnit distanceUnit() const
Returns the distance unit to use for distance calculations.
QString ellipsoid() const
Returns the ellipsoid to use for distance and area calculations.
Custom exception class for processing related exceptions.
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
@ FlagSkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Base class for providing feedback from a processing algorithm.
@ TypeVector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
QList< QgsFeature > QgsFeatureList