QGIS API Documentation 3.99.0-Master (09f76ad7019)
Loading...
Searching...
No Matches
qgsalgorithmjoinbyattribute.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmjoinbyattribute.cpp
3 ---------------------
4 begin : April 2017
5 copyright : (C) 2017 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
21
22#include <QString>
23
24using namespace Qt::StringLiterals;
25
27
28QString QgsJoinByAttributeAlgorithm::name() const
29{
30 return u"joinattributestable"_s;
31}
32
33QString QgsJoinByAttributeAlgorithm::displayName() const
34{
35 return QObject::tr( "Join attributes by field value" );
36}
37
38QStringList QgsJoinByAttributeAlgorithm::tags() const
39{
40 return QObject::tr( "join,connect,attributes,values,fields,tables" ).split( ',' );
41}
42
43QString QgsJoinByAttributeAlgorithm::group() const
44{
45 return QObject::tr( "Vector general" );
46}
47
48QString QgsJoinByAttributeAlgorithm::groupId() const
49{
50 return u"vectorgeneral"_s;
51}
52
53void QgsJoinByAttributeAlgorithm::initAlgorithm( const QVariantMap & )
54{
55 QStringList methods;
56 methods << QObject::tr( "Create separate feature for each matching feature (one-to-many)" )
57 << QObject::tr( "Take attributes of the first matching feature only (one-to-one)" );
58
59 addParameter( new QgsProcessingParameterFeatureSource( u"INPUT"_s, QObject::tr( "Input layer" ), QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::Vector ) ) );
60 addParameter( new QgsProcessingParameterField( u"FIELD"_s, QObject::tr( "Table field" ), QVariant(), u"INPUT"_s ) );
61
62 addParameter( new QgsProcessingParameterFeatureSource( u"INPUT_2"_s, QObject::tr( "Input layer 2" ), QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::Vector ) ) );
63 addParameter( new QgsProcessingParameterField( u"FIELD_2"_s, QObject::tr( "Table field 2" ), QVariant(), u"INPUT_2"_s ) );
64
65 addParameter( new QgsProcessingParameterField( u"FIELDS_TO_COPY"_s, QObject::tr( "Layer 2 fields to copy (leave empty to copy all fields)" ), QVariant(), u"INPUT_2"_s, Qgis::ProcessingFieldParameterDataType::Any, true, true ) );
66
67 addParameter( new QgsProcessingParameterEnum( u"METHOD"_s, QObject::tr( "Join type" ), methods, false, 1 ) );
68 addParameter( new QgsProcessingParameterBoolean( u"DISCARD_NONMATCHING"_s, QObject::tr( "Discard records which could not be joined" ), false ) );
69
70 addParameter( new QgsProcessingParameterString( u"PREFIX"_s, QObject::tr( "Joined field prefix" ), QVariant(), false, true ) );
71
72 addParameter( new QgsProcessingParameterFeatureSink( u"OUTPUT"_s, QObject::tr( "Joined layer" ), Qgis::ProcessingSourceType::VectorAnyGeometry, QVariant(), true, true ) );
73
74 auto nonMatchingSink = std::make_unique<QgsProcessingParameterFeatureSink>(
75 u"NON_MATCHING"_s, QObject::tr( "Unjoinable features from first layer" ), Qgis::ProcessingSourceType::VectorAnyGeometry, QVariant(), true, false
76 );
77 // TODO GUI doesn't support advanced outputs yet
78 //nonMatchingSink->setFlags(nonMatchingSink->flags() | Qgis::ProcessingParameterFlag::Advanced );
79 addParameter( nonMatchingSink.release() );
80
81 addOutput( new QgsProcessingOutputNumber( u"JOINED_COUNT"_s, QObject::tr( "Number of joined features from input table" ) ) );
82 addOutput( new QgsProcessingOutputNumber( u"UNJOINABLE_COUNT"_s, QObject::tr( "Number of unjoinable features from input table" ) ) );
83}
84
85QString QgsJoinByAttributeAlgorithm::shortHelpString() const
86{
87 return QObject::tr( "This algorithm takes an input vector layer and creates a new vector layer that is an extended version of the "
88 "input one, with additional attributes in its attribute table.\n\n"
89 "The additional attributes and their values are taken from a second vector layer. An attribute is selected "
90 "in each of them to define the join criteria." );
91}
92
93QString QgsJoinByAttributeAlgorithm::shortDescription() const
94{
95 return QObject::tr( "Creates a vector layer that is an extended version of the input one, "
96 "with additional attributes taken from a second vector layer." );
97}
98
99Qgis::ProcessingAlgorithmDocumentationFlags QgsJoinByAttributeAlgorithm::documentationFlags() const
100{
102}
103
104QgsJoinByAttributeAlgorithm *QgsJoinByAttributeAlgorithm::createInstance() const
105{
106 return new QgsJoinByAttributeAlgorithm();
107}
108
109QVariantMap QgsJoinByAttributeAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
110{
111 const int joinMethod = parameterAsEnum( parameters, u"METHOD"_s, context );
112 const bool discardNonMatching = parameterAsBoolean( parameters, u"DISCARD_NONMATCHING"_s, context );
113
114 std::unique_ptr<QgsProcessingFeatureSource> input( parameterAsSource( parameters, u"INPUT"_s, context ) );
115 if ( !input )
116 throw QgsProcessingException( invalidSourceError( parameters, u"INPUT"_s ) );
117
118 std::unique_ptr<QgsProcessingFeatureSource> input2( parameterAsSource( parameters, u"INPUT_2"_s, context ) );
119 if ( !input2 )
120 throw QgsProcessingException( invalidSourceError( parameters, u"INPUT_2"_s ) );
121
122 const QString prefix = parameterAsString( parameters, u"PREFIX"_s, context );
123
124 const QString field1Name = parameterAsString( parameters, u"FIELD"_s, context );
125 const QString field2Name = parameterAsString( parameters, u"FIELD_2"_s, context );
126 const QStringList fieldsToCopy = parameterAsStrings( parameters, u"FIELDS_TO_COPY"_s, context );
127
128 const int joinField1Index = input->fields().lookupField( field1Name );
129 if ( joinField1Index < 0 )
130 throw QgsProcessingException( QObject::tr( "Invalid join field from layer 1: “%1” does not exist" ).arg( field1Name ) );
131
132 const int joinField2Index = input2->fields().lookupField( field2Name );
133 if ( joinField2Index < 0 )
134 throw QgsProcessingException( QObject::tr( "Invalid join field from layer 2: “%1” does not exist" ).arg( field2Name ) );
135
136 QgsFields outFields2;
137 QgsAttributeList fields2Indices;
138 if ( fieldsToCopy.empty() )
139 {
140 outFields2 = input2->fields();
141 fields2Indices.reserve( outFields2.count() );
142 for ( int i = 0; i < outFields2.count(); ++i )
143 {
144 fields2Indices << i;
145 }
146 }
147 else
148 {
149 fields2Indices.reserve( fieldsToCopy.count() );
150 for ( const QString &field : fieldsToCopy )
151 {
152 const int index = input2->fields().lookupField( field );
153 if ( index >= 0 )
154 {
155 fields2Indices << index;
156 outFields2.append( input2->fields().at( index ) );
157 }
158 }
159 }
160
161 if ( !prefix.isEmpty() )
162 {
163 for ( int i = 0; i < outFields2.count(); ++i )
164 {
165 outFields2.rename( i, prefix + outFields2[i].name() );
166 }
167 }
168
169 QgsAttributeList fields2Fetch = fields2Indices;
170 fields2Fetch << joinField2Index;
171
172 const QgsFields outFields = QgsProcessingUtils::combineFields( input->fields(), outFields2 );
173
174 QString dest;
175 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u"OUTPUT"_s, context, dest, outFields, input->wkbType(), input->sourceCrs(), QgsFeatureSink::RegeneratePrimaryKey ) );
176 if ( parameters.value( u"OUTPUT"_s ).isValid() && !sink )
177 throw QgsProcessingException( invalidSinkError( parameters, u"OUTPUT"_s ) );
178
179 QString destNonMatching1;
180 std::unique_ptr<QgsFeatureSink> sinkNonMatching1( parameterAsSink( parameters, u"NON_MATCHING"_s, context, destNonMatching1, input->fields(), input->wkbType(), input->sourceCrs(), QgsFeatureSink::RegeneratePrimaryKey ) );
181 if ( parameters.value( u"NON_MATCHING"_s ).isValid() && !sinkNonMatching1 )
182 throw QgsProcessingException( invalidSinkError( parameters, u"NON_MATCHING"_s ) );
183
184 // cache attributes of input2
185 QMultiHash<QVariant, QgsAttributes> input2AttributeCache;
186 QgsFeatureIterator features = input2->getFeatures( QgsFeatureRequest().setFlags( Qgis::FeatureRequestFlag::NoGeometry ).setSubsetOfAttributes( fields2Fetch ), Qgis::ProcessingFeatureSourceFlag::SkipGeometryValidityChecks );
187 double step = input2->featureCount() > 0 ? 50.0 / input2->featureCount() : 1;
188 int i = 0;
189 QgsFeature feat;
190 while ( features.nextFeature( feat ) )
191 {
192 i++;
193 if ( feedback->isCanceled() )
194 {
195 break;
196 }
197
198 feedback->setProgress( i * step );
199
200 if ( joinMethod == 1 && input2AttributeCache.contains( feat.attribute( joinField2Index ) ) )
201 continue;
202
203 // only keep selected attributes
204 QgsAttributes attributes;
205 for ( int field2Index : fields2Indices )
206 {
207 attributes << feat.attribute( field2Index );
208 }
209
210 input2AttributeCache.insert( feat.attribute( joinField2Index ), attributes );
211 }
212
213 // Create output vector layer with additional attribute
214 step = input->featureCount() > 0 ? 50.0 / input->featureCount() : 1;
216 i = 0;
217 long long joinedCount = 0;
218 long long unjoinedCount = 0;
219 while ( features.nextFeature( feat ) )
220 {
221 i++;
222 if ( feedback->isCanceled() )
223 {
224 break;
225 }
226
227 feedback->setProgress( 50 + i * step );
228
229 if ( input2AttributeCache.count( feat.attribute( joinField1Index ) ) > 0 )
230 {
231 joinedCount++;
232 if ( sink )
233 {
234 const QgsAttributes attrs = feat.attributes();
235
236 QList<QgsAttributes> attributes = input2AttributeCache.values( feat.attribute( joinField1Index ) );
237 QList<QgsAttributes>::iterator attrsIt = attributes.begin();
238 for ( ; attrsIt != attributes.end(); ++attrsIt )
239 {
240 QgsAttributes newAttrs = attrs;
241 newAttrs.append( *attrsIt );
242 feat.setAttributes( newAttrs );
243 if ( !sink->addFeature( feat, QgsFeatureSink::FastInsert ) )
244 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, u"OUTPUT"_s ) );
245 }
246 }
247 }
248 else
249 {
250 // no matching for input feature
251 if ( sink && !discardNonMatching )
252 {
253 if ( !sink->addFeature( feat, QgsFeatureSink::FastInsert ) )
254 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, u"OUTPUT"_s ) );
255 }
256 if ( sinkNonMatching1 )
257 {
258 if ( !sinkNonMatching1->addFeature( feat, QgsFeatureSink::FastInsert ) )
259 throw QgsProcessingException( writeFeatureError( sinkNonMatching1.get(), parameters, u"NON_MATCHING"_s ) );
260 }
261 unjoinedCount++;
262 }
263 }
264
265 feedback->pushInfo( QObject::tr( "%n feature(s) from input layer were successfully matched", nullptr, joinedCount ) );
266 if ( unjoinedCount > 0 )
267 feedback->reportError( QObject::tr( "%n feature(s) from input layer could not be matched", nullptr, unjoinedCount ) );
268
269 QVariantMap outputs;
270 if ( sink )
271 {
272 sink->finalize();
273 outputs.insert( u"OUTPUT"_s, dest );
274 }
275 outputs.insert( u"JOINED_COUNT"_s, joinedCount );
276 outputs.insert( u"UNJOINABLE_COUNT"_s, unjoinedCount );
277 if ( sinkNonMatching1 )
278 {
279 sinkNonMatching1->finalize();
280 outputs.insert( u"NON_MATCHING"_s, destNonMatching1 );
281 }
282 return outputs;
283}
284
285
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition qgis.h:3610
@ VectorAnyGeometry
Any vector layer with geometry.
Definition qgis.h:3604
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
Definition qgis.h:2254
@ RegeneratesPrimaryKey
Algorithm always drops any existing primary keys or FID values and regenerates them in outputs.
Definition qgis.h:3690
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
Definition qgis.h:3701
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3782
A vector of attributes.
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
Wraps a request for features to a vector layer (or directly its vector data provider).
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
@ 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
QgsAttributes attributes
Definition qgsfeature.h:69
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
Q_INVOKABLE QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:55
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.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:76
int count
Definition qgsfields.h:50
bool rename(int fieldIdx, const QString &name)
Renames a name of field.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
A numeric output for processing algorithms.
A boolean parameter for processing algorithms.
An enum based parameter for processing algorithms, allowing for selection from predefined values.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
A vector layer or feature source field parameter for processing algorithms.
A string parameter for processing algorithms.
static QgsFields combineFields(const QgsFields &fieldsA, const QgsFields &fieldsB, const QString &fieldsBPrefix=QString())
Combines two field lists, avoiding duplicate field names (in a case-insensitive manner).
QList< int > QgsAttributeList
Definition qgsfield.h:30