pub mod dir;
pub mod mbtiles;
pub mod pmtiles;
use std::path::PathBuf;
use tippecanoe::mbtiles::Metadata;
use crate::mlt::TileFormat;
use crate::options::Output;
pub trait TileSink: Send + Sync {
fn write_tile(&self, z: u8, x: u32, y: u32, data: &[u8]) -> crate::Result<()>;
fn erase_zoom(&self, z: u8) -> crate::Result<()>;
fn finish(self: Box<Self>, metadata: &Metadata) -> crate::Result<()>;
fn finish_with(
self: Box<Self>,
metadata: &Metadata,
extra: &[(String, String)],
) -> crate::Result<()> {
let _ = extra;
self.finish(metadata)
}
}
#[derive(Clone, Debug)]
pub struct SinkSettings {
pub force: bool,
pub allow_existing: bool,
pub tile_compression: bool,
pub tmpdir: PathBuf,
pub memory_budget: usize,
pub file_writers: usize,
pub tile_format: TileFormat,
pub program: String,
}
impl Default for SinkSettings {
fn default() -> SinkSettings {
SinkSettings {
force: false,
allow_existing: false,
tile_compression: true,
tmpdir: PathBuf::from(tippecanoe::platform::default_tmpdir()),
memory_budget: physical_memory() / 4,
file_writers: 4,
tile_format: TileFormat::Mvt,
program: "tippecanoe".to_string(),
}
}
}
#[cfg(windows)]
pub fn physical_memory() -> usize {
tippecanoe::platform::calc_memsize()
}
#[cfg(unix)]
pub fn physical_memory() -> usize {
let (pages, page_size) = unsafe {
(
libc::sysconf(libc::_SC_PHYS_PAGES),
libc::sysconf(libc::_SC_PAGESIZE),
)
};
if pages > 0 && page_size > 0 {
(pages as usize).saturating_mul(page_size as usize)
} else {
4 << 30
}
}
pub fn open(output: &Output, settings: &SinkSettings) -> crate::Result<Box<dyn TileSink>> {
match output {
Output::Pmtiles(path) => Ok(Box::new(pmtiles::PmtilesSink::create(path, settings)?)),
Output::Mbtiles(path) => Ok(Box::new(mbtiles::MbtilesSink::create(path, settings)?)),
Output::Directory(path) => Ok(Box::new(dir::DirSink::create(path, settings)?)),
Output::None => Err(crate::Error::new(
tippecanoe::errors::EXIT_ARGS,
"must specify -o out.mbtiles or -e directory",
)),
}
}
pub(crate) fn tileset_metadata(
m: &Metadata,
tile_format: TileFormat,
) -> std::borrow::Cow<'_, Metadata> {
match tile_format {
TileFormat::Mvt => std::borrow::Cow::Borrowed(m),
TileFormat::Mlt => {
let mut m = m.clone();
m.format = tile_format.metadata_format().to_string();
std::borrow::Cow::Owned(m)
}
}
}
pub(crate) fn first_tile_id(z: u8) -> u64 {
((1u64 << (2 * z as u32)) - 1) / 3
}
pub(crate) fn check_tile(z: u8, x: u32, y: u32) -> crate::Result<()> {
if z > 31 || x >> z != 0 || y >> z != 0 {
return Err(crate::Error::new(
tippecanoe::errors::EXIT_IMPOSSIBLE,
format!("tile {}/{}/{} is outside zoom level bounds", z, x, y),
));
}
Ok(())
}
pub(crate) fn to_string_double(d: f64) -> String {
if d.is_nan() {
return "nan".to_string();
}
format!("{:.6}", d)
}
pub(crate) fn io_message(path: &str, e: &std::io::Error) -> String {
match e.raw_os_error() {
Some(errno) => format!("{}: {}", path, tippecanoe::cstd::strerror(errno)),
None => format!("{}: {}", path, e),
}
}
#[cfg(test)]
pub(crate) mod tests;
#[cfg(test)]
mod unit {
use super::*;
#[test]
fn tile_id_ranges() {
for z in 0..=31u8 {
let n = 1u32 << z;
assert_eq!(
first_tile_id(z),
tippecanoe::pmtiles::zxy_to_tileid(z, 0, 0)
);
if z < 31 {
assert_eq!(
first_tile_id(z + 1),
tippecanoe::pmtiles::zxy_to_tileid(z, n - 1, 0) + 1
);
}
}
assert!(check_tile(0, 0, 0).is_ok());
assert!(check_tile(3, 7, 7).is_ok());
assert!(check_tile(3, 8, 0).is_err());
assert!(check_tile(32, 0, 0).is_err());
}
}