Skip to content

Commit e2e5338

Browse files
committed
feat: Support rendering lines with dasharrays
Dasharray property is added ot the VT sytle and maplibre layers using it are correctly converted.
1 parent 552923b commit e2e5338

11 files changed

Lines changed: 229 additions & 58 deletions

File tree

galileo-maplibre/src/layer/vector_tile.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -263,14 +263,6 @@ fn fill_rule(fill: &FillLayer, tile_schema: &TileSchema) -> Option<StyleRule> {
263263

264264
/// Converts a [`LineLayer`] to a [`StyleRule`], or logs and returns `None` if unsupported.
265265
fn line_rule(line: &LineLayer, tile_schema: &TileSchema) -> Option<StyleRule> {
266-
if line.paint.line_dasharray.is_some() {
267-
log::debug!(
268-
"{UNSUPPORTED} Line dasharray is not supported yet; skipping layer {}",
269-
line.id
270-
);
271-
return None;
272-
}
273-
274266
log_unsupported_field!(line.paint.line_blur);
275267
log_unsupported_field!(line.paint.line_gap_width);
276268
log_unsupported_field!(line.paint.line_gradient);
@@ -306,12 +298,14 @@ fn line_rule(line: &LineLayer, tile_schema: &TileSchema) -> Option<StyleRule> {
306298
.minzoom
307299
.and_then(|lod| tile_schema.lod_resolution(lod.round() as u32));
308300
let filter = line.filter.as_ref().and_then(|v| v.to_galileo_expr());
301+
let dasharray = line.paint.line_dasharray.clone();
309302

310303
Some(StyleRule {
311304
layer_name: Some(source_layer),
312305
symbol: VectorTileSymbol::Line(VectorTileLineSymbol {
313306
width,
314307
stroke_color: color,
308+
dasharray,
315309
}),
316310
min_resolution,
317311
max_resolution,

galileo-maplibre/src/style/layer/line.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub struct LinePaint {
4848

4949
/// Dash pattern for the line. Supports expressions.
5050
#[serde(rename = "line-dasharray", skip_serializing_if = "Option::is_none")]
51-
pub line_dasharray: Option<Value>,
51+
pub line_dasharray: Option<Vec<f32>>,
5252

5353
/// Gap width for a casing effect. Supports expressions.
5454
#[serde(rename = "line-gap-width", skip_serializing_if = "Option::is_none")]

galileo/src/layer/feature_layer/symbol/contour.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ impl<F> Symbol<F> for SimpleContourSymbol {
3636
width: self.width,
3737
offset: 0.0,
3838
line_cap: LineCap::Butt,
39+
dasharray: None,
3940
};
4041

4142
match geometry {

galileo/src/layer/feature_layer/symbol/polygon.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ impl SimplePolygonSymbol {
7878
width: self.stroke_width,
7979
offset: self.stroke_offset,
8080
line_cap: LineCap::Butt,
81+
dasharray: None,
8182
};
8283

8384
for contour in polygon.iter_contours() {

galileo/src/layer/vector_tile_layer/builder.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ impl VectorTileLayerBuilder {
498498
symbol: VectorTileSymbol::Line(VectorTileLineSymbol {
499499
width: 1.0.into(),
500500
stroke_color: Color::BLACK.into(),
501+
dasharray: None,
501502
}),
502503
},
503504
StyleRule {

galileo/src/layer/vector_tile_layer/style.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,15 +172,21 @@ pub struct VectorTileLineSymbol {
172172
pub width: NumExpr,
173173
/// Color of the line in pixels.
174174
pub stroke_color: ColorExpr,
175+
/// Parameters of dash array for the line.
176+
///
177+
/// Sets length of "dash - gap - dash - ..." of widths of the line. If the specification contains not even number of
178+
/// values, the whole pattern is repeated twice when applied.
179+
pub dasharray: Option<Vec<f32>>,
175180
}
176181

177182
impl VectorTileLineSymbol {
178-
pub(crate) fn to_paint(&self, feature: &MvtFeature, view: ExprView) -> Option<LinePaint> {
183+
pub(crate) fn to_paint(&self, feature: &MvtFeature, view: ExprView) -> Option<LinePaint<'_>> {
179184
Some(LinePaint {
180185
color: self.stroke_color.eval(feature, view)?,
181186
width: self.width.eval(feature, view)?,
182187
offset: 0.0,
183188
line_cap: LineCap::Butt,
189+
dasharray: self.dasharray.as_deref(),
184190
})
185191
}
186192
}

galileo/src/layer/vector_tile_layer/tile_provider/vt_processor.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,11 @@ impl VtProcessor {
196196
))
197197
}
198198

199-
fn get_line_symbol(
200-
rule: &StyleRule,
199+
fn get_line_symbol<'a>(
200+
rule: &'a StyleRule,
201201
feature: &MvtFeature,
202202
view: ExprView,
203-
) -> Option<LinePaint> {
203+
) -> Option<LinePaint<'a>> {
204204
rule.symbol.line().and_then(|s| s.to_paint(feature, view))
205205
}
206206

galileo/src/render/mod.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use render_bundle::RenderBundle;
1212
use serde::{Deserialize, Serialize};
1313

1414
use crate::Color;
15+
use crate::render::render_bundle::world_set::{DashArray, LineParameters};
1516

1617
#[cfg(feature = "wgpu")]
1718
mod wgpu;
@@ -101,8 +102,8 @@ pub struct PolygonPaint {
101102
}
102103

103104
/// Parameter to draw a line primitive with.
104-
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
105-
pub struct LinePaint {
105+
#[derive(Debug, Clone)]
106+
pub struct LinePaint<'a> {
106107
/// Color of the line.
107108
pub color: Color,
108109
/// Width of the line in pixels.
@@ -112,6 +113,26 @@ pub struct LinePaint {
112113
pub offset: f64,
113114
/// Type of the cap of the line.
114115
pub line_cap: LineCap,
116+
/// Parameters of dash array for the line.
117+
///
118+
/// Sets length of "dash - gap - dash - ..." of widths of the line. If the specification contains not even number of
119+
/// values, the whole pattern is repeated twice when applied.
120+
pub dasharray: Option<&'a [f32]>,
121+
}
122+
123+
impl LinePaint<'_> {
124+
pub(crate) fn line_parameters(&self) -> LineParameters {
125+
LineParameters {
126+
color: self.color,
127+
width: self.width as f32,
128+
offset: self.offset as f32,
129+
cap: self.line_cap,
130+
}
131+
}
132+
133+
pub(crate) fn dasharray(&self) -> Option<DashArray<'_>> {
134+
self.dasharray.as_ref().map(|v| DashArray(v))
135+
}
115136
}
116137

117138
/// Cap (end point) style of the line.

galileo/src/render/point_paint.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::render::text::TextStyle;
1313
use crate::render::{LineCap, LinePaint};
1414

1515
/// Specifies the way a point should be drawn to the map.
16-
#[derive(Debug, Clone, Serialize, Deserialize)]
16+
#[derive(Debug, Clone)]
1717
pub struct PointPaint<'a> {
1818
pub(crate) shape: PointShape<'a>,
1919
pub(crate) offset: Vector2<f32>,
@@ -36,13 +36,15 @@ impl<'a> PointPaint<'a> {
3636
pub fn sector(color: Color, diameter: f32, start_angle: f32, end_angle: f32) -> Self {
3737
Self {
3838
offset: Vector2::default(),
39-
shape: PointShape::Sector(SectorParameters {
40-
fill: color.into(),
41-
radius: diameter / 2.0,
42-
start_angle,
43-
end_angle,
39+
shape: PointShape::Sector {
40+
parameters: SectorParameters {
41+
fill: color.into(),
42+
radius: diameter / 2.0,
43+
start_angle,
44+
end_angle,
45+
},
4446
outline: None,
45-
}),
47+
},
4648
}
4749
}
4850

@@ -112,6 +114,7 @@ impl<'a> PointPaint<'a> {
112114
width: width as f64,
113115
offset: 0.0,
114116
line_cap: LineCap::Round,
117+
dasharray: None,
115118
})
116119
}
117120
_ => {}
@@ -133,27 +136,29 @@ impl<'a> PointPaint<'a> {
133136
}
134137
}
135138

136-
#[derive(Debug, Clone, Serialize, Deserialize)]
137-
#[serde(tag = "type")]
139+
#[derive(Debug, Clone)]
138140
pub(crate) enum PointShape<'a> {
139141
Dot {
140142
color: Color,
141143
},
142144
Circle {
143145
fill: CircleFill,
144146
radius: f32,
145-
outline: Option<LinePaint>,
147+
outline: Option<LinePaint<'a>>,
148+
},
149+
Sector {
150+
parameters: SectorParameters,
151+
outline: Option<LinePaint<'a>>,
146152
},
147-
Sector(SectorParameters),
148153
Square {
149154
fill: Color,
150155
size: f32,
151-
outline: Option<LinePaint>,
156+
outline: Option<LinePaint<'a>>,
152157
},
153158
FreeShape {
154159
fill: Color,
155160
scale: f32,
156-
outline: Option<LinePaint>,
161+
outline: Option<LinePaint<'a>>,
157162
shape: Cow<'a, ClosedContour<Point2<f32>>>,
158163
},
159164
Label {
@@ -176,13 +181,12 @@ pub enum MarkerStyle {
176181
},
177182
}
178183

179-
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
184+
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
180185
pub(crate) struct SectorParameters {
181186
pub fill: CircleFill,
182187
pub radius: f32,
183188
pub start_angle: f32,
184189
pub end_angle: f32,
185-
pub outline: Option<LinePaint>,
186190
}
187191

188192
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]

galileo/src/render/render_bundle/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl RenderBundle {
6868
pub fn add_line<N, P, C>(&mut self, line: &C, paint: &LinePaint, min_resolution: f64)
6969
where
7070
N: AsPrimitive<f32>,
71-
P: CartesianPoint3d<Num = N>,
71+
P: CartesianPoint3d<Num = N> + Copy,
7272
C: Contour<Point = P>,
7373
{
7474
self.world_set.add_line(line, paint, min_resolution);
@@ -82,7 +82,7 @@ impl RenderBundle {
8282
min_resolution: f64,
8383
) where
8484
N: AsPrimitive<f32>,
85-
P: CartesianPoint3d<Num = N>,
85+
P: CartesianPoint3d<Num = N> + Copy,
8686
Poly: Polygon,
8787
Poly::Contour: Contour<Point = P>,
8888
{

0 commit comments

Comments
 (0)