QGIS API Documentation  3.20.0-Odense (decaadbb31)
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"
23 #include "qgsgeometrycollection.h"
24 #include <QRegularExpression>
25 
27 
28 QString QgsSplitFeaturesByAttributeCharacterAlgorithm::name() const
29 {
30  return QStringLiteral( "splitfeaturesbycharacter" );
31 }
32 
33 QString QgsSplitFeaturesByAttributeCharacterAlgorithm::displayName() const
34 {
35  return QObject::tr( "Split features by character" );
36 }
37 
38 QStringList QgsSplitFeaturesByAttributeCharacterAlgorithm::tags() const
39 {
40  return QObject::tr( "separate,attribute,value,string" ).split( ',' );
41 }
42 
43 QString QgsSplitFeaturesByAttributeCharacterAlgorithm::group() const
44 {
45  return QObject::tr( "Vector general" );
46 }
47 
48 QString QgsSplitFeaturesByAttributeCharacterAlgorithm::groupId() const
49 {
50  return QStringLiteral( "vectorgeneral" );
51 }
52 
53 QString 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 
62 QString QgsSplitFeaturesByAttributeCharacterAlgorithm::shortDescription() const
63 {
64  return QObject::tr( "Splits features into multiple output features by splitting a field by a character." );
65 }
66 
67 QList<int> QgsSplitFeaturesByAttributeCharacterAlgorithm::inputLayerTypes() const
68 {
69  return QList<int>() << QgsProcessing::TypeVector;
70 }
71 
72 void QgsSplitFeaturesByAttributeCharacterAlgorithm::initParameters( const QVariantMap & )
73 {
74  addParameter( new QgsProcessingParameterField( QStringLiteral( "FIELD" ), QObject::tr( "Split using values in field" ), QVariant(), QStringLiteral( "INPUT" ) ) );
75  addParameter( new QgsProcessingParameterString( QStringLiteral( "CHAR" ), QObject::tr( "Split values using character" ) ) );
76  std::unique_ptr< QgsProcessingParameterDefinition > regexParam = std::make_unique< QgsProcessingParameterBoolean >( QStringLiteral( "REGEX" ), QObject::tr( "Use regular expression separator" ) );
77  regexParam->setFlags( regexParam->flags() | QgsProcessingParameterDefinition::FlagAdvanced );
78  addParameter( regexParam.release() );
79 }
80 
81 QgsProcessing::SourceType QgsSplitFeaturesByAttributeCharacterAlgorithm::outputLayerType() const
82 {
84 }
85 
86 QgsSplitFeaturesByAttributeCharacterAlgorithm *QgsSplitFeaturesByAttributeCharacterAlgorithm::createInstance() const
87 {
88  return new QgsSplitFeaturesByAttributeCharacterAlgorithm();
89 }
90 
91 QgsFields QgsSplitFeaturesByAttributeCharacterAlgorithm::outputFields( const QgsFields &inputFields ) const
92 {
93  mFieldIndex = inputFields.lookupField( mFieldName );
94  QgsFields outputFields;
95  for ( int i = 0; i < inputFields.count(); ++i )
96  {
97  if ( i != mFieldIndex )
98  {
99  outputFields.append( inputFields.at( i ) );
100  }
101  else
102  {
103  // we need to convert the split field to a string field
104  outputFields.append( QgsField( inputFields.at( i ).name(), QVariant::String ) );
105  }
106  }
107  return outputFields;
108 }
109 
110 QString QgsSplitFeaturesByAttributeCharacterAlgorithm::outputName() const
111 {
112  return QObject::tr( "Split" );
113 }
114 
115 bool QgsSplitFeaturesByAttributeCharacterAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
116 {
117  mChar = parameterAsString( parameters, QStringLiteral( "CHAR" ), context );
118  mFieldName = parameterAsString( parameters, QStringLiteral( "FIELD" ), context );
119  mUseRegex = parameterAsBoolean( parameters, QStringLiteral( "REGEX" ), context );
120  if ( mUseRegex )
121  mRegex = QRegularExpression( mChar );
122  return true;
123 }
124 
125 QgsFeatureList QgsSplitFeaturesByAttributeCharacterAlgorithm::processFeature( const QgsFeature &f, QgsProcessingContext &, QgsProcessingFeedback * )
126 {
127  QgsFeatureList res;
128  const QString val = f.attribute( mFieldIndex ).toString();
129  const QStringList parts = mUseRegex ? val.split( mRegex ) : val.split( mChar );
130  res.reserve( parts.size() );
131  for ( const QString &p : parts )
132  {
133  QgsFeature out = f;
134  out.setAttribute( mFieldIndex, p );
135  res << out;
136  }
137  return res;
138 }
139 
140 QgsFeatureSink::SinkFlags QgsSplitFeaturesByAttributeCharacterAlgorithm::sinkFlags() const
141 {
143 }
144 
146 
147 
148 
@ 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:56
bool setAttribute(int field, const QVariant &attr)
Sets an attribute's value by field index.
Definition: qgsfeature.cpp:237
QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
Definition: qgsfeature.cpp:302
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:51
QString name
Definition: qgsfield.h:60
Container of fields for a vector layer.
Definition: qgsfields.h:45
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Appends a field. The field must have unique name, otherwise it is rejected (returns false)
Definition: qgsfields.cpp:59
int count() const
Returns number of items.
Definition: qgsfields.cpp:133
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
Definition: qgsfields.cpp:163
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
Definition: qgsfields.cpp:344
Contains information about the context in which a processing algorithm is executed.
Base class for providing feedback from a processing algorithm.
@ FlagAdvanced
Parameter is an advanced parameter which should be hidden from users by default.
A vector layer or feature source field parameter for processing algorithms.
A string parameter for processing algorithms.
SourceType
Data source types enum.
Definition: qgsprocessing.h:46
@ TypeVector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition: qgsprocessing.h:54
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:736