MvtExpression

sealed class MvtExpression<out T>

Mapbox-style expression tree. Each node evaluates to T? against an EvalContext (current tile zoom + feature property map); null propagates when an upstream node has no value (e.g. Get on a missing property).

Supported subset of the Mapbox GL spec:

  • Literals: numbers, strings, booleans, colors, arrays.

  • Sources: ["zoom"], ["get", key], ["has", key], ["literal", value].

  • Comparisons: ==, !=, <, <=, >, >=.

  • Boolean: all, any, !.

  • Arithmetic: +, -, *, / (variadic where Mapbox is variadic).

  • Conditionals: case, match, step.

  • Interpolation: interpolate ["linear"] and interpolate ["exponential", base] for Float and Color.

  • Coercion: to-number, to-string, to-boolean.

Not supported (returns null / falls through to default):

  • feature-state, geometry-type, color expression functions (rgb/rgba/to-color), string functions (concat/downcase/upcase/length), cubic-bezier curves.

Construction is allocation-free at evaluation; trees are built once at style time and evaluated per-feature/per-frame.

Inheritors

Types

Link copied to clipboard
class Add(val operands: List<MvtExpression<*>>) : MvtExpression<Double>

Variadic sum. Mapbox + accepts ≥ 2 operands; empty list → 0.

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
class Case<T>(val branches: List<MvtExpression.Case.Branch<T>>, val default: MvtExpression<T>) : MvtExpression<T>

["case", cond1, val1, cond2, val2, …, default] — first true condition wins. Both sides of each branch are evaluated lazily — non-matching value-side expressions never run.

Link copied to clipboard
object Companion
Link copied to clipboard
class Concat(val parts: List<MvtExpression<*>>) : MvtExpression<String>

["concat", arg, arg, …] — joins string-coerced operands.

Link copied to clipboard
class Div(val a: MvtExpression<*>, val b: MvtExpression<*>) : MvtExpression<Double>
Link copied to clipboard
class Downcase(val child: MvtExpression<*>) : MvtExpression<String>
Link copied to clipboard
class Eq(val a: MvtExpression<*>, val b: MvtExpression<*>) : MvtExpression<Boolean>

Equality via Kotlin ==. Numbers compare by toDouble for cross-type matches.

Link copied to clipboard
class EvalContext(val zoom: Double, val properties: Map<String, Any?>, val featureState: Map<String, Any?>? = null, val geometryType: MvtGeometryType? = null, val lineProgress: Double = 0.0)

Tile-zoom + feature properties + optional feature state. Construct once per feature and frame. featureState surfaces dynamic per-feature data (hover/select highlight, pulse animation phase) — read by FeatureState expressions.

Link copied to clipboard
class FeatureState(val key: String) : MvtExpression<Any?>

["feature-state", key] — reads dynamic per-feature state set on the layer at runtime (typically for hover / select highlight). Looks up key in EvalContext.featureState (set per-feature via MvtVectorLayer.setFeatureState). Returns null when no state is set for the current feature.

Link copied to clipboard

["geometry-type"] — returns the feature's geometry type as one of "Point", "LineString", "Polygon", or "Unknown" (Mapbox spelling). Useful for filter expressions like ["==", ["geometry-type"], "Polygon"].

Link copied to clipboard
class Get(val key: String) : MvtExpression<Any?>
Link copied to clipboard
class Gt(val a: MvtExpression<*>, val b: MvtExpression<*>) : MvtExpression<Boolean>
Link copied to clipboard
class Gte(val a: MvtExpression<*>, val b: MvtExpression<*>) : MvtExpression<Boolean>
Link copied to clipboard
class Has(val key: String) : MvtExpression<Boolean>
Link copied to clipboard
class Interpolate<T>(val interpolation: MvtExpression.Interpolation, val input: MvtExpression<*>, val stops: List<Pair<Double, MvtExpression<T>>>, lerp: (T, T, Double) -> T) : MvtExpression<T>

