22 QString QgsJoinByAttributeAlgorithm::name()
const 24 return QStringLiteral(
"joinattributestable" );
27 QString QgsJoinByAttributeAlgorithm::displayName()
const 29 return QObject::tr(
"Join attributes table" );
32 QStringList QgsJoinByAttributeAlgorithm::tags()
const 34 return QObject::tr(
"join,connect,attributes,values,fields" ).split(
',' );
37 QString QgsJoinByAttributeAlgorithm::group()
const 39 return QObject::tr(
"Vector general" );
42 QString QgsJoinByAttributeAlgorithm::groupId()
const 44 return QStringLiteral(
"vectorgeneral" );
47 void QgsJoinByAttributeAlgorithm::initAlgorithm(
const QVariantMap & )
52 QObject::tr(
"Table field" ), QVariant(), QStringLiteral(
"INPUT" ) ) );
57 QObject::tr(
"Table field 2" ), QVariant(), QStringLiteral(
"INPUT_2" ) ) );
60 QObject::tr(
"Layer 2 fields to copy (leave empty to copy all fields)" ),
67 QString QgsJoinByAttributeAlgorithm::shortHelpString()
const 69 return QObject::tr(
"This algorithm takes an input vector layer and creates a new vector layer that is an extended version of the " 70 "input one, with additional attributes in its attribute table.\n\n" 71 "The additional attributes and their values are taken from a second vector layer. An attribute is selected " 72 "in each of them to define the join criteria." );
75 QgsJoinByAttributeAlgorithm *QgsJoinByAttributeAlgorithm::createInstance()
const 77 return new QgsJoinByAttributeAlgorithm();
82 std::unique_ptr< QgsFeatureSource > input( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
83 std::unique_ptr< QgsFeatureSource > input2( parameterAsSource( parameters, QStringLiteral(
"INPUT_2" ), context ) );
84 if ( !input || !input2 )
87 QString field1Name = parameterAsString( parameters, QStringLiteral(
"FIELD" ), context );
88 QString field2Name = parameterAsString( parameters, QStringLiteral(
"FIELD_2" ), context );
89 const QStringList fieldsToCopy = parameterAsFields( parameters, QStringLiteral(
"FIELDS_TO_COPY" ), context );
91 int joinField1Index = input->fields().lookupField( field1Name );
92 int joinField2Index = input2->fields().lookupField( field2Name );
93 if ( joinField1Index < 0 || joinField2Index < 0 )
98 if ( fieldsToCopy.empty() )
100 outFields2 = input2->fields();
101 for (
int i = 0; i < outFields2.
count(); ++i )
108 for (
const QString &field : fieldsToCopy )
113 fields2Indices << index;
114 outFields2.
append( input2->fields().at( index ) );
120 fields2Fetch << joinField2Index;
125 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, outFields,
126 input->wkbType(), input->sourceCrs() ) );
132 QHash< QVariant, QgsAttributes > input2AttributeCache;
134 double step = input2->featureCount() > 0 ? 50.0 / input2->featureCount() : 1;
147 if ( input2AttributeCache.contains( feat.
attribute( joinField2Index ) ) )
152 for (
int j = 0; j < feat.
attributes().count(); ++j )
154 if ( ! fields2Indices.contains( j ) )
159 input2AttributeCache.insert( feat.
attribute( joinField2Index ), attributes );
163 step = input->featureCount() > 0 ? 50.0 / input->featureCount() : 1;
164 features = input->getFeatures();
177 attrs.append( input2AttributeCache.value( feat.
attribute( joinField1Index ) ) );
183 outputs.insert( QStringLiteral(
"OUTPUT" ), dest );
int lookupField(const QString &fieldName) const
Look up field's index from the field name.
Wrapper for iterator of features from vector data provider or vector layer.
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
Base class for providing feedback from a processing algorithm.
A vector layer or feature source field parameter for processing algorithms.
void setProgress(double progress)
Sets the current progress for the feedback object.
Container of fields for a vector layer.
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
int count() const
Return number of items.
A feature sink output for processing algorithms.
static QgsFields combineFields(const QgsFields &fieldsA, const QgsFields &fieldsB)
Combines two field lists, avoiding duplicate field names (in a case-insensitive manner).
This class wraps a request for features to a vector layer (or directly its vector data provider)...
Custom exception class for processing related exceptions.
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Append a field. The field must have unique name, otherwise it is rejected (returns false) ...
bool isCanceled() const
Tells whether the operation has been canceled already.
An input feature source (such as vector layers) parameter for processing algorithms.
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
QList< int > QgsAttributeList
bool nextFeature(QgsFeature &f)
Geometry is not required. It may still be returned if e.g. required for a filter condition.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Contains information about the context in which a processing algorithm is executed.