drawland-core 0.5.1

Maps drawn for pen plotters: OSM features to a layered SVG. No I/O.
Documentation
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//! Map data from OpenStreetMap vector tiles, read by terramask: whatever a
//! document picks with `tiles_select` (buildings, roads, parks, ocean…).
//!
//! Overpass answers with every tag but is slow and often busy; the tiles are
//! static files on a CDN, a handful per output, with the OpenMapTiles schema's
//! layers and classes instead of raw tags. The host downloads them (the
//! command line through terramask's fetcher, the page with `fetch`); this
//! says which, and reads them.

use crate::output::Output;
use crate::osm::{needs_ocean, Element, Feature, Tags};
use geo::{BoundingRect, Coord, Geometry, LineString, MultiPolygon, Polygon};
use terramask::{merc_to_lonlat, tiles_touching, Features, Filter, TileId};

/// OpenFreeMap's planet tiles, as a TileJSON: no key, CORS open, weekly builds.
/// Attribution: "© OpenMapTiles © OpenStreetMap contributors".
pub const TILES: &str = "https://tiles.openfreemap.org/planet";

/// The tag holding a tile feature's layer (`building`, `transportation`…).
/// The other tags are the tile's own: `class`, `subclass`, `brunnel`…
pub const LAYER_TAG: &str = "tiles:layer";

/// Features are cut this much (a fraction of the output's size, on each side)
/// beyond the output: the outline of a park cut at the box would otherwise be
/// drawn along the output's edge.
const PAD: f64 = 0.05;

/// Features come from the deepest zoom, where every building is; past this
/// many tiles the output is big enough to step out a zoom at a time.
///
/// Buildings live deeper still: OpenFreeMap has none at z12 and below, so a
/// document that reads them pads out the cap rather than losing them. Tiles
/// are small (tens of KB) and the host fetches them eight at a time, so a
/// few hundred is a few seconds, not a memory problem.
const MAX_FEATURE_TILES: usize = 64;
const MAX_BUILDING_TILES: usize = 1024;

/// Whether a selector asks for buildings specifically: only those pay for the
/// deeper zoom, since the shallower layers (roads, water, landcover) read as
/// well from fewer tiles.
fn wants_buildings(selectors: &[String]) -> bool {
    selectors.iter().any(|s| s.split(&[',', ' '][..]).any(|part| part.trim().split(':').next() == Some("building") || part.trim() == "buildings"))
}

fn selectors_mention_ocean(selectors: &[String]) -> bool {
    selectors.iter().any(|s| s.split(&[',', ' '][..]).any(|part| part.trim() == "ocean" || part.trim().starts_with("water:") && part.contains("ocean")))
}

/// Filter items for the tile download: drop `ocean` inland when Overpass shows
/// no coastline, and ensure it is present when the profile asks and the coast
/// may show.
fn filter_selectors(selectors: &[String], osm: Option<&[Feature]>) -> Vec<String> {
    let mut out = selectors.to_vec();
    if !selectors_mention_ocean(&out) {
        return out;
    }
    let fetch = osm.is_none_or(needs_ocean);
    if fetch {
        if !out.iter().any(|s| s.trim() == "ocean") {
            out.push("ocean".into());
        }
    } else {
        out.retain(|s| s.trim() != "ocean");
    }
    out
}

/// What a document reads from the tiles, decided once Overpass (if the
/// document asks it anything) has answered. `None` when nothing is needed.
pub fn plan(output: &Output, selectors: &[String], osm: Option<&[Feature]>) -> Result<Option<Tiles>, String> {
    if selectors.is_empty() {
        return Ok(None);
    }
    let mut tiles = Tiles::new(&output.query_polygon(), selectors, osm)?;
    tiles.set_zoom(output.tile_zoom);
    // Round a pole each tile's pieces stay apart, or an area joined across
    // every longitude meets itself once projected. A globe cuts its areas
    // in 3D at the horizon, so they are joined as on any page: no seams.
    let projector = crate::projection::Projector::new(output);
    tiles.pieces = output.pole_on_page(&projector).is_some() && projector.globe().is_none();
    Ok(Some(tiles))
}

