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

Access metadata: load properties and expose them #88

Merged
merged 5 commits into from
Dec 3, 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
25 changes: 23 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "liblrs"
version = "0.2.3"
version = "0.2.4"
edition = "2021"
description = "Library to manipulate linear referencing systems"
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion python/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "liblrs_python"
description = "Python bindings for liblrs: a library to work with linear referencing systems"
version = "0.2.3"
version = "0.2.4"
edition = "2021"
license = "MIT"
repository = "https://github.com/OpenRailAssociation/liblrs/"
Expand Down
18 changes: 18 additions & 0 deletions python/liblrs_python.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,24 @@ class Lrs:
"""
...

def lrs_properties(self) -> dict[str, str]:
r"""
[`Properties`] of the lrs
"""
...

def lrm_properties(self, lrm_index:int) -> dict[str, str]:
r"""
[`Properties`] for a given lrm
"""
...

def anchor_properties(self, lrm_index:int, anchor_index:int) -> dict[str, str]:
r"""
[`Properties`] for a given anchor
"""
...


class Point:
r"""
Expand Down
17 changes: 16 additions & 1 deletion python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
use std::path::PathBuf;

use liblrs::lrs::LrmHandle;
use liblrs::lrs::{LrsBase, Properties};
use liblrs::lrs_ext::*;
use liblrs::{builder::Properties, lrs::LrsBase};
use pyo3::{exceptions::PyTypeError, prelude::*};

/// Holds the whole Linear Referencing System.
Expand Down Expand Up @@ -341,6 +341,21 @@ impl Lrs {
})
.collect()
}

/// [`Properties`] of the lrs
pub fn lrs_properties(&self) -> Properties {
self.lrs.lrs_properties().clone()
}

/// [`Properties`] for a given lrm
pub fn lrm_properties(&self, lrm_index: usize) -> Properties {
self.lrs.lrm_properties(lrm_index).clone()
}

/// [`Properties`] for a given anchor
pub fn anchor_properties(&self, lrm_index: usize, anchor_index: usize) -> Properties {
self.lrs.anchor_properties(lrm_index, anchor_index).clone()
}
}

#[pyclass]
Expand Down
30 changes: 18 additions & 12 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,11 @@ use geo::{Coord, Distance};

use crate::curves::{Curve, CurveError, CurveProjection, SphericalLineStringCurve};

use crate::lrs::Properties;
use crate::lrs_ext::ExtLrs;
use crate::lrs_generated::{self, *};
use crate::osm_helpers::sort_edges;

/// A key-value `HashMap` to add metadata to the objects.
pub type Properties = std::collections::HashMap<String, String>;

#[macro_export]
/// Build a properties map:
/// `properties!("source" => "openstreetmap", "license" => "ODbL")`.
macro_rules! properties {
($($k:expr => $v:expr),* $(,)?) => {{
core::convert::From::from([$(($k.to_owned(), $v.to_owned()),)*])
}};
}
use crate::properties;

/// The linear position of an [`Anchor`] doesn’t always match the measured distance.
/// For example if a road was transformed into a bypass, resulting in a longer road,
Expand Down Expand Up @@ -656,4 +646,20 @@ mod tests {
assert_relative_eq!(lrm.x(), 0.);
assert_relative_eq!(lrm.y(), 0.);
}

#[test]
fn properties() {
let mut b = Builder::new();
let traversal = build_traversal(&mut b);
b.add_lrm(
"lrm",
traversal,
&[],
properties!("test_key" => "test_value"),
);
let lrs = b.build_lrs(properties!()).unwrap();

let p = lrs.lrm_properties(0);
assert_eq!(p["test_key"], "test_value")
}
}
37 changes: 24 additions & 13 deletions src/lrm_scale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
use geo::Point;
use thiserror::Error;

use crate::lrs::Properties;

/// Measurement along the `Curve`. Typically in meters.
pub type CurvePosition = f64;

