QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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
20#include "qgscircularstring.h"
21#include "qgscompoundcurve.h"
22#include "qgscurve.h"
24#include "qgslinestring.h"
25
26#include <QRegularExpression>
27#include <QString>
28
29using namespace Qt::StringLiterals;
30
32
33QString QgsSplitFeaturesByAttributeCharacterAlgorithm::name() const
34{
35 return u"splitfeaturesbycharacter"_s;
36}
37
38QString QgsSplitFeaturesByAttributeCharacterAlgorithm::displayName() const
39{
40 return QObject::tr( "Split features by character" );
41}
42
43QStringList QgsSplitFeaturesByAttributeCharacterAlgorithm::tags() const
44{
45 return QObject::tr( "separate,attribute,value,string" ).split( ',' );
46}
47
48QString QgsSplitFeaturesByAttributeCharacterAlgorithm::group() const
49{
50 return QObject::tr( "Vector general" );
51}
52
53QString QgsSplitFeaturesByAttributeCharacterAlgorithm::groupId() const
54{
55 return u"vectorgeneral"_s;
56}
57
58QString QgsSplitFeaturesByAttributeCharacterAlgorithm::shortHelpString() const
59{
60 return QObject::tr(
61 "This algorithm splits features into multiple output features by splitting a field's value with a specified character.\n\n"
62 "For instance, if a layer contains features with multiple comma separated values contained in a single field, this "
63 "algorithm can be used to split these values up across multiple output features.\n\n"
64 "Geometries and other attributes remain unchanged in the output.\n\n"
65 "Optionally, the separator string can be a regular expression for added flexibility."
66 );
67}
68
69QString QgsSplitFeaturesByAttributeCharacterAlgorithm::shortDescription() const
70{
71 return QObject::tr( "Splits features into multiple output features by splitting a field by a character." );
72}
73
74Qgis::ProcessingAlgorithmDocumentationFlags QgsSplitFeaturesByAttributeCharacterAlgorithm::documentationFlags() const
75{
77}
78
79QList<int> QgsSplitFeaturesByAttributeCharacterAlgorithm::inputLayerTypes() const
80{
81 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::Vector );
82}
83
84void QgsSplitFeaturesByAttributeCharacterAlgorithm::initParameters( const QVariantMap & )
85{
86 addParameter( new QgsProcessingParameterField( u"FIELD"_s, QObject::tr( "Split using values in field" ), QVariant(), u"INPUT"_s ) );
87 addParameter( new QgsProcessingParameterString( u"CHAR"_s, QObject::tr( "Split values using character" ) ) );
88 std::unique_ptr<QgsProcessingParameterDefinition> regexParam = std::make_unique<QgsProcessingParameterBoolean>( u"REGEX"_s, QObject::tr( "Use regular expression separator" ) );
89 regexParam->setFlags( regexParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
90 addParameter( regexParam.release() );
91}
92
93Qgis::ProcessingSourceType QgsSplitFeaturesByAttributeCharacterAlgorithm::outputLayerType() const
94{
96}
97
98QgsSplitFeaturesByAttributeCharacterAlgorithm *QgsSplitFeaturesByAttributeCharacterAlgorithm::createInstance() const
99{
100 return new QgsSplitFeaturesByAttributeCharacterAlgorithm();
101}
102
103QgsFields QgsSplitFeaturesByAttributeCharacterAlgorithm::outputFields( const QgsFields &inputFields ) const
104{
105 mFieldIndex = inputFields.lookupField( mFieldName );
106 QgsFields outputFields;
107 for ( int i = 0; i < inputFields.count(); ++i )
108 {
109 if ( i != mFieldIndex )
110 {
111 outputFields.append( inputFields.at( i ) );
112 }
113 else
114 {
115 // we need to convert the split field to a string field
116 outputFields.append( QgsField( inputFields.at( i ).name(), QMetaType::Type::QString ) );
117 }
118 }
119 return outputFields;
120}
121
122QString QgsSplitFeaturesByAttributeCharacterAlgorithm::outputName() const
123{
124 return QObject::tr( "Split" );
125}
126
127bool QgsSplitFeaturesByAttributeCharacterAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
128{
129 mChar = parameterAsString( parameters, u"CHAR"_s, context );
130 mFieldName = parameterAsString( parameters, u"FIELD"_s, context );
131 mUseRegex = parameterAsBoolean( parameters, u"REGEX"_s, context );
132 if ( mUseRegex )
133 mRegex = QRegularExpression( mChar );
134 return true;
135}
136
137QgsFeatureList QgsSplitFeaturesByAttributeCharacterAlgorithm::processFeature( const QgsFeature &f, QgsProcessingContext &, QgsProcessingFeedback * )
138{
139 QgsFeatureList res;
140 const QString val = f.attribute( mFieldIndex ).toString();
141 const QStringList parts = mUseRegex ? val.split( mRegex ) : val.split( mChar );
142 res.reserve( parts.size() );
143 for ( const QString &p : parts )
144 {
145 QgsFeature out = f;
146 out.setAttribute( mFieldIndex, p );
147 res << out;
148 }
149 return res;
150}
151
152QgsFeatureSink::SinkFlags QgsSplitFeaturesByAttributeCharacterAlgorithm::sinkFlags() const
153{
155}
156
ProcessingSourceType
Processing data source types.
Definition qgis.h:3645
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition qgis.h:3653
@ RegeneratesPrimaryKey
Algorithm always drops any existing primary keys or FID values and regenerates them in outputs.
Definition qgis.h:3734
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
Definition qgis.h:3745
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition qgis.h:3880
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:60
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:56
QString name
Definition qgsfield.h:65
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:75
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