QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
26 
27 QString QgsSplitFeaturesByAttributeCharacterAlgorithm::name() const
28 {
29  return QStringLiteral( "splitfeaturesbycharacter" );
30 }
31 
32 QString QgsSplitFeaturesByAttributeCharacterAlgorithm::displayName() const
33 {
34  return QObject::tr( "Split features by character" );
35 }
36 
37 QStringList QgsSplitFeaturesByAttributeCharacterAlgorithm::tags() const
38 {
39  return QObject::tr( "separate,attribute,value,string" ).split( ',' );
40 }
41 
42 QString QgsSplitFeaturesByAttributeCharacterAlgorithm::group() const
43 {
44  return QObject::tr( "Vector general" );
45 }
46 
47 QString QgsSplitFeaturesByAttributeCharacterAlgorithm::groupId() const
48 {
49  return QStringLiteral( "vectorgeneral" );
50 }
51 
52 QString QgsSplitFeaturesByAttributeCharacterAlgorithm::shortHelpString() const
53 {
54  return QObject::tr( "This algorithm splits features into multiple output features by splitting a field's value with a specified character.\n\n"
55  "For instance, if a layer contains features with multiple comma separated values contained in a single field, this "
56  "algorithm can be used to split these values up across multiple output features.\n\n"
57  "Geometries and other attributes remain unchanged in the output.\n\n"
58  "Optionally, the separator string can be a regular expression for added flexibility." );
59 }
60 
61 QString QgsSplitFeaturesByAttributeCharacterAlgorithm::shortDescription() const
62 {
63  return QObject::tr( "Splits features into multiple output features by splitting a field by a character." );
64 }
65 
66 QList<int> QgsSplitFeaturesByAttributeCharacterAlgorithm::inputLayerTypes() const
67 {
68  return QList<int>() << QgsProcessing::TypeVector;
69 }
70 
71 void QgsSplitFeaturesByAttributeCharacterAlgorithm::initParameters( const QVariantMap & )
72 {
73  addParameter( new QgsProcessingParameterField( QStringLiteral( "FIELD" ), QObject::tr( "Split using values in field" ), QVariant(), QStringLiteral( "INPUT" ) ) );
74  addParameter( new QgsProcessingParameterString( QStringLiteral( "CHAR" ), QObject::tr( "Split values using character" ) ) );
75  std::unique_ptr< QgsProcessingParameterDefinition > regexParam = qgis::make_unique< QgsProcessingParameterBoolean >( QStringLiteral( "REGEX" ), QObject::tr( "Use regular expression separator" ) );
76  regexParam->setFlags( regexParam->flags() | QgsProcessingParameterDefinition::FlagAdvanced );
77  addParameter( regexParam.release() );
78 }
79 
80 QgsProcessing::SourceType QgsSplitFeaturesByAttributeCharacterAlgorithm::outputLayerType() const
81 {
83 }
84 
85 QgsSplitFeaturesByAttributeCharacterAlgorithm *QgsSplitFeaturesByAttributeCharacterAlgorithm::createInstance() const
86 {
87  return new QgsSplitFeaturesByAttributeCharacterAlgorithm();
88 }
89 
90 QgsFields QgsSplitFeaturesByAttributeCharacterAlgorithm::outputFields( const QgsFields &inputFields ) const
91 {
92  mFieldIndex = inputFields.lookupField( mFieldName );
93  QgsFields outputFields;
94  for ( int i = 0; i < inputFields.count(); ++i )
95  {
96  if ( i != mFieldIndex )
97  {
98  outputFields.append( inputFields.at( i ) );
99  }
100  else
101  {
102  // we need to convert the split field to a string field
103  outputFields.append( QgsField( inputFields.at( i ).name(), QVariant::String ) );
104  }
105  }
106  return outputFields;
107 }
108 
109 QString QgsSplitFeaturesByAttributeCharacterAlgorithm::outputName() const
110 {
111  return QObject::tr( "Split" );
112 }
113 
114 bool QgsSplitFeaturesByAttributeCharacterAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
115 {
116  mChar = parameterAsString( parameters, QStringLiteral( "CHAR" ), context );
117  mFieldName = parameterAsString( parameters, QStringLiteral( "FIELD" ), context );
118  mUseRegex = parameterAsBoolean( parameters, QStringLiteral( "REGEX" ), context );
119  if ( mUseRegex )
120  mRegex = QRegularExpression( mChar );
121  return true;
122 }
123 
124 QgsFeatureList QgsSplitFeaturesByAttributeCharacterAlgorithm::processFeature( const QgsFeature &f, QgsProcessingContext &, QgsProcessingFeedback * )
125 {
126  QgsFeatureList res;
127  const QString val = f.attribute( mFieldIndex ).toString();
128  const QStringList parts = mUseRegex ? val.split( mRegex ) : val.split( mChar );
129  res.reserve( parts.size() );
130  for ( const QString &p : parts )
131  {
132  QgsFeature out = f;
133  out.setAttribute( mFieldIndex, p );
134  res << out;
135  }
136  return res;
137 }
138 
139 QgsFeatureSink::SinkFlags QgsSplitFeaturesByAttributeCharacterAlgorithm::sinkFlags() const
140 {
142 }
143 
145 
146 
147 
qgslinestring.h
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:38
qgscompoundcurve.h
QgsFields::count
int count() const
Returns number of items.
Definition: qgsfields.cpp:133
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:45
QgsProcessingParameterDefinition::FlagAdvanced
@ FlagAdvanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition: qgsprocessingparameters.h:423
QgsFields::append
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
QgsField::name
QString name
Definition: qgsfield.h:59
QgsProcessing::TypeVector
@ TypeVector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition: qgsprocessing.h:53
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:44
QgsProcessingParameterString
A string parameter for processing algorithms.
Definition: qgsprocessingparameters.h:2324
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:583
QgsFeature::attribute
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Definition: qgsfeature.cpp:264
qgscircularstring.h
QgsFeatureSink::RegeneratePrimaryKey
@ RegeneratePrimaryKey
This flag indicates, that a primary key field cannot be guaranteed to be unique and the sink should i...
Definition: qgsfeaturesink.h:55
qgsalgorithmsplitfeaturesbyattributecharacter.h
QgsFeature::setAttribute
bool setAttribute(int field, const QVariant &attr)
Set an attribute's value by field index.
Definition: qgsfeature.cpp:213
qgscurve.h
qgsgeometrycollection.h
QgsFeature
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
QgsFields::lookupField
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
Definition: qgsfields.cpp:344
QgsFields::at
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1)
Definition: qgsfields.cpp:163
QgsProcessing::SourceType
SourceType
Data source types enum.
Definition: qgsprocessing.h:45
QgsProcessingParameterField
A vector layer or feature source field parameter for processing algorithms.
Definition: qgsprocessingparameters.h:2617
QgsField
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:50