Skip to content

Commit

Permalink
editoast: refactor layers get records
Browse files Browse the repository at this point in the history
  • Loading branch information
Wadjetz committed Jul 4, 2024
1 parent c61b95f commit bd5d5e7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
17 changes: 4 additions & 13 deletions editoast/src/views/layers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ use actix_web::web::Json;
use actix_web::web::Path;
use actix_web::web::Query;
use actix_web::HttpResponse;
use diesel::sql_query;
use diesel::sql_types::Integer;
use diesel_async::RunQueryDsl;
use editoast_derive::EditoastError;
use mvt_utils::create_and_fill_mvt_tile;
use mvt_utils::get_geo_json_sql_query;
use mvt_utils::GeoJsonAndData;
use mvt_utils::GeoPoint;
use redis::AsyncCommands;
use serde::Deserialize;
use serde::Serialize;
Expand Down Expand Up @@ -208,15 +205,9 @@ async fn cache_and_get_mvt_tile(
.body(value));
}

let geo_json_query = get_geo_json_sql_query(&layer.table_name, view);
let mut conn = db_pool.get().await?;
let records = sql_query(geo_json_query)
.bind::<Integer, _>(z as i32)
.bind::<Integer, _>(x as i32)
.bind::<Integer, _>(y as i32)
.bind::<Integer, _>(infra as i32)
.get_results::<GeoJsonAndData>(&mut conn)
.await?;
let conn = &mut db_pool.get().await?;
let records =
GeoJsonAndData::get_records(conn, &layer, view, infra, &GeoPoint::new(z, x, y)).await?;

let mvt_bytes: Vec<u8> = create_and_fill_mvt_tile(layer_slug, records)
.to_bytes()
Expand Down
38 changes: 38 additions & 0 deletions editoast/src/views/layers/mvt_utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use diesel::sql_query;
use diesel::sql_types::Integer;
use diesel::sql_types::Jsonb;
use diesel::sql_types::Text;
use diesel_async::RunQueryDsl;
use editoast_models::DbConnection;
use editoast_models::EditoastModelsError;
use geos::geojson::Geometry;
use geos::geojson::Value as GeoJsonValue;
use mvt::Feature;
Expand All @@ -11,6 +16,7 @@ use serde::Deserialize;
use serde::Serialize;
use serde_json::Value as JsonValue;

use crate::map::Layer;
use crate::map::View;

#[derive(Clone, QueryableByName, Queryable, Debug, Serialize, Deserialize)]
Expand All @@ -31,7 +37,39 @@ fn geometry_into_mvt_geom_type(geometry: &Geometry) -> GeomType {
}
}

#[derive(Debug)]
pub struct GeoPoint {
x: u64,
y: u64,
z: u64,
}

impl GeoPoint {
pub fn new(x: u64, y: u64, z: u64) -> Self {
Self { x, y, z }
}
}

impl GeoJsonAndData {
pub async fn get_records(
conn: &mut DbConnection,
layer: &Layer,
view: &View,
infra: i64,
geo_point: &GeoPoint,
) -> Result<Vec<GeoJsonAndData>, EditoastModelsError> {
let geo_json_query = get_geo_json_sql_query(&layer.table_name, view);
let records = sql_query(geo_json_query)
.bind::<Integer, _>(geo_point.z as i32)
.bind::<Integer, _>(geo_point.x as i32)
.bind::<Integer, _>(geo_point.y as i32)
.bind::<Integer, _>(infra as i32)
.get_results::<GeoJsonAndData>(conn)
.await?;

Ok(records)
}

/// Converts GeoJsonAndData as mvt GeomData
pub fn as_geom_data(&self) -> GeomData {
let geo_json = serde_json::from_str::<Geometry>(&self.geo_json).unwrap();
Expand Down

0 comments on commit bd5d5e7

Please sign in to comment.