Skip to content

Commit

Permalink
move getValues, getDoubleValues to vector layer utils source file
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Feb 7, 2018
1 parent 8495e8b commit 715677e
Show file tree
Hide file tree
Showing 11 changed files with 227 additions and 172 deletions.
34 changes: 0 additions & 34 deletions python/core/qgsvectorlayer.sip.in
Expand Up @@ -2013,40 +2013,6 @@ Calculates an aggregated value from the layer's features.
:return: calculated aggregate value

.. versionadded:: 2.16
%End

QList< QVariant > getValues( const QString &fieldOrExpression, bool &ok, bool selectedOnly = false, QgsFeedback *feedback = 0 ) const;
%Docstring
Fetches all values from a specified field name or expression.

:param fieldOrExpression: field name or an expression string
:param ok: will be set to false if field or expression is invalid, otherwise true
:param selectedOnly: set to true to get values from selected features only
:param feedback: optional feedback object to allow cancelation

:return: list of fetched values

.. versionadded:: 2.9

.. seealso:: :py:func:`getDoubleValues`
%End

QList< double > getDoubleValues( const QString &fieldOrExpression, bool &ok, bool selectedOnly = false, int *nullCount = 0, QgsFeedback *feedback = 0 ) const;
%Docstring
Fetches all double values from a specified field name or expression. Null values or
invalid expression results are skipped.

:param fieldOrExpression: field name or an expression string evaluating to a double value
:param ok: will be set to false if field or expression is invalid, otherwise true
:param selectedOnly: set to true to get values from selected features only
:param nullCount: optional pointer to integer to store number of null values encountered in
:param feedback: optional feedback object to allow cancelation

:return: list of fetched values

.. versionadded:: 2.9

.. seealso:: :py:func:`getValues`
%End

void setFeatureBlendMode( QPainter::CompositionMode blendMode );
Expand Down
50 changes: 50 additions & 0 deletions python/core/qgsvectorlayerutils.sip.in
Expand Up @@ -57,6 +57,56 @@ Returns the duplicated features in the given layer

};

static QgsFeatureIterator getValuesIterator( const QgsVectorLayer *layer, const QString &fieldOrExpression, bool &ok, bool selectedOnly );
%Docstring
Create a feature iterator for a specified field name or expression.

:param layer: vector layer to retrieve values from
:param fieldOrExpression: field name or an expression string
:param ok: will be set to false if field or expression is invalid, otherwise true
:param selectedOnly: set to true to get values from selected features only

:return: feature iterator

.. versionadded:: 3.0
%End

static QList< QVariant > getValues( const QgsVectorLayer *layer, const QString &fieldOrExpression, bool &ok, bool selectedOnly = false, QgsFeedback *feedback = 0 );
%Docstring
Fetches all values from a specified field name or expression.

:param layer: vector layer to retrieve values from
:param fieldOrExpression: field name or an expression string
:param ok: will be set to false if field or expression is invalid, otherwise true
:param selectedOnly: set to true to get values from selected features only
:param feedback: optional feedback object to allow cancelation

:return: list of fetched values

.. versionadded:: 3.0

.. seealso:: :py:func:`getDoubleValues`
%End

static QList< double > getDoubleValues( const QgsVectorLayer *layer, const QString &fieldOrExpression, bool &ok, bool selectedOnly = false, int *nullCount = 0, QgsFeedback *feedback = 0 );
%Docstring
Fetches all double values from a specified field name or expression. Null values or
invalid expression results are skipped.

:param layer: vector layer to retrieve values from
:param fieldOrExpression: field name or an expression string evaluating to a double value
:param ok: will be set to false if field or expression is invalid, otherwise true
:param selectedOnly: set to true to get values from selected features only
:param nullCount: optional pointer to integer to store number of null values encountered in
:param feedback: optional feedback object to allow cancelation

:return: list of fetched values

.. versionadded:: 3.0

.. seealso:: :py:func:`getValues`
%End

static bool valueExists( const QgsVectorLayer *layer, int fieldIndex, const QVariant &value, const QgsFeatureIds &ignoreIds = QgsFeatureIds() );
%Docstring
Returns true if the specified value already exists within a field. This method can be used to test for uniqueness
Expand Down
3 changes: 2 additions & 1 deletion src/app/qgsstatisticalsummarydockwidget.h
Expand Up @@ -24,6 +24,7 @@
#include "qgsdockwidget.h"
#include "qgsfeedback.h"
#include "qgsvectorlayer.h"
#include "qgsvectorlayerutils.h"
#include "qgis_app.h"

class QMenu;
Expand Down Expand Up @@ -60,7 +61,7 @@ class QgsStatisticsValueGatherer: public QThread
connect( mFeedback, &QgsFeedback::progressChanged, this, &QgsStatisticsValueGatherer::progressChanged );

