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

Allow ignore small geometries #792

Draft
wants to merge 1 commit 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
14 changes: 14 additions & 0 deletions martin/src/args/pg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ pub struct PgArgs {
/// Limit the number of features in a tile from a PG table source.
#[arg(short, long)]
pub max_feature_count: Option<usize>,
/// Hide geometries which can not fullfill one single pixel
#[arg(short, long)]
pub exclude_small_geometries: Option<bool>,
}

impl PgArgs {
Expand All @@ -48,6 +51,7 @@ impl PgArgs {
None
},
max_feature_count: self.max_feature_count,
exclude_small_geometries: self.exclude_small_geometries,
pool_size: self.pool_size,
auto_publish: None,
tables: None,
Expand Down Expand Up @@ -82,6 +86,16 @@ impl PgArgs {
});
}

if self.exclude_small_geometries.is_some() {
info!(
"Overriding exclude small geometries to {} because of a CLI parameter",
self.exclude_small_geometries.unwrap()
);
pg_config.iter_mut().for_each(|c| {
c.exclude_small_geometries = self.exclude_small_geometries;
});
}

#[cfg(feature = "ssl")]
if self.ca_root_file.is_some() {
info!("Overriding root certificate file to {} on all Postgres connections because of a CLI parameter",
Expand Down
2 changes: 2 additions & 0 deletions martin/src/pg/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ pub struct PgConfig {
#[serde(skip_serializing_if = "Option::is_none")]
pub max_feature_count: Option<usize>,
#[serde(skip_serializing_if = "Option::is_none")]
pub exclude_small_geometries: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub pool_size: Option<usize>,
#[serde(skip_serializing_if = "Option::is_none")]
pub auto_publish: Option<BoolOrObject<PgCfgPublish>>,
Expand Down
4 changes: 4 additions & 0 deletions martin/src/pg/configurator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub struct PgBuilder {
default_srid: Option<i32>,
disable_bounds: bool,
max_feature_count: Option<usize>,
exclude_small_geometries: Option<bool>,
auto_functions: Option<PgBuilderAuto>,
auto_tables: Option<PgBuilderAuto>,
id_resolver: IdResolver,
Expand All @@ -51,6 +52,7 @@ impl PgBuilder {
default_srid: config.default_srid,
disable_bounds: config.disable_bounds.unwrap_or_default(),
max_feature_count: config.max_feature_count,
exclude_small_geometries: config.exclude_small_geometries,
id_resolver,
tables: config.tables.clone().unwrap_or_default(),
functions: config.functions.clone().unwrap_or_default(),
Expand Down Expand Up @@ -90,6 +92,7 @@ impl PgBuilder {
self.pool.clone(),
self.disable_bounds,
self.max_feature_count,
self.exclude_small_geometries,
));
}

Expand Down Expand Up @@ -129,6 +132,7 @@ impl PgBuilder {
self.pool.clone(),
self.disable_bounds,
self.max_feature_count,
self.exclude_small_geometries,
));
}
}
Expand Down
36 changes: 33 additions & 3 deletions martin/src/pg/table_source.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::collections::HashMap;

use log::{info, warn};
use postgis::ewkb;
use postgres_protocol::escape::{escape_identifier, escape_literal};
use std::collections::HashMap;
use tilejson::Bounds;

use crate::pg::config::PgInfo;
Expand Down Expand Up @@ -90,6 +89,7 @@ pub async fn table_to_query(
pool: PgPool,
disable_bounds: bool,
max_feature_count: Option<usize>,
exclude_small_geometries: Option<bool>,
) -> Result<(String, PgSqlInfo, TableInfo)> {
let schema = escape_identifier(&info.schema);
let table = escape_identifier(&info.table);
Expand Down Expand Up @@ -139,6 +139,17 @@ pub async fn table_to_query(
let limit_clause = max_feature_count.map_or(String::new(), |v| format!("LIMIT {v}"));
let layer_id = escape_literal(info.layer_id.as_ref().unwrap_or(&id));
let clip_geom = info.clip_geom.unwrap_or(DEFAULT_CLIP_GEOM);

let small_geometries_condition = if let Some(allow_exclude_geoms) = exclude_small_geometries {
if allow_exclude_geoms {
create_small_geomtries_condition(&geometry_column, &info.geometry_type)
} else {
String::new()
}
} else {
String::new()
};

let query = format!(
r#"
SELECT
Expand All @@ -154,7 +165,7 @@ FROM (
FROM
{schema}.{table}
WHERE
{geometry_column} && ST_Transform({bbox_search}, {srid})
{geometry_column} && ST_Transform({bbox_search}, {srid}) {small_geometries_condition}
{limit_clause}
) AS tile;
"#
Expand All @@ -165,6 +176,25 @@ FROM (
Ok((id, PgSqlInfo::new(query, false, info.format_id()), info))
}

fn create_small_geomtries_condition(
geometry_column: &str,
geometry_type: &Option<String>,
) -> String {
if let Some(t) = geometry_type {
match t.as_str() {
"LINESTRING" => format!(" AND ST_LENGTH(ST_Transform({geometry_column},3857)) >= ( 40075016.68 / ( 2 ^ $1::integer * 512 ) )"),
"MULTILineString" => format!(" AND ST_LENGTH(ST_Transform({geometry_column},3857)) >= ( 40075016.68 / ( 2 ^ $1::integer * 512 ))"),
"POLYGON" => format!(" AND ST_AREA(ST_Transform({geometry_column},3857)) >= ( 40075016.68 / ( 2 ^ $1::integer * 512 ) ) ^ 2 )"),
"MULTIPOLYGON" => format!(" AND ST_AREA(ST_Transform({geometry_column},3857)) >= ( 40075016.68 / ( 2 ^ $1::integer * 512 ) ) ^ 2)"),
_ =>{
String::new()
}
}
} else {
String::new()
}
}

async fn calc_bounds(
pool: &PgPool,
schema: &str,
Expand Down
Loading