24QString QgsRefactorFieldsAlgorithm::name()
const
26 return QStringLiteral(
"refactorfields" );
29QString QgsRefactorFieldsAlgorithm::displayName()
const
31 return QObject::tr(
"Refactor fields" );
34QString QgsRefactorFieldsAlgorithm::shortHelpString()
const
36 return QObject::tr(
"This algorithm allows editing the structure of the attributes table of a vector layer. Fields can be modified "
37 "in their type and name, using a fields mapping.\n\n"
38 "The original layer is not modified. A new layer is generated, which contains a modified attribute table, according "
39 "to the provided fields mapping.\n\n"
40 "Rows in orange have constraints in the template layer from which these fields were loaded. Treat this information "
41 "as a hint during configuration. No constraints will be added on an output layer nor will they be checked or "
42 "enforced by the algorithm." );
45QString QgsRefactorFieldsAlgorithm::shortDescription()
const
47 return QObject::tr(
"Allows editing the structure of the attributes table of a vector layer, permitting field renaming, creation and deletion." );
50QStringList QgsRefactorFieldsAlgorithm::tags()
const
52 return QObject::tr(
"attributes,table" ).split(
',' );
55QString QgsRefactorFieldsAlgorithm::group()
const
57 return QObject::tr(
"Vector table" );
60QString QgsRefactorFieldsAlgorithm::groupId()
const
62 return QStringLiteral(
"vectortable" );
65QString QgsRefactorFieldsAlgorithm::outputName()
const
67 return QObject::tr(
"Refactored" );
70QList<int> QgsRefactorFieldsAlgorithm::inputLayerTypes()
const
85QgsRefactorFieldsAlgorithm *QgsRefactorFieldsAlgorithm::createInstance()
const
87 return new QgsRefactorFieldsAlgorithm();
90void QgsRefactorFieldsAlgorithm::initParameters(
const QVariantMap & )
92 auto param = std::make_unique<QgsProcessingParameterFieldMapping>( QStringLiteral(
"FIELDS_MAPPING" ), QObject::tr(
"Fields mapping" ), QStringLiteral(
"INPUT" ) );
93 addParameter( param.release() );
103 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
110 mExpressionContext = createExpressionContext( parameters, context, source.get() );
112 const QVariantList mapping = parameters.value( QStringLiteral(
"FIELDS_MAPPING" ) ).toList();
113 for (
const QVariant &map : mapping )
115 const QVariantMap fieldDef = map.toMap();
116 const QString name = fieldDef.value( QStringLiteral(
"name" ) ).toString();
117 if ( name.isEmpty() )
120 const QMetaType::Type type =
static_cast<QMetaType::Type
>( fieldDef.value( QStringLiteral(
"type" ) ).toInt() );
121 const QString typeName = fieldDef.value( QStringLiteral(
"sub_name" ) ).toString();
122 const QMetaType::Type subType =
static_cast<QMetaType::Type
>( fieldDef.value( QStringLiteral(
"sub_type" ) ).toInt() );
124 const int length = fieldDef.value( QStringLiteral(
"length" ), 0 ).toInt();
125 const int precision = fieldDef.value( QStringLiteral(
"precision" ), 0 ).toInt();
127 const QString alias = fieldDef.value( QStringLiteral(
"alias" ) ).toString();
128 const QString comment = fieldDef.value( QStringLiteral(
"comment" ) ).toString();
130 QgsField newField( name, type, typeName, length, precision, QString(), subType );
131 newField.setAlias( alias );
132 newField.setComment( comment );
133 mFields.append( newField );
135 const QString expressionString = fieldDef.value( QStringLiteral(
"expression" ) ).toString();
136 if ( !expressionString.isEmpty() )
139 expression.setGeomCalculator( &mDa );
141 expression.setAreaUnits( context.
areaUnit() );
142 if ( expression.hasParserError() )
148 expression.parserErrorString()
151 mExpressions.append( expression );
164 if ( !mExpressionsPrepared )
166 for (
auto it = mExpressions.begin(); it != mExpressions.end(); ++it )
169 it->prepare( &mExpressionContext );
174 attributes.reserve( mExpressions.size() );
175 for (
auto it = mExpressions.begin(); it != mExpressions.end(); ++it )
179 mExpressionContext.setFeature( feature );
180 mExpressionContext.lastScope()->setVariable( QStringLiteral(
"row_number" ), mRowNumber );
181 const QVariant value = it->evaluate( &mExpressionContext );
182 if ( it->hasEvalError() )
184 throw QgsProcessingException( QObject::tr(
"Evaluation error in expression \"%1\": %2" ).arg( it->expression(), it->evalErrorString() ) );
186 attributes.append( value );
190 attributes.append( QVariant() );
200bool 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