/// Web Mercator's edge, in degrees of latitude: the tiles end there.
const MERC_LAT: f64 = 85.051_128_78;

/// Past this many tiles a download is worth a word to whoever waits for it.
const LARGE_DOWNLOAD: usize = 300;

/// The tiles under one output, gathered one by one.
pub struct Tiles {
    bounds: [f64; 4],
    /// The area itself (lon/lat), padded like `bounds`: only the tiles it
    /// touches are read, a fraction of the box's when the output is turned.
    ring: Vec<[f64; 2]>,
    /// The zoom the document asks for, if it does.
    zoom: Option<u8>,
    /// Whether the selections read buildings, which only the deepest zooms hold.
    buildings: bool,
    /// How many tiles the deepest zoom may need before stepping out: the
    /// larger cap when buildings are read, the smaller one otherwise.
    max_tiles: usize,
    /// Keep each tile's piece of an area apart rather than joining them:
    /// round a pole on the page, an area joined across every longitude
    /// meets itself along the antimeridian once projected.
    pub pieces: bool,
    /// Everything read from each tile.
    filter: Filter,
    data: Features,
}

impl Tiles {
    /// The tiles inside `area` (lon/lat), usually the output's query polygon.
    /// `selectors` are terramask's: presets (`buildings`, `roads`) and
    /// `layer:class` rules.
    pub fn new(area: &Polygon<f64>, selectors: &[String], osm: Option<&[Feature]>) -> Result<Tiles, String> {
        // The tiles are web Mercator's: nothing past its edge (a polar cap's
        // pole is infinitely far in it, and clipping to it never ends).
        // A hair inside, so no edge of the box lies on a tile's own edge.
        let (lon, lat) = (|x: f64| x.clamp(-179.9999, 179.9999), |y: f64| y.clamp(-MERC_LAT + 1e-4, MERC_LAT - 1e-4));
        let bounds = area
            .bounding_rect()
            .map(|r| {
                let (dx, dy) = (r.width() * PAD, r.height() * PAD);
                [lon(r.min().x - dx), lat(r.min().y - dy), lon(r.max().x + dx), lat(r.max().y + dy)]
            })
            .unwrap_or_default();
        let ring = {
            let points = &area.exterior().0;
            let n = points.len().saturating_sub(1).max(1) as f64;
            let c = points.iter().take(points.len().saturating_sub(1).max(1)).fold(Coord { x: 0.0, y: 0.0 }, |c, p| c + *p) / n;
            points.iter().map(|p| [lon(c.x + (p.x - c.x) * (1.0 + 2.0 * PAD)), lat(c.y + (p.y - c.y) * (1.0 + 2.0 * PAD))]).collect()
        };
        let filter_items = filter_selectors(selectors, osm);
        let filter = Filter::parse(&filter_items).map_err(|e| e.to_string())?;
        let buildings = wants_buildings(selectors);
        let max_tiles = if buildings { MAX_BUILDING_TILES } else { MAX_FEATURE_TILES };
        Ok(Tiles { bounds, ring, zoom: None, buildings, max_tiles, pieces: false, filter, data: Features::new() })
    }

    /// Read the tiles at this zoom (capped by the source's), not the deepest
    /// that fits: `None` goes back to choosing.
    pub fn set_zoom(&mut self, zoom: Option<u8>) {
        self.zoom = zoom;
    }

    /// `[west, south, east, north]`.
    pub fn bounds(&self) -> [f64; 4] {
        self.bounds
    }

    /// What to read from each tile.
    pub fn filter(&self) -> &Filter {
        &self.filter
    }

    /// The tile zoom, no deeper than `max_zoom` (the tile source's).
    pub fn zoom(&self, max_zoom: u8) -> u8 {
        if let Some(z) = self.zoom {
            return z.min(max_zoom);
        }
        let mut z = max_zoom;
        while z > 0 && self.tiles_at(z).len() > self.max_tiles {
            z -= 1;
        }
        z
    }

    /// The tiles to download, for a source whose deepest zoom is `max_zoom`.
    pub fn tiles(&self, max_zoom: u8) -> Vec<TileId> {
        self.tiles_at(self.zoom(max_zoom))
    }

