pub struct PhysicalGraph<R, T: CoordNum> {
    pub graph: UnGraph<TransitNode<R>, TransitEdge<T>, u32>,
    /* private fields */
}
Expand description

Represents the physical layout of the transit network.

PhysicalGraph is an undirected graph where each node represents a transit node (a point in the transit network where a vehicle can stop) and each edge represents a transit edge (a path between two transit nodes). The PhysicalGraph uses the UnGraph structure from the petgraph crate to internally represent this data. The PhysicalGraph maintains mappings between NodeIds and NodeIndexes (from petgraph), allowing for efficient conversion between the two.

Examples

Creating a new PhysicalGraph and adding a TransitNode:

use transit_grid::core::TransitNode;
use transit_grid::prelude::PhysicalGraph;
use geo::{coord, Coord};

let mut graph: PhysicalGraph<Coord, f64> = PhysicalGraph::new();
let node = TransitNode { id: 1, location: coord! { x:0.0, y:0.0 } };
graph.add_transit_node(node);

Adding a TransitEdge to the PhysicalGraph:

use transit_grid::core::{TransitNode, TransitEdge};
use transit_grid::prelude::PhysicalGraph;
use geo::{coord, Coord, LineString};

let mut graph: PhysicalGraph<Coord, f64> = PhysicalGraph::new();
let node1 = TransitNode { id: 1, location: coord! { x:0.0, y:0.0 } };
let node2 = TransitNode { id: 2, location: coord! { x:1.0, y:1.0 } };
let node1_id = graph.add_transit_node(node1);
let node2_id = graph.add_transit_node(node2);
let edge = TransitEdge {
    id: 1,
    source: 1,
    target: 2,
    length: 1.0,
    path: LineString(vec![coord! { x:0.0, y:0.0 }, coord! { x:1.0, y:1.0 }]),
};
graph.add_transit_edge(edge);

Fields§

§graph: UnGraph<TransitNode<R>, TransitEdge<T>, u32>

Underlying undirected graph.

Implementations§

source§

impl<R: Copy, T: CoordNum> PhysicalGraph<R, T>

source

pub fn new() -> Self

Creates a new, empty PhysicalGraph.

source

pub fn index_to_id(&self, index: NodeIndex) -> Option<&NodeId>

Converts a NodeIndex to a NodeId.

This method provides a way to map from the petgraph’s NodeIndex to the NodeId used in the TransitNode.

Arguments
  • index - The NodeIndex to be converted.
Returns
  • NodeId - The corresponding NodeId of the provided NodeIndex.
Example
use transit_grid::prelude::PhysicalGraph;
use transit_grid::core::TransitNode;
use geo::{coord, Coord};

let mut graph: PhysicalGraph<Coord, f64> = PhysicalGraph::new();
let node = TransitNode { id: 1, location: coord! { x:0.0, y:0.0 } };
let node_index = graph.add_transit_node(node);
let node_id = graph.index_to_id(node_index);
assert_eq!(node_id, Some(&1));
source

pub fn id_to_index(&self, id: NodeId) -> Option<&NodeIndex>

Converts a NodeId to a NodeIndex.

This method provides a way to map from a NodeId used in the TransitNode to the petgraph’s NodeIndex.

Arguments
  • id - The NodeId to be converted.
Returns
  • NodeIndex - The corresponding NodeIndex of the provided NodeId.
Example
use transit_grid::prelude::PhysicalGraph;
use transit_grid::core::TransitNode;
use geo::{coord, Coord};

let mut graph: PhysicalGraph<Coord, f64> = PhysicalGraph::new();
let node = TransitNode { id: 1, location: coord! { x:0.0, y:0.0 } };
let node_index = graph.add_transit_node(node);
let queried_index = graph.id_to_index(1);
assert_eq!(Some(&node_index), queried_index);
source

pub fn add_transit_node(&mut self, node: TransitNode<R>) -> NodeIndex

Adds a TransitNode to the PhysicalGraph.

Example
use transit_grid::prelude::PhysicalGraph;
use transit_grid::core::TransitNode;
use geo::{coord, Coord};

let mut graph: PhysicalGraph<Coord, f64> = PhysicalGraph::new();
let node = TransitNode { id: 1, location: coord! { x:0.0, y:0.0 } };
graph.add_transit_node(node);
source

pub fn add_transit_edge(&mut self, edge: TransitEdge<T>) -> EdgeIndex

