mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-14 00:07:35 -04:00
Remove QgsProcessingUtils::uniqueValues
Use QgsFeatureSource method instead
This commit is contained in:
parent
b6fb41d4ee
commit
0e991bf62c
@ -86,15 +86,6 @@ class QgsProcessingUtils
|
||||
:rtype: str
|
||||
%End
|
||||
|
||||
static QList< QVariant > uniqueValues( QgsVectorLayer *layer, int fieldIndex, const QgsProcessingContext &context );
|
||||
%Docstring
|
||||
Returns a list of unique values contained in a single field in a ``layer``, when
|
||||
the settings from the supplied ``context`` are respected. E.g. if the
|
||||
context is set to only use selected features, then calling this will
|
||||
return unique values from selected features in the layer.
|
||||
:rtype: list of QVariant
|
||||
%End
|
||||
|
||||
|
||||
static void createFeatureSinkPython(
|
||||
QgsFeatureSink **sink /Out,TransferBack/,
|
||||
|
@ -226,36 +226,6 @@ QString QgsProcessingUtils::normalizeLayerSource( const QString &source )
|
||||
return normalized.trimmed();
|
||||
}
|
||||
|
||||
|
||||
QList<QVariant> QgsProcessingUtils::uniqueValues( QgsVectorLayer *layer, int fieldIndex, const QgsProcessingContext &context )
|
||||
{
|
||||
if ( !layer )
|
||||
return QList<QVariant>();
|
||||
|
||||
if ( fieldIndex < 0 || fieldIndex >= layer->fields().count() )
|
||||
return QList<QVariant>();
|
||||
|
||||
bool useSelection = context.flags() & QgsProcessingContext::UseSelectionIfPresent && layer->selectedFeatureCount() > 0;
|
||||
if ( !useSelection )
|
||||
{
|
||||
// not using selection, so use provider optimised version
|
||||
QSet<QVariant> values = layer->uniqueValues( fieldIndex );
|
||||
return values.toList();
|
||||
}
|
||||
else
|
||||
{
|
||||
// using selection, so we have to iterate through selected features
|
||||
QSet<QVariant> values;
|
||||
QgsFeature f;
|
||||
QgsFeatureIterator it = layer->getSelectedFeatures( QgsFeatureRequest().setSubsetOfAttributes( QgsAttributeList() << fieldIndex ).setFlags( QgsFeatureRequest::NoGeometry ) );
|
||||
while ( it.nextFeature( f ) )
|
||||
{
|
||||
values.insert( f.attribute( fieldIndex ) );
|
||||
}
|
||||
return values.toList();
|
||||
}
|
||||
}
|
||||
|
||||
void parseDestinationString( QString &destination, QString &providerKey, QString &uri, QString &format, QMap<QString, QVariant> &options )
|
||||
{
|
||||
QRegularExpression splitRx( "^(.*?):(.*)$" );
|
||||
|
@ -99,14 +99,6 @@ class CORE_EXPORT QgsProcessingUtils
|
||||
*/
|
||||
static QString normalizeLayerSource( const QString &source );
|
||||
|
||||
/**
|
||||
* Returns a list of unique values contained in a single field in a \a layer, when
|
||||
* the settings from the supplied \a context are respected. E.g. if the
|
||||
* context is set to only use selected features, then calling this will
|
||||
* return unique values from selected features in the layer.
|
||||
*/
|
||||
static QList< QVariant > uniqueValues( QgsVectorLayer *layer, int fieldIndex, const QgsProcessingContext &context );
|
||||
|
||||
/**
|
||||
* Creates a feature sink ready for adding features. The \a destination specifies a destination
|
||||
* URI for the resultant layer. It may be updated in place to reflect the actual destination
|
||||
|
@ -789,20 +789,27 @@ void TestQgsProcessing::uniqueValues()
|
||||
QgsProcessingContext context;
|
||||
context.setFlags( QgsProcessingContext::Flags( 0 ) );
|
||||
|
||||
QgsProject p;
|
||||
p.addMapLayer( layer );
|
||||
context.setProject( &p );
|
||||
|
||||
QgsProcessingParameterDefinition *def = new QgsProcessingParameterString( QStringLiteral( "string" ) );
|
||||
QVariantMap params;
|
||||
params.insert( QStringLiteral( "string" ), layer->id() );
|
||||
|
||||
std::unique_ptr< QgsFeatureSource > source( QgsProcessingParameters::parameterAsSource( def, params, context ) );
|
||||
|
||||
// some bad checks
|
||||
QVERIFY( QgsProcessingUtils::uniqueValues( nullptr, 0, context ).isEmpty() );
|
||||
QVERIFY( QgsProcessingUtils::uniqueValues( nullptr, -1, context ).isEmpty() );
|
||||
QVERIFY( QgsProcessingUtils::uniqueValues( nullptr, 10001, context ).isEmpty() );
|
||||
QVERIFY( QgsProcessingUtils::uniqueValues( layer, -1, context ).isEmpty() );
|
||||
QVERIFY( QgsProcessingUtils::uniqueValues( layer, 10001, context ).isEmpty() );
|
||||
QVERIFY( source->uniqueValues( -1 ).isEmpty() );
|
||||
QVERIFY( source->uniqueValues( 10001 ).isEmpty() );
|
||||
|
||||
// good checks
|
||||
QList< QVariant > vals = QgsProcessingUtils::uniqueValues( layer, 0, context );
|
||||
QSet< QVariant > vals = source->uniqueValues( 0 );
|
||||
QCOMPARE( vals.count(), 3 );
|
||||
QVERIFY( vals.contains( 1 ) );
|
||||
QVERIFY( vals.contains( 2 ) );
|
||||
QVERIFY( vals.contains( 3 ) );
|
||||
vals = QgsProcessingUtils::uniqueValues( layer, 1, context );
|
||||
vals = source->uniqueValues( 1 );
|
||||
QCOMPARE( vals.count(), 3 );
|
||||
QVERIFY( vals.contains( QString( "A" ) ) );
|
||||
QVERIFY( vals.contains( QString( "B" ) ) );
|
||||
@ -811,12 +818,13 @@ void TestQgsProcessing::uniqueValues()
|
||||
//using only selected features
|
||||
layer->selectByIds( QgsFeatureIds() << 1 << 2 << 4 );
|
||||
// but not using selection yet...
|
||||
vals = QgsProcessingUtils::uniqueValues( layer, 0, context );
|
||||
source.reset( QgsProcessingParameters::parameterAsSource( def, params, context ) );
|
||||
vals = source->uniqueValues( 0 );
|
||||
QCOMPARE( vals.count(), 3 );
|
||||
QVERIFY( vals.contains( 1 ) );
|
||||
QVERIFY( vals.contains( 2 ) );
|
||||
QVERIFY( vals.contains( 3 ) );
|
||||
vals = QgsProcessingUtils::uniqueValues( layer, 1, context );
|
||||
vals = source->uniqueValues( 1 );
|
||||
QCOMPARE( vals.count(), 3 );
|
||||
QVERIFY( vals.contains( QString( "A" ) ) );
|
||||
QVERIFY( vals.contains( QString( "B" ) ) );
|
||||
@ -824,18 +832,17 @@ void TestQgsProcessing::uniqueValues()
|
||||
|
||||
// selection and using selection
|
||||
context.setFlags( QgsProcessingContext::UseSelectionIfPresent );
|
||||
QVERIFY( QgsProcessingUtils::uniqueValues( layer, -1, context ).isEmpty() );
|
||||
QVERIFY( QgsProcessingUtils::uniqueValues( layer, 10001, context ).isEmpty() );
|
||||
vals = QgsProcessingUtils::uniqueValues( layer, 0, context );
|
||||
source.reset( QgsProcessingParameters::parameterAsSource( def, params, context ) );
|
||||
QVERIFY( source->uniqueValues( -1 ).isEmpty() );
|
||||
QVERIFY( source->uniqueValues( 10001 ).isEmpty() );
|
||||
vals = source->uniqueValues( 0 );
|
||||
QCOMPARE( vals.count(), 2 );
|
||||
QVERIFY( vals.contains( 1 ) );
|
||||
QVERIFY( vals.contains( 2 ) );
|
||||
vals = QgsProcessingUtils::uniqueValues( layer, 1, context );
|
||||
vals = source->uniqueValues( 1 );
|
||||
QCOMPARE( vals.count(), 2 );
|
||||
QVERIFY( vals.contains( QString( "A" ) ) );
|
||||
QVERIFY( vals.contains( QString( "B" ) ) );
|
||||
|
||||
delete layer;
|
||||
}
|
||||
|
||||
void TestQgsProcessing::createIndex()
|
||||
|
Loading…
x
Reference in New Issue
Block a user