crisol-plot 1.10.0

Crisol plot: interactive 2D charts and a 3D sibling with SVG, PNG, and PDF export.
Documentation
//! GeoJSON features as plot series.

use std::sync::Arc;

use crisol_geo::{GeoJson, Projection};
use crisol_plot_core::transform::YAxis;

use crate::plot::Series;

/// Convert a GeoJSON layer to line and scatter series in plot coordinates.
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
}

/// A tile URL and the layers that cover it, for map overlays.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TileRequest {
    /// URL template with `{z}`, `{x}`, and `{y}`.
    pub template: String,
}

impl TileRequest {
    /// A template request.
    pub fn new(template: impl Into<String>) -> Self {
        Self {
            template: template.into(),
        }
    }

    /// The URL for `tile`.
    pub fn url(&self, grid: crisol_geo::TileGrid, tile: crisol_geo::Tile) -> String {
        grid.url(&self.template, tile)
    }
}

/// A convenience type for applications that keep one projection and one tile template.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct GeoLayer {
    /// Projection for the GeoJSON.
    pub projection: Projection,
    /// Tile request template.
    pub tiles: Option<TileRequest>,
}

impl Default for GeoLayer {
    fn default() -> Self {
        Self {
            projection: Projection::WebMercator,
            tiles: None,
        }
    }
}

/// Apply a layer to an existing plot.
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
}

/// The y axis used by geographic series (the left axis).
pub const GEO_AXIS: YAxis = YAxis::Left;

/// Keep the Arc import used by applications that share a layer between windows.
pub fn shared(layer: GeoJson) -> Arc<GeoJson> {
    Arc::new(layer)
}