Expand Down Expand Up @@ -44,6 +46,9 @@ pub struct Anchor {

/// Position of the anchor on the `Curve`.
pub point: Option<Point>,

/// Metadata to describe the node
pub properties: Properties,
}

impl Anchor {
Expand All @@ -53,12 +58,14 @@ impl Anchor {
scale_position: ScalePosition,
curve_position: CurvePosition,
point: Option<Point>,
properties: Properties,
) -> Self {
Self {
id: Some(name.to_owned()),
scale_position,
curve_position,
point,
properties,
}
}

Expand All @@ -67,12 +74,14 @@ impl Anchor {
scale_position: ScalePosition,
curve_position: CurvePosition,
point: Option<Point>,
properties: Properties,
) -> Self {
Self {
id: None,
scale_position,
curve_position,
point,
properties,
}
}

Expand Down Expand Up @@ -258,14 +267,16 @@ impl LrmScale {
pub(crate) mod tests {
use geo::point;

use crate::properties;

use super::*;

pub(crate) fn scale() -> LrmScale {
LrmScale {
id: "id".to_owned(),
anchors: vec![
Anchor::new("a", 0., 0., Some(point! { x: 0., y: 0. })),
Anchor::new("b", 10., 0.5, Some(point! { x: 0., y: 0. })),
Anchor::new("a", 0., 0., Some(point! { x: 0., y: 0. }), properties!()),
Anchor::new("b", 10., 0.5, Some(point! { x: 0., y: 0. }), properties!()),
],
}
}
Expand Down Expand Up @@ -300,8 +311,8 @@ pub(crate) mod tests {
let scale = LrmScale {
id: "id".to_owned(),
anchors: vec![
Anchor::new("a", 0., 2., Some(point! { x: 0., y: 0. })),
Anchor::new("b", 10., 3., Some(point! { x: 0., y: 0. })),
Anchor::new("a", 0., 2., Some(point! { x: 0., y: 0. }), properties!()),
Anchor::new("b", 10., 3., Some(point! { x: 0., y: 0. }), properties!()),
],
};

Expand Down Expand Up @@ -332,10 +343,10 @@ pub(crate) mod tests {
let scale = LrmScale {
id: "id".to_owned(),
anchors: vec![
Anchor::new_unnamed(0., 100., Some(point! { x: 0., y: 0. })),
Anchor::new("a", 1., 200., Some(point! { x: 0., y: 0. })),
Anchor::new("b", 3., 300., Some(point! { x: 0., y: 0. })),
Anchor::new_unnamed(4., 400., Some(point! { x: 0., y: 0. })),
Anchor::new_unnamed(0., 100., Some(point! { x: 0., y: 0. }), properties!()),
Anchor::new("a", 1., 200., Some(point! { x: 0., y: 0. }), properties!()),
Anchor::new("b", 3., 300., Some(point! { x: 0., y: 0. }), properties!()),
Anchor::new_unnamed(4., 400., Some(point! { x: 0., y: 0. }), properties!()),
],
};

Expand Down Expand Up @@ -403,8 +414,8 @@ pub(crate) mod tests {
let scale = LrmScale {
id: "id".to_owned(),
anchors: vec![
Anchor::new("a", 1000. + 0., -2., None),
Anchor::new_unnamed(1000. + 300., 1., None),
Anchor::new("a", 1000. + 0., -2., None, properties!()),
Anchor::new_unnamed(1000. + 300., 1., None, properties!()),
],
};

Expand All @@ -426,9 +437,9 @@ pub(crate) mod tests {
let scale = LrmScale {
id: "id".to_owned(),
anchors: vec![
Anchor::new("a", 0., 0., None),
Anchor::new_unnamed(1., 0.4, None),
Anchor::new_unnamed(9., 0.6, None),
Anchor::new("a", 0., 0., None, properties!()),
Anchor::new_unnamed(1., 0.4, None, properties!()),
Anchor::new_unnamed(9., 0.6, None, properties!()),
],
};

Expand Down
Loading
Loading