Operators and utilities used for style expressions
Members

module:ol/style/expressions.ValueTypes{number}

Possible inferred types from a given value or expression. Note: these are binary flags.
Type Definitions

ExpressionValue{Array.<*>} {Color} {string} {number} {boolean}

Base type used for literal style parameters; can be a number literal or the output of an operator, which in turns takes
ExpressionValue
arguments.The following operators can be used:
Reading operators:
['band', bandIndex, xOffset, yOffset]
For tile layers only. Fetches pixel values from bandbandIndex
of the source's data. The firstbandIndex
of the source data is1
. Fetched values are in the 0..1 range.TileImage
sources have 4 bands: red, green, blue and alpha.DataTileSource
sources can have any number of bands, depending on the underlying data source andconfiguration
.xOffset
andyOffset
are optional and allow specifying pixel offsets for x and y. This is used for sampling data from neighboring pixels.['get', 'attributeName']
fetches a feature attribute (it will be prefixed bya_
in the shader) Note: those will be taken from the attributes provided to the renderer['resolution']
returns the current resolution['time']
returns the time in seconds since the creation of the layer['var', 'varName']
fetches a value from the style variables, or 0 if undefined['zoom']
returns the current zoom level
Math operators:
['*', value1, value2]
multipliesvalue1
byvalue2
['/', value1, value2]
dividesvalue1
byvalue2
['+', value1, value2]
addsvalue1
andvalue2
['', value1, value2]
subtractsvalue2
fromvalue1
['clamp', value, low, high]
clampsvalue
betweenlow
andhigh
['%', value1, value2]
returns the result ofvalue1 % value2
(modulo)['^', value1, value2]
returns the value ofvalue1
raised to thevalue2
power['abs', value1]
returns the absolute value ofvalue1
['floor', value1]
returns the nearest integer less than or equal tovalue1
['round', value1]
returns the nearest integer tovalue1
['ceil', value1]
returns the nearest integer greater than or equal tovalue1
['sin', value1]
returns the sine ofvalue1
['cos', value1]
returns the cosine ofvalue1
['atan', value1, value2]
returnsatan2(value1, value2)
. Ifvalue2
is not provided, returnsatan(value1)
Transform operators:
['case', condition1, output1, ...conditionN, outputN, fallback]
selects the first output whose corresponding condition evaluates totrue
. If no match is found, returns thefallback
value. All conditions should beboolean
, output and fallback can be any kind.['match', input, match1, output1, ...matchN, outputN, fallback]
compares theinput
value against all providedmatchX
values, returning the output associated with the first valid match. If no match is found, returns thefallback
value.input
andmatchX
values must all be of the same type, and can benumber
orstring
.outputX
andfallback
values must be of the same type, and can be of any kind.['interpolate', interpolation, input, stop1, output1, ...stopN, outputN]
returns a value by interpolating between pairs of inputs and outputs;interpolation
can either be['linear']
or['exponential', base]
wherebase
is the rate of increase from stop A to stop B (i.e. power to which the interpolation ratio is raised); a value of 1 is equivalent to['linear']
.input
andstopX
values must all be of typenumber
.outputX
values can benumber
orcolor
values. Note:input
will be clamped betweenstop1
andstopN
, meaning that all output values will be comprised betweenoutput1
andoutputN
.
Logical operators:
['<', value1, value2]
returnstrue
ifvalue1
is strictly lower thanvalue2
, orfalse
otherwise.['<=', value1, value2]
returnstrue
ifvalue1
is lower than or equalsvalue2
, orfalse
otherwise.['>', value1, value2]
returnstrue
ifvalue1
is strictly greater thanvalue2
, orfalse
otherwise.['>=', value1, value2]
returnstrue
ifvalue1
is greater than or equalsvalue2
, orfalse
otherwise.['==', value1, value2]
returnstrue
ifvalue1
equalsvalue2
, orfalse
otherwise.['!=', value1, value2]
returnstrue
ifvalue1
does not equalvalue2
, orfalse
otherwise.['!', value1]
returnsfalse
ifvalue1
istrue
or greater than0
, ortrue
otherwise.['all', value1, value2, ...]
returnstrue
if all the inputs aretrue
,false
otherwise.['any', value1, value2, ...]
returnstrue
if any of the inputs aretrue
,false
otherwise.['between', value1, value2, value3]
returnstrue
ifvalue1
is contained betweenvalue2
andvalue3
(inclusively), orfalse
otherwise.
Conversion operators:
['array', value1, ...valueN]
creates a numerical array fromnumber
values; please note that the amount of values can currently only be 2, 3 or 4.['color', red, green, blue, alpha]
creates acolor
value fromnumber
values; thealpha
parameter is optional; if not specified, it will be set to 1. Note:red
,green
andblue
components must be values between 0 and 255;alpha
between 0 and 1.['palette', index, colors]
picks acolor
value from an array of colors using the given index; theindex
expression must evaluate to a number; the items in thecolors
array must be strings with hex colors (e.g.'#86A136'
), colors using the rgba[a] functional notation (e.g.'rgb(134, 161, 54)'
or'rgba(134, 161, 54, 1)'
), named colors (e.g.'red'
), or array literals with 3 ([r, g, b]) or 4 ([r, g, b, a]) values (with r, g, and b in the 0255 range and a in the 01 range).
Values can either be literals or another operator, as they will be evaluated recursively. Literal values can be of the following types:
boolean
number
string
Color

Operator{Object}

An operator declaration must contain two methods:
getReturnType
which returns a type based on the operator arguments, andtoGlsl
which returns a GLSLcompatible string. Note: both methods can process arguments recursively.Properties:
Name Type Description getReturnType
function Returns one or several types
toGlsl
function Returns a GLSLcompatible string Note: takes in an optional type hint as 3rd parameter

ParsingContext{Object}

Context available during the parsing of an expression.
Properties:
Name Type Argument Description inFragmentShader
boolean <optional>
If false, means the expression output should be made for a vertex shader
variables
Array.<string> List of variables used in the expression; contains unprefixed names
attributes
Array.<string> List of attributes used in the expression; contains unprefixed names
stringLiteralsMap
Object.<string, number> This object maps all encountered string values to a number
functions
Object.<string, string> Lookup of functions used by the style.
bandCount
number <optional>
Number of bands per pixel.
paletteTextures
Array <optional>
List of palettes used by the style.