ExpressionEvaluator

The MapsGL expression evaluator: one implementation of the Mapbox-compatible expression language for both paint values and filters, mirroring MapsGL JS evaluateExpression.

Values are plain Kotlin: Double for numbers (JS has no integer type, and mixing Int and Double would make ["==", 5, 5.0] false), String, Boolean, List, Map and null. A missing property is null.

Time operators return Unix seconds, matching JS 1.9.4.

Supported operators:

  • Data: literal, get, has, var, properties, zoom, coalesce

  • Comparison and logic: ==, !=, <, <=, >, >=, !, all, any

  • Types: to-number, to-string, to-boolean, to-date, to-time, to-locale-string, to-unit, typeof, number, string, boolean, object

  • Math: +, -, *, /, %, ^, abs, ceil, floor, round, min, max, sqrt, ln, ln2, log2, log10, sin, cos, tan, asin, acos, atan, e, pi

  • Strings and arrays: at, in, regex, index-of, length, slice, concat, downcase, upcase

  • Control flow: step, interpolate, case, match, let

  • Time: map-time (and its aliases time, mapTime, timeline), now

format, geometry-type and interpolate-hcl / interpolate-lab are not evaluated.

Functions

Link copied to clipboard
fun evaluate(expression: Any?, context: ExpressionContext): Any?

Evaluates expression against context.

Link copied to clipboard
fun evaluateOrNull(expression: Any?, context: ExpressionContext): Any?

Evaluates expression, or returns null when it cannot be evaluated.

Link copied to clipboard
fun matches(filter: Any?, context: ExpressionContext): Boolean

Evaluates a filter to a boolean, using JS truthiness: a feature is kept when the result is truthy. A filter that fails to evaluate drops the feature rather than admitting everything.