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
40 return QObject::tr(
"This algorithm allows editing the structure of the attributes table of a vector layer. Fields can be modified "
41 "in their type and name, using a fields mapping.\n\n"
42 "The original layer is not modified. A new layer is generated, which contains a modified attribute table, according "
43 "to the provided fields mapping.\n\n"
44 "Rows in orange have constraints in the template layer from which these fields were loaded. Treat this information "
45 "as a hint during configuration. No constraints will be added on an output layer nor will they be checked or "
46 "enforced by the algorithm." );
49QString QgsRefactorFieldsAlgorithm::shortDescription()
const
51 return QObject::tr(
"Allows editing the structure of the attributes table of a vector layer, permitting field renaming, creation and deletion." );
54QStringList QgsRefactorFieldsAlgorithm::tags()
const
56 return QObject::tr(
"attributes,table" ).split(
',' );
59QString QgsRefactorFieldsAlgorithm::group()
const
61 return QObject::tr(
"Vector table" );
64QString QgsRefactorFieldsAlgorithm::groupId()
const
66 return u
"vectortable"_s;
69QString QgsRefactorFieldsAlgorithm::outputName()
const
71 return QObject::tr(
"Refactored" );
74QList<int> QgsRefactorFieldsAlgorithm::inputLayerTypes()
const
89QgsRefactorFieldsAlgorithm *QgsRefactorFieldsAlgorithm::createInstance()
const
91 return new QgsRefactorFieldsAlgorithm();
94void QgsRefactorFieldsAlgorithm::initParameters(
const QVariantMap & )
96 auto param = std::make_unique<QgsProcessingParameterFieldMapping>( u
"FIELDS_MAPPING"_s, QObject::tr(
"Fields mapping" ), u
"INPUT"_s );
97 addParameter( param.release() );
107 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u
"INPUT"_s, context ) );
114 mExpressionContext = createExpressionContext( parameters, context, source.get() );
116 const QVariantList mapping = parameters.value( u
"FIELDS_MAPPING"_s ).toList();
117 for (
const QVariant &map : mapping )
119 const QVariantMap fieldDef = map.toMap();
120 const QString name = fieldDef.value( u
"name"_s ).toString();
121 if ( name.isEmpty() )
124 const QMetaType::Type type =
static_cast<QMetaType::Type
>( fieldDef.value( u
"type"_s ).toInt() );
125 const QString typeName = fieldDef.value( u
"sub_name"_s ).toString();
126 const QMetaType::Type subType =
static_cast<QMetaType::Type
>( fieldDef.value( u
"sub_type"_s ).toInt() );
128 const int length = fieldDef.value( u
"length"_s, 0 ).toInt();
129 const int precision = fieldDef.value( u
"precision"_s, 0 ).toInt();
131 const QString alias = fieldDef.value( u
"alias"_s ).toString();
132 const QString comment = fieldDef.value( u
"comment"_s ).toString();
134 QgsField newField( name, type, typeName, length, precision, QString(), subType );
135 newField.setAlias( alias );
136 newField.setComment( comment );
137 mFields.append( newField );
139 const QString expressionString = fieldDef.value( u
"expression"_s ).toString();
140 if ( !expressionString.isEmpty() )
143 expression.setGeomCalculator( &mDa );
145 expression.setAreaUnits( context.
areaUnit() );
146 if ( expression.hasParserError() )
152 expression.parserErrorString()
155 mExpressions.append( expression );
168 if ( !mExpressionsPrepared )
170 for (
auto it = mExpressions.begin(); it != mExpressions.end(); ++it )
173 it->prepare( &mExpressionContext );
178 attributes.reserve( mExpressions.size() );
179 for (
auto it = mExpressions.begin(); it != mExpressions.end(); ++it )
183 mExpressionContext.setFeature( feature );
184 mExpressionContext.lastScope()->setVariable( u
"row_number"_s, mRowNumber );
185 const QVariant value = it->evaluate( &mExpressionContext );
186 if ( it->hasEvalError() )
188 throw QgsProcessingException( QObject::tr(
"Evaluation error in expression \"%1\": %2" ).arg( it->expression(), it->evalErrorString() ) );
190 attributes.append( value );
194 attributes.append( QVariant() );
204bool 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