Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable tile systems for projections other than web mercator #611

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,18 @@ postgres:
id_format: 'table.{schema}.{table}.{column}'
# Add more schemas to the ones listed above
from_schemas: my_other_schema
# Optionally, publish tiles in more than one projection/tiling scheme
tile_systems:
# the default web mercator tiles, enabled by default
- type: WebMercatorQuad
# an additional custom tiling scheme
- type: Custom
srid: 4326
# bounds of tile 0/0/0
bounds: [ -180.0, -90.0, 180.0, 90.0 ]
# name for the tiling scheme
# auto published table will be given the name {source_id}:{tile_system.identifier}
identifier: WGS84Quad
functions:
id_format: '{schema}.{function}'

Expand Down Expand Up @@ -601,6 +613,15 @@ postgres:
# List of columns, that should be encoded as tile properties (required)
properties:
gid: int4

# Output tiles with a custom projection and tiling
tile_system:
# output srid
srid: 4326
# bounds of tile 0/0/0
bounds: [ -180.0, -90.0, 180.0, 90.0 ]
# name for the tiling scheme
identifier: WGS84Quad

# Associative arrays of function sources
functions:
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub mod pg;
pub mod pmtiles;
mod source;
pub mod srv;
mod tilesystems;
mod utils;

#[cfg(test)]
Expand Down
13 changes: 13 additions & 0 deletions src/pg/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::pg::config_table::TableInfoSources;
use crate::pg::configurator::PgBuilder;
use crate::pg::utils::Result;
use crate::source::{IdResolver, Sources};
use crate::tilesystems::TileSystem;
use crate::utils::{sorted_opt_map, BoolOrObject, OneOrMany};

pub trait PgInfo {
Expand Down Expand Up @@ -68,6 +69,7 @@ pub struct PgCfgPublish {
pub struct PgCfgPublishType {
pub from_schemas: Option<OneOrMany<String>>,
pub id_format: Option<String>,
pub tile_systems: Option<Vec<TileSystem>>,
}

impl PgConfig {
Expand Down Expand Up @@ -116,6 +118,7 @@ mod tests {
use crate::pg::config_function::FunctionInfo;
use crate::pg::config_table::TableInfo;
use crate::test_utils::some;
use crate::tilesystems::TileSystemConfig;
use crate::utils::OneOrMany::{Many, One};

#[test]
Expand Down Expand Up @@ -188,6 +191,11 @@ mod tests {
geometry_type: GEOMETRY
properties:
gid: int4
tile_system:
identifier: WGS84Quad
srid: 4326
bounds: [-180, -90, 180, 90]


functions:
function_zxy_query:
Expand Down Expand Up @@ -221,6 +229,11 @@ mod tests {
"gid".to_string(),
"int4".to_string(),
)])),
tile_system: Some(TileSystemConfig {
srid: 4326,
bounds: Bounds::new(-180.0, -90.0, 180.0, 90.0),
identifier: "WGS84Quad".to_string(),
}),
..Default::default()
},
)])),
Expand Down
5 changes: 5 additions & 0 deletions src/pg/config_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use serde_yaml::Value;
use tilejson::{Bounds, TileJSON, VectorLayer};

use crate::pg::config::PgInfo;
use crate::tilesystems::TileSystemConfig;
use crate::utils::{sorted_opt_map, InfoMap};

pub type TableInfoSources = InfoMap<TableInfo>;
Expand Down Expand Up @@ -78,6 +79,10 @@ pub struct TableInfo {
#[serde(skip_deserializing, skip_serializing)]
pub prop_mapping: HashMap<String, String>,

/// Alternative tiling to standard web-mercator
#[serde(skip_serializing_if = "Option::is_none")]
pub tile_system: Option<TileSystemConfig>,

#[serde(flatten, skip_serializing)]
pub unrecognized: HashMap<String, Value>,
}
Expand Down
118 changes: 91 additions & 27 deletions src/pg/configurator.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::cmp::Ordering;
use std::collections::{HashMap, HashSet};

