use std::collections::HashMap;
use std::num::NonZeroU8;
use std::sync::Arc;
use martin_tile_utils::{EARTH_CIRCUMFERENCE, Encoding, TileCoord, TileData, TileRect};
use martin_tilegen::plan::Plan;
use martin_tilegen::{Summary, TileFormat, TileGenError, render_block};
use tracing::{debug, warn};
use super::{PostgresError, PostgresPool, PostgresResult};
use crate::tiles::postgres::scan::{ScanLayout, scan_features};
#[derive(Clone, Debug)]
pub struct PostgresEngineInfo {
pub plan: Arc<Plan>,
pub sql_queries: Vec<String>,
pub layout: ScanLayout,
pub margin: f64,
pub prefetch: Option<NonZeroU8>,
}
impl PostgresEngineInfo {
pub(super) async fn render_tile(
&self,
pool: &PostgresPool,
source_id: &str,
xyz: TileCoord,
) -> PostgresResult<TileData> {
let block = TileRect::new(xyz.z(), xyz.x(), xyz.y(), xyz.x(), xyz.y());
let tiles = self.render(pool, source_id, xyz, block).await?;
Ok(tiles.into_iter().next().map(|(_, data)| data).unwrap_or_default())
}
pub(super) async fn render_prefetch(
&self,
pool: &PostgresPool,
source_id: &str,
xyz: TileCoord,
) -> PostgresResult<Option<Vec<(TileCoord, TileData)>>> {
let Some(side) = self.prefetch else {
return Ok(None);
};
let side = u32::from(side.get());
let last = (1u32 << xyz.z()) - 1;
let (x, y) = (xyz.x() / side * side, xyz.y() / side * side);
let block = TileRect::new(xyz.z(), x, y, (x + side - 1).min(last), (y + side - 1).min(last));
let mut rendered: HashMap<_, _> = self.render(pool, source_id, xyz, block).await?.into_iter().collect();
let tiles = (block.min_x..=block.max_x)
.flat_map(|x| (block.min_y..=block.max_y).map(move |y| (x, y)))
.map(|(x, y)| {
let coord = TileCoord::new_unchecked(xyz.z(), x, y);
(coord, rendered.remove(&coord).unwrap_or_default())
})
.collect();
Ok(Some(tiles))
}
async fn render(
&self,
pool: &PostgresPool,
source_id: &str,
xyz: TileCoord,
block: TileRect,
) -> PostgresResult<Vec<(TileCoord, TileData)>> {
let failed = |err: TileGenError| PostgresError::EngineError(Box::new(err), source_id.to_owned(), xyz);
let [[x1, y1, x2, y2], [x3, y3, x4, y4]] = self.envelopes(block);
let sql = self.sql_query(block.zoom);
let conn = pool.get().await?;
let _cancel_guard = pool.active_query_registry().register(conn.cancel_token());
let statement = conn
.prepare_cached(sql)
.await
.map_err(|e| PostgresError::PostgresError(e, "preparing a table's feature query"))?;
debug!(sql = %sql, tile.coord = %xyz, "Running feature query");
let mut batches = Vec::new();
scan_features(
&conn,
&statement,
&[&x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4],
&self.layout,
0,
&self.plan.table_keys()[usize::from(self.layout.table)],
|batch| {
batches.push(batch);
Ok(())
},
)
.await
.map_err(|err| failed(err.into()))?;
drop(conn);
let plan = Arc::clone(&self.plan);
let (tiles, summary) = tokio::task::spawn_blocking(move || {
render_block(&plan, &mut batches, block, TileFormat::Mvt, Encoding::Uncompressed)
})
.await
.unwrap_or_else(|panic| std::panic::resume_unwind(panic.into_panic()))
.map_err(failed)?;
report(source_id, xyz, &summary);
Ok(tiles
.into_iter()
.map(|tile| (tile.coord, TileData::from(tile.data)))
.collect())
}
fn sql_query(&self, zoom: u8) -> &str {
self.sql_queries
.get(usize::from(zoom))
.or_else(|| self.sql_queries.last())
.expect("an engine has a query for zoom 0")
}
fn envelopes(&self, block: TileRect) -> [[f64; 4]; 2] {
let side = EARTH_CIRCUMFERENCE / f64::from(1u32 << block.zoom);
let half = EARTH_CIRCUMFERENCE / 2.0;
let margin = self.margin * side;
let xmin = f64::from(block.min_x).mul_add(side, -half) - margin;
let xmax = f64::from(block.max_x + 1).mul_add(side, -half) + margin;
let ymin = half - f64::from(block.max_y + 1) * side - margin;
let ymax = half - f64::from(block.min_y) * side + margin;
let main = [xmin.max(-half), ymin, xmax.min(half), ymax];
let wrapped = if xmax - xmin >= EARTH_CIRCUMFERENCE {
main
} else if xmin < -half {
[xmin + EARTH_CIRCUMFERENCE, ymin, half, ymax]
} else if xmax > half {
[-half, ymin, xmax - EARTH_CIRCUMFERENCE, ymax]
} else {
main
};
[main, wrapped]
}
}
fn report(source_id: &str, xyz: TileCoord, summary: &Summary) {
for failed in &summary.slice_errors {
warn!(
source.id = source_id,
tile.coord = %xyz,
layer.id = %failed.layer,
feature_zooms.skipped = failed.errors,
feature.id = failed.id,
tile.zoom = failed.zoom,
error = %failed.error,
"Skipped feature-zooms whose geometry the slicer could not handle"
);
}
for failed in &summary.expr_errors {
debug!(
source.id = source_id,
tile.coord = %xyz,
"Layer `{}`: `{}` failed {} times and was taken as null",
failed.layer,
failed.expr,
failed.errors
);
}
debug!(
source.id = source_id,
tile.coord = %xyz,
features = summary.features,
features.skipped = summary.skipped,
tiles = summary.tiles,
"Rendered the block around a tile"
);
}