Skip to content

Commit

Permalink
feat: added in get tile functionality for generating vector tiles
Browse files Browse the repository at this point in the history
  • Loading branch information
CHRISCARLON committed Oct 21, 2024
1 parent e89cd94 commit 2efb718
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
9 changes: 8 additions & 1 deletion backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ axum-extra = { version = "0.9.4", features = ["typed-header"] }
base64 = "0.22.1"
deadpool-postgres = "0.14.0"
duckdb-postgis = "0.1.4"
martin = { git = "https://github.com/enmeshed-analytics/martin.git", features = ["postgres"] }
geozero = { version = "0.14.0", features = [
"with-postgis-postgres",
"with-postgis-sqlx",
"with-mvt",
] }
martin = { git = "https://github.com/enmeshed-analytics/martin.git", features = [
"postgres",
] }
martin-tile-utils = { git = "https://github.com/enmeshed-analytics/martin.git" }
rand = "0.8.5"
rand_core = { version = "0.6", features = ["std"] }
Expand Down
41 changes: 36 additions & 5 deletions backend/src/core/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub trait GeoConnector: Send + Sync {
async fn connect(&mut self) -> Result<()>;
async fn disconnect(&mut self) -> Result<()>;
async fn list_sources(&self) -> Result<Vec<String>>;
async fn get_tile(&self) -> Result<Vec<u8>>;
}

#[derive(Clone, Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -112,11 +113,11 @@ impl GeoConnector for PostgisConfig {

let rows = client
.query(
"SELECT DISTINCT f.table_name
FROM information_schema.columns f
JOIN pg_type t ON f.udt_name = t.typname
WHERE t.typtype = 'b'
AND t.typname IN ('geometry', 'geography')
"SELECT DISTINCT f.table_name
FROM information_schema.columns f
JOIN pg_type t ON f.udt_name = t.typname
WHERE t.typtype = 'b'
AND t.typname IN ('geometry', 'geography')
AND f.table_schema = 'public'",
&[],
)
Expand All @@ -127,6 +128,36 @@ impl GeoConnector for PostgisConfig {
println!("Found {} sources", sources.len());
Ok(sources)
}

async fn get_tile(&self) -> Result<Vec<u8>> {
println!("Fetching all data as MVT...");
let pool = self.pool.as_ref();
let client = pool.get().await?;

// Replace these with your actual table and geometry column names
let table_name = "your_table_name";
let geom_column = "geom";

let query = format!(
"SELECT ST_AsMVT(q, 'layer', 4096, 'geom')
FROM (
SELECT ST_AsMVTGeom(
{geom},
ST_TileEnvelope(0, 0, 0), -- World bounds
4096, 256, true
) AS geom,
*
FROM {table}
) AS q",
geom = geom_column,
table = table_name
);

let row = client.query_one(&query, &[]).await?;
let mvt_data: Vec<u8> = row.get(0);

Ok(mvt_data)
}
}

#[derive(Clone)]
Expand Down

0 comments on commit 2efb718

Please sign in to comment.