Module: ol/expr/expression

ol/expr/expression


Type Definitions

ArgValidator()

参数验证器对编码表达式的参数应用各种检查, 并返回解析后的参数(如果有)。第二个参数是调用表达式的返回类型。

EncodedExpression{LiteralValue} {Array}

Expression{LiteralExpression} {CallExpression}

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

用于字面样式参数的基础类型;可以是数字字面量或运算符的输出, 运算符又接受 ExpressionValue 参数。

有关可用运算符的详细信息请参见下方(标注了仅限 WebGL 或 Canvas 的说明)。

读取运算符:

  • ['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. TileImage sources have 4 bands: red, green, blue and alpha. 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 (WebGL only).
  • ['get', attributeName] fetches a feature property value, similar to feature.get('attributeName').
  • ['get', attributeName, keyOrArrayIndex, ...] (Canvas only) Access nested properties and array items of a feature property. The result is undefined when there is nothing at the specified key or index.
  • ['geometry-type'] returns a feature's geometry type as string, either: 'LineString', 'Point' or 'Polygon' Multi* values are returned as their singular equivalent Circle geometries are returned as 'Polygon' GeometryCollection geometries are returned as the type of the first geometry found in the collection (WebGL only).
  • ['resolution'] returns the current resolution
  • ['time'] The time in seconds since the creation of the layer (WebGL only).
  • ['var', 'varName'] fetches a value from the style variables; will throw an error if that variable is undefined
  • ['zoom'] The current zoom level (WebGL only).
  • ['line-metric'] returns the M component of the current point on a line (WebGL only); in case where the geometry layout of the line does not contain an M component (e.g. XY or XYZ), 0 is returned; 0 is also returned for geometries other than lines. Please note that the M component will be linearly interpolated between the two points composing a segment.

数学运算符:

  • ['*', value1, value2, ...] 将值相乘(数字或颜色)

  • ['/', value1, value2]value1 除以 value2

  • ['+', value1, value2, ...] 将值相加

  • ['-', value1, value2]value1 中减去 value2

  • ['clamp', value, low, high]value 限制在 lowhigh 之间

  • ['%', value1, value2] 返回 value1 % value2 的结果(取模)

  • ['^', value1, value2] 返回 value1value2 次幂

  • ['abs', value1] 返回 value1 的绝对值

  • ['floor', value1] 返回小于或等于 value1 的最大整数

  • ['round', value1] 返回最接近 value1 的整数

  • ['ceil', value1] 返回大于或等于 value1 的最小整数

  • ['sin', value1] 返回 value1 的正弦值

  • ['cos', value1] 返回 value1 的余弦值

  • ['atan', value1, value2] 返回 atan2(value1, value2)。如果未提供 value2,则返回 atan(value1)

  • ['sqrt', value1] 返回 value1 的平方根

  • 变换运算符:

    • ['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.
    • ['string', value1, value2, ...] returns the first value in the list that evaluates to a string. An example would be to provide a default value for get: ['string', ['get', 'propertyname'], 'default value']] (Canvas only).
    • ['number', value1, value2, ...] returns the first value in the list that evaluates to a number. An example would be to provide a default value for get: ['string', ['get', 'propertyname'], 42]] (Canvas only).
    • ['coalesce', value1, value2, ...] returns the first value in the list which is not null or undefined. An example would be to provide a default value for get: ['coalesce', ['get','propertyname'], 'default value']] (Canvas only).
  • 逻辑运算符:

    • ['<', 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.
    • ['has', attributeName, keyOrArrayIndex, ...] returns true if feature properties include the (nested) key attributeName, false otherwise. Note that for WebGL layers, the hardcoded value -9999999 is used to distinguish when a property is not defined.
    • ['between', value1, value2, value3] returns true if value1 is contained between value2 and value3 (inclusively), or false otherwise.
    • ['in', needle, haystack] returns true if needle is found in haystack, and false otherwise. This operator has the following limitations:
      • haystack has 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 haystack for now; this means that haystack cannot be the result of an expression. If haystack is an array of strings, use the literal operator to disambiguate from an expression: ['literal', ['abc', 'def', 'ghi']] This works as well for number arrays although it is not required. Mixing types (numbers and strings) will produce undefined results.
  • 转换运算符:

    • ['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 (WebGL only).
    • ['color', red, green, blue, alpha] or ['color', shade, alpha] creates a color value from number values; the alpha parameter is optional; if not specified, it will be set to 1 (WebGL only). Note: red, green and blue or shade components must be values between 0 and 255; alpha between 0 and 1.
    • ['palette', index, colors] picks a color value from an array of colors using the given index; the index expression must evaluate to a number; the items in the colors 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 0-255 range and a in the 0-1 range) (WebGL only).
    • ['to-string', value] converts the input value to a string. If the input is a boolean, the result is "true" or "false". If the input is a number, it is converted to a string as specified by the "NumberToString" algorithm of the ECMAScript Language Specification. If the input is a color, it is converted to a string of the form "rgba(r,g,b,a)". (Canvas only)

值可以是字面量或另一个运算符,因为它们将被递归求值。 字面值可以是以下类型:

  • boolean
  • number
  • number[] (number arrays can only have a length of 2, 3 or 4)
  • string
  • Color

LiteralValue{boolean} {number} {string} {Array.<number>}

第二个参数是期望的类型。

ParsingContext{Object}

Properties:
Name Type Description
variables Set.<string>

使用 'var' 运算符引用的变量。

properties Set.<string>

使用 'get' 运算符引用的属性。

featureId boolean

样式使用了要素 ID。

geometryType boolean

样式使用了要素几何图形类型。

mCoordinate boolean

样式使用了几何图形的 M 坐标。

mapState boolean

样式使用了地图状态(视图状态或已用时间)。