系列文章目录
Types
Lookup
Decision
Math
String
Color
Zoom
Heatmap
Feature data
Variable binding
Ramps, scales, curves
目录
前言
在上一篇中介绍了Expression的数学运算符操作,本篇对其他的方法进行简单介绍。
一、String
1. concat
/**
* concat
*返回由输入的串联组成的字符串。每个输入都将转换为字符串,就像通过to-string一样。
*
* ["concat", value, value, ...]: string
*/
Expression concat(@NonNull Expression... input)
Expression concat(@NonNull String... input)
eg:
SymbolLayer symbolLayer = new SymbolLayer("layer-id", "source-id");
symbolLayer.setProperties(
textField(concat("foo", "bar"))
);
2. upcase
/**
* upcase
* 返回转换为大写的输入字符串。遵循Unicode默认大小写转换算法和Unicode字符数据库中不区分语言环境的大小写映射。
*
* ["upcase", string]: string
*/
Expression upcase(@NonNull Expression string)
Expression upcase(@NonNull String string)
eg:
SymbolLayer symbolLayer = new SymbolLayer("layer-id", "source-id");
symbolLayer.setProperties(
textField(upcase("text"))
);
3.downcase
/**
* downcase
* 返回转换为小写的输入字符串。遵循Unicode默认大小写转换算法和Unicode字符数据库中不区分语言环境的大小写映射。
*
* ["downcase", string]: string
*/
Expression downcase(@NonNull Expression input)
Expression downcase(@NonNull String input)
eg:
SymbolLayer symbolLayer = new SymbolLayer("layer-id", "source-id");
symbolLayer.setProperties(
textField(upcase("key-to-string-value"))
);
二、Color
1.rgb
/**
* rgb
* 从红色,绿色和蓝色分量创建颜色值,该值必须在0到255之间,并且alpha分量为1。如果任何分量超出范围,则表达式为错误。
*/
Expression rgb(@NonNull Expression red, @NonNull Expression green, @NonNull Expression blue)
Expression rgb(@NonNull Number red, @NonNull Number green, @NonNull Number blue)
eg:
FillLayer fillLayer = new FillLayer("layer-id", "source-id");
fillLayer.setProperties(
fillColor(
rgb(255.0f, 255.0f, 255.0f)
)
);
2.rgba
/**
* rgba
* 从必须在0到255之间的红色,绿色,蓝色分量和必须在0到1之间的alpha分量创建颜色值。如果任何分量超出范围,则表达式为错误。
*/
Expression rgba(@NonNull Expression red, @NonNull Expression green,
@NonNull Expression blue, @NonNull Expression alpha)
Expression rgba(@NonNull Number red, @NonNull Number green,
@NonNull Number blue, @NonNull Number alpha)
eg:
FillLayer fillLayer = new FillLayer("layer-id", "source-id");
fillLayer.setProperties(
fillColor(
rgba(
literal(255.0f),
literal(255.0f),
literal(255.0f),
literal(1.0f)
)
)
);
3.toRgba
/**
* to-rgba
* 返回一个四元素的数组,该数组包含输入颜色的红色,绿色,蓝色和Alpha分量(按此顺序)。
*
* ["to-rgba", color]: array<number, 4>
*/
Expression toRgba(@NonNull Expression expression)
三、Zoom
/**
* 获取当前的缩放级别。请注意,在样式布局和绘画属性中,[“ zoom”]只能作为顶级“ step”或“ interpolate”表达式的输入出现。
* <p>
* ["zoom"]: number
*/
Expression zoom()
eg:
FillLayer fillLayer = new FillLayer("layer-id", "source-id");
fillLayer.setProperties(
fillColor(
interpolate(
exponential(0.5f), zoom(),
stop(1.0f, color(Color.RED)),
stop(5.0f, color(Color.BLUE)),
stop(10.0f, color(Color.GREEN))
)
)
);
四、heatmap-density
/**
* heatmap-density
* 获取热图图层中像素的内核密度估计,这是在特定像素周围拥挤多少数据点的相对度量。只能在heatmap-color属性中使用。
* <p>
* ["heatmap-density"]: number
*/
Expression heatmapDensity()
eg:
HeatmapLayer layer = new HeatmapLayer("layer-id", "source-id");
layer.setProperties(
heatmapColor(interpolate(linear(), heatmapDensity(),
literal(0), rgba(33, 102, 172, 0),
literal(0.2), rgb(103, 169, 207),
literal(0.4), rgb(209, 229, 240),
literal(0.6), rgb(253, 219, 199),
literal(0.8), rgb(239, 138, 98),
literal(1), rgb(178, 24, 43))
)
);
五、FeatureData
1.accumulated
/**
* accumulated
* 获取到目前为止累积的集群属性的值。只能在集群化GeoJSON源的clusterProperties选项中使用。
*
* ["accumulated"]: value
*/
Expression accumulated()
eg:
GeoJsonOptions options = new GeoJsonOptions()
.withCluster(true)
.withClusterProperty("max", max(accumulated(), get("max")).toArray(), get("mag").toArray());
2.geometry-type
/**
* geometry-type
* 获取要素的几何类型: Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon.
*
* ["geometry-type"]: string
*/
Expression geometryType()
eg:
SymbolLayer symbolLayer = new SymbolLayer("layer-id", "source-id");
symbolLayer.setProperties(
textField(concat(get("key-to-value"), literal(" "), geometryType()))
);
3.id
/**
* id
* 获取feature的ID(如果有的话)。
*
* ["id"]: value
*/
Expression id()
eg:
SymbolLayer idSymbolLayer = new SymbolLayer("layer-id", "source-id");
idSymbolLayer.setProperties(
textField(id())
);
4.line-progress
/**
* line-progress
* 获取沿渐变线的进度。只能在line-gradient属性中使用。
*
* ["line-progress"]: number
*/
Expression lineProgress()
eg:
LineLayer layer = new LineLayer("layer-id", "source-id");
layer.setProperties(
lineGradient(interpolate(
linear(), lineProgress(),
stop(0f, rgb(0, 0, 255)),
stop(0.5f, rgb(0, 255, 0)),
stop(1f, rgb(255, 0, 0)))
)
);
5.properties
/**
* properties
* 获取要素属性对象。请注意,在某些情况下,直接使用[“ get”,“ property_name”]可能更有效。
*
* ["properties"]: object
*/
Expression properties()
eg:
SymbolLayer propertiesSymbolLayer = new SymbolLayer("layer-id", "source-id");
propertiesSymbolLayer.setProperties(
textField(get("key-to-value", properties()))
);
六、Variable binding(无示例)
1.let
/**
* let
* 将表达式绑定到命名变量,然后可以使用[“ var”,“ variable_name”]在结果表达式中引用它们。
*
* ["let",
* string (alphanumeric literal), any, string (alphanumeric literal), any, ...,
* OutputType
* ]: OutputType
*/
2.var
/**
* let
* 将表达式绑定到命名变量,然后可以使用[“ var”,“ variable_name”]在结果表达式中引用它们。
*
* ["let",
* string (alphanumeric literal), any, string (alphanumeric literal), any, ...,
* OutputType
* ]: OutputType
*/
七、Ramps、scales、curves
1.interpolate
/**
* interpolate
* 通过在成对的输入和输出值(“停止”)之间进行插值来产生连续,平滑的结果。
* 输入可以是任何数字表达式(例如[“ get”,“ population”])。
* 停止输入必须是严格按升序排列的数字文字。
* 输出类型必须为数字,数组<数字>或颜色。
*
*插补类型:
*
* ["linear"]: 在这两个停止点之间线性地插值,该插值刚好小于输入,而刚大于输入。
* ["exponential", base]: 在停靠点之间以指数方式插值,该插值刚好小于输入并大于输入。
* base控制输出增加的速率:值越高,输出越接近范围的高端。值接近1时,输出线性增加。
* ["cubic-bezier", x1, y1, x2, y2]: 使用给定控制点定义的三次贝塞尔曲线进行插值。
*
* ["interpolate",
* interpolation: ["linear"] | ["exponential", base] | ["cubic-bezier", x1, y1, x2, y2],
* input: number,
* stop_input_1: number, stop_output_1: OutputType,
* stop_input_n: number, stop_output_n: OutputType, ...
* ]: OutputType (number, array<number>, or Color)
*/
Expression interpolate(@NonNull Interpolator interpolation,
@NonNull Expression number, @NonNull Expression... stops)
Expression interpolate(@NonNull Interpolator interpolation,
@NonNull Expression number, Stop... stops)
eg:
FillLayer fillLayer = new FillLayer("layer-id", "source-id");
fillLayer.setProperties(
fillColor(
interpolate(
exponential(0.5f), zoom(),
stop(1.0f, color(Color.RED)),
stop(5.0f, color(Color.BLUE)),
stop(10.0f, color(Color.GREEN))
)
)
);
2.step
/**
*通过评估由成对的*输入和输出值(\“ stops \”)定义的分段常数函数,可以产生离散的步进结果。 “输入”可以是任何数字表达式(例如,“ [\“ get \”,\“人口\”]“)。
*停止输入必须是严格按升序排列的数字文字。
*返回止动点的输出值,该止动点仅小于输入值;如果输入小于第一个止动点,则返回第一个输入。
*
* @param input the input value
* @param defaultOutput the default output expression
* @param stops pair of input and output values
*/
Expression step(@NonNull Number input, @NonNull Expression defaultOutput, Expression... stops)
Expression step(@NonNull Expression input, @NonNull Expression defaultOutput,
@NonNull Expression... stops)
Expression step(@NonNull Number input, @NonNull Expression defaultOutput, Stop... stops)
Expression step(@NonNull Expression input, @NonNull Expression defaultOutput, Stop... stops)
Expression step(@NonNull Number input, @NonNull Number defaultOutput, Expression... stops)
Expression step(@NonNull Expression input, @NonNull Number defaultOutput, Expression... stops)
Expression step(@NonNull Number input, @NonNull Number defaultOutput, Stop... stops)
Expression step(@NonNull Expression input, @NonNull Number defaultOutput, Stop... stops)
eg:
CircleLayer circleLayer = new CircleLayer("layer-id", "source-id");
circleLayer.setProperties(
circleRadius(
step(zoom(), literal(0.0f),
literal(1.0f), literal(2.5f),
literal(10.0f), literal(5.0f))
)
);
}
总结
-
本篇是最后一篇,经过这几篇的介绍,你应该已经了解Expression其实并不复杂,它只是在固定场景有些固定用法,固定的方法只适用于固定的layer。