    fn tiles_at(&self, z: u8) -> Vec<TileId> {
        tiles_touching(&self.ring, z)
    }

    /// What the download will be, in a line for whoever waits for it, and
    /// whether it deserves a warning: detail given up (buildings lost, or
    /// any zoom short of the deepest), or a long download.
    pub fn note(&self, max_zoom: u8) -> (String, bool) {
        let z = self.zoom(max_zoom);
        let n = self.tiles_at(z).len();
        let mut text = format!("zoom {z}, {n} tiles");
        let mut warn = false;
        if z < max_zoom && self.zoom.is_none() {
            warn = true;
            let deepest = self.tiles_at(max_zoom).len();
            text += &format!(" — zoom {max_zoom} would be {deepest}, past the {} allowed", self.max_tiles);
            if z <= 12 && self.buildings {
                text += "; there are no buildings below zoom 13";
            }
            text += &format!(". Set output({{ tileZoom: {max_zoom} }}) to read it anyway");
        } else if let Some(asked) = self.zoom.filter(|&a| a > max_zoom) {
            warn = true;
            text += &format!(" — tileZoom {asked} is deeper than the source's {max_zoom}");
        }
        if n > LARGE_DOWNLOAD {
            warn = true;
            text += &format!(" — a large download (~{} MB)", (n * 40).div_ceil(1000).max(1));
        }
        (text, warn)
    }

    /// One downloaded tile (raw or gzipped protobuf).
    pub fn add_tile(&mut self, id: TileId, bytes: &[u8]) -> Result<(), String> {
        self.data.add_tile(id, bytes, &self.filter).map_err(|e| e.to_string())
    }

    /// Tiles read elsewhere, by terramask's fetcher with [`Tiles::filter`].
    pub fn add_features(&mut self, features: Features) {
        self.data.areas.extend(features.areas);
        self.data.lines.extend(features.lines);
        // Points too (places, POIs, water names): without them the command
        // line labelled none of what the page labels.
        self.data.pins.extend(features.pins);
    }

    /// What `tiles_select` can pick from, in lon/lat: each feature whole
    /// (mended across tile edges, cut to the bounds), its layer in
    /// [`LAYER_TAG`] and the tile's attributes as tags.
    pub fn features(&self) -> Vec<Feature> {
        let [w, s, e, n] = self.bounds;
        let joined = if self.pieces { self.data.clipped([w, s, e, n]) } else { self.data.joined(Some(self.bounds)) };
        let tags = |layer: &str, class: &str, subclass: &str, attrs: &[(String, String)]| -> Tags {
            let mut t: Tags = attrs.iter().cloned().collect();
            t.insert(LAYER_TAG.into(), layer.into());
            for (k, v) in [("class", class), ("subclass", subclass)] {
                if !v.is_empty() {
                    t.insert(k.into(), v.into());
                }
            }
            t
        };
        let areas = joined.areas.iter().filter_map(|a| {
            let mut p = polygons(&a.rings);
            let geometry = match p.len() {
                0 => return None,
                1 => Geometry::Polygon(p.remove(0)),
                _ => Geometry::MultiPolygon(MultiPolygon::new(p)),
            };
            Some((tags(&a.layer, &a.class, &a.subclass, &a.tags), geometry))
        });
        let lines = joined
            .lines
            .iter()
            .map(|l| (tags(&l.layer, &l.class, &l.subclass, &l.tags), Geometry::LineString(line(&l.points))));
        let pins = joined.pins.iter().map(|p| {
            let [x, y] = merc_to_lonlat(p.position[0], p.position[1]);
            (tags(&p.layer, &p.class, &p.subclass, &p.tags), Geometry::Point(geo::Point::new(x, y)))
        });
        areas
            .chain(lines)
            .chain(pins)
            .enumerate()
            .map(|(i, (tags, geometry))| Feature { element: Element::Tile, id: i as i64, tags, geometry })
            .collect()
    }
}

