Expressions
MapLibre expressions provide a powerful way to dynamically style map features based on their properties and zoom level. Expressions use a JSON array format to define operations that can manipulate data, perform calculations, and apply conditional logic.
1. Expression Types & Syntax 📝
Expressions are represented as JSON arrays where the first element is a string naming the expression operator, and subsequent elements are the arguments to the operator.
Basic Syntax
Types of Expressions
- Data expressions: Access feature properties
["get", "propertyName"] - Camera expressions: Access zoom level
["zoom"] - Composition expressions: Combine multiple expressions
- Type expressions: Assert or convert data types
- Math expressions: Perform mathematical operations
- String expressions: Manipulate text data
- Boolean expressions: Evaluate logical conditions
2. Data Expressions & Feature Properties 📊
Retrieves a property value from the current feature's properties. If the property is missing, returns the optional default value.
any (The property value)Usage Example: Accessing Feature Properties
"text-field": ["get", "name"],
// Get 'population' with a default of 0
"text-size": ["get", "population", 0]
Returns true if the specified property exists in the current feature's properties, false otherwise.
booleanUsage Example: Checking Property Existence
"text-field": [
"case",
["has", "name"],
["get", "name"],
""
]
Returns the entire properties object of the current feature.
objectUsage Example: Accessing All Properties
// in combination with other expressions
"text-field": ["get", "name"] // Better to use ["get", "name"] directly
3. Camera Expressions & Zoom Levels 🎥
Returns the current zoom level. Useful for creating zoom-dependent styling.
number (Current zoom level)Usage Example: Zoom-Dependent Styling
"circle-radius": [
"interpolate",
["linear"],
["zoom"],
0, 1,
10, 5,
20, 15
]
Produces continuous, smooth results by interpolating between pairs of input and output values.
number (Interpolated value)Usage Example: Smooth Zoom Interpolation
"line-width": [
"interpolate",
["linear"],
["zoom"],
0, 1,
10, 2,
15, 4,
20, 8
]
Produces discrete results by evaluating a piecewise-constant function defined by stops.
any (Step-based output)Usage Example: Discrete Zoom-Based Changes
"icon-size": [
"step",
["zoom"],
0.5,
10, 1,
15, 1.5,
20, 2
]
4. Composition Expressions 🧩
Binds values to named variables, which can then be referenced in the result expression.
any (Result of the final expression)Usage Example: Variable Binding
"text-field": [
"let", "area", ["/", ["get", "sq_meters"], 1000000],
["concat", "Area: ", ["to-string", ["var", "area"]], " km²"]
]
References a variable bound in the current scope using the "let" expression.
any (The variable's value)Usage Example: Using Bound Variables
"circle-radius": [
"let", "population", ["get", "population"],
"let", "scaled", ["/", ["var", "population"], 1000],
["sqrt", ["var", "scaled"]]
]
5. Type Expressions 🔧
["array", valueType]
Asserts that the input is an array of the specified value type. If the input is null or not an array, returns the provided default value or null.
array (The input as an array)Usage Example: Type Assertion
"circle-radius": [
"case",
[">", ["length", ["array", "number", ["get", "coordinates"]]], 0],
5,
0
]
Converts the input value to a string. If the input is null, returns null.
stringUsage Example: String Conversion
"text-field": [
"concat",
"Population: ",
["to-string", ["get", "population"]]
]
Converts the input value to a number, if possible. If conversion fails, returns the fallback value or null.
numberUsage Example: Numeric Conversion
"circle-radius": [
"to-number",
["get", "radius"],
5 // default if conversion fails
]
6. Math Expressions ➗
Returns the sum of the inputs.
number (Sum of inputs)Usage Example: Addition
"circle-radius": ["+", ["get", "base_radius"], 2]
For two inputs, returns the result of subtracting the second input from the first. For one input, returns the negation of the input.
number (Difference or negation)Usage Example: Subtraction
"text-field": [
"concat",
"Diff: ",
["to-string", ["-", ["get", "current"], ["get", "previous"]]]
]
Returns the square root of the input number.
number (Square root)Usage Example: Square Root Calculation
"circle-radius": [
"sqrt",
["/", ["get", "population"], 1000]
]
Returns the result of raising the first number to the power of the second number.
number (Base raised to exponent)Usage Example: Power Calculation
"circle-radius": ["^", 2, ["get", "scale_factor"]]
7. String Expressions 🔤
Returns a string consisting of the concatenation of the inputs. Non-string values will be converted to strings.
string (Concatenated result)Usage Example: String Concatenation
"text-field": [
"concat",
["get", "name"],
" (",
["to-string", ["get", "population"]],
")"
]
Returns the input string converted to uppercase.
string (Uppercase string)Usage Example: Uppercase Conversion
"text-field": ["upcase", ["get", "name"]]
Returns the input string converted to lowercase.
string (Lowercase string)Usage Example: Lowercase Conversion
"text-field": ["downcase", ["get", "category"]]
8. Boolean Expressions & Logic 🧠
Selects the first output whose corresponding condition evaluates to true.
any (Selected output)Usage Example: Conditional Styling
"circle-color": [
"case",
["==", ["get", "category"], "school"], "#ff0000",
["==", ["get", "category"], "hospital"], "#00ff00",
["==", ["get", "category"], "park"], "#0000ff",
"#cccccc" // default color
]
Selects the output whose label value matches the input, or the fallback if no match is found.
any (Matched output)Usage Example: Value Matching
"circle-color": [
"match",
["get", "category"],
"school", "#ff0000",
"hospital", "#00ff00",
"park", "#0000ff",
"#cccccc" // default color
]
Evaluates each expression in order until the first non-null value is obtained, and returns that value.
any (First non-null value)Usage Example: Fallback Values
"text-field": [
"coalesce",
["get", "name"],
["get", "title"],
["get", "id"],
"Unknown"
]