Module: ol/style/expressions

ol/style/expressions


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.<*>} {module:ol/color~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 module:ol/style/expressions~ExpressionValue arguments.

The following operators can be used:

  • Reading operators:

    • ['band', bandIndex, xOffset, yOffset] For tile layers only. Fetches pixel values from band bandIndex of the source's data. The first bandIndex of the source data is 1. Fetched values are in the 0..1 range. module:ol/source/TileImage~TileImage sources have 4 bands: red, green, blue and alpha. module:ol/source/DataTile~DataTileSource sources can have any number of bands, depending on the underlying data source and configuration. xOffset and yOffset 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 by a_ 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] multiplies value1 by value2
    • ['/', value1, value2] divides value1 by value2
    • ['+', value1, value2] adds value1 and value2
    • ['-', value1, value2] subtracts value2 from value1
    • ['clamp', value, low, high] clamps value between low and high
    • ['%', value1, value2] returns the result of value1 % value2 (modulo)
    • ['^', value1, value2] returns the value of value1 raised to the value2 power
    • ['abs', value1] returns the absolute value of value1
    • ['sin', value1] returns the sine of value1
    • ['cos', value1] returns the cosine of value1
    • ['atan', value1, value2] returns atan2(value1, value2). If value2 is not provided, returns atan(value1)
  • Transform operators:

    • ['case', condition1, output1, ...conditionN, outputN, fallback] selects the first output whose corresponding condition evaluates to true. If no match is found, returns the fallback value. All conditions should be boolean, output and fallback can be any kind.
    • ['match', input, match1, output1, ...matchN, outputN, fallback] compares the input value against all provided matchX values, returning the output associated with the first valid match. If no match is found, returns the fallback value. input and matchX values must all be of the same type, and can be number or string. outputX and fallback 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] where base 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 and stopX values must all be of type number. outputX values can be number or color values. Note: input will be clamped between stop1 and stopN, meaning that all output values will be comprised between output1 and outputN.
  • Logical operators:

    • ['<', value1, value2] returns true if value1 is strictly lower than value2, or false otherwise.
    • ['<=', value1, value2] returns true if value1 is lower than or equals value2, or false otherwise.
    • ['>', value1, value2] returns true if value1 is strictly greater than value2, or false otherwise.
    • ['>=', value1, value2] returns true if value1 is greater than or equals value2, or false otherwise.
    • ['==', value1, value2] returns true if value1 equals value2, or false otherwise.
    • ['!=', value1, value2] returns true if value1 does not equal value2, or false otherwise.
    • ['!', value1] returns false if value1 is true or greater than 0, or true otherwise.
    • ['all', value1, value2, ...] returns true if all the inputs are true, false otherwise.
    • ['any', value1, value2, ...] returns true if any of the inputs are true, false otherwise.
    • ['between', value1, value2, value3] returns true if value1 is contained between value2 and value3 (inclusively), or false otherwise.
  • Conversion operators:

    • ['array', value1, ...valueN] creates a numerical array from number values; please note that the amount of values can currently only be 2, 3 or 4.
    • ['color', red, green, blue, alpha] creates a color value from number values; the alpha parameter is optional; if not specified, it will be set to 1. Note: red, green and blue components must be values between 0 and 255; alpha between 0 and 1.

Values can either be literals or another operator, as they will be evaluated recursively. Literal values can be of the following types:

Operator{Object}

An operator declaration must contain two methods: getReturnType which returns a type based on the operator arguments, and toGlsl which returns a GLSL-compatible string. Note: both methods can process arguments recursively.

Properties:
Name Type Description
getReturnType function

Returns one or several types

toGlsl function

Returns a GLSL-compatible 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

bandCount number <optional>

Number of bands per pixel.