use crate::profile::{OutputSpec, ProjectionMode};
use geo::{Coord, LineString, Polygon};
use geographiclib_rs::{DirectGeodesic, Geodesic};
#[derive(Debug, Clone, PartialEq)]
pub struct Output {
pub center: (f64, f64),
pub width_mm: f64,
pub height_mm: f64,
pub scale: f64,
pub bearing: f64,
pub projection: ProjectionMode,
pub tile_zoom: Option<u8>,
}
impl Output {
pub fn new(map: &OutputSpec, center: (f64, f64)) -> Output {
Output {
center,
width_mm: map.width.0,
height_mm: map.height.0,
scale: map.scale.0,
bearing: map.bearing,
projection: map.projection.clone(),
tile_zoom: map.tile_zoom,
}
}
pub fn ground_size_m(&self) -> (f64, f64) {
(self.width_mm / 1000.0 * self.scale, self.height_mm / 1000.0 * self.scale)
}
pub fn mm_per_m(&self) -> f64 {
1000.0 / self.scale
}
pub fn query_polygon(&self) -> Polygon<f64> {
if let Some(frame) = self.projected_frame() {
return frame;
}
let g = Geodesic::wgs84();
let (lon, lat) = self.center;
let (w, h) = self.ground_size_m();
let walk = |from: (f64, f64), azimuth: f64, dist: f64| -> (f64, f64) {
let (lat2, lon2): (f64, f64) = g.direct(from.1, from.0, azimuth, dist);
(lon2, lat2)
};
let b = self.bearing;
let north = walk((lon, lat), b, h / 2.0);
let south = walk((lon, lat), (b + 180.0) % 360.0, h / 2.0);
let east = (b + 90.0) % 360.0;
let west = (b + 270.0) % 360.0;
let ne = walk(north, east, w / 2.0);
let nw = walk(north, west, w / 2.0);
let se = walk(south, east, w / 2.0);
let sw = walk(south, west, w / 2.0);
let ring: Vec<Coord<f64>> = [ne, nw, sw, se, ne].iter().map(|&(x, y)| Coord { x, y }).collect();
Polygon::new(LineString::new(ring), vec![])
}
fn projected_frame(&self) -> Option<Polygon<f64>> {
if self.projection == ProjectionMode::Utm {
return None;
}
const PER_SIDE: usize = 16;
let p = crate::projection::Projector::new(self);
let (w, h) = (self.width_mm, self.height_mm);
if let Some(globe) = p.globe() {
let mut radius: f64 = 0.0;
for i in 0..PER_SIDE * 4 {
let t = (i % PER_SIDE) as f64 / PER_SIDE as f64;
let (x, y) = match i / PER_SIDE {
0 => (w * t, 0.0),
1 => (w, h * t),
2 => (w * (1.0 - t), h),
_ => (0.0, h * (1.0 - t)),
};
radius = radius.max(p.unproject(x, y).map(|(lon, lat)| globe.distance(lon, lat) + 0.5).unwrap_or(90.0));
}
return Some(globe.ground(radius));
}
let edge = (0..PER_SIDE * 4).map(|i| {
let t = (i % PER_SIDE) as f64 / PER_SIDE as f64;
match i / PER_SIDE {
0 => (w * t, 0.0),
1 => (w, h * t),
2 => (w * (1.0 - t), h),
_ => (0.0, h * (1.0 - t)),
}
});
let mut ring: Vec<Coord<f64>> =
edge.map(|(x, y)| p.unproject(x, y).map(|(x, y)| Coord { x, y })).collect::<Option<_>>()?;
if let Some(pole) = self.pole_on_page(&p) {
let far = if pole > 0.0 { ring.iter().map(|c| c.y).fold(90.0, f64::min) } else { ring.iter().map(|c| c.y).fold(-90.0, f64::max) };
let (a, b) = if pole > 0.0 { (far, 90.0) } else { (-90.0, far) };
let cap = vec![
Coord { x: -180.0, y: a },
Coord { x: 180.0, y: a },
Coord { x: 180.0, y: b },
Coord { x: -180.0, y: b },
Coord { x: -180.0, y: a },
];
return Some(Polygon::new(LineString::new(cap), vec![]));
}
ring.push(ring[0]);
Some(Polygon::new(LineString::new(ring), vec![]))
}
pub fn pole_on_page(&self, p: &crate::projection::Projector) -> Option<f64> {
if self.projection == ProjectionMode::Webmercator {
return None;
}
[90.0, -90.0].into_iter().find(|&lat| {
let (x, y) = p.project(0.0, lat);
x.is_finite() && y.is_finite() && (0.0..=self.width_mm).contains(&x) && (0.0..=self.height_mm).contains(&y)
})
}
pub fn bounds(&self) -> [f64; 4] {
let g = Geodesic::wgs84();
let (lon, lat) = self.center;
let (w, h) = self.ground_size_m();
let at = |azimuth: f64, dist: f64| -> (f64, f64) {
let (lat2, lon2): (f64, f64) = g.direct(lat, lon, azimuth, dist);
(lon2, lat2)
};
[at(270.0, w / 2.0).0, at(180.0, h / 2.0).1, at(90.0, w / 2.0).0, at(0.0, h / 2.0).1]
}
}
pub const OSM_TAGS: &[&str] =
&["building", "highway", "railway", "natural", "water", "landuse", "amenity", "leisure", "waterway"];
pub fn fetch_keys(needed: &[String]) -> Vec<String> {
let mut keys: Vec<String> = OSM_TAGS.iter().map(|k| k.to_string()).collect();
for k in needed {
if !keys.contains(k) {
keys.push(k.clone());
}
}
keys
}
pub const OVERPASS_ENDPOINTS: &[&str] = &[
"https://overpass-api.de/api/interpreter",
"https://z.overpass-api.de/api/interpreter",
"https://lz4.overpass-api.de/api/interpreter",
"https://maps.mail.ru/osm/tools/overpass/api/interpreter",
];
pub const OVERPASS_ROUND_PAUSE_S: &[u64] = &[0, 10, 30, 60];
pub const OVERPASS_MAX_SLOT_WAIT_S: u64 = 120;
pub fn overpass_status_url(interpreter: &str) -> String {
match interpreter.strip_suffix("interpreter") {
Some(base) => format!("{base}status"),
None => format!("{}/status", interpreter.trim_end_matches('/')),
}
}
pub fn overpass_slot_wait_s(status: &str) -> Option<u64> {
if status.lines().any(|l| l.trim_end().ends_with("slots available now.") && !l.starts_with("0 ")) {
return Some(0);
}
status
.lines()
.filter_map(|l| l.split(", in ").nth(1)?.strip_suffix(" seconds.")?.trim().parse::<u64>().ok())
.min()
}
pub fn overpass_error(json: &str) -> Option<String> {
let mut start = json.len().saturating_sub(2000);
while !json.is_char_boundary(start) {
start += 1;
}
let tail = &json[start..];
let at = tail.rfind("\"remark\"")?;
let rest = tail[at + 8..].trim_start().strip_prefix(':')?.trim_start().strip_prefix('"')?;
let text = rest.split('"').next()?.trim();
text.contains("error").then(|| text.to_string())
}
pub const OVERPASS_ENDPOINT_TIMEOUT_S: u64 = 60;
pub fn overpass_query(polygon: &Polygon<f64>, tags: &[&str], timeout_s: u32) -> String {
let coords: Vec<String> =
polygon.exterior().coords().map(|c| format!("{:.6} {:.6}", c.y, c.x)).collect();
let poly = coords.join(" ");
let mut components = String::new();
for key in tags {
let tag_str = format!("['{key}'](poly:'{poly}');(._;>;);");
for kind in ["node", "way", "relation"] {
components.push_str(&format!("({kind}{tag_str});"));
}
}
format!("[out:json][timeout:{timeout_s}];({components});out;")
}
#[cfg(test)]
mod tests {
use super::*;
use geo::BoundingRect;
fn ferry() -> Output {
Output {
center: (-122.3934746, 37.7955487),
width_mm: 152.4,
height_mm: 101.6,
scale: 10000.0,
bearing: 0.0,
projection: ProjectionMode::Utm,
tile_zoom: None,
}
}
#[test]
fn query_matches_osmnx() {
let q = overpass_query(&ferry().query_polygon(), OSM_TAGS, 180);
assert!(q.starts_with(
"[out:json][timeout:180];((node['building'](poly:'37.800125 -122.384822 37.800125 -122.402127 \
37.790971 -122.402126 37.790971 -122.384823 37.800125 -122.384822');(._;>;););"
));
assert!(q.ends_with(");out;"));
}
#[test]
fn a_wide_web_mercator_page_reads_its_whole_ground() {
let scale = 4_859_118.0;
let o = Output { center: (12.85, 41.5), width_mm: 2205.0, height_mm: 1014.0, scale, bearing: 0.0, projection: ProjectionMode::Webmercator, tile_zoom: None };
let r = o.query_polygon().bounding_rect().unwrap();
let a = 6_378_137.0;
let y0 = a * (std::f64::consts::FRAC_PI_4 + 41.5f64.to_radians() / 2.0).tan().ln();
let half = 507.0 * scale / 1000.0 / 41.5f64.to_radians().cos();
let lat = |y: f64| (2.0 * (y / a).exp().atan() - std::f64::consts::FRAC_PI_2).to_degrees();
assert!((r.max().y - lat(y0 + half)).abs() < 1e-6 && (r.min().y - lat(y0 - half)).abs() < 1e-6, "{r:?}");
let ring = o.query_polygon();
let north = ring.exterior().0.iter().filter(|c| (c.y - lat(y0 + half)).abs() < 1e-6).count();
assert!(north >= 17, "{ring:?}");
}
#[test]
fn status_pages_and_remarks() {
let busy = "Connected as: 1\nRate limit: 2\n0 slots available now.\n\
Slot available after: 2026-10-02T08:32:01Z, in 36 seconds.\n\
Slot available after: 2026-10-02T08:32:20Z, in 55 seconds.\n";
let free = "Rate limit: 4\n3 slots available now.\nSlot available after: 2026-10-02T08:33:06Z, in 26 seconds.\n";
assert_eq!(overpass_slot_wait_s(busy), Some(36));
assert_eq!(overpass_slot_wait_s(free), Some(0));
assert_eq!(overpass_slot_wait_s("<html>406</html>"), None);
assert_eq!(
overpass_status_url("https://overpass-api.de/api/interpreter"),
"https://overpass-api.de/api/status"
);
let cut = r#"{"elements":[{"type":"node","id":1,"lat":0,"lon":0}],
"remark": "runtime error: Query timed out in \"query\" at line 1 after 61 seconds." }"#;
assert!(overpass_error(cut).unwrap().starts_with("runtime error: Query timed out"));
assert_eq!(overpass_error(r#"{"elements":[]}"#), None);
}
#[test]
fn bounds_are_geodesic() {
let [w, s, e, n] = ferry().bounds();
assert!((n - 37.800125).abs() < 1e-6 && (s - 37.790971).abs() < 1e-6);
assert!(w < -122.4021 && e > -122.3849);
}
}