Skip to content

Commit 86f670e

Browse files
committed
feat: Select expresssions
Add `Expr::Select` variant and support conversion of MapLibre `Step` expression into Galileo `Select`.
1 parent e4c7b2e commit 86f670e

4 files changed

Lines changed: 57 additions & 2 deletions

File tree

galileo-maplibre/src/layer/vector_tile.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ fn fill_rule(fill: &FillLayer, tile_schema: &TileSchema) -> Option<StyleRule> {
235235
let fill_opacity = &fill.paint.fill_opacity;
236236
let color = get_color_value(fill_color, fill_opacity)?;
237237

238-
if fill.paint.fill_antialias == false {
238+
if !fill.paint.fill_antialias {
239239
log::debug!(
240240
"{} not-antialised polygons are not supported yet",
241241
crate::layer::UNSUPPORTED,

galileo-maplibre/src/style/expression.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use std::fmt;
2424

2525
use galileo::expr::{
2626
ControlPoint, CubicBezierInterpolation, ExponentialInterpolation, Expr, ExprGeometryType,
27-
ExprValue, LinearInterpolation, MatchCase, MatchExpr,
27+
ExprValue, LinearInterpolation, MatchCase, MatchExpr, SelectCase, SelectExpr,
2828
};
2929
use serde::de::{self, SeqAccess, Visitor};
3030
use serde::{Deserialize, Deserializer, Serialize};
@@ -883,6 +883,35 @@ impl MlExpr {
883883
.collect::<Option<Vec<_>>>()?,
884884
fallback: fallback.to_galileo_expr()?,
885885
})),
886+
MlExpr::Step {
887+
input,
888+
default_output,
889+
stops,
890+
} => {
891+
let last_stop = stops.iter().last()?;
892+
let mut cases = vec![];
893+
let boxed_input = Box::new(input.to_galileo_expr()?);
894+
895+
let mut prev_out = default_output.to_galileo_expr()?;
896+
for stop in stops {
897+
cases.push(SelectCase {
898+
condition: Expr::Lte(boxed_input.clone(), Box::new(stop.0.into())),
899+
out: prev_out,
900+
});
901+
902+
prev_out = stop.1.to_galileo_expr()?;
903+
}
904+
905+
cases.push(SelectCase {
906+
condition: Expr::Gte(boxed_input.clone(), Box::new(last_stop.0.into())),
907+
out: prev_out,
908+
});
909+
910+
Expr::Select(Box::new(SelectExpr {
911+
cases,
912+
fallback: default_output.to_galileo_expr()?,
913+
}))
914+
}
886915
_ => {
887916
log::debug!("{UNSUPPORTED} Expression {self:?} is not supported yet");
888917
return None;

galileo/src/expr/match_expr.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,30 @@ use serde::{Deserialize, Serialize};
22

33
use crate::expr::{Expr, ExprFeature, ExprValue, ExprView};
44

5+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
6+
pub struct SelectExpr {
7+
pub cases: Vec<SelectCase>,
8+
pub fallback: Expr,
9+
}
10+
11+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
12+
pub struct SelectCase {
13+
pub condition: Expr,
14+
pub out: Expr,
15+
}
16+
17+
impl SelectExpr {
18+
pub fn eval<'a>(&'a self, f: &'a impl ExprFeature, v: ExprView) -> ExprValue<'a> {
19+
for case in &self.cases {
20+
if case.condition.eval(f, v).to_bool() {
21+
return case.out.eval(f, v);
22+
}
23+
}
24+
25+
self.fallback.eval(f, v)
26+
}
27+
}
28+
529
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
630
pub struct MatchExpr {
731
pub input: Expr,

galileo/src/expr/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub enum Expr {
4747
CubicBezier(Box<CubicBezierInterpolation>),
4848

4949
Match(Box<MatchExpr>),
50+
Select(Box<SelectExpr>),
5051

5152
WithOpacity(WithOpacityExpr),
5253

@@ -229,6 +230,7 @@ impl Expr {
229230
Expr::Exponential(ip) => ip.eval(f, v),
230231
Expr::CubicBezier(ip) => ip.eval(f, v),
231232
Expr::Match(m) => m.eval(f, v),
233+
Expr::Select(s) => s.eval(f, v),
232234
Expr::WithOpacity(wo) => wo.eval(f, v),
233235
}
234236
}

0 commit comments

Comments
 (0)