bool ok;
mValues = mLayer->getValues( mExpression, ok, mSelectedOnly, mFeedback );
mValues = QgsVectorLayerUtils::getValues( mLayer, mExpression, ok, mSelectedOnly, mFeedback );
if ( !ok )
{
mWasCanceled = true;
Expand Down
3 changes: 2 additions & 1 deletion src/core/qgshistogram.cpp
Expand Up @@ -19,6 +19,7 @@

#include "qgsstatisticalsummary.h"
#include "qgsvectorlayer.h"
#include "qgsvectorlayerutils.h"

void QgsHistogram::prepareValues()
{
Expand All @@ -45,7 +46,7 @@ bool QgsHistogram::setValues( const QgsVectorLayer *layer, const QString &fieldO
return false;

bool ok;
mValues = layer->getDoubleValues( fieldOrExpression, ok, false, nullptr, feedback );
mValues = QgsVectorLayerUtils::getDoubleValues( layer, fieldOrExpression, ok, false, nullptr, feedback );
if ( !ok )
return false;

Expand Down
100 changes: 0 additions & 100 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -3655,106 +3655,6 @@ QVariant QgsVectorLayer::aggregate( QgsAggregateCalculator::Aggregate aggregate,
return c.calculate( aggregate, fieldOrExpression, context, ok );
}

QList<QVariant> QgsVectorLayer::getValues( const QString &fieldOrExpression, bool &ok, bool selectedOnly, QgsFeedback *feedback ) const
{
QList<QVariant> values;

std::unique_ptr<QgsExpression> expression;
QgsExpressionContext context;

int attrNum = mFields.lookupField( fieldOrExpression );

if ( attrNum == -1 )
{
// try to use expression
expression.reset( new QgsExpression( fieldOrExpression ) );
context.appendScopes( QgsExpressionContextUtils::globalProjectLayerScopes( this ) );

if ( expression->hasParserError() || !expression->prepare( &context ) )
{
ok = false;
return values;
}
}

QgsFeature f;
QSet<QString> lst;
if ( !expression )
lst.insert( fieldOrExpression );
else
lst = expression->referencedColumns();

QgsFeatureRequest request = QgsFeatureRequest()
.setFlags( ( expression && expression->needsGeometry() ) ?
QgsFeatureRequest::NoFlags :
QgsFeatureRequest::NoGeometry )
.setSubsetOfAttributes( lst, fields() );

QgsFeatureIterator fit;
if ( !selectedOnly )
{
fit = getFeatures( request );
}
else
{
fit = getSelectedFeatures( request );
}

// create list of non-null attribute values
while ( fit.nextFeature( f ) )
{
if ( expression )
{
context.setFeature( f );
QVariant v = expression->evaluate( &context );
values << v;
}
else
{
values << f.attribute( attrNum );
}
if ( feedback && feedback->isCanceled() )
{
ok = false;
return values;
}
}
ok = true;
return values;
}

QList<double> QgsVectorLayer::getDoubleValues( const QString &fieldOrExpression, bool &ok, bool selectedOnly, int *nullCount, QgsFeedback *feedback ) const
{
QList<double> values;

if ( nullCount )
*nullCount = 0;

QList<QVariant> variantValues = getValues( fieldOrExpression, ok, selectedOnly, feedback );
if ( !ok )
return values;

bool convertOk;
Q_FOREACH ( const QVariant &value, variantValues )
{
double val = value.toDouble( &convertOk );
if ( convertOk )
values << val;
else if ( value.isNull() )
{
if ( nullCount )
*nullCount += 1;
}
if ( feedback && feedback->isCanceled() )
{
ok = false;
return values;
}
}
ok = true;
return values;
}

void QgsVectorLayer::setFeatureBlendMode( QPainter::CompositionMode featureBlendMode )
{
mFeatureBlendMode = featureBlendMode;
Expand Down
26 changes: 0 additions & 26 deletions src/core/qgsvectorlayer.h
Expand Up @@ -1821,32 +1821,6 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
QgsExpressionContext *context = nullptr,
bool *ok = nullptr ) const;

/**
* Fetches all values from a specified field name or expression.
* \param fieldOrExpression field name or an expression string
* \param ok will be set to false if field or expression is invalid, otherwise true
* \param selectedOnly set to true to get values from selected features only
* \param feedback optional feedback object to allow cancelation
* \returns list of fetched values
* \since QGIS 2.9
* \see getDoubleValues
*/
QList< QVariant > getValues( const QString &fieldOrExpression, bool &ok, bool selectedOnly = false, QgsFeedback *feedback = nullptr ) const;

/**
* Fetches all double values from a specified field name or expression. Null values or
* invalid expression results are skipped.
* \param fieldOrExpression field name or an expression string evaluating to a double value
* \param ok will be set to false if field or expression is invalid, otherwise true
* \param selectedOnly set to true to get values from selected features only
* \param nullCount optional pointer to integer to store number of null values encountered in
* \param feedback optional feedback object to allow cancelation
* \returns list of fetched values
* \since QGIS 2.9
* \see getValues
*/
QList< double > getDoubleValues( const QString &fieldOrExpression, bool &ok, bool selectedOnly = false, int *nullCount = nullptr, QgsFeedback *feedback = nullptr ) const;

//! Set the blending mode used for rendering each feature
void setFeatureBlendMode( QPainter::CompositionMode blendMode );
//! Returns the current blending mode for features
Expand Down

0 comments on commit 715677e

Please sign in to comment.