mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-13 00:03:09 -04:00
parent
81487f9f18
commit
a8794001e0
@ -156,6 +156,16 @@ The duplicated feature will be automatically inserted into the layer.
|
||||
.. versionadded:: 3.0
|
||||
%End
|
||||
|
||||
|
||||
static void matchAttributesToFields( QgsFeature &feature, const QgsFields &fields );
|
||||
%Docstring
|
||||
Matches the attributes in ``feature`` to the specified ``fields``.
|
||||
|
||||
This causes the attributes contained within the given ``feature`` to be rearranged (or in
|
||||
some cases dropped) in order to match the fields and order indicated by ``fields``.
|
||||
|
||||
.. versionadded:: 3.4
|
||||
%End
|
||||
};
|
||||
|
||||
|
||||
|
@ -522,6 +522,44 @@ std::unique_ptr<QgsVectorLayerFeatureSource> QgsVectorLayerUtils::getFeatureSour
|
||||
return featureSource;
|
||||
}
|
||||
|
||||
void QgsVectorLayerUtils::matchAttributesToFields( QgsFeature &feature, const QgsFields &fields )
|
||||
{
|
||||
if ( !feature.fields().isEmpty() )
|
||||
{
|
||||
QgsAttributes attributes;
|
||||
attributes.reserve( fields.size() );
|
||||
// feature has a field mapping, so we can match attributes to field names
|
||||
for ( const QgsField &field : fields )
|
||||
{
|
||||
int index = feature.fields().lookupField( field.name() );
|
||||
attributes.append( index >= 0 ? feature.attribute( index ) : QVariant( field.type() ) );
|
||||
}
|
||||
feature.setAttributes( attributes );
|
||||
}
|
||||
else
|
||||
{
|
||||
// no field name mapping in feature, just use order
|
||||
const int lengthDiff = feature.attributes().count() - fields.count();
|
||||
if ( lengthDiff > 0 )
|
||||
{
|
||||
// truncate extra attributes
|
||||
QgsAttributes attributes = feature.attributes().mid( 0, fields.count() );
|
||||
feature.setAttributes( attributes );
|
||||
}
|
||||
else if ( lengthDiff < 0 )
|
||||
{
|
||||
// add missing null attributes
|
||||
QgsAttributes attributes = feature.attributes();
|
||||
attributes.reserve( fields.count() );
|
||||
for ( int i = feature.attributes().count(); i < fields.count(); ++i )
|
||||
{
|
||||
attributes.append( QVariant( fields.at( i ).type() ) );
|
||||
}
|
||||
feature.setAttributes( attributes );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QList<QgsVectorLayer *> QgsVectorLayerUtils::QgsDuplicateFeatureContext::layers() const
|
||||
{
|
||||
QList<QgsVectorLayer *> layers;
|
||||
|
@ -167,6 +167,16 @@ class CORE_EXPORT QgsVectorLayerUtils
|
||||
* \since QGIS 3.4
|
||||
*/
|
||||
static std::unique_ptr<QgsVectorLayerFeatureSource> getFeatureSource( QPointer<QgsVectorLayer> layer ) SIP_SKIP;
|
||||
|
||||
/**
|
||||
* Matches the attributes in \a feature to the specified \a fields.
|
||||
*
|
||||
* This causes the attributes contained within the given \a feature to be rearranged (or in
|
||||
* some cases dropped) in order to match the fields and order indicated by \a fields.
|
||||
*
|
||||
* \since QGIS 3.4
|
||||
*/
|
||||
static void matchAttributesToFields( QgsFeature &feature, const QgsFields &fields );
|
||||
};
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user