expr
qgis_render::expr
The QGIS expression engine: the same language used for rule-based symbology, labels, and field calculators, available from Rust.
use qgis_render::expr::*;Parsing and evaluating
let expr = Expression::parse("\"height\" > 50 AND \"type\" = 'residential'")?;
let result = expr.evaluate(&feature, &context)?;let matched = result.as_bool();evaluate returns a Value, which converts to Rust types:
| Method | Returns |
|---|---|
as_bool() | bool |
as_f64() | f64 |
as_i64() | i64 |
as_string() | String |
as_geometry() | Option<Geometry> |
is_null() | bool |
Supported syntax
Expression::parse("length($geometry)")?;Expression::parse("concat(\"name\", ' (', \"height\", 'm)')")?;Expression::parse("CASE WHEN \"pop\" > 1000 THEN 'big' ELSE 'small' END")?;Expression::parse("color_ramp_interpolate('RdYlGn', \"value\", 0, 100)")?;Field names are double-quoted, string literals single-quoted. $geometry,
$id, and $area refer to the current feature.
Building expressions programmatically
let expr = Expression::gt( Expression::field("height"), Expression::literal(50),);| Constructor | Description |
|---|---|
Expression::parse(&str) | Parse expression text. |
Expression::field(&str) | A field reference. |
Expression::literal(v) | A constant. |
Expression::gt / lt / gte / lte / eq / ne | Comparisons. |
Expression::and / or / not | Boolean logic. |
Expression::function(name, args) | Any registered QGIS function. |
Compiling for repeated evaluation
Parsing is the expensive part. Compile once against a layer’s fields, then evaluate per feature:
let compiled = expr.compile(&layer.fields())?;
for feature in layer.features() { if compiled.evaluate_bool(&feature)? { // ... }}Using expressions as filters
Filter::expr wraps an expression so it can be pushed
into a FeatureRequest:
use qgis_render::features::{FeatureRequest, Filter};
let request = FeatureRequest::new() .filter(Filter::expr("\"height\" > 50 AND \"type\" = 'residential'"));
for feature in layer.features_with(request) { // ...}Errors
Expression::parse and evaluate return QgisError::ExpressionError { expr, message }
with the offending expression and the parser’s diagnostic.
See also
features— filters built on expressionsFeature— the evaluation contextqgis-cli export—--filteron the command line