QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
|
Class for parsing and evaluation of expressions (formerly called "search strings"). More...
#include <qgsexpression.h>
Classes | |
struct | ParserError |
Details about any parser errors that were found when parsing the expression. More... | |
Public Types | |
enum | SpatialOperator { soBbox , soIntersects , soContains , soCrosses , soEquals , soDisjoint , soOverlaps , soTouches , soWithin } |
Public Member Functions | |
QgsExpression () | |
Create an empty expression. More... | |
QgsExpression (const QgsExpression &other) | |
Create a copy of this expression. More... | |
QgsExpression (const QString &expr) | |
Creates a new expression based on the provided string. More... | |
~QgsExpression () | |
Qgis::AreaUnit | areaUnits () const |
Returns the desired areal units for calculations involving geomCalculator(), e.g., "$area". More... | |
Qgis::DistanceUnit | distanceUnits () const |
Returns the desired distance units for calculations involving geomCalculator(), e.g., "$length" and "$perimeter". More... | |
QString | dump () const |
Returns an expression string, constructed from the internal abstract syntax tree. More... | |
QString | evalErrorString () const |
Returns evaluation error. More... | |
QVariant | evaluate () |
Evaluate the feature and return the result. More... | |
QVariant | evaluate (const QgsExpressionContext *context) |
Evaluate the expression against the specified context and return the result. More... | |
QString | expression () const |
Returns the original, unmodified expression string. More... | |
template<class T > | |
QList< const T * > | findNodes () const |
Returns a list of all nodes of the given class which are used in this expression. More... | |
QgsDistanceArea * | geomCalculator () |
Returns calculator used for distance and area calculations (used by $length, $area and $perimeter functions only) More... | |
bool | hasEvalError () const |
Returns true if an error occurred when evaluating last input. More... | |
bool | hasParserError () const |
Returns true if an error occurred when parsing the input expression. More... | |
bool | isField () const |
Checks whether an expression consists only of a single field reference. More... | |
bool | isValid () const |
Checks if this expression is valid. More... | |
bool | needsGeometry () const |
Returns true if the expression uses feature geometry for some computation. More... | |
QList< const QgsExpressionNode * > | nodes () const |
Returns a list of all nodes which are used in this expression. More... | |
operator QString () const | |
Automatically convert this expression to a string where requested. More... | |
QgsExpression & | operator= (const QgsExpression &other) |
Create a copy of this expression. More... | |
bool | operator== (const QgsExpression &other) const |
Compares two expressions. More... | |
QList< QgsExpression::ParserError > | parserErrors () const |
Returns parser error details including location of error. More... | |
QString | parserErrorString () const |
Returns parser error. More... | |
bool | prepare (const QgsExpressionContext *context) |
Gets the expression ready for evaluation - find out column indexes. More... | |
QSet< int > | referencedAttributeIndexes (const QgsFields &fields) const |
Returns a list of field name indexes obtained from the provided fields. More... | |
QSet< QString > | referencedColumns () const |
Gets list of columns referenced by the expression. More... | |
QSet< QString > | referencedFunctions () const |
Returns a list of the names of all functions which are used in this expression. More... | |
QSet< QString > | referencedVariables () const |
Returns a list of all variables which are used in this expression. More... | |
const QgsExpressionNode * | rootNode () const |
Returns the root node of the expression. More... | |
void | setAreaUnits (Qgis::AreaUnit unit) |
Sets the desired areal units for calculations involving geomCalculator(), e.g., "$area". More... | |
void | setDistanceUnits (Qgis::DistanceUnit unit) |
Sets the desired distance units for calculations involving geomCalculator(), e.g., "$length" and "$perimeter". More... | |
void | setEvalErrorString (const QString &str) |
Sets evaluation error (used internally by evaluation functions) More... | |
void | setExpression (const QString &expression) |
Set the expression string, will reset the whole internal structure. More... | |
void | setGeomCalculator (const QgsDistanceArea *calc) |
Sets the geometry calculator used for distance and area calculations in expressions. More... | |
Static Public Member Functions | |
static bool | addVariableHelpText (const QString name, const QString &description) |
Adds a help string for a custom variable. More... | |
static bool | attemptReduceToInClause (const QStringList &expressions, QString &result) |
Attempts to reduce a list of expressions to a single "field IN (val1, val2, ... )" type expression. More... | |
static const QStringList & | BuiltinFunctions () |
static bool | checkExpression (const QString &text, const QgsExpressionContext *context, QString &errorMessage) |
Tests whether a string is a valid expression. More... | |
static void | cleanRegisteredFunctions () |
Deletes all registered functions whose ownership have been transferred to the expression engine. More... | |
static QString | createFieldEqualityExpression (const QString &fieldName, const QVariant &value, QVariant::Type fieldType=QVariant::Type::Invalid) |
Create an expression allowing to evaluate if a field is equal to a value. More... | |
static double | evaluateToDouble (const QString &text, double fallbackValue) |
Attempts to evaluate a text string as an expression to a resultant double value. More... | |
static int | expressionToLayerFieldIndex (const QString &expression, const QgsVectorLayer *layer) |
Attempts to resolve an expression to a field index from the given layer. More... | |
static QString | formatPreviewString (const QVariant &value, bool htmlOutput=true, int maximumPreviewLength=60) |
Formats an expression result for friendly display to the user. More... | |
static QString | formatVariableHelp (const QString &description, bool showValue=true, const QVariant &value=QVariant()) |
Returns formatted help text for a variable. More... | |
static int | functionCount () |
Returns the number of functions defined in the parser. More... | |
static int | functionIndex (const QString &name) |
Returns index of the function in Functions array. More... | |
static const QList< QgsExpressionFunction * > & | Functions () |
static QString | group (const QString &group) |
Returns the translated name for a function group. More... | |
static PRIVATE QString | helpText (QString name) |
Returns the help text for a specified function. More... | |
static bool | isFieldEqualityExpression (const QString &expression, QString &field, QVariant &value) |
Returns true if the given expression is a simple "field=value" type expression. More... | |
static bool | isFunctionName (const QString &name) |
tells whether the identifier is a name of existing function More... | |
static QString | quotedColumnRef (QString name) |
Returns a quoted column reference (in double quotes) More... | |
static QString | quotedString (QString text) |
Returns a quoted version of a string (in single quotes) More... | |
static QString | quotedValue (const QVariant &value) |
Returns a string representation of a literal value, including appropriate quotations where required. More... | |
static QString | quotedValue (const QVariant &value, QVariant::Type type) |
Returns a string representation of a literal value, including appropriate quotations where required. More... | |
static QString | quoteFieldExpression (const QString &expression, const QgsVectorLayer *layer) |
Validate if the expression is a field in the layer and ensure it is quoted. More... | |
static QSet< QString > | referencedVariables (const QString &text) |
This function returns variables in each expression between [% and %]. More... | |
static bool | registerFunction (QgsExpressionFunction *function, bool transferOwnership=false) |
Registers a function to the expression engine. More... | |
static QString | replaceExpressionText (const QString &action, const QgsExpressionContext *context, const QgsDistanceArea *distanceArea=nullptr) |
This function replaces each expression between [% and %] in the string with the result of its evaluation with the specified context. More... | |
static QStringList | tags (const QString &name) |
Returns a string list of search tags for a specified function. More... | |
static bool | unregisterFunction (const QString &name) |
Unregisters a function from the expression engine. More... | |
static QString | variableHelpText (const QString &variableName) |
Returns the help text for a specified variable. More... | |
Friends | |
class | QgsOgcUtils |
Class for parsing and evaluation of expressions (formerly called "search strings").
The expressions try to follow both syntax and semantics of SQL expressions.
Usage:
Similarly to SQL, this class supports three-value logic: true/false/unknown. Unknown value may be a result of operations with missing data (NULL). Please note that NULL is different value than zero or an empty string. For example 3 > NULL returns unknown.
There is no special (three-value) 'boolean' type: true/false is represented as 1/0 integer, unknown value is represented the same way as NULL values: NULL QVariant.
For better performance with many evaluations you may first call prepare(fields) function to find out indices of columns and then repeatedly call evaluate(feature).
Operators and functions that expect arguments to be of a particular type automatically convert the arguments to that type, e.g. sin('2.1') will convert the argument to a double, length(123) will first convert the number to a string. Explicit conversion can be achieved with to_int, to_real, to_string functions. If implicit or explicit conversion is invalid, the evaluation returns an error. Comparison operators do numeric comparison in case both operators are numeric (int/double) or they can be converted to numeric types.
This class is implicitly shared, copying has a very low overhead. It is normally preferable to call QgsExpression( otherExpression )
instead of QgsExpression( otherExpression.expression() )
. A deep copy will only be made when prepare() is called. For usage this means mainly, that you should normally keep an unprepared master copy of a QgsExpression and whenever using it with a particular QgsFeatureIterator copy it just before and prepare it using the same context as the iterator.
Implicit sharing was added in 2.14
Definition at line 187 of file qgsexpression.h.
Enumerator | |
---|---|
soBbox | |
soIntersects | |
soContains | |
soCrosses | |
soEquals | |
soDisjoint | |
soOverlaps | |
soTouches | |
soWithin |
Definition at line 602 of file qgsexpression.h.
QgsExpression::QgsExpression | ( | const QString & | expr | ) |
Creates a new expression based on the provided string.
The string will immediately be parsed. For optimization prepare() should always be called before every loop in which this expression is used.
Definition at line 154 of file qgsexpression.cpp.
QgsExpression::QgsExpression | ( | const QgsExpression & | other | ) |
Create a copy of this expression.
This is preferred over recreating an expression from a string since it does not need to be re-parsed.
Definition at line 162 of file qgsexpression.cpp.
QgsExpression::QgsExpression | ( | ) |
QgsExpression::~QgsExpression | ( | ) |
Definition at line 193 of file qgsexpression.cpp.
|
static |
Adds a help string for a custom variable.
The specified variable name should not have an existing help string set. If a help string is already present then false
will be returned and no changes will occur.
name | variable name |
description | the help string to add. This is user visible, and should be a translated string. |
true
if the help string was successfully added Definition at line 924 of file qgsexpression.cpp.
Qgis::AreaUnit QgsExpression::areaUnits | ( | ) | const |
Returns the desired areal units for calculations involving geomCalculator(), e.g., "$area".
Definition at line 429 of file qgsexpression.cpp.
|
static |
Attempts to reduce a list of expressions to a single "field IN (val1, val2, ... )" type expression.
This will only be possible if all the input expressions form simple "field=value" OR "field IN (value1, value2)" expressions, and all reference the same field name.
Returns true
if the given expressions could be converted to an IN type expression.
expressions | expressions to test |
result | will be set to the calculated "field IN (...)" expression, wherever possible |
true
if the expression was converted to a field IN type expressionDefinition at line 1178 of file qgsexpression.cpp.
|
static |
Definition at line 9211 of file qgsexpressionfunction.cpp.
|
static |
Tests whether a string is a valid expression.
text | string to test |
context | optional expression context |
errorMessage | will be filled with any error message from the validation |
true
if string is a valid expression Definition at line 47 of file qgsexpression.cpp.
|
static |
Deletes all registered functions whose ownership have been transferred to the expression engine.
Definition at line 9205 of file qgsexpressionfunction.cpp.
|
static |
Create an expression allowing to evaluate if a field is equal to a value.
The value may be null.
fieldName | the name of the field |
value | the value of the field |
fieldType | the type of the field on the left side used to quote the value. If not given, the value type is used instead |
Definition at line 1140 of file qgsexpression.cpp.
Qgis::DistanceUnit QgsExpression::distanceUnits | ( | ) | const |
Returns the desired distance units for calculations involving geomCalculator(), e.g., "$length" and "$perimeter".
Definition at line 419 of file qgsexpression.cpp.
QString QgsExpression::dump | ( | ) | const |
Returns an expression string, constructed from the internal abstract syntax tree.
This does not contain any nice whitespace formatting or comments. In general it is preferable to use expression() instead.
Definition at line 398 of file qgsexpression.cpp.
QString QgsExpression::evalErrorString | ( | ) | const |
Returns evaluation error.
Definition at line 388 of file qgsexpression.cpp.
QVariant QgsExpression::evaluate | ( | ) |
Evaluate the feature and return the result.
Definition at line 355 of file qgsexpression.cpp.
QVariant QgsExpression::evaluate | ( | const QgsExpressionContext * | context | ) |
Evaluate the expression against the specified context and return the result.
context | context for evaluating expression |
Definition at line 367 of file qgsexpression.cpp.
|
static |
Attempts to evaluate a text string as an expression to a resultant double value.
text | text to evaluate as expression |
fallbackValue | value to return if text can not be evaluated as a double |
Definition at line 523 of file qgsexpression.cpp.
QString QgsExpression::expression | ( | ) | const |
Returns the original, unmodified expression string.
If there was none supplied because it was constructed by sole API calls, dump() will be used to create one instead.
Definition at line 64 of file qgsexpression.cpp.
|
static |
Attempts to resolve an expression to a field index from the given layer.
Given a string which may either directly match a field name from a layer, OR may be an expression which consists only of a single field reference for that layer, this method will return the corresponding field index.
Definition at line 1379 of file qgsexpression.cpp.
|
inline |
Returns a list of all nodes of the given class which are used in this expression.
Definition at line 379 of file qgsexpression.h.
|
static |
Formats an expression result for friendly display to the user.
Truncates the result to a sensible length, and presents text representations of non numeric/text types (e.g., geometries and features).
value | expression result to format |
htmlOutput | set to true to allow HTML formatting, or false for plain text output |
maximumPreviewLength | define the maximum character length of the preview |
true
Definition at line 992 of file qgsexpression.cpp.
|
static |
Returns formatted help text for a variable.
description | translated description of variable |
showValue | set to true to include current value of variable in help text |
value | current value of variable to show in help text |
Definition at line 940 of file qgsexpression.cpp.
|
static |
Returns the number of functions defined in the parser.
Definition at line 148 of file qgsexpression.cpp.
|
static |
Returns index of the function in Functions array.
Definition at line 131 of file qgsexpression.cpp.
|
static |
Definition at line 7990 of file qgsexpressionfunction.cpp.
QgsDistanceArea * QgsExpression::geomCalculator | ( | ) |
Returns calculator used for distance and area calculations (used by $length, $area and $perimeter functions only)
Definition at line 406 of file qgsexpression.cpp.
|
static |
Returns the translated name for a function group.
group | untranslated group name |
Definition at line 959 of file qgsexpression.cpp.
bool QgsExpression::hasEvalError | ( | ) | const |
Returns true
if an error occurred when evaluating last input.
Definition at line 383 of file qgsexpression.cpp.
bool QgsExpression::hasParserError | ( | ) | const |
Returns true
if an error occurred when parsing the input expression.
Definition at line 210 of file qgsexpression.cpp.
|
static |
Returns the help text for a specified function.
name | function name |
Definition at line 551 of file qgsexpression.cpp.
bool QgsExpression::isField | ( | ) | const |
Checks whether an expression consists only of a single field reference.
Definition at line 1374 of file qgsexpression.cpp.
|
static |
Returns true
if the given expression is a simple "field=value" type expression.
expression | expression to test |
field | will be set to the field name if the expression is a field equality expression |
value | will be set to the value if the expression is a field equality expression |
true
if the expression is a field equality expressionDefinition at line 1154 of file qgsexpression.cpp.
|
static |
tells whether the identifier is a name of existing function
Definition at line 126 of file qgsexpression.cpp.
bool QgsExpression::isValid | ( | ) | const |
Checks if this expression is valid.
A valid expression could be parsed but does not necessarily evaluate properly.
Definition at line 205 of file qgsexpression.cpp.
bool QgsExpression::needsGeometry | ( | ) | const |
Returns true
if the expression uses feature geometry for some computation.
Definition at line 275 of file qgsexpression.cpp.
QList< const QgsExpressionNode * > QgsExpression::nodes | ( | ) | const |
Returns a list of all nodes which are used in this expression.
Definition at line 1416 of file qgsexpression.cpp.
QgsExpression::operator QString | ( | ) | const |
Automatically convert this expression to a string where requested.
Definition at line 183 of file qgsexpression.cpp.
QgsExpression & QgsExpression::operator= | ( | const QgsExpression & | other | ) |
Create a copy of this expression.
This is preferred over recreating an expression from a string since it does not need to be re-parsed.
Definition at line 168 of file qgsexpression.cpp.
bool QgsExpression::operator== | ( | const QgsExpression & | other | ) | const |
Compares two expressions.
The operator returns true
if the expression string is equal.
Definition at line 200 of file qgsexpression.cpp.
QList< QgsExpression::ParserError > QgsExpression::parserErrors | ( | ) | const |
Returns parser error details including location of error.
Definition at line 220 of file qgsexpression.cpp.
QString QgsExpression::parserErrorString | ( | ) | const |
Returns parser error.
Definition at line 215 of file qgsexpression.cpp.
bool QgsExpression::prepare | ( | const QgsExpressionContext * | context | ) |
Gets the expression ready for evaluation - find out column indexes.
context | context for preparing expression |
Definition at line 332 of file qgsexpression.cpp.
|
static |
Returns a quoted column reference (in double quotes)
Definition at line 72 of file qgsexpression.cpp.
|
static |
Returns a quoted version of a string (in single quotes)
Definition at line 77 of file qgsexpression.cpp.
|
static |
Returns a string representation of a literal value, including appropriate quotations where required.
value | value to convert to a string representation |
Definition at line 86 of file qgsexpression.cpp.
|
static |
Returns a string representation of a literal value, including appropriate quotations where required.
value | value to convert to a string representation |
type | value type |
Definition at line 91 of file qgsexpression.cpp.
|
static |
Validate if the expression is a field in the layer and ensure it is quoted.
Given a string which may either directly match a field name from a layer, OR may be an expression which consists only of a single field reference for that layer, this method will return the quoted field.
Definition at line 1399 of file qgsexpression.cpp.
QSet< int > QgsExpression::referencedAttributeIndexes | ( | const QgsFields & | fields | ) | const |
Returns a list of field name indexes obtained from the provided fields.
Definition at line 249 of file qgsexpression.cpp.
QSet< QString > QgsExpression::referencedColumns | ( | ) | const |
Gets list of columns referenced by the expression.
Definition at line 225 of file qgsexpression.cpp.
QSet< QString > QgsExpression::referencedFunctions | ( | ) | const |
Returns a list of the names of all functions which are used in this expression.
Definition at line 241 of file qgsexpression.cpp.
QSet< QString > QgsExpression::referencedVariables | ( | ) | const |
Returns a list of all variables which are used in this expression.
If the list contains a NULL QString, there is a variable name used which is determined at runtime.
Definition at line 233 of file qgsexpression.cpp.
|
static |
This function returns variables in each expression between [% and %].
text | The source string in which variables should be searched. |
Definition at line 502 of file qgsexpression.cpp.
|
static |
Registers a function to the expression engine.
This is required to allow expressions to utilize the function.
function | function to register |
transferOwnership | set to true to transfer ownership of function to expression engine |
true
on successful registration Definition at line 9176 of file qgsexpressionfunction.cpp.
|
static |
This function replaces each expression between [% and %] in the string with the result of its evaluation with the specified context.
Additional substitutions can be passed through the substitutionMap parameter
action | The source string in which placeholders should be replaced. |
context | Expression context |
distanceArea | Optional QgsDistanceArea. If specified, the QgsDistanceArea is used for distance and area conversion |
Definition at line 439 of file qgsexpression.cpp.
const QgsExpressionNode * QgsExpression::rootNode | ( | ) | const |
Returns the root node of the expression.
The root node is nullptr
if parsing has failed.
Definition at line 1369 of file qgsexpression.cpp.
void QgsExpression::setAreaUnits | ( | Qgis::AreaUnit | unit | ) |
Sets the desired areal units for calculations involving geomCalculator(), e.g., "$area".
If distance units are set to QgsUnitTypes::AreaUnknownUnit (default), prepare() will read variables from the expression context ("project_distance_units") to determine distance units.
Definition at line 434 of file qgsexpression.cpp.
void QgsExpression::setDistanceUnits | ( | Qgis::DistanceUnit | unit | ) |
Sets the desired distance units for calculations involving geomCalculator(), e.g., "$length" and "$perimeter".
If distance units are set to QgsUnitTypes::DistanceUnknownUnit (default), prepare() will read variables from the expression context ("project_distance_units") to determine distance units.
Definition at line 424 of file qgsexpression.cpp.
void QgsExpression::setEvalErrorString | ( | const QString & | str | ) |
Sets evaluation error (used internally by evaluation functions)
Definition at line 393 of file qgsexpression.cpp.
void QgsExpression::setExpression | ( | const QString & | expression | ) |
Set the expression string, will reset the whole internal structure.
Definition at line 55 of file qgsexpression.cpp.
void QgsExpression::setGeomCalculator | ( | const QgsDistanceArea * | calc | ) |
Sets the geometry calculator used for distance and area calculations in expressions.
(used by $length, $area and $perimeter functions only). If the geometry calculator is set to nullptr
(default), prepare() will read variables from the expression context ("project_ellipsoid", "_project_transform_context" and "_layer_crs") to build a geometry calculator. If these variables does not exist and if setGeomCalculator() is not called, all distance and area calculations are performed using simple Cartesian methods (ie no ellipsoidal calculations).
calc | geometry calculator. Ownership is not transferred. Set to nullptr to force Cartesian calculations. |
Definition at line 323 of file qgsexpression.cpp.
|
static |
Returns a string list of search tags for a specified function.
name | function name |
Definition at line 686 of file qgsexpression.cpp.
|
static |
Unregisters a function from the expression engine.
The function will no longer be usable in expressions.
name | function name |
Definition at line 9189 of file qgsexpressionfunction.cpp.
|
static |
Returns the help text for a specified variable.
variableName | name of variable |
Definition at line 934 of file qgsexpression.cpp.
|
friend |
Definition at line 829 of file qgsexpression.h.