Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

GeoJSON

This map shows the street gyms in Wrocław. They are mapped in OpenStreetMap, and the app has them in a GeoJSON file, built into it.

The data

include_str! builds the file into the app, so there is nothing to download or read when it starts. A GeoJsonSource, from walkers_extras, turns the parsed GeoJSON into features.

/// Street gyms in Wrocław, from OpenStreetMap, built into the app.
fn street_gyms() -> GeoJsonSource {
    let geojson = include_str!("../assets/wroclaw-street-gyms.geojson")
        .parse()
        .expect("the bundled GeoJSON is valid");
    GeoJsonSource::new(geojson)
}

Drawing it

A GeoJsonSource puts all of its features in one layer. Style layers draw from it by leaving source-layer empty. Here a symbol layer draws each gym as an icon of the style: a dumbbell in an orange circle, made of four filled shapes.

/// Every gym as a dumbbell in an orange circle.
fn gym_style() -> Style {
    Style {
        layers: vec![StyleLayer::Symbol {
            source: None,
            source_layer: "".into(),
            minzoom: None,
            filter: None,
            layout: Layout {
                icon_image: Some(json!("gym")),
                ..Default::default()
            },
            paint: None,
        }],
        icons: [(
            "gym".to_string(),
            vec![
                IconShape::fill(circle(8.0), "#f4a261"),
                IconShape::fill(
                    vec![[-4.0, -1.0], [4.0, -1.0], [4.0, 1.0], [-4.0, 1.0]],
                    "#264653",
                ),
                IconShape::fill(
                    vec![[-6.0, -4.0], [-3.5, -4.0], [-3.5, 4.0], [-6.0, 4.0]],
                    "#264653",
                ),
                IconShape::fill(
                    vec![[3.5, -4.0], [6.0, -4.0], [6.0, 4.0], [3.5, 4.0]],
                    "#264653",
                ),
            ],
        )]
        .into(),
    }
}

/// Corners of a circle of that `radius`, around where the icon stands.
fn circle(radius: f32) -> Vec<[f32; 2]> {
    (0..16)
        .map(|corner| {
            let angle = corner as f32 * std::f32::consts::TAU / 16.0;
            [radius * angle.cos(), radius * angle.sin()]
        })
        .collect()
}

Adding it to the map

The gyms have a StyledSources of their own, which the map draws over the basemap.

impl GeoJsonMap {
    pub fn new(cc: &CreationContext<'_>) -> Self {
        let ctx = &cc.egui_ctx;
        let tiles = HttpSource::new(OpenFreeMap, HttpOptions::default(), ctx.clone());
        let mut map_memory = MapMemory::default();
        let _ = map_memory.set_zoom(13.0);

        Self {
            basemap: StyledSources::new(Waymark::new(Palette::dark()).build(), ctx.clone())
                .with_tiled_source("openmaptiles", tiles),
            gyms: StyledSources::new(gym_style(), ctx.clone()).with_source("gyms", street_gyms()),
            map_memory,
        }
    }
}
impl App for GeoJsonMap {
    fn ui(&mut self, ui: &mut Ui, _frame: &mut Frame) {
        let attributions = self.basemap.attributions();
        let wroclaw = lon_lat(17.03664, 51.10916);

        Map::new(None, &mut self.map_memory, wroclaw)
            .with_layer(&mut self.basemap, 1.0)
            .with_layer(&mut self.gyms, 1.0)
            .show(ui, |ui, _, _, _| {
                for attribution in attributions {
                    ui.hyperlink_to(attribution.text, attribution.url);
                }
            });
    }
}

Full source