QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgsalgorithmsplitfeaturesbyattributecharacter.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmsplitfeaturesbyattributecharacter.cpp
3 ---------------------
4 begin : September 2019
5 copyright : (C) 2019 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19#include "qgscurve.h"
20#include "qgslinestring.h"
21#include "qgscircularstring.h"
22#include "qgscompoundcurve.h"
24#include <QRegularExpression>
25
27
28QString QgsSplitFeaturesByAttributeCharacterAlgorithm::name() const
29{
30 return QStringLiteral( "splitfeaturesbycharacter" );
31}
32
33QString QgsSplitFeaturesByAttributeCharacterAlgorithm::displayName() const
34{
35 return QObject::tr( "Split features by character" );
36}
37
38QStringList QgsSplitFeaturesByAttributeCharacterAlgorithm::tags() const
39{
40 return QObject::tr( "separate,attribute,value,string" ).split( ',' );
41}
42
43QString QgsSplitFeaturesByAttributeCharacterAlgorithm::group() const
44{
45 return QObject::tr( "Vector general" );
46}
47
48QString QgsSplitFeaturesByAttributeCharacterAlgorithm::groupId() const
49{
50 return QStringLiteral( "vectorgeneral" );
51}
52
53QString QgsSplitFeaturesByAttributeCharacterAlgorithm::shortHelpString() const
54{
55 return QObject::tr( "This algorithm splits features into multiple output features by splitting a field's value with a specified character.\n\n"
56 "For instance, if a layer contains features with multiple comma separated values contained in a single field, this "
57 "algorithm can be used to split these values up across multiple output features.\n\n"
58 "Geometries and other attributes remain unchanged in the output.\n\n"
59 "Optionally, the separator string can be a regular expression for added flexibility." );
60}
61
62QString QgsSplitFeaturesByAttributeCharacterAlgorithm::shortDescription() const
63{
64 return QObject::tr( "Splits features into multiple output features by splitting a field by a character." );
65}
66
67Qgis::ProcessingAlgorithmDocumentationFlags QgsSplitFeaturesByAttributeCharacterAlgorithm::documentationFlags() const
68{
70}
71
72QList<int> QgsSplitFeaturesByAttributeCharacterAlgorithm::inputLayerTypes() const
73{
74 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::Vector );
75}
76
77void QgsSplitFeaturesByAttributeCharacterAlgorithm::initParameters( const QVariantMap & )
78{
79 addParameter( new QgsProcessingParameterField( QStringLiteral( "FIELD" ), QObject::tr( "Split using values in field" ), QVariant(), QStringLiteral( "INPUT" ) ) );
80 addParameter( new QgsProcessingParameterString( QStringLiteral( "CHAR" ), QObject::tr( "Split values using character" ) ) );
81 std::unique_ptr<QgsProcessingParameterDefinition> regexParam = std::make_unique<QgsProcessingParameterBoolean>( QStringLiteral( "REGEX" ), QObject::tr( "Use regular expression separator" ) );
82 regexParam->setFlags( regexParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
83 addParameter( regexParam.release() );
84}
85
86Qgis::ProcessingSourceType QgsSplitFeaturesByAttributeCharacterAlgorithm::outputLayerType() const
87{
89}
90
91QgsSplitFeaturesByAttributeCharacterAlgorithm *QgsSplitFeaturesByAttributeCharacterAlgorithm::createInstance() const
92{
93 return new QgsSplitFeaturesByAttributeCharacterAlgorithm();
94}
95
96QgsFields QgsSplitFeaturesByAttributeCharacterAlgorithm::outputFields( const QgsFields &inputFields ) const
97{
98 mFieldIndex = inputFields.lookupField( mFieldName );
99 QgsFields outputFields;
100 for ( int i = 0; i < inputFields.count(); ++i )
101 {
102 if ( i != mFieldIndex )
103 {
104 outputFields.append( inputFields.at( i ) );
105 }
106 else
107 {
108 // we need to convert the split field to a string field
109 outputFields.append( QgsField( inputFields.at( i ).name(), QMetaType::Type::QString ) );
110 }
111 }
112 return outputFields;
113}
114
115QString QgsSplitFeaturesByAttributeCharacterAlgorithm::outputName() const
116{
117 return QObject::tr( "Split" );
118}
119
120bool QgsSplitFeaturesByAttributeCharacterAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
121{
122 mChar = parameterAsString( parameters, QStringLiteral( "CHAR" ), context );
123 mFieldName = parameterAsString( parameters, QStringLiteral( "FIELD" ), context );
124 mUseRegex = parameterAsBoolean( parameters, QStringLiteral( "REGEX" ), context );
125 if ( mUseRegex )
126 mRegex = QRegularExpression( mChar );
127 return true;
128}
129
130QgsFeatureList QgsSplitFeaturesByAttributeCharacterAlgorithm::processFeature( const QgsFeature &f, QgsProcessingContext &, QgsProcessingFeedback * )
131{
132 QgsFeatureList res;
133 const QString val = f.attribute( mFieldIndex ).toString();
134 const QStringList parts = mUseRegex ? val.split( mRegex ) : val.split( mChar );
135 res.reserve( parts.size() );
136 for ( const QString &p : parts )
137 {
138 QgsFeature out = f;
139 out.setAttribute( mFieldIndex, p );
140 res << out;
141 }
142 return res;
143}
144
145QgsFeatureSink::SinkFlags QgsSplitFeaturesByAttributeCharacterAlgorithm::sinkFlags() const
146{
148}
149
ProcessingSourceType
Processing data source types.
Definition qgis.h:3333
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
@ RegeneratesPrimaryKey
Algorithm always drops any existing primary keys or FID values and regenerates them in outputs.
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
Definition qgis.h:3430
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
QFlags< SinkFlag > SinkFlags
@ RegeneratePrimaryKey
This flag indicates, that a primary key field cannot be guaranteed to be unique and the sink should i...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
Q_INVOKABLE bool setAttribute(int field, const QVariant &attr)
Sets an attribute's value by field index.
Q_INVOKABLE QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:53
QString name
Definition qgsfield.h:62
Container of fields for a vector layer.
Definition qgsfields.h:46
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Definition qgsfields.cpp:70
int count
Definition qgsfields.h:50
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
Q_INVOKABLE int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
Contains information about the context in which a processing algorithm is executed.
Base class for providing feedback from a processing algorithm.
A vector layer or feature source field parameter for processing algorithms.
A string parameter for processing algorithms.
QList< QgsFeature > QgsFeatureList