1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use std::{borrow::Cow, collections::HashSet, marker::PhantomData, rc::Rc};
use crate::{
context::MapContext,
environment::{Environment, OffscreenKernelEnvironment},
io::{
apc::{AsyncProcedureCall, AsyncProcedureFuture, Context, Input, ProcedureError},
source_type::{RasterSource, SourceType},
},
kernel::Kernel,
raster::{
process_raster::{process_raster_tile, ProcessRasterContext, RasterTileRequest},
transferables::{LayerRasterMissing, RasterTransferables},
RasterLayersDataComponent,
},
style::layer::LayerPaint,
tcs::system::System,
};
pub struct RequestSystem<E: Environment, T: RasterTransferables> {
kernel: Rc<Kernel<E>>,
phantom_t: PhantomData<T>,
}
impl<E: Environment, T: RasterTransferables> RequestSystem<E, T> {
pub fn new(kernel: &Rc<Kernel<E>>) -> Self {
Self {
kernel: kernel.clone(),
phantom_t: Default::default(),
}
}
}
impl<E: Environment, T: RasterTransferables> System for RequestSystem<E, T> {
fn name(&self) -> Cow<'static, str> {
"raster_request".into()
}
fn run(
&mut self,
MapContext {
style,
view_state,
world,
..
}: &mut MapContext,
) {
let _tiles = &mut world.tiles;
let view_region = view_state.create_view_region();
if view_state.did_camera_change() || view_state.did_zoom_change() {
if let Some(view_region) = &view_region {
for coords in view_region.iter() {
if coords.build_quad_key().is_none() {
continue;
}
if world
.tiles
.query::<&RasterLayersDataComponent>(coords)
.is_some()
{
continue;
}
world
.tiles
.spawn_mut(coords)
.unwrap()
.insert(RasterLayersDataComponent::default());
tracing::event!(tracing::Level::ERROR, %coords, "tile request started: {coords}");
log::info!("tile request started: {coords}");
self.kernel
.apc()
.call(
Input::TileRequest {
coords,
style: style.clone(), },
fetch_raster_apc::<
E::OffscreenKernelEnvironment,
T,
<E::AsyncProcedureCall as AsyncProcedureCall<
E::OffscreenKernelEnvironment,
>>::Context,
>,
)
.unwrap(); }
}
}
view_state.update_references();
}
}
pub fn fetch_raster_apc<
K: OffscreenKernelEnvironment,
T: RasterTransferables,
C: Context + Clone + Send,
>(
input: Input,
context: C,
kernel: K,
) -> AsyncProcedureFuture {
Box::pin(async move {
let Input::TileRequest {coords, style} = input else {
return Err(ProcedureError::IncompatibleInput)
};
let raster_layers: HashSet<String> = style
.layers
.iter()
.filter_map(|layer| {
if matches!(layer.paint, Some(LayerPaint::Raster(_))) {
layer.source_layer.clone()
} else {
None
}
})
.collect();
let client = kernel.source_client();
if !raster_layers.is_empty() {
let context = context.clone();
let source = SourceType::Raster(RasterSource::default());
match client.fetch(&coords, &source).await {
Ok(data) => {
let data = data.into_boxed_slice();
let mut process_context = ProcessRasterContext::<T, C>::new(context);
process_raster_tile(&data, RasterTileRequest { coords }, &mut process_context)
.map_err(|e| ProcedureError::Execution(Box::new(e)))?;
}
Err(e) => {
log::error!("{e:?}");
context
.send(<T as RasterTransferables>::LayerRasterMissing::build_from(
coords,
))
.map_err(ProcedureError::Send)?;
}
}
}
Ok(())
})
}