Struct transit_grid::graphs::PhysicalGraph
source · 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>
impl<R: Copy, T: CoordNum> PhysicalGraph<R, T>
sourcepub fn index_to_id(&self, index: NodeIndex) -> Option<&NodeId>
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- TheNodeIndexto be converted.
Returns
NodeId- The correspondingNodeIdof the providedNodeIndex.
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));sourcepub fn id_to_index(&self, id: NodeId) -> Option<&NodeIndex>
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- TheNodeIdto be converted.
Returns
NodeIndex- The correspondingNodeIndexof the providedNodeId.
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);sourcepub fn add_transit_node(&mut self, node: TransitNode<R>) -> NodeIndex
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);sourcepub fn add_transit_edge(&mut self, edge: TransitEdge<T>) -> EdgeIndex
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);sourcepub fn get_transit_edge(
&self,
node1: NodeId,
node2: NodeId
) -> Option<&TransitEdge<T>>
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- TheNodeIdof the first node.node2- TheNodeIdof 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
node1ornode2are not valid node IDs in the graph. - If there is no edge between
node1andnode2.
sourcepub fn get_transit_edge_by_id(&self, edge_id: EdgeId) -> Option<&TransitEdge<T>>
pub fn get_transit_edge_by_id(&self, edge_id: EdgeId) -> Option<&TransitEdge<T>>
Returns a reference to the TransitEdge with the specified EdgeId.
sourcepub fn repair_edge(&mut self, node1: NodeId, node2: NodeId)where
R: EuclideanDistance<T, Coord<T>>,
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- TheTransitEdgeto 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>
impl<R: Clone, T: Clone + CoordNum> Clone for PhysicalGraph<R, T>
source§fn clone(&self) -> PhysicalGraph<R, T>
fn clone(&self) -> PhysicalGraph<R, T>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more