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
ExpressionValuearguments.The following operators can be used:
Reading operators:
['band', bandIndex, xOffset, yOffset]For tile layers only. Fetches pixel values from bandbandIndexof the source's data. The firstbandIndexof the source data is1. Fetched values are in the 0..1 range.TileImagesources have 4 bands: red, green, blue and alpha.DataTileSourcesources can have any number of bands, depending on the underlying data source andconfiguration.xOffsetandyOffsetare optional and allow specifying pixel offsets for x and y. This is used for sampling data from neighboring pixels.['get', 'attributeName', typeHint]fetches a feature property value, similar tofeature.get('attributeName')A type hint can optionally be specified, in case the resulting expression contains a type ambiguity which will make it invalid. Type hints can be one of: 'string', 'color', 'number', 'boolean', 'number[]'['geometry-type']returns a feature's geometry type as string, either: 'LineString', 'Point' or 'Polygon'Multi*values are returned as their singular equivalentCirclegeometries are returned as 'Polygon'GeometryCollectiongeometries are returned as the type of the first geometry found in the collection['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; will throw an error if that variable is undefined['zoom']returns the current zoom level
Math operators:
['*', value1, value2, ...]multiplies the values (either numbers or colors)['/', value1, value2]dividesvalue1byvalue2['+', value1, value2, ...]adds the values['-', value1, value2]subtractsvalue2fromvalue1['clamp', value, low, high]clampsvaluebetweenlowandhigh['%', value1, value2]returns the result ofvalue1 % value2(modulo)['^', value1, value2]returns the value ofvalue1raised to thevalue2power['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). Ifvalue2is not provided, returnsatan(value1)['sqrt', value1]returns the square root ofvalue1
Transform operators:
['case', condition1, output1, ...conditionN, outputN, fallback]selects the first output whose corresponding condition evaluates totrue. If no match is found, returns thefallbackvalue. All conditions should beboolean, output and fallback can be any kind.['match', input, match1, output1, ...matchN, outputN, fallback]compares theinputvalue against all providedmatchXvalues, returning the output associated with the first valid match. If no match is found, returns thefallbackvalue.inputandmatchXvalues must all be of the same type, and can benumberorstring.outputXandfallbackvalues 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;interpolationcan either be['linear']or['exponential', base]wherebaseis 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'].inputandstopXvalues must all be of typenumber.outputXvalues can benumberorcolorvalues. Note:inputwill be clamped betweenstop1andstopN, meaning that all output values will be comprised betweenoutput1andoutputN.
Logical operators:
['<', value1, value2]returnstrueifvalue1is strictly lower thanvalue2, orfalseotherwise.['<=', value1, value2]returnstrueifvalue1is lower than or equalsvalue2, orfalseotherwise.['>', value1, value2]returnstrueifvalue1is strictly greater thanvalue2, orfalseotherwise.['>=', value1, value2]returnstrueifvalue1is greater than or equalsvalue2, orfalseotherwise.['==', value1, value2]returnstrueifvalue1equalsvalue2, orfalseotherwise.['!=', value1, value2]returnstrueifvalue1does not equalvalue2, orfalseotherwise.['!', value1]returnsfalseifvalue1istrueor greater than0, ortrueotherwise.['all', value1, value2, ...]returnstrueif all the inputs aretrue,falseotherwise.['any', value1, value2, ...]returnstrueif any of the inputs aretrue,falseotherwise.['between', value1, value2, value3]returnstrueifvalue1is contained betweenvalue2andvalue3(inclusively), orfalseotherwise.['in', needle, haystack]returnstrueifneedleis found inhaystack, andfalseotherwise. This operator has the following limitations:haystackhas to be an array of numbers or strings (searching for a substring in a string is not supported yet)- Only literal arrays are supported as
haystackfor now; this means thathaystackcannot be the result of an expression. Ifhaystackis an array of strings, use theliteraloperator to disambiguate from an expression:['literal', ['abc', 'def', 'ghi']]
Conversion operators:
['array', value1, ...valueN]creates a numerical array fromnumbervalues; please note that the amount of values can currently only be 2, 3 or 4.['color', red, green, blue, alpha]creates acolorvalue fromnumbervalues; thealphaparameter is optional; if not specified, it will be set to 1. Note:red,greenandbluecomponents must be values between 0 and 255;alphabetween 0 and 1.['palette', index, colors]picks acolorvalue from an array of colors using the given index; theindexexpression must evaluate to a number; the items in thecolorsarray 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 0-255 range and a in the 0-1 range).
Values can either be literals or another operator, as they will be evaluated recursively. Literal values can be of the following types:
booleannumbernumber[](number arrays can only have a length of 2, 3 or 4)stringColor
-
Operator{Object}
-
An operator declaration must contain two methods:
getReturnTypewhich returns a type based on the operator arguments, andtoGlslwhich returns a GLSL-compatible string. Note: both methods can process arguments recursively.Properties:
Name Type Description getReturnTypefunction Returns one or several types
toGlslfunction Returns a GLSL-compatible string given a parsing context, an array of arguments and an expected type. Note: the expected type can be a combination such as ValueTypes.NUMBER | ValueTypes.STRING or ValueTypes.ANY for instance
-
ParsingContext{Object}
-
Context available during the parsing of an expression.
Properties:
Name Type Description inFragmentShaderboolean | undefined If false, means the expression output should be made for a vertex shader
variablesArray<ParsingContextExternal> External variables used in the expression
attributesArray<ParsingContextExternal> External attributes used in the expression
functionsObject.<string, string> Lookup of functions used by the style.
bandCountnumber | undefined Number of bands per pixel.
paletteTexturesArray<PaletteTexture> | undefined List of palettes used by the style.
styleLiteralStyle The style being parsed
-
ParsingContextExternal{Object}
-
Properties:
Name Type Description namestring Name, unprefixed
typeValueTypes One of the value types constants
callbackfunction | undefined Function used for computing the attribute value; if undefined,
feature.get(attribute.name)will be used