use std::sync::Arc;
use crisol_geo::{GeoJson, Projection};
use crisol_plot_core::transform::YAxis;
use crate::plot::Series;
pub fn series(layer: &GeoJson, projection: Projection) -> Vec<Series<'static>> {
let mut out = Vec::new();
for (name, paths) in layer.project(projection) {
let label = name.unwrap_or_else(|| "geo".to_owned());
for (index, path) in paths.into_iter().enumerate() {
if path.len() == 1 {
let mut points = Vec::with_capacity(path.len());
points.extend(path);
out.push(Series::scatter(format!("{label} {index}"), points));
} else if path.len() >= 2 {
out.push(Series::line(format!("{label} {index}"), path));
}
}
}
out
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TileRequest {
pub template: String,
}
impl TileRequest {
pub fn new(template: impl Into<String>) -> Self {
Self {
template: template.into(),
}
}
pub fn url(&self, grid: crisol_geo::TileGrid, tile: crisol_geo::Tile) -> String {
grid.url(&self.template, tile)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct GeoLayer {
pub projection: Projection,
pub tiles: Option<TileRequest>,
}
impl Default for GeoLayer {
fn default() -> Self {
Self {
projection: Projection::WebMercator,
tiles: None,
}
}
}
pub fn add_to(plot: crate::plot::Plot<'static>, layer: &GeoJson, style: &GeoLayer) -> crate::plot::Plot<'static> {
let mut plot = plot;
for series in series(layer, style.projection) {
plot = plot.series(series);
}
plot
}
pub const GEO_AXIS: YAxis = YAxis::Left;
pub fn shared(layer: GeoJson) -> Arc<GeoJson> {
Arc::new(layer)
}