mirror of
https://github.com/maplibre/maplibre-rs.git
synced 2025-12-08 19:05:57 +00:00
79 lines
2.2 KiB
Rust
79 lines
2.2 KiB
Rust
use crate::coords::{Quadkey, WorldTileCoords};
|
|
use crate::io::LayerResult;
|
|
use std::collections::{btree_map, BTreeMap, HashSet};
|
|
|
|
#[derive(Default)]
|
|
pub struct TileCache {
|
|
index: BTreeMap<Quadkey, Vec<LayerResult>>,
|
|
}
|
|
|
|
impl TileCache {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
index: BTreeMap::new(),
|
|
}
|
|
}
|
|
|
|
pub fn push(&mut self, result: LayerResult) {
|
|
match self.index.entry(result.get_coords().build_quad_key()) {
|
|
btree_map::Entry::Vacant(entry) => {
|
|
entry.insert(vec![result]);
|
|
}
|
|
btree_map::Entry::Occupied(mut entry) => {
|
|
entry.get_mut().push(result);
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn get_tessellated_layers_at(
|
|
&self,
|
|
coords: &WorldTileCoords,
|
|
skip_layers: &HashSet<String>,
|
|
) -> Vec<LayerResult> {
|
|
let mut ret = Vec::new();
|
|
|
|
if let Some(results) = self.index.get(&coords.build_quad_key()) {
|
|
for result in results {
|
|
if !skip_layers.contains(&result.layer_name().to_string()) {
|
|
ret.push(result.clone());
|
|
}
|
|
}
|
|
}
|
|
|
|
ret
|
|
}
|
|
|
|
pub fn retain_missing_layer_names(
|
|
&self,
|
|
coords: &WorldTileCoords,
|
|
layers: &mut HashSet<String>,
|
|
) {
|
|
if let Some(results) = self.index.get(&coords.build_quad_key()) {
|
|
let tessellated_set: HashSet<String> = results
|
|
.iter()
|
|
.map(|tessellated_layer| tessellated_layer.layer_name().to_string())
|
|
.collect();
|
|
|
|
layers.retain(|layer| !tessellated_set.contains(layer));
|
|
}
|
|
}
|
|
|
|
pub fn is_layers_missing(&self, coords: &WorldTileCoords, layers: &HashSet<String>) -> bool {
|
|
if let Some(results) = self.index.get(&coords.build_quad_key()) {
|
|
let tessellated_set: HashSet<&str> = results
|
|
.iter()
|
|
.map(|tessellated_layer| tessellated_layer.layer_name())
|
|
.collect();
|
|
|
|
for layer in layers {
|
|
if !tessellated_set.contains(layer.as_str()) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
true
|
|
}
|
|
}
|