24using namespace Qt::StringLiterals;
28QString QgsRefactorFieldsAlgorithm::name()
const
30 return u
"refactorfields"_s;
33QString QgsRefactorFieldsAlgorithm::displayName()
const
35 return QObject::tr(
"Refactor fields" );
38QString QgsRefactorFieldsAlgorithm::shortHelpString()
const
41 "This algorithm allows editing the structure of the attributes table of a vector layer. Fields can be modified "
42 "in their type and name, using a fields mapping.\n\n"
43 "The original layer is not modified. A new layer is generated, which contains a modified attribute table, according "
44 "to the provided fields mapping.\n\n"
45 "Rows in orange have constraints in the template layer from which these fields were loaded. Treat this information "
46 "as a hint during configuration. No constraints will be added on an output layer nor will they be checked or "
47 "enforced by the algorithm."
51QString QgsRefactorFieldsAlgorithm::shortDescription()
const
53 return QObject::tr(
"Allows editing the structure of the attributes table of a vector layer, permitting field renaming, creation and deletion." );
56QStringList QgsRefactorFieldsAlgorithm::tags()
const
58 return QObject::tr(
"attributes,table" ).split(
',' );
61QString QgsRefactorFieldsAlgorithm::group()
const
63 return QObject::tr(
"Vector table" );
66QString QgsRefactorFieldsAlgorithm::groupId()
const
68 return u
"vectortable"_s;
71QString QgsRefactorFieldsAlgorithm::outputName()
const
73 return QObject::tr(
"Refactored" );
76QList<int> QgsRefactorFieldsAlgorithm::inputLayerTypes()
const
91QgsRefactorFieldsAlgorithm *QgsRefactorFieldsAlgorithm::createInstance()
const
93 return new QgsRefactorFieldsAlgorithm();
96void QgsRefactorFieldsAlgorithm::initParameters(
const QVariantMap & )
98 auto param = std::make_unique<QgsProcessingParameterFieldMapping>( u
"FIELDS_MAPPING"_s, QObject::tr(
"Fields mapping" ), u
"INPUT"_s );
99 addParameter( param.release() );
109 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u
"INPUT"_s, context ) );
116 mExpressionContext = createExpressionContext( parameters, context, source.get() );
118 const QVariantList mapping = parameters.value( u
"FIELDS_MAPPING"_s ).toList();
119 for (
const QVariant &map : mapping )
121 const QVariantMap fieldDef = map.toMap();
122 const QString name = fieldDef.value( u
"name"_s ).toString();
123 if ( name.isEmpty() )
126 const QMetaType::Type type =
static_cast<QMetaType::Type
>( fieldDef.value( u
"type"_s ).toInt() );
127 const QString typeName = fieldDef.value( u
"sub_name"_s ).toString();
128 const QMetaType::Type subType =
static_cast<QMetaType::Type
>( fieldDef.value( u
"sub_type"_s ).toInt() );
130 const int length = fieldDef.value( u
"length"_s, 0 ).toInt();
131 const int precision = fieldDef.value( u
"precision"_s, 0 ).toInt();
133 const QString alias = fieldDef.value( u
"alias"_s ).toString();
134 const QString comment = fieldDef.value( u
"comment"_s ).toString();
136 QgsField newField( name, type, typeName, length, precision, QString(), subType );
137 newField.setAlias( alias );
138 newField.setComment( comment );
139 mFields.append( newField );
141 const QString expressionString = fieldDef.value( u
"expression"_s ).toString();
142 if ( !expressionString.isEmpty() )
145 expression.setGeomCalculator( &mDa );
147 expression.setAreaUnits( context.
areaUnit() );
148 if ( expression.hasParserError() )
150 throw QgsProcessingException( QObject::tr(
"Parser error for field \"%1\" with expression \"%2\": %3" ).arg( name, expressionString, expression.parserErrorString() ) );
152 mExpressions.append( expression );
165 if ( !mExpressionsPrepared )
167 for (
auto it = mExpressions.begin(); it != mExpressions.end(); ++it )
170 it->prepare( &mExpressionContext );
172 mExpressionsPrepared =
true;
176 attributes.reserve( mExpressions.size() );
177 for (
auto it = mExpressions.begin(); it != mExpressions.end(); ++it )
181 mExpressionContext.setFeature( feature );
182 mExpressionContext.lastScope()->setVariable( u
"row_number"_s, mRowNumber );
183 const QVariant value = it->evaluate( &mExpressionContext );
184 if ( it->hasEvalError() )
186 throw QgsProcessingException( QObject::tr(
"Evaluation error in expression \"%1\": %2" ).arg( it->expression(), it->evalErrorString() ) );
188 attributes.append( value );
192 attributes.append( QVariant() );
202bool QgsRefactorFieldsAlgorithm::supportInPlaceEdit(
const QgsMapLayer *layer )
const
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
@ RespectsEllipsoid
Algorithm respects the context's ellipsoid settings, and uses ellipsoidal based measurements.
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Handles 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.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
Qgis::AreaUnit areaUnit() const
Returns the area unit to use for area calculations.
Qgis::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.
Base class for providing feedback from a processing algorithm.
QList< QgsFeature > QgsFeatureList