Skip to content

Commit 1edfc3c

Browse files
committed
editoast: use geojson crate instead of our manual implementation
1 parent 9f0d883 commit 1edfc3c

File tree

6 files changed

+40
-53
lines changed

6 files changed

+40
-53
lines changed

editoast/Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

editoast/editoast_common/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ edition = "2021"
99
chrono.workspace = true
1010
geos.workspace = true
1111
iso8601 = "0.6.1"
12-
mvt.workspace = true
1312
postgis_diesel.workspace = true
1413
rand.workspace = true
1514
rangemap.workspace = true

editoast/editoast_common/src/geo_json.rs

-25
This file was deleted.

editoast/editoast_common/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
pub mod duration;
2-
pub mod geo_json;
32
pub mod geometry;
43
mod hash_rounded_float;
54
mod identifier;

editoast/src/views/infra/objects.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ use diesel::sql_types::Nullable;
1414
use diesel::sql_types::Text;
1515
use diesel::QueryableByName;
1616
use diesel_async::RunQueryDsl;
17-
use diesel_json::Json as DieselJson;
1817
use editoast_derive::EditoastError;
18+
use postgis_diesel::sql_types::Geometry;
19+
use postgis_diesel::types::GeometryContainer;
20+
use postgis_diesel::types::Point;
1921
use serde::Deserialize;
2022
use serde::Serialize;
2123
use serde_json::Value as JsonValue;
@@ -25,7 +27,6 @@ use crate::error::Result;
2527
use crate::modelsv2::get_geometry_layer_table;
2628
use crate::modelsv2::get_table;
2729
use crate::DbPool;
28-
use editoast_common::geo_json::GeoJson;
2930
use editoast_schemas::primitives::ObjectType;
3031

3132
/// Return `/infra/<infra_id>/objects` routes
@@ -53,10 +54,10 @@ struct ObjectQueryable {
5354
obj_id: String,
5455
#[diesel(sql_type = Jsonb)]
5556
railjson: JsonValue,
56-
#[diesel(sql_type = Nullable<Jsonb>)]
57-
geographic: Option<DieselJson<GeoJson>>,
58-
#[diesel(sql_type = Nullable<Jsonb>)]
59-
schematic: Option<DieselJson<GeoJson>>,
57+
#[diesel(sql_type = Nullable<Geometry>)]
58+
geographic: Option<GeometryContainer<Point>>,
59+
#[diesel(sql_type = Nullable<Geometry>)]
60+
schematic: Option<GeometryContainer<Point>>,
6061
}
6162

6263
/// Return the railjson list of a specific OSRD object
@@ -75,16 +76,16 @@ async fn get_objects(
7576
let query = if [ObjectType::SwitchType, ObjectType::Route].contains(&obj_type) {
7677
format!(
7778
"SELECT obj_id as obj_id, data as railjson, NULL as geographic, NULL as schematic
78-
FROM {} WHERE infra_id = $1 AND obj_id = ANY($2) ",
79+
FROM {} WHERE infra_id = $1 AND obj_id = ANY($2)",
7980
get_table(&obj_type)
8081
)
8182
} else {
8283
format!("
8384
SELECT
8485
object_table.obj_id as obj_id,
8586
object_table.data as railjson,
86-
ST_AsGeoJSON(ST_Transform(geographic, 4326))::jsonb as geographic,
87-
ST_AsGeoJSON(ST_Transform(schematic, 4326))::jsonb as schematic
87+
ST_Transform(geographic, 4326) as geographic,
88+
ST_Transform(schematic, 4326) as schematic
8889
FROM {} AS object_table
8990
LEFT JOIN {} AS geometry_table ON object_table.obj_id = geometry_table.obj_id AND object_table.infra_id = geometry_table.infra_id
9091
WHERE object_table.infra_id = $1 AND object_table.obj_id = ANY($2)

editoast/src/views/layers/mvt_utils.rs

+30-16
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
use diesel::sql_types::Jsonb;
22
use diesel::sql_types::Text;
3+
use geos::geojson::Geometry;
4+
use geos::geojson::Value as GeoJsonValue;
35
use mvt::Feature;
46
use mvt::GeomData;
57
use mvt::GeomEncoder;
8+
use mvt::GeomType;
69
use mvt::Tile as MvtTile;
710
use serde::Deserialize;
811
use serde::Serialize;
912
use serde_json::Value as JsonValue;
1013

1114
use crate::map::View;
12-
use editoast_common::geo_json::GeoJson;
1315

1416
#[derive(Clone, QueryableByName, Queryable, Debug, Serialize, Deserialize)]
1517
pub struct GeoJsonAndData {
@@ -19,33 +21,45 @@ pub struct GeoJsonAndData {
1921
pub data: JsonValue,
2022
}
2123

24+
fn geometry_into_mvt_geom_type(geometry: &Geometry) -> GeomType {
25+
match geometry.value {
26+
GeoJsonValue::Point { .. } => GeomType::Point,
27+
GeoJsonValue::MultiPoint { .. } => GeomType::Point,
28+
GeoJsonValue::LineString { .. } => GeomType::Linestring,
29+
GeoJsonValue::MultiLineString { .. } => GeomType::Linestring,
30+
_ => panic!("geometry type unsupported by editoast tiling system"),
31+
}
32+
}
33+
2234
impl GeoJsonAndData {
2335
/// Converts GeoJsonAndData as mvt GeomData
2436
pub fn as_geom_data(&self) -> GeomData {
25-
let geo_json = serde_json::from_str::<GeoJson>(&self.geo_json).unwrap();
26-
let mut encoder = GeomEncoder::new(geo_json.get_geom_type());
27-
match geo_json {
28-
GeoJson::Point { coordinates } => {
29-
encoder.add_point(coordinates.0, coordinates.1).unwrap();
37+
let geo_json = serde_json::from_str::<Geometry>(&self.geo_json).unwrap();
38+
let geom_type = geometry_into_mvt_geom_type(&geo_json);
39+
let mut encoder = GeomEncoder::new(geom_type);
40+
match geo_json.value {
41+
GeoJsonValue::Point(point) => {
42+
encoder.add_point(point[0], point[1]).unwrap();
3043
}
31-
GeoJson::MultiPoint { coordinates } => {
32-
for (x, y) in coordinates {
33-
encoder.add_point(x, y).unwrap();
44+
GeoJsonValue::MultiPoint(points) => {
45+
for point in points {
46+
encoder.add_point(point[0], point[1]).unwrap();
3447
}
3548
}
36-
GeoJson::LineString { coordinates } => {
37-
for (x, y) in coordinates {
38-
encoder.add_point(x, y).unwrap();
49+
GeoJsonValue::LineString(linestring) => {
50+
for point in linestring {
51+
encoder.add_point(point[0], point[1]).unwrap();
3952
}
4053
}
41-
GeoJson::MultiLineString { coordinates } => {
42-
for line in coordinates {
43-
for (x, y) in line.iter() {
44-
encoder.add_point(*x, *y).unwrap();
54+
GeoJsonValue::MultiLineString(linestrings) => {
55+
for linestring in linestrings {
56+
for point in linestring.iter() {
57+
encoder.add_point(point[0], point[1]).unwrap();
4558
}
4659
encoder.complete_geom().unwrap();
4760
}
4861
}
62+
_ => panic!("geometry type unsupported by editoast tiling system"),
4963
};
5064
encoder.complete().unwrap().encode().unwrap()
5165
}

0 commit comments

Comments
 (0)