fn line(pts: &[[f64; 2]]) -> LineString<f64> {
    pts.iter()
        .map(|p| {
            let [x, y] = merc_to_lonlat(p[0], p[1]);
            Coord { x, y }
        })
        .collect()
}

/// Each exterior ring with the holes that follow it, in lon/lat.
fn polygons(rings: &[terramask::Ring]) -> Vec<Polygon<f64>> {
    let mut out: Vec<Polygon<f64>> = Vec::new();
    for r in rings {
        if r.exterior {
            out.push(Polygon::new(line(&r.points), vec![]));
        } else if let Some(last) = out.last_mut() {
            last.interiors_push(line(&r.points));
        }
    }
    out
}

/// Whether a tile feature (see [`Tiles::features`]) passes `filter`.
/// Where each layer's features are in `tiles`, by layer (`LAYER_TAG`).
pub fn by_layer(tiles: &[Feature]) -> std::collections::BTreeMap<&str, Vec<usize>> {
    let mut out: std::collections::BTreeMap<&str, Vec<usize>> = std::collections::BTreeMap::new();
    for (i, f) in tiles.iter().enumerate() {
        if let Some(layer) = f.tag(LAYER_TAG) {
            out.entry(layer).or_default().push(i);
        }
    }
    out
}

/// The layers whose features `filter` can keep ([`keeps`] holds only for
/// these): each rule reads one.
pub fn layers_kept(filter: &Filter) -> impl Iterator<Item = &str> {
    let mut seen: Vec<&str> = Vec::new();
    filter.rules.iter().map(|r| r.layer.as_str()).filter(move |l| if seen.contains(l) { false } else { seen.push(l); true })
}

pub fn keeps(filter: &Filter, f: &Feature) -> bool {
    let tag = |k: &str| f.tag(k).unwrap_or("");
    f.element == Element::Tile && filter.keeps(tag(LAYER_TAG), tag("class"), tag("subclass"))
}

#[cfg(test)]
mod tests {
    use super::*;
    use geo::{polygon, Area};

    fn area() -> Polygon<f64> {
        polygon![(x: -122.5, y: 37.7), (x: -122.4, y: 37.7), (x: -122.4, y: 37.8), (x: -122.5, y: 37.8)]
    }

    #[test]
    fn no_tiles_without_selectors() {
        assert!(plan(&Output {
            center: (-122.45, 37.85),
            width_mm: 100.0,
            height_mm: 100.0,
            scale: 10_000.0,
            bearing: 0.0,
            projection: crate::profile::ProjectionMode::Webmercator,
            tile_zoom: None,
        }, &[], None).unwrap().is_none());
    }

    #[test]
    fn selections_read_at_the_deepest_zoom_that_fits() {
        let small = polygon![(x: -122.26, y: 37.87), (x: -122.25, y: 37.87), (x: -122.25, y: 37.88), (x: -122.26, y: 37.88)];
        let t = Tiles::new(&small, &["buildings".into(), "roads".into()], None).unwrap();
        assert_eq!(t.zoom(14), 14);
        assert_eq!(t.filter().layers(), ["building", "transportation"]);
        let wide = polygon![(x: -124.0, y: 36.0), (x: -120.0, y: 36.0), (x: -120.0, y: 39.0), (x: -124.0, y: 39.0)];
        let big = Tiles::new(&wide, &["buildings".into()], None).unwrap();
        assert!(big.zoom(14) < 14 && big.tiles(14).len() <= MAX_BUILDING_TILES);
        let strip = polygon![(x: 8.88, y: 45.33), (x: 9.47, y: 45.33), (x: 9.47, y: 45.61), (x: 8.88, y: 45.61)];
        let with = Tiles::new(&strip, &["buildings".into()], None).unwrap();
        let without = Tiles::new(&strip, &["roads".into()], None).unwrap();
        assert_eq!(with.zoom(14), 14);
        assert!(without.zoom(14) < 14);
        assert!(Tiles::new(&small, &["forrest".into()], None).is_err());
    }

    /// The 50 km strip over Milan, turned 34°.
    fn milan_strip() -> Output {
        Output {
            center: (9.1716726, 45.4684961),
            width_mm: 5000.0,
            height_mm: 100.0,
            scale: 10000.0,
            bearing: -34.0,
            projection: crate::profile::ProjectionMode::Webmercator,
            tile_zoom: None,
        }
    }

