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};
pub const TILES: &str = "https://tiles.openfreemap.org/planet";
pub const LAYER_TAG: &str = "tiles:layer";
const PAD: f64 = 0.05;
const MAX_FEATURE_TILES: usize = 64;
const MAX_BUILDING_TILES: usize = 1024;
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")))
}
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
}
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);
Ok(Some(tiles))
}
const LARGE_DOWNLOAD: usize = 300;
pub struct Tiles {
bounds: [f64; 4],
ring: Vec<[f64; 2]>,
zoom: Option<u8>,
buildings: bool,
max_tiles: usize,
filter: Filter,
data: Features,
}
impl Tiles {
pub fn new(area: &Polygon<f64>, selectors: &[String], osm: Option<&[Feature]>) -> Result<Tiles, String> {
let bounds = area
.bounding_rect()
.map(|r| {
let (dx, dy) = (r.width() * PAD, r.height() * PAD);
[r.min().x - dx, r.min().y - dy, r.max().x + dx, 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| [c.x + (p.x - c.x) * (1.0 + 2.0 * PAD), 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, filter, data: Features::new() })
}
pub fn set_zoom(&mut self, zoom: Option<u8>) {
self.zoom = zoom;
}
pub fn bounds(&self) -> [f64; 4] {
self.bounds
}
pub fn filter(&self) -> &Filter {
&self.filter
}
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
}
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)
}
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)
}
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())
}
pub fn add_features(&mut self, features: Features) {
self.data.areas.extend(features.areas);
self.data.lines.extend(features.lines);
self.data.pins.extend(features.pins);
}
pub fn features(&self) -> Vec<Feature> {
let joined = 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()
}
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
}
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());
}
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"));
}
#[test]
fn ocean_tile_features_span_the_query() {
use geo::polygon;
const WEST: &[u8] = include_bytes!("../../../../terramask/crates/terramask/tests/fixtures/12-1243-1528.pbf");
const EAST: &[u8] = include_bytes!("../../../../terramask/crates/terramask/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")));
}
}