Linear or exponential interpolation between typed stops. lerp supplies the per-T blend (lerp(a, b, t) where t ∈ 0, 1); standard implementations live in Interpolators.

Link copied to clipboard
sealed class Interpolation
Link copied to clipboard

Built-in lerp functions for the value types interpolation actually targets in MVT styling: floats and colors. Plugged into Interpolate's lerp parameter at construction.

Link copied to clipboard
class Length(val child: MvtExpression<*>) : MvtExpression<Double>

Length in characters of a string or number of items in an array.

Link copied to clipboard

["line-progress"] — arc-length parameter along the current line feature, in 0, 1. Read from EvalContext.lineProgress which the layer fills when sampling a line gradient at progress points along each line subdivision.

Link copied to clipboard
class Literal<T>(val value: T) : MvtExpression<T>
Link copied to clipboard
class Lt(val a: MvtExpression<*>, val b: MvtExpression<*>) : MvtExpression<Boolean>
Link copied to clipboard
class Lte(val a: MvtExpression<*>, val b: MvtExpression<*>) : MvtExpression<Boolean>
Link copied to clipboard
class Match<T>(val input: MvtExpression<*>, val branches: List<MvtExpression.Match.Branch<T>>, val default: MvtExpression<T>) : MvtExpression<T>

["match", input, label, value, label, value, …, default] — first label that equals the input wins. Labels can be a single value or a list (the latter is the variadic-label Mapbox extension).

Link copied to clipboard
class Mul(val operands: List<MvtExpression<*>>) : MvtExpression<Double>
Link copied to clipboard
class Neq(val a: MvtExpression<*>, val b: MvtExpression<*>) : MvtExpression<Boolean>
Link copied to clipboard
Link copied to clipboard
class Rgb(val r: MvtExpression<*>, val g: MvtExpression<*>, val b: MvtExpression<*>) : MvtExpression<Color>

["rgb", r, g, b] — RGB channels in 0..255; produces a Color with alpha = 1.

Link copied to clipboard
class Rgba(val r: MvtExpression<*>, val g: MvtExpression<*>, val b: MvtExpression<*>, val a: MvtExpression<*>) : MvtExpression<Color>

["rgba", r, g, b, a] — channels in 0..255 for RGB and 0..1 for alpha.

Link copied to clipboard
class Step<T>(val input: MvtExpression<*>, val base: MvtExpression<T>, val stops: List<Pair<Double, MvtExpression<T>>>) : MvtExpression<T>

["step", input, base, stop1, val1, stop2, val2, …] — piecewise-constant. Returns base when input < stops[0]; otherwise the value of the largest stop ≤ input.

Link copied to clipboard
class Sub(val operands: List<MvtExpression<*>>) : MvtExpression<Double>

Variadic difference (left-fold): a - b - c = (a - b) - c.

Link copied to clipboard
class ToBoolean(val child: MvtExpression<*>) : MvtExpression<Boolean>
Link copied to clipboard
class ToColor(val child: MvtExpression<*>, val parseColor: (String) -> Color?? = null) : MvtExpression<Color>

["to-color", value] — pass-through if child already evaluates to a Color; otherwise yields null. Mapbox also accepts a parseColor hex/css coercion of String inputs; pass parseColor to enable that path.

Link copied to clipboard
class ToNumber(val child: MvtExpression<*>) : MvtExpression<Double>
Link copied to clipboard
class ToString(val child: MvtExpression<*>) : MvtExpression<String>
Link copied to clipboard
class Upcase(val child: MvtExpression<*>) : MvtExpression<String>
Link copied to clipboard

Functions

Link copied to clipboard
abstract fun evaluate(ctx: MvtExpression.EvalContext): T?
Link copied to clipboard

True when this expression (or any sub-expression) reads per-feature data — i.e. its result can differ between two features in the same tile at the same zoom. That means a reference to a feature property (Get / Has), the feature's dynamic state (FeatureState), its geometry type (GeometryType), or its line-progress parameter (LineProgress).