-
-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add composite sources support (#184)
* feat: add composite sources support WIP * feat: handle empty composite sources * fix: decompose queries * docs: add docs on composite sources * ci: push docker image * test: add composite source tests
- Loading branch information
1 parent
0fc303a
commit 3c01125
Showing
21 changed files
with
499 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use itertools::Itertools; | ||
use std::io; | ||
|
||
use tilejson::{TileJSON, TileJSONBuilder}; | ||
|
||
use crate::db::Connection; | ||
use crate::source::{Query, Source, Tile, XYZ}; | ||
use crate::table_source::TableSource; | ||
use crate::utils; | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct CompositeSource { | ||
pub id: String, | ||
pub table_sources: Vec<TableSource>, | ||
} | ||
|
||
impl CompositeSource { | ||
fn get_bounds_cte(&self, xyz: &XYZ) -> String { | ||
let srid_bounds: String = self | ||
.table_sources | ||
.clone() | ||
.into_iter() | ||
.map(|source| source.srid) | ||
.unique() | ||
.map(|srid| utils::get_srid_bounds(srid, xyz)) | ||
.collect::<Vec<String>>() | ||
.join(", "); | ||
|
||
utils::get_bounds_cte(srid_bounds) | ||
} | ||
|
||
fn get_tile_query(&self, xyz: &XYZ) -> String { | ||
let tile_query: String = self | ||
.table_sources | ||
.clone() | ||
.into_iter() | ||
.map(|source| format!("({})", source.get_tile_query(xyz))) | ||
.collect::<Vec<String>>() | ||
.join(" || "); | ||
|
||
format!("SELECT {} AS tile", tile_query) | ||
} | ||
|
||
pub fn build_tile_query(&self, xyz: &XYZ) -> String { | ||
let bounds_cte = self.get_bounds_cte(xyz); | ||
let tile_query = self.get_tile_query(xyz); | ||
|
||
format!("{} {}", bounds_cte, tile_query) | ||
} | ||
} | ||
|
||
impl Source for CompositeSource { | ||
fn get_id(&self) -> &str { | ||
self.id.as_str() | ||
} | ||
|
||
fn get_tilejson(&self) -> Result<TileJSON, io::Error> { | ||
let mut tilejson_builder = TileJSONBuilder::new(); | ||
|
||
tilejson_builder.scheme("xyz"); | ||
tilejson_builder.name(&self.id); | ||
|
||
Ok(tilejson_builder.finalize()) | ||
} | ||
|
||
fn get_tile( | ||
&self, | ||
conn: &mut Connection, | ||
xyz: &XYZ, | ||
_query: &Option<Query>, | ||
) -> Result<Tile, io::Error> { | ||
let tile_query = self.build_tile_query(xyz); | ||
|
||
let tile: Tile = conn | ||
.query_one(tile_query.as_str(), &[]) | ||
.map(|row| row.get("tile")) | ||
.map_err(|err| io::Error::new(io::ErrorKind::Other, err.to_string()))?; | ||
|
||
Ok(tile) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
#[macro_use] | ||
extern crate log; | ||
|
||
pub mod composite_source; | ||
pub mod config; | ||
pub mod coordinator_actor; | ||
pub mod db; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
WITH bounds AS (SELECT {srid_bounds}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
SELECT | ||
ST_AsMVTGeom (ST_Transform ({geometry_column}, 3857), {mercator_bounds}, {extent}, {buffer}, {clip_geom}) AS geom {properties} FROM {id}, bounds | ||
WHERE | ||
{geometry_column} && bounds.srid_{srid} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ST_Transform({mercator_bounds}, {srid}) AS srid_{srid} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,2 @@ | ||
WITH bounds AS (SELECT {mercator_bounds} as mercator, {original_bounds} as original) | ||
SELECT ST_AsMVT(tile, '{id}', {extent}, 'geom' {id_column}) FROM ( | ||
SELECT | ||
ST_AsMVTGeom({geometry_column_mercator}, bounds.mercator, {extent}, {buffer}, {clip_geom}) AS geom {properties} | ||
FROM {id}, bounds | ||
WHERE {geometry_column} && bounds.original | ||
) AS tile WHERE geom IS NOT NULL | ||
SELECT | ||
ST_AsMVT (tile, '{id}', {extent}, 'geom' {id_column}) FROM ({geom_query}) AS tile |
Oops, something went wrong.