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