Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

editoast: refacto path item location to add track and label information #9800

Merged
merged 2 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions editoast/editoast_models/src/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ diesel::table! {
geographic -> Geometry,
infra_id -> Int8,
kp -> Nullable<Text>,
track_section -> Text,
}
}

Expand Down
3 changes: 3 additions & 0 deletions editoast/editoast_schemas/src/train_schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ pub use schedule_item::ReceptionSignal;
pub use schedule_item::ScheduleItem;

mod path_item;
pub use path_item::OperationalPointIdentifier;
pub use path_item::OperationalPointReference;
pub use path_item::PathItem;
pub use path_item::PathItemLocation;
pub use path_item::TrackReference;

mod train_schedule_options;
pub use train_schedule_options::TrainScheduleOptions;
Expand Down
30 changes: 30 additions & 0 deletions editoast/editoast_schemas/src/train_schedule/path_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use crate::infra::TrackOffset;
editoast_common::schemas! {
PathItem,
PathItemLocation,
OperationalPointReference,
TrackReference,
}

/// A location on the path of a train
Expand All @@ -32,6 +34,34 @@ pub struct PathItem {
#[serde(untagged, deny_unknown_fields)]
pub enum PathItemLocation {
TrackOffset(#[schema(inline)] TrackOffset),
OperationalPointReference(#[schema(inline)] OperationalPointReference),
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema, Hash)]
pub struct OperationalPointReference {
#[serde(flatten)]
#[schema(inline)]
pub reference: OperationalPointIdentifier,
#[serde(default)]
pub track_reference: Option<TrackReference>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema, Hash)]
#[serde(untagged, deny_unknown_fields)]
pub enum TrackReference {
Id {
#[schema(inline)]
track_id: Identifier,
},
Name {
#[schema(inline)]
track_name: NonBlankString,
},
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema, Hash)]
#[serde(untagged, deny_unknown_fields)]
pub enum OperationalPointIdentifier {
OperationalPointId {
/// The object id of an operational point
#[schema(inline)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ mod tests {
use serde_json::from_str;
use serde_json::to_string;

use crate::train_schedule::path_item::OperationalPointIdentifier::OperationalPointId;
use crate::train_schedule::path_item::OperationalPointReference;
use crate::train_schedule::schedule_item::ReceptionSignal;
use crate::train_schedule::Margins;
use crate::train_schedule::PathItemLocation;
Expand All @@ -180,9 +182,12 @@ mod tests {
/// Test deserialize an invalid train schedule
#[test]
fn deserialize_duplicate_path_id_train_schedule() {
let location = PathItemLocation::OperationalPointId {
operational_point: "op".into(),
};
let location = PathItemLocation::OperationalPointReference(OperationalPointReference {
reference: OperationalPointId {
operational_point: "op".into(),
},
track_reference: None,
});
let path_item = PathItem {
id: "a".into(),
location,
Expand Down Expand Up @@ -235,9 +240,12 @@ mod tests {
/// Test deserialize an invalid train schedule
#[test]
fn deserialize_duplicate_schedule_points_train_schedule() {
let location = PathItemLocation::OperationalPointId {
operational_point: "op".into(),
};
let location = PathItemLocation::OperationalPointReference(OperationalPointReference {
reference: OperationalPointId {
operational_point: "op".into(),
},
track_reference: None,
});
let path_item = PathItem {
id: "a".into(),
location,
Expand Down Expand Up @@ -270,9 +278,12 @@ mod tests {
/// Test deserialize an invalid train schedule
#[test]
fn deserialize_arrival_time_first_waypoint_schedule_train_schedule() {
let location = PathItemLocation::OperationalPointId {
operational_point: "op".into(),
};
let location = PathItemLocation::OperationalPointReference(OperationalPointReference {
reference: OperationalPointId {
operational_point: "op".into(),
},
track_reference: None,
});
let path_item = PathItem {
id: "a".into(),
location,
Expand Down
3 changes: 2 additions & 1 deletion editoast/map_layers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ layers:
views:
geo:
on_field: geographic
data_expr: operational_point.data - 'parts' || jsonb_build_object('kp', layer.kp)
data_expr: operational_point.data - 'parts' || jsonb_build_object('kp', layer.kp, 'track_id', layer.track_section, 'track_name', track_section.data->'extensions'->'sncf'->'track_name')
joins:
- inner join infra_object_operational_point operational_point on operational_point.obj_id = layer.obj_id and operational_point.infra_id = layer.infra_id
- inner join infra_object_track_section track_section on track_section.obj_id = layer.track_section and track_section.infra_id = layer.infra_id

electrifications:
table_name: infra_layer_electrification
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE infra_layer_operational_point DROP COLUMN track_section;
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
DELETE FROM infra_layer_operational_point;
ALTER TABLE infra_layer_operational_point ADD COLUMN track_section text NOT NULL;

WITH ops AS (
SELECT obj_id AS op_id,
infra_id,
(
jsonb_array_elements(data->'parts')->'position'
)::float AS position,
jsonb_array_elements(data->'parts')->>'track' AS track_id,
jsonb_array_elements(data->'parts')->'extensions'->'sncf'->>'kp' AS kp
FROM infra_object_operational_point
),
collect AS (
SELECT ops.op_id,
ST_LineInterpolatePoint(
tracks_layer.geographic,
LEAST(
GREATEST(
ops.position / (tracks.data->'length')::float,
0.
),
1.
)
) AS geo,
ops.kp AS kp,
ops.infra_id AS infra_id,
ops.track_id AS track_section
FROM ops
INNER JOIN infra_object_track_section AS tracks ON tracks.obj_id = ops.track_id AND tracks.infra_id = ops.infra_id
INNER JOIN infra_layer_track_section AS tracks_layer ON tracks.obj_id = tracks_layer.obj_id
AND tracks.infra_id = tracks_layer.infra_id
)
INSERT INTO infra_layer_operational_point (obj_id, infra_id, geographic, kp, track_section)
SELECT op_id,
infra_id,
geo,
kp,
track_section
FROM collect;
91 changes: 59 additions & 32 deletions editoast/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7073,6 +7073,47 @@ components:
maxLength: 255
minLength: 1
additionalProperties: false
OperationalPointReference:
allOf:
- oneOf:
- type: object
required:
- operational_point
properties:
operational_point:
type: string
maxLength: 255
minLength: 1
- type: object
required:
- trigram
properties:
secondary_code:
type: string
description: An optional secondary code to identify a more specific location
nullable: true
trigram:
type: string
minLength: 1
- type: object
required:
- uic
properties:
secondary_code:
type: string
description: An optional secondary code to identify a more specific location
nullable: true
uic:
type: integer
format: int32
description: The [UIC](https://en.wikipedia.org/wiki/List_of_UIC_country_codes) code of an operational point
minimum: 0
- type: object
properties:
track_reference:
allOf:
- $ref: '#/components/schemas/TrackReference'
nullable: true
Ordering:
type: string
enum:
Expand Down Expand Up @@ -7234,38 +7275,7 @@ components:
PathItemLocation:
oneOf:
- $ref: '#/components/schemas/TrackOffset'
- type: object
required:
- operational_point
properties:
operational_point:
type: string
maxLength: 255
minLength: 1
- type: object
required:
- trigram
properties:
secondary_code:
type: string
description: An optional secondary code to identify a more specific location
nullable: true
trigram:
type: string
minLength: 1
- type: object
required:
- uic
properties:
secondary_code:
type: string
description: An optional secondary code to identify a more specific location
nullable: true
uic:
type: integer
format: int32
description: The [UIC](https://en.wikipedia.org/wiki/List_of_UIC_country_codes) code of an operational point
minimum: 0
- $ref: '#/components/schemas/OperationalPointReference'
description: The location of a path waypoint
PathProperties:
type: object
Expand Down Expand Up @@ -10478,6 +10488,23 @@ components:
type: string
maxLength: 255
minLength: 1
TrackReference:
oneOf:
- type: object
required:
- track_id
properties:
track_id:
type: string
maxLength: 255
minLength: 1
- type: object
required:
- track_name
properties:
track_name:
type: string
minLength: 1
TrackSection:
type: object
required:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ collect AS (
1.
)
) AS geo,
ops.kp AS kp
ops.kp AS kp,
ops.track_id AS track_section
FROM ops
INNER JOIN infra_object_track_section AS tracks ON tracks.obj_id = ops.track_id
AND tracks.infra_id = $1
INNER JOIN infra_layer_track_section AS tracks_layer ON tracks.obj_id = tracks_layer.obj_id
AND tracks.infra_id = tracks_layer.infra_id
)
INSERT INTO infra_layer_operational_point (obj_id, infra_id, geographic, kp)
INSERT INTO infra_layer_operational_point (obj_id, infra_id, geographic, kp, track_section)
SELECT op_id,
$1,
geo,
kp
kp,
track_section
FROM collect
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@ collect AS (
1.
)
) AS geo,
ops.kp AS kp
ops.kp AS kp,
ops.track_id AS track_section
FROM ops
INNER JOIN infra_object_track_section AS tracks ON tracks.obj_id = ops.track_id
AND tracks.infra_id = $1
INNER JOIN infra_layer_track_section AS tracks_layer ON tracks.obj_id = tracks_layer.obj_id
AND tracks.infra_id = tracks_layer.infra_id
)
INSERT INTO infra_layer_operational_point (obj_id, infra_id, geographic, kp)
INSERT INTO infra_layer_operational_point (obj_id, infra_id, geographic, kp, track_section)
SELECT op_id,
$1,
geo,
kp
kp,
track_section
FROM collect
16 changes: 12 additions & 4 deletions editoast/src/models/infra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,19 @@ impl Infra {

if let Some(layer_table) = get_geometry_layer_table(&object) {
let layer_table = layer_table.to_string();
let sql = if object != ObjectType::Signal {
format!("INSERT INTO {layer_table}(obj_id,geographic,infra_id) SELECT obj_id,geographic,$1 FROM {layer_table} WHERE infra_id=$2")
} else {
format!("INSERT INTO {layer_table}(obj_id,geographic,infra_id, angle_geo, signaling_system, sprite)
let sql = match object {
ObjectType::Signal => {
format!("INSERT INTO {layer_table}(obj_id,geographic,infra_id, angle_geo, signaling_system, sprite)
SELECT obj_id,geographic,$1,angle_geo, signaling_system, sprite FROM {layer_table} WHERE infra_id = $2")
}
,
ObjectType::OperationalPoint => {
format!("INSERT INTO {layer_table}(obj_id,geographic,infra_id, kp, track_section)
SELECT obj_id,geographic,$1,kp, track_section FROM {layer_table} WHERE infra_id = $2")
}
_ => {
format!("INSERT INTO {layer_table}(obj_id,geographic,infra_id) SELECT obj_id,geographic,$1 FROM {layer_table} WHERE infra_id=$2")
}
};

sql_query(sql)
Expand Down
Loading
Loading