Skip to content

Commit

Permalink
fix: update tile endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
zerj9 committed Dec 15, 2024
1 parent ef515c0 commit e79a132
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 19 deletions.
29 changes: 23 additions & 6 deletions gridwalk-backend/src/core/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,14 @@ pub trait GeoConnector: Send + Sync {
async fn disconnect(&mut self) -> Result<()>;
async fn create_namespace(&self, name: &str) -> Result<()>;
async fn list_sources(&self, namespace: &str) -> Result<Vec<String>>;
async fn get_tile(&self, z: u32, x: u32, y: u32) -> Result<Vec<u8>>;
async fn get_tile(
&self,
namespace: &str,
source_name: &str,
z: u32,
x: u32,
y: u32,
) -> Result<Vec<u8>>;
}

#[derive(Clone, Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -195,14 +202,24 @@ impl GeoConnector for PostgisConnector {
.await
.map_err(|e| anyhow!("Failed to execute query to list sources: {}", e))?;
let sources: Vec<String> = rows.iter().map(|row| row.get(0)).collect();
let test_tile = self
.get_tile("f57e7ba0-a30f-47bd-b641-11d5e25b9978", "test", 0, 0, 0)
.await;
println!("{:?}", test_tile);
Ok(sources)
}

async fn get_tile(&self, z: u32, x: u32, y: u32) -> Result<Vec<u8>> {
async fn get_tile(
&self,
namespace: &str,
source_name: &str,
z: u32,
x: u32,
y: u32,
) -> Result<Vec<u8>> {
println!("Fetching MVT for tile z:{} x:{} y:{}", z, x, y);
let pool = self.pool.as_ref();
let client = pool.get().await?;
let _table_name = "test";
let _geom_column = "geom";
let query = format!(
"
Expand All @@ -223,22 +240,22 @@ mvt_data AS (
) AS geom,
t.name
FROM
test t,
{table} t,
bounds
WHERE
ST_Intersects(t.geom, bounds.geom)
)
SELECT ST_AsMVT(mvt_data.*, 'blah', 4096, 'geom') AS mvt
FROM mvt_data;
",
table = format!("\"{}\".\"{}\"", namespace, source_name),
z = z,
x = x,
y = y,
);
let row = client.query_one(&query, &[]).await?;
println!("{row:?}");
//println!("{row:?}"); // Debugging
let mvt_data: Vec<u8> = row.get(0);
println!("{mvt_data:?}");
Ok(mvt_data)
}
}
Expand Down
4 changes: 2 additions & 2 deletions gridwalk-backend/src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ mod connector;
mod layer;
mod os_token;
mod project;
//mod tiles;
mod tiles;
mod user;
mod workspace;

pub use connector::*;
pub use layer::*;
pub use os_token::*;
pub use project::*;
//pub use tiles::*;
pub use tiles::*;
pub use user::*;
pub use workspace::*;
51 changes: 40 additions & 11 deletions gridwalk-backend/src/routes/tiles.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::app_state::AppState;
use crate::core::Connection;
//use crate::data::Database;
use crate::auth::AuthUser;
use crate::core::{ConnectionAccess, Workspace};
use axum::{
extract::{Path, State},
extract::{Extension, Path, State},
http::{header, StatusCode},
response::{IntoResponse, Response},
};
Expand All @@ -11,22 +11,51 @@ use std::sync::Arc;

pub async fn tiles(
State(state): State<Arc<AppState>>,
Path((workspace_id, connection_id, z, x, y)): Path<(String, String, u32, u32, u32)>,
Extension(auth_user): Extension<AuthUser>,
Path((workspace_id, source_name, connection_id, z, x, y)): Path<(
String,
String,
String,
u32,
u32,
u32,
)>,
) -> impl IntoResponse {
let connection = Connection::from_id(&state.app_data, &workspace_id, &connection_id)
let _user = auth_user
.user
.as_ref()
.ok_or_else(|| (StatusCode::UNAUTHORIZED, ""));

// Get the workspace
let workspace = match Workspace::from_id(&state.app_data, &workspace_id).await {
Ok(ws) => ws,
Err(_) => return "workspace not found".into_response(),
};

// Check if user is a member of the workspace
let _workspace_member = workspace
.get_member(&state.app_data, &auth_user.user.unwrap())
.await
.unwrap();
.map_err(|_| (StatusCode::FORBIDDEN, ""));

// Check if workspace has access to the connection namespace
let _connection_access = ConnectionAccess::get(&state.app_data, &workspace, &connection_id)
.await
.map_err(|_| (StatusCode::NOT_FOUND, ""));

let geoconnector = state
.geospatial_config
.get_connection(&connection.id)
.geo_connections
.get_connection(&connection_id)
.await
.unwrap();

let tile = geoconnector.get_tile(z, x, y).await.unwrap();
let tile = geoconnector
.get_tile(&workspace_id, &source_name, z, x, y)
.await
.unwrap();

println!("tile");
println!("{tile:?}");
//println!("tile");
//println!("{tile:?}");

Response::builder()
.status(StatusCode::OK)
Expand Down
1 change: 1 addition & 0 deletions gridwalk-backend/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub fn create_app(app_state: AppState) -> Router {
)
.route("/create_project", post(create_project))
.route("/upload_layer", post(upload_layer))
.route("/workspaces/:workspace_id/connections/:connection_id/sources/:source_name/tiles/:z/:x/:y", get(tiles))
.layer(DefaultBodyLimit::disable())
.layer(RequestBodyLimitLayer::new(100 * 1024 * 1024))
.layer(middleware::from_fn_with_state(
Expand Down

0 comments on commit e79a132

Please sign in to comment.