Adds a TransitEdge to the PhysicalGraph.

Example
use transit_grid::prelude::PhysicalGraph;
use transit_grid::core::{TransitNode, TransitEdge};
use geo::{coord, Coord, LineString};
use petgraph::csr::IndexType;

let mut graph: PhysicalGraph<Coord, f64> = PhysicalGraph::new();
let node1 = TransitNode { id: 1, location: coord! { x:0.0, y:0.0 } };
let node2 = TransitNode { id: 2, location: coord! { x:1.0, y:1.0 } };

let node1_id = graph.add_transit_node(node1);
let node2_id = graph.add_transit_node(node2);

let edge = TransitEdge {
    id: 1,
    source: 1,
    target: 2,
    length: 1.0,
    path: LineString(vec![coord! { x:0.0, y:0.0 }, coord! { x:1.0, y:1.0 }]),
};

graph.add_transit_edge(edge);
source

pub fn get_transit_edge( &self, node1: NodeId, node2: NodeId ) -> Option<&TransitEdge<T>>

Returns a reference to the TransitEdge connecting the two nodes specified by node1 and node2.

Arguments
  • node1 - The NodeId of the first node.
  • node2 - The NodeId of the second node.
Returns

A reference to the TransitEdge connecting node1 and node2. This function will panic if there is no edge between the nodes.

Panics

This function will panic in the following cases:

  • If node1 or node2 are not valid node IDs in the graph.
  • If there is no edge between node1 and node2.
source

pub fn get_transit_edge_by_id(&self, edge_id: EdgeId) -> Option<&TransitEdge<T>>

Returns a reference to the TransitEdge with the specified EdgeId.

source

pub fn repair_edge(&mut self, node1: NodeId, node2: NodeId)where R: EuclideanDistance<T, Coord<T>>,

Repairs a physical edge in the PhysicalGraph based on its nodes’ locations.

Arguments
  • edge - The TransitEdge to be repaired.
Example
use transit_grid::prelude::PhysicalGraph;
use transit_grid::core::{TransitNode, TransitEdge};
use geo::{coord, Coord, LineString};

let mut graph: PhysicalGraph<Coord, f64> = PhysicalGraph::new();
let node1 = TransitNode { id: 1, location: coord! { x:0.0, y:0.0 } };
let node2 = TransitNode { id: 2, location: coord! { x:1.0, y:1.0 } };

let node1_id = graph.add_transit_node(node1);
let node2_id = graph.add_transit_node(node2);

let mut edge = TransitEdge {
    id: 1,
    source: 1,
    target: 2,
    length: 1.0,
    path: LineString(vec![coord! { x:1.0, y:1.0 }, coord! { x:0.0, y:0.0 }]),  // Note that the direction is initially reversed
};

graph.add_transit_edge(edge.clone());
graph.repair_edge(1, 2);
let edge = graph.get_transit_edge(1, 2).unwrap();
assert_eq!(
   edge.path,
   LineString(vec![Coord { x: 0.0, y: 0.0 }, Coord { x: 1.0, y: 1.0 }])
);

// After repair, the edge path should be from 0,0 to 1,1
//assert_eq!(edge.path.0.first().unwrap(), &coord! { x:0.0, y:0.0 });
//assert_eq!(edge.path.0.last().unwrap(), &coord! { x:1.0, y:1.0 });

Trait Implementations§

source§

impl<R: Clone, T: Clone + CoordNum> Clone for PhysicalGraph<R, T>

source§

fn clone(&self) -> PhysicalGraph<R, T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<R: Debug, T: Debug + CoordNum> Debug for PhysicalGraph<R, T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<R: Copy, T: CoordNum> Default for PhysicalGraph<R, T>

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<R, T> RefUnwindSafe for PhysicalGraph<R, T>where R: RefUnwindSafe, T: RefUnwindSafe,

§

impl<R, T> Send for PhysicalGraph<R, T>where R: Send, T: Send,

§

impl<R, T> Sync for PhysicalGraph<R, T>where R: Sync, T: Sync,

§

impl<R, T> Unpin for PhysicalGraph<R, T>where R: Unpin, T: Unpin,

§

impl<R, T> UnwindSafe for PhysicalGraph<R, T>where R: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<G1, G2> Within<G2> for G1where G2: Contains<G1>,

§

fn is_within(&self, b: &G2) -> bool