1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
|
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/expression/qgsexpression.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.py again *
************************************************************************/
class QgsExpression
{
%Docstring(signature="appended")
Class for parsing and evaluation of expressions (formerly called "search
strings"). The expressions try to follow both syntax and semantics of
SQL expressions.
Usage:
.. code-block:: python
exp = QgsExpression("gid*2 > 10 and type not in ('D','F')")
if exp.hasParserError():
# show error message with parserErrorString() and exit
result = exp.evaluate(feature, fields)
if exp.hasEvalError():
# show error message with evalErrorString()
else:
# examine the result
Three Value Logic
-----------------
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.
Performance
-----------
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).
Type conversion
---------------
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.
Implicit sharing
----------------
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 :py:func:`~prepare` is called. For usage this means
mainly, that you should normally keep an unprepared master copy of a
:py:class:`QgsExpression` and whenever using it with a particular
:py:class:`QgsFeatureIterator` copy it just before and prepare it using
the same context as the iterator.
Implicit sharing was added in 2.14
%End
%TypeHeaderCode
#include "qgsexpression.h"
%End
public:
struct ParserError
{
enum ParserErrorType /BaseType=IntEnum/
{
Unknown,
FunctionUnknown,
FunctionWrongArgs,
FunctionInvalidParams,
FunctionNamedArgsError
};
ParserErrorType errorType;
QString errorMsg;
int firstLine;
int firstColumn;
int lastLine;
int lastColumn;
};
QgsExpression( const QString &expr );
%Docstring
Creates a new expression based on the provided string. The string will
immediately be parsed. For optimization
:py:func:`~QgsExpression.prepare` should always be called before every
loop in which this expression is used.
%End
QgsExpression( const QgsExpression &other );
%Docstring
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.
%End
QgsExpression();
%Docstring
Create an empty expression.
%End
~QgsExpression();
bool operator==( const QgsExpression &other ) const;
bool isValid() const;
%Docstring
Checks if this expression is valid. A valid expression could be parsed
but does not necessarily evaluate properly.
%End
bool hasParserError() const;
%Docstring
Returns ``True`` if an error occurred when parsing the input expression
%End
QString parserErrorString() const;
%Docstring
Returns parser error
%End
QList<QgsExpression::ParserError> parserErrors() const;
%Docstring
Returns parser error details including location of error.
%End
const QgsExpressionNode *rootNode() const;
%Docstring
Returns the root node of the expression.
The root node is ``None`` if parsing has failed.
%End
bool prepare( const QgsExpressionContext *context );
%Docstring
Gets the expression ready for evaluation - find out column indexes.
:param context: context for preparing expression
%End
QSet<QString> referencedColumns() const;
%Docstring
Gets list of columns referenced by the expression.
.. note::
If the returned list contains the :py:class:`QgsFeatureRequest`.AllAttributes constant then
all attributes from the layer are required for evaluation of the expression.
:py:class:`QgsFeatureRequest`.setSubsetOfAttributes automatically handles this case.
.. warning::
If the expression has been prepared via a call to :py:func:`QgsExpression.prepare()`,
or a call to :py:func:`QgsExpressionNode.prepare()` for a node has been made, then parts of
the expression may have been determined to evaluate to a static pre-calculatable value.
In this case the results will omit attribute indices which are used by these
pre-calculated nodes, regardless of their actual referenced columns.
If you are seeking to use these functions to introspect an expression you must
take care to do this with an unprepared expression.
.. seealso:: :py:func:`referencedAttributeIndexes`
%End
QSet<QString> referencedVariables() const;
%Docstring
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.
.. note::
In contrast to the :py:func:`~QgsExpression.referencedColumns` function this method
is not affected by any previous calls to :py:func:`QgsExpression.prepare()`,
or :py:func:`QgsExpressionNode.prepare()`.
%End
QSet<QString> referencedFunctions() const;
%Docstring
Returns a list of the names of all functions which are used in this
expression.
.. note::
In contrast to the :py:func:`~QgsExpression.referencedColumns` function this method
is not affected by any previous calls to :py:func:`QgsExpression.prepare()`,
or :py:func:`QgsExpressionNode.prepare()`.
.. versionadded:: 3.2
%End
QSet<int> referencedAttributeIndexes( const QgsFields &fields ) const;
%Docstring
Returns a list of field name indexes obtained from the provided fields.
.. warning::
If the expression has been prepared via a call to :py:func:`QgsExpression.prepare()`,
or a call to :py:func:`QgsExpressionNode.prepare()` for a node has been made, then parts of
the expression may have been determined to evaluate to a static pre-calculatable value.
In this case the results will omit attribute indices which are used by these
pre-calculated nodes, regardless of their actual referenced columns.
If you are seeking to use these functions to introspect an expression you must
take care to do this with an unprepared expression.
%End
bool needsGeometry() const;
%Docstring
Returns ``True`` if the expression uses feature geometry for some
computation
%End
QVariant evaluate();
%Docstring
Evaluate the feature and return the result.
.. note::
this method does not expect that :py:func:`~QgsExpression.prepare` has been called on this instance
%End
QVariant evaluate( const QgsExpressionContext *context );
%Docstring
Evaluate the expression against the specified context and return the
result.
:param context: context for evaluating expression
.. note::
:py:func:`~QgsExpression.prepare` should be called before calling this method.
%End
bool hasEvalError() const;
%Docstring
Returns ``True`` if an error occurred when evaluating last input
%End
QString evalErrorString() const;
%Docstring
Returns evaluation error
%End
void setEvalErrorString( const QString &str );
%Docstring
Sets evaluation error (used internally by evaluation functions)
%End
bool isField() const;
%Docstring
Checks whether an expression consists only of a single field reference
.. seealso:: :py:func:`expressionToLayerFieldIndex`
%End
static int expressionToLayerFieldIndex( const QString &expression, const QgsVectorLayer *layer );
%Docstring
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.
:return: field index if found, or -1 if ``expression`` does not
represent a field from the layer.
.. seealso:: :py:func:`isField`
.. versionadded:: 3.22
%End
static QString quoteFieldExpression( const QString &expression, const QgsVectorLayer *layer );
%Docstring
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.
:return: the ``expression`` if not a field or quotes are not required,
otherwise returns a quoted field.
.. seealso:: :py:func:`expressionToLayerFieldIndex`
.. versionadded:: 3.24
%End
static bool checkExpression( const QString &text, const QgsExpressionContext *context, QString &errorMessage /Out/ );
%Docstring
Tests whether a string is a valid expression.
:param text: string to test
:param context: optional expression context
:return: - ``True`` if string is a valid expression
- errorMessage: will be filled with any error message from the
validation
%End
void setExpression( const QString &expression );
%Docstring
Set the expression string, will reset the whole internal structure.
%End
QString expression() const;
%Docstring
Returns the original, unmodified expression string. If there was none
supplied because it was constructed by sole API calls,
:py:func:`~QgsExpression.dump` will be used to create one instead.
%End
QString dump() const;
%Docstring
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
:py:func:`~QgsExpression.expression` instead.
%End
QgsDistanceArea *geomCalculator();
%Docstring
Returns calculator used for distance and area calculations (used by
$length, $area and $perimeter functions only)
.. seealso:: :py:func:`setGeomCalculator`
.. seealso:: :py:func:`distanceUnits`
.. seealso:: :py:func:`areaUnits`
%End
void setGeomCalculator( const QgsDistanceArea *calc );
%Docstring
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 ``None`` (default),
:py:func:`~QgsExpression.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 :py:func:`~QgsExpression.setGeomCalculator` is not
called, all distance and area calculations are performed using simple
Cartesian methods (ie no ellipsoidal calculations).
:param calc: geometry calculator. Ownership is not transferred. Set to
``None`` to force Cartesian calculations.
.. seealso:: :py:func:`geomCalculator`
%End
Qgis::DistanceUnit distanceUnits() const;
%Docstring
Returns the desired distance units for calculations involving
:py:func:`~QgsExpression.geomCalculator`, e.g., "$length" and
"$perimeter".
.. note::
distances are only converted when a :py:func:`~QgsExpression.geomCalculator` has been set
.. seealso:: :py:func:`setDistanceUnits`
.. seealso:: :py:func:`areaUnits`
%End
void setDistanceUnits( Qgis::DistanceUnit unit );
%Docstring
Sets the desired distance units for calculations involving
:py:func:`~QgsExpression.geomCalculator`, e.g., "$length" and
"$perimeter". If distance units are set to
:py:class:`QgsUnitTypes`.DistanceUnknownUnit (default),
:py:func:`~QgsExpression.prepare` will read variables from the
expression context ("project_distance_units") to determine distance
units.
.. note::
distances are only converted when a :py:func:`~QgsExpression.geomCalculator` has been set
.. seealso:: :py:func:`distanceUnits`
.. seealso:: :py:func:`setAreaUnits`
%End
Qgis::AreaUnit areaUnits() const;
%Docstring
Returns the desired areal units for calculations involving
:py:func:`~QgsExpression.geomCalculator`, e.g., "$area".
.. note::
areas are only converted when a :py:func:`~QgsExpression.geomCalculator` has been set
.. seealso:: :py:func:`setAreaUnits`
.. seealso:: :py:func:`distanceUnits`
%End
void setAreaUnits( Qgis::AreaUnit unit );
%Docstring
Sets the desired areal units for calculations involving
:py:func:`~QgsExpression.geomCalculator`, e.g., "$area". If distance
units are set to :py:class:`QgsUnitTypes`.AreaUnknownUnit (default),
:py:func:`~QgsExpression.prepare` will read variables from the
expression context ("project_distance_units") to determine distance
units.
.. note::
areas are only converted when a :py:func:`~QgsExpression.geomCalculator` has been set
.. seealso:: :py:func:`areaUnits`
.. seealso:: :py:func:`setDistanceUnits`
%End
static QString replaceExpressionText( const QString &action, const QgsExpressionContext *context,
const QgsDistanceArea *distanceArea = 0 );
%Docstring
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
:param action: The source string in which placeholders should be
replaced.
:param context: Expression context
:param distanceArea: Optional :py:class:`QgsDistanceArea`. If specified,
the :py:class:`QgsDistanceArea` is used for
distance and area conversion
%End
static QSet<QString> referencedVariables( const QString &text );
%Docstring
This function returns variables in each expression between [% and %].
:param text: The source string in which variables should be searched.
.. versionadded:: 3.2
%End
static double evaluateToDouble( const QString &text, double fallbackValue );
%Docstring
Attempts to evaluate a text string as an expression to a resultant
double value.
:param text: text to evaluate as expression
:param fallbackValue: value to return if text can not be evaluated as a
double
:return: evaluated double value, or fallback value
.. note::
this method is inefficient for bulk evaluation of expressions, it is intended
for one-off evaluations only.
%End
enum SpatialOperator /BaseType=IntEnum/
{
soBbox,
soIntersects,
soContains,
soCrosses,
soEquals,
soDisjoint,
soOverlaps,
soTouches,
soWithin,
};
static const QList<QgsExpressionFunction *> &Functions();
static const QStringList &BuiltinFunctions();
static bool registerFunction( QgsExpressionFunction *function, bool transferOwnership = false );
%Docstring
Registers a function to the expression engine. This is required to allow
expressions to utilize the function.
:param function: function to register
:param transferOwnership: set to ``True`` to transfer ownership of
function to expression engine
:return: ``True`` on successful registration
.. seealso:: :py:func:`unregisterFunction`
%End
static bool unregisterFunction( const QString &name );
%Docstring
Unregisters a function from the expression engine. The function will no
longer be usable in expressions.
:param name: function name
.. seealso:: :py:func:`registerFunction`
%End
static void cleanRegisteredFunctions();
%Docstring
Deletes all registered functions whose ownership have been transferred
to the expression engine.
%End
static bool isFunctionName( const QString &name );
%Docstring
tells whether the identifier is a name of existing function
%End
static int functionIndex( const QString &name );
%Docstring
Returns index of the function in Functions array
%End
static int functionCount();
%Docstring
Returns the number of functions defined in the parser
:return: The number of function defined in the parser.
%End
static QString quotedColumnRef( QString name );
%Docstring
Returns a quoted column reference (in double quotes)
.. seealso:: :py:func:`quotedString`
.. seealso:: :py:func:`quotedValue`
%End
static QString quotedString( QString text );
%Docstring
Returns a quoted version of a string (in single quotes)
.. seealso:: :py:func:`quotedValue`
.. seealso:: :py:func:`quotedColumnRef`
%End
static QString quotedValue( const QVariant &value );
%Docstring
Returns a string representation of a literal value, including
appropriate quotations where required.
:param value: value to convert to a string representation
.. seealso:: :py:func:`quotedString`
.. seealso:: :py:func:`quotedColumnRef`
%End
static QString quotedValue( const QVariant &value, QMetaType::Type type );
%Docstring
Returns a string representation of a literal value, including
appropriate quotations where required.
:param value: value to convert to a string representation
:param type: value type
.. seealso:: :py:func:`quotedString`
.. seealso:: :py:func:`quotedColumnRef`
%End
static QString quotedValue( const QVariant &value, QVariant::Type type ) /Deprecated/;
%Docstring
Returns a string representation of a literal value, including
appropriate quotations where required.
:param value: value to convert to a string representation
:param type: value type
.. seealso:: :py:func:`quotedString`
.. seealso:: :py:func:`quotedColumnRef`
.. deprecated:: 3.38
Use the method with a QMetaType.Type argument instead.
%End
static QString helpText( QString name );
%Docstring
Returns the help text for a specified function.
:param name: function name
.. seealso:: :py:func:`variableHelpText`
.. seealso:: :py:func:`formatVariableHelp`
%End
static QStringList tags( const QString &name );
%Docstring
Returns a string list of search tags for a specified function.
:param name: function name
.. versionadded:: 3.12
%End
static bool addVariableHelpText( const QString name, const QString &description );
%Docstring
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.
:param name: variable name
:param description: the help string to add. This is user visible, and
should be a translated string.
:return: ``True`` if the help string was successfully added
.. seealso:: :py:func:`variableHelpText`
.. versionadded:: 3.22
%End
static QString variableHelpText( const QString &variableName );
%Docstring
Returns the help text for a specified variable.
:param variableName: name of variable
.. seealso:: :py:func:`helpText`
.. seealso:: :py:func:`addVariableHelpText`
%End
static QString formatVariableHelp( const QString &description, bool showValue = true, const QVariant &value = QVariant() );
%Docstring
Returns formatted help text for a variable.
:param description: translated description of variable
:param showValue: set to ``True`` to include current value of variable
in help text
:param value: current value of variable to show in help text
.. seealso:: :py:func:`helpText`
.. seealso:: :py:func:`variableHelpText`
%End
static QString group( const QString &group );
%Docstring
Returns the translated name for a function group.
:param group: untranslated group name
%End
static QString formatPreviewString( const QVariant &value, bool htmlOutput = true, int maximumPreviewLength = 60 );
%Docstring
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).
:param value: expression result to format
:param htmlOutput: set to ``True`` to allow HTML formatting, or
``False`` for plain text output
:param maximumPreviewLength: define the maximum character length of the
preview
:return: formatted string, may contain HTML formatting characters if
``htmlOutput`` is ``True``
%End
static QString createFieldEqualityExpression( const QString &fieldName, const QVariant &value, QMetaType::Type fieldType = QMetaType::Type::UnknownType );
%Docstring
Create an expression allowing to evaluate if a field is equal to a
value. The value may be null.
:param fieldName: the name of the field
:param value: the value of the field
:param fieldType: the type of the field on the left side used to quote
the value. If not given, the value type is used
instead
:return: the expression to evaluate field equality
%End
static QString createFieldEqualityExpression( const QString &fieldName, const QVariant &value, QVariant::Type fieldType ) /Deprecated/;
%Docstring
Create an expression allowing to evaluate if a field is equal to a
value. The value may be null.
:param fieldName: the name of the field
:param value: the value of the field
:param fieldType: the type of the field on the left side used to quote
the value. If not given, the value type is used
instead
:return: the expression to evaluate field equality
.. deprecated:: 3.38
Use the method with a QMetaType.Type argument instead.
%End
static bool isFieldEqualityExpression( const QString &expression, QString &field /Out/, QVariant &value /Out/ );
%Docstring
Returns ``True`` if the given ``expression`` is a simple "field=value"
type expression.
:param expression: expression to test
:return: - ``True`` if the expression is a field equality expression
- field: the field name if the expression is a field equality
expression
- value: the value if the expression is a field equality
expression
.. versionadded:: 3.18
%End
static bool attemptReduceToInClause( const QStringList &expressions, QString &result /Out/ );
%Docstring
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.
:param expressions: expressions to test
:return: - ``True`` if the expression was converted to a field IN type
expression
- result: the calculated "field IN (...)" expression, wherever
possible
.. versionadded:: 3.18
%End
SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsExpression: '%1'>" ).arg( sipCpp->expression() );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
%End
};
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/expression/qgsexpression.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.py again *
************************************************************************/
|