    #[test]
    fn a_turned_output_reads_only_the_tiles_under_it() {
        let output = milan_strip();
        let t = plan(&output, &["buildings".into()], None).unwrap().unwrap();
        let box_tiles = terramask::tiles_for(&t.bounds(), 14).len();
        let read = t.tiles(14);
        assert_eq!(read[0].z, 14);
        assert!(read.len() * 3 < box_tiles, "{} of {box_tiles}", read.len());
        assert!(!t.note(14).1, "{}", t.note(14).0);
    }

    #[test]
    fn the_document_can_set_the_zoom_and_hears_when_detail_is_lost() {
        let mut output = milan_strip();
        output.height_mm = 8000.0;
        output.width_mm = 8000.0;
        let auto = plan(&output, &["buildings".into()], None).unwrap().unwrap();
        let (text, warn) = auto.note(14);
        assert!(warn && text.contains("tileZoom: 14"), "{text}");
        output.tile_zoom = Some(11);
        let set = plan(&output, &["buildings".into()], None).unwrap().unwrap();
        assert_eq!(set.zoom(14), 11);
        output.tile_zoom = Some(16);
        let deep = plan(&output, &["buildings".into()], None).unwrap().unwrap();
        assert_eq!(deep.zoom(14), 14);
        assert!(deep.note(14).0.contains("deeper than the source's"));
    }

    /// Open water across two tiles (Martha's Vineyard fixtures): ocean features
    /// must span the query west–east.
    #[test]
    fn ocean_tile_features_span_the_query() {
        use geo::polygon;
        const WEST: &[u8] = include_bytes!("../tests/fixtures/12-1243-1528.pbf");
        const EAST: &[u8] = include_bytes!("../tests/fixtures/12-1244-1528.pbf");
        let (a, b) = (
            TileId::new(12, 1243, 1528).merc_bounds(),
            TileId::new(12, 1244, 1528).merc_bounds(),
        );
        let [w, s] = merc_to_lonlat(a[0], a[1]);
        let [e, n] = merc_to_lonlat(b[2], b[3]);
        let area = polygon![(x: w, y: s), (x: e, y: s), (x: e, y: n), (x: w, y: n)];
        let mut tiles = Tiles::new(&area, &["ocean".into()], None).unwrap();
        tiles.add_tile(TileId::new(12, 1243, 1528), WEST).unwrap();
        tiles.add_tile(TileId::new(12, 1244, 1528), EAST).unwrap();
        let ocean = Filter::parse(&["ocean"]).unwrap();
        let feats = tiles.features();
        let parts: Vec<MultiPolygon<f64>> = feats
            .iter()
            .filter(|f| keeps(&ocean, f))
            .map(|f| MultiPolygon::new(crate::geom::polygons(&f.geometry)))
            .collect();
        let sea = crate::geom::union_multi(&parts);
        let q = area.bounding_rect().expect("query");
        let r = sea.bounding_rect().expect("sea");
        let eps = q.width() * 0.02;
        assert!(
            (r.min().x - q.min().x).abs() < eps && (r.max().x - q.max().x).abs() < eps,
            "ocean features should span the query ({:?}), got {:?}",
            q,
            r
        );
    }

    #[test]
    fn tile_features_match_by_layer_class_and_subclass() {
        let f = |layer: &str, class: &str, subclass: &str| Feature {
            element: Element::Tile,
            id: 0,
            tags: [(LAYER_TAG, layer), ("class", class), ("subclass", subclass)].into_iter().map(|(k, v)| (k.into(), v.into())).collect(),
            geometry: Geometry::Point(geo::Point::new(0.0, 0.0)),
        };
        let parks = Filter::parse(&["landcover:park,garden"]).unwrap();
        assert!(keeps(&parks, &f("landcover", "grass", "park")));
        assert!(!keeps(&parks, &f("landcover", "grass", "grass")));
        assert!(!keeps(&parks, &f("landuse", "grass", "park")));
    }
}