mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-17 00:04:02 -04:00
makeFeatureCompatible on a single input feature
This commit is contained in:
parent
7e8592bca2
commit
8d82ce86cc
@ -177,8 +177,26 @@ are padded with NULL values to match the required length).
|
||||
.. versionadded:: 3.4
|
||||
%End
|
||||
|
||||
static const QgsFeatureList makeFeatureCompatible( const QgsFeature &feature, const QgsVectorLayer &layer );
|
||||
%Docstring
|
||||
Converts input ``feature`` to be compatible with the given ``layer``.
|
||||
|
||||
static QgsFeatureList makeFeaturesCompatible( const QgsFeatureList &features, const QgsVectorLayer &layer );
|
||||
This function returns a new list of transformed features compatible with the input
|
||||
layer, note that the number of features returned might be greater than one.
|
||||
|
||||
The following operations will be performed to convert the input features:
|
||||
- convert single geometries to multi part
|
||||
- drop additional attributes
|
||||
- drop geometry if layer is geometry-less
|
||||
- add missing attribute fields
|
||||
- add back M/Z values (initialized to 0)
|
||||
- drop Z/M
|
||||
- convert multi part geometries to single part
|
||||
|
||||
.. versionadded:: 3.4
|
||||
%End
|
||||
|
||||
static const QgsFeatureList makeFeaturesCompatible( const QgsFeatureList &features, const QgsVectorLayer &layer );
|
||||
%Docstring
|
||||
Converts input ``features`` to be compatible with the given ``layer``.
|
||||
|
||||
|
@ -561,13 +561,11 @@ void QgsVectorLayerUtils::matchAttributesToFields( QgsFeature &feature, const Qg
|
||||
}
|
||||
}
|
||||
|
||||
QgsFeatureList QgsVectorLayerUtils::makeFeaturesCompatible( const QgsFeatureList &features, const QgsVectorLayer &layer )
|
||||
const QgsFeatureList QgsVectorLayerUtils::makeFeatureCompatible( const QgsFeature &feature, const QgsVectorLayer &layer )
|
||||
{
|
||||
QgsWkbTypes::Type inputWkbType( layer.wkbType( ) );
|
||||
QgsFeatureList resultFeatures;
|
||||
for ( const QgsFeature &f : features )
|
||||
{
|
||||
QgsFeature newF( f );
|
||||
QgsFeature newF( feature );
|
||||
// Fix attributes
|
||||
QgsVectorLayerUtils::matchAttributesToFields( newF, layer.fields( ) );
|
||||
// Does geometry need tranformations?
|
||||
@ -584,8 +582,9 @@ QgsFeatureList QgsVectorLayerUtils::makeFeaturesCompatible( const QgsFeatureList
|
||||
QgsFeature _f = QgsFeature( layer.fields() );
|
||||
_f.setAttributes( newF.attributes() );
|
||||
resultFeatures.append( _f );
|
||||
continue; // Skip the rest
|
||||
}
|
||||
else
|
||||
{
|
||||
// Geometry need fixing
|
||||
if ( newFHasGeom && layerHasGeom && newF.geometry().wkbType() != inputWkbType )
|
||||
{
|
||||
@ -627,15 +626,15 @@ QgsFeatureList QgsVectorLayerUtils::makeFeaturesCompatible( const QgsFeatureList
|
||||
{
|
||||
QgsGeometry newGeom( newF.geometry( ) );
|
||||
const QgsGeometryCollection *parts( static_cast< const QgsGeometryCollection * >( newGeom.constGet() ) );
|
||||
for ( int i = 0; i < parts->partCount( ); i++ )
|
||||
{
|
||||
QgsGeometry g( parts->geometryN( i )->clone() );
|
||||
QgsAttributeMap attrMap;
|
||||
for ( int j = 0; j < newF.fields().count(); j++ )
|
||||
{
|
||||
attrMap[j] = newF.attribute( j );
|
||||
}
|
||||
QgsFeature _f( QgsVectorLayerUtils::createFeature( &layer, g, attrMap ) );
|
||||
for ( int i = 0; i < parts->partCount( ); i++ )
|
||||
{
|
||||
QgsGeometry g( parts->geometryN( i )->clone() );
|
||||
QgsFeature _f( createFeature( &layer, g, attrMap ) );
|
||||
resultFeatures.append( _f );
|
||||
}
|
||||
}
|
||||
@ -652,6 +651,19 @@ QgsFeatureList QgsVectorLayerUtils::makeFeaturesCompatible( const QgsFeatureList
|
||||
return resultFeatures;
|
||||
}
|
||||
|
||||
const QgsFeatureList QgsVectorLayerUtils::makeFeaturesCompatible( const QgsFeatureList &features, const QgsVectorLayer &layer )
|
||||
{
|
||||
QgsFeatureList resultFeatures;
|
||||
for ( const QgsFeature &f : features )
|
||||
{
|
||||
for ( const auto &_f : makeFeatureCompatible( f, layer ) )
|
||||
{
|
||||
resultFeatures.append( _f );
|
||||
}
|
||||
}
|
||||
return resultFeatures;
|
||||
}
|
||||
|
||||
QList<QgsVectorLayer *> QgsVectorLayerUtils::QgsDuplicateFeatureContext::layers() const
|
||||
{
|
||||
QList<QgsVectorLayer *> layers;
|
||||
|
@ -188,6 +188,24 @@ class CORE_EXPORT QgsVectorLayerUtils
|
||||
*/
|
||||
static void matchAttributesToFields( QgsFeature &feature, const QgsFields &fields );
|
||||
|
||||
/**
|
||||
* Converts input \a feature to be compatible with the given \a layer.
|
||||
*
|
||||
* This function returns a new list of transformed features compatible with the input
|
||||
* layer, note that the number of features returned might be greater than one.
|
||||
*
|
||||
* The following operations will be performed to convert the input features:
|
||||
* - convert single geometries to multi part
|
||||
* - drop additional attributes
|
||||
* - drop geometry if layer is geometry-less
|
||||
* - add missing attribute fields
|
||||
* - add back M/Z values (initialized to 0)
|
||||
* - drop Z/M
|
||||
* - convert multi part geometries to single part
|
||||
*
|
||||
* \since QGIS 3.4
|
||||
*/
|
||||
static const QgsFeatureList makeFeatureCompatible( const QgsFeature &feature, const QgsVectorLayer &layer );
|
||||
|
||||
/**
|
||||
* Converts input \a features to be compatible with the given \a layer.
|
||||
@ -207,7 +225,7 @@ class CORE_EXPORT QgsVectorLayerUtils
|
||||
*
|
||||
* \since QGIS 3.4
|
||||
*/
|
||||
static QgsFeatureList makeFeaturesCompatible( const QgsFeatureList &features, const QgsVectorLayer &layer );
|
||||
static const QgsFeatureList makeFeaturesCompatible( const QgsFeatureList &features, const QgsVectorLayer &layer );
|
||||
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user