use crate::OneOrMany::Many;
use futures::future::join_all;
use itertools::Itertools;
use log::{debug, error, info, warn};
Expand All @@ -15,7 +16,8 @@ use crate::pg::table_source::{calc_srid, get_table_sources, merge_table_info, ta
use crate::pg::utils::PgError::InvalidTableExtent;
use crate::pg::utils::Result;
use crate::source::{IdResolver, Sources};
use crate::utils::{find_info, normalize_key, BoolOrObject, InfoMap, OneOrMany};
use crate::tilesystems::{TileSystem, TileSystemConfig};
use crate::utils::{find_info, normalize_key, BoolOrObject, InfoMap, OneOrMany, OneOrMany::One};

pub type SqlFuncInfoMapMap = InfoMap<InfoMap<(PgSqlInfo, FunctionInfo)>>;
pub type SqlTableInfoMapMapMap = InfoMap<InfoMap<InfoMap<TableInfo>>>;
Expand All @@ -24,18 +26,35 @@ pub type SqlTableInfoMapMapMap = InfoMap<InfoMap<InfoMap<TableInfo>>>;
pub struct PgBuilderPublish {
id_format: String,
schemas: Option<HashSet<String>>,
tile_systems: OneOrMany<TileSystem>,
}

impl PgBuilderPublish {
pub fn new(
is_function: bool,
id_format: Option<&String>,
schemas: Option<HashSet<String>>,
maybe_tile_systems: &Option<Vec<TileSystem>>,
) -> Self {
let tile_systems = match maybe_tile_systems {
None => One(TileSystem::default()),
Some(tss) => {
if tss.len() < 2 {
One(tss.first().map_or_else(Default::default, Clone::clone))
} else {
Many(tss.clone())
}
}
};

let id_format = id_format
.cloned()
.unwrap_or_else(|| (if is_function { "{function}" } else { "{table}" }).to_string());
Self { id_format, schemas }
Self {
id_format,
schemas,
tile_systems,
}
}
}

Expand Down Expand Up @@ -75,6 +94,7 @@ impl PgBuilder {
// Match configured sources with the discovered ones and add them to the pending list.
let mut used = HashSet::<(&str, &str, &str)>::new();
let mut pending = Vec::new();

for (id, cfg_inf) in &self.tables {
// TODO: move this validation to serde somehow?
if let Some(extent) = cfg_inf.extent {
Expand All @@ -94,6 +114,7 @@ impl PgBuilder {
let Some(cfg_inf) = merge_table_info(self.default_srid, &id2, cfg_inf, src_inf) else { continue };
warn_on_rename(id, &id2, "Table");
info!("Configured {dup}source {id2} from {}", summary(&cfg_inf));

pending.push(table_to_query(
id2,
cfg_inf,
Expand All @@ -105,6 +126,8 @@ impl PgBuilder {

// Sort the discovered sources by schema, table and geometry column to ensure a consistent behavior
if let Some(auto_tables) = &self.auto_tables {
let tile_systems = &auto_tables.tile_systems;

let schemas = auto_tables
.schemas
.as_ref()
Expand All @@ -114,7 +137,7 @@ impl PgBuilder {
let Some(schema) = normalize_key(&all_tables, schema, "schema", "") else { continue };
let tables = all_tables.remove(&schema).unwrap();
for (table, geoms) in tables.into_iter().sorted_by(by_key) {
for (column, mut src_inf) in geoms.into_iter().sorted_by(by_key) {
for (column, src_inf) in geoms.into_iter().sorted_by(by_key) {
if used.contains(&(schema.as_str(), table.as_str(), column.as_str())) {
continue;
}
Expand All @@ -123,17 +146,33 @@ impl PgBuilder {
.replace("{schema}", &schema)
.replace("{table}", &table)
.replace("{column}", &column);
let id2 = self.resolve_id(&source_id, &src_inf);
let Some(srid) = calc_srid(&src_inf.format_id(), &id2, src_inf.srid, 0, self.default_srid) else { continue };
src_inf.srid = srid;
info!("Discovered source {id2} from {}", summary(&src_inf));
pending.push(table_to_query(
id2,
src_inf,
self.pool.clone(),
self.disable_bounds,
self.max_feature_count,
));

for tile_system in tile_systems.clone() {
let mut id2 = self.resolve_id(&source_id, &src_inf);

match &tile_system {
TileSystem::Custom(TileSystemConfig { identifier, .. }) => {
id2 += ":";
id2 += identifier.as_str();
}
TileSystem::WebMercatorQuad => {}
}

let Some(srid) = calc_srid(&src_inf.format_id(), &id2, src_inf.srid, 0, self.default_srid) else { continue };
let mut table_info = src_inf.clone();
table_info.srid = srid;
table_info.tile_system = tile_system.clone().into();

info!("Discovered source {id2} from {}", summary(&src_inf));

pending.push(table_to_query(
id2,
table_info,
self.pool.clone(),
self.disable_bounds,
self.max_feature_count,
));
}
}
}
}
Expand Down Expand Up @@ -232,7 +271,7 @@ impl PgBuilder {
}

fn new_auto_publish(config: &PgConfig, is_function: bool) -> Option<PgBuilderPublish> {
let default = |schemas| Some(PgBuilderPublish::new(is_function, None, schemas));
let default = |schemas| Some(PgBuilderPublish::new(is_function, None, schemas, &None));

if let Some(bo_a) = &config.auto_publish {
match bo_a {
Expand All @@ -242,6 +281,7 @@ fn new_auto_publish(config: &PgConfig, is_function: bool) -> Option<PgBuilderPub
is_function,
item.id_format.as_ref(),
merge_opt_hs(&a.from_schemas, &item.from_schemas),
&item.tile_systems,
)),
BoolOrObject::Bool(true) => default(merge_opt_hs(&a.from_schemas, &None)),
BoolOrObject::Bool(false) => None,
Expand Down Expand Up @@ -313,14 +353,20 @@ fn merge_opt_hs(
#[cfg(test)]
mod tests {
use indoc::indoc;
use tilejson::Bounds;

use super::*;

#[allow(clippy::unnecessary_wraps)]
fn builder(id_format: &str, schemas: Option<&[&str]>) -> Option<PgBuilderPublish> {
fn builder(
id_format: &str,
schemas: Option<&[&str]>,
tile_systems: Option<Vec<TileSystem>>,
) -> Option<PgBuilderPublish> {
Some(PgBuilderPublish {
id_format: id_format.to_string(),
schemas: schemas.map(|s| s.iter().map(|s| (*s).to_string()).collect()),
tile_systems: tile_systems.map_or_else(|| One(TileSystem::default()), Many),
})
}

Expand All @@ -332,9 +378,9 @@ mod tests {
fn test_auto_publish_no_auto() {
let config = parse_yaml("{}");
let res = new_auto_publish(&config, false);
assert_eq!(res, builder("{table}", None));
assert_eq!(res, builder("{table}", None, None));
let res = new_auto_publish(&config, true);
assert_eq!(res, builder("{function}", None));
assert_eq!(res, builder("{function}", None, None));

let config = parse_yaml("tables: {}");
assert_eq!(new_auto_publish(&config, false), None);
Expand All @@ -349,9 +395,9 @@ mod tests {
fn test_auto_publish_bool() {
let config = parse_yaml("auto_publish: true");
let res = new_auto_publish(&config, false);
assert_eq!(res, builder("{table}", None));
assert_eq!(res, builder("{table}", None, None));
let res = new_auto_publish(&config, true);
assert_eq!(res, builder("{function}", None));
assert_eq!(res, builder("{function}", None, None));

let config = parse_yaml("auto_publish: false");
assert_eq!(new_auto_publish(&config, false), None);
Expand All @@ -365,7 +411,7 @@ mod tests {
from_schemas: public
tables: true"});
let res = new_auto_publish(&config, false);
assert_eq!(res, builder("{table}", Some(&["public"])));
assert_eq!(res, builder("{table}", Some(&["public"]), None));
assert_eq!(new_auto_publish(&config, true), None);

let config = parse_yaml(indoc! {"
Expand All @@ -374,22 +420,22 @@ mod tests {
functions: true"});
assert_eq!(new_auto_publish(&config, false), None);
let res = new_auto_publish(&config, true);
assert_eq!(res, builder("{function}", Some(&["public"])));
assert_eq!(res, builder("{function}", Some(&["public"]), None));

let config = parse_yaml(indoc! {"
auto_publish:
from_schemas: public
tables: false"});
assert_eq!(new_auto_publish(&config, false), None);
let res = new_auto_publish(&config, true);
assert_eq!(res, builder("{function}", Some(&["public"])));
assert_eq!(res, builder("{function}", Some(&["public"]), None));

let config = parse_yaml(indoc! {"
auto_publish:
from_schemas: public
functions: false"});
let res = new_auto_publish(&config, false);
assert_eq!(res, builder("{table}", Some(&["public"])));
assert_eq!(res, builder("{table}", Some(&["public"]), None));
assert_eq!(new_auto_publish(&config, true), None);
}

Expand All @@ -400,9 +446,27 @@ mod tests {
from_schemas: public
tables:
from_schemas: osm
id_format: '{schema}.{table}'"});
id_format: '{schema}.{table}'
tile_systems:
- type: WebMercatorQuad
- type: Custom
srid: 4326
identifier: WGS84Quad
bounds: [-180, -90, 180, 90]"});
let res = new_auto_publish(&config, false);
assert_eq!(res, builder("{schema}.{table}", Some(&["public", "osm"])));
let tile_systems = Some(vec![
TileSystem::WebMercatorQuad,
TileSystem::Custom(TileSystemConfig {
identifier: "WGS84Quad".to_string(),
srid: 4326,
bounds: Bounds::new(-180.0, -90.0, 180.0, 90.0),
}),
]);

assert_eq!(
res,
builder("{schema}.{table}", Some(&["public", "osm"]), tile_systems)
);
assert_eq!(new_auto_publish(&config, true), None);

let config = parse_yaml(indoc! {"
Expand All @@ -412,7 +476,7 @@ mod tests {
- osm
- public"});
let res = new_auto_publish(&config, false);
assert_eq!(res, builder("{table}", Some(&["public", "osm"])));
assert_eq!(res, builder("{table}", Some(&["public", "osm"]), None));
assert_eq!(new_auto_publish(&config, true), None);
}
}
Loading