QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 
int lookupField(const QString &fieldName) const
Looks up field&#39;s index from the field name.
Definition: qgsfields.cpp:324
Base class for providing feedback from a processing algorithm.
Parameter is an advanced parameter which should be hidden from users by default.
QString name
Definition: qgsfield.h:58
A vector layer or feature source field parameter for processing algorithms.
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:571
Container of fields for a vector layer.
Definition: qgsfields.h:42
bool setAttribute(int field, const QVariant &attr)
Set an attribute&#39;s value by field index.
Definition: qgsfeature.cpp:211
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
int count() const
Returns number of items.
Definition: qgsfields.cpp:133
This flag indicates, that a primary key field cannot be guaranteed to be unique and the sink should i...
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1)
Definition: qgsfields.cpp:163
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
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:48
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition: qgsprocessing.h:53
SourceType
Data source types enum.
Definition: qgsprocessing.h:44
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Definition: qgsfeature.cpp:262
Contains information about the context in which a processing algorithm is executed.
A string parameter for processing algorithms.