mappymatch.maps.igraph.igraph_map#

Classes

IGraphMap(graph)

A road network map implementation using igraph for high-performance graph operations.

class mappymatch.maps.igraph.igraph_map.IGraphMap(graph: Graph)[source]#

A road network map implementation using igraph for high-performance graph operations.

IGraphMap wraps an igraph.Graph to represent a road network, providing potentially faster performance than NetworkX for very large networks. It uses an R-tree spatial index for efficient nearest-neighbor searches and supports both distance and time-based routing.

The underlying igraph must have: - A pyproj CRS stored in graph['crs'] - Road geometries (LineStrings) stored as edge attributes - Node IDs stored in vertex attributes - Edge IDs (keys) for multi-edges between the same nodes - Optional distance and time weights for routing

This implementation is particularly useful when working with very large road networks where performance is critical.

Attributes:

g: The igraph.Graph representing the road network road_mapping: A dictionary mapping RoadId tuples to igraph edge indices crs: The coordinate reference system of the map

Note:

igraph must be installed to use this class. Install with: pip install igraph

property distance_weight: str#

Get the name of the edge attribute used for distance-based routing.

This property identifies which edge attribute should be used when computing shortest paths based on physical distance (as opposed to time or other metrics).

Returns:

The name of the distance weight attribute (e.g., 'kilometers', 'miles', 'length')

property time_weight: str#

Get the name of the edge attribute used for time-based routing.

This property identifies which edge attribute should be used when computing fastest paths based on travel time (as opposed to distance or other metrics).

Returns:

The name of the time weight attribute (e.g., 'minutes', 'seconds', 'travel_time')

road_by_id(road_id: RoadId) Road | None[source]#

Get a road by its id

Args:

road_id: The id of the road to get

Returns:

The road with the given id, or None if it does not exist

set_road_attributes(attributes: Dict[RoadId, Dict[str, Any]])[source]#

Set the attributes of the roads in the map

Args:

attributes: A dictionary mapping road ids to dictionaries of attributes

Returns:

None

property roads: List[Road]#

Get a list of all the roads in the map

Returns:

A list of all the roads in the map

classmethod from_nx_graph(nx_graph: MultiDiGraph) IGraphMap[source]#

Build an IGraphMap from a networkx graph

classmethod from_file(file: str | Path) IGraphMap[source]#

Build a IGraphMap instance from a file

Args:

file: The graph pickle file to load the graph from

Returns:

A IGraphMap instance

classmethod from_geofence(geofence: Geofence, xy: bool = True, network_type: NetworkType = NetworkType.DRIVE) IGraphMap[source]#

Read an OSM network graph into a IGraphMap

Args:

geofence: the geofence to clip the graph to xy: whether to use xy coordinates or lat/lon network_type: the network type to use for the graph

Returns:

a IGraphMap

to_file(outfile: str | Path)[source]#

Save the graph to a pickle file

Args:

outfile: The file to save the graph to

nearest_road(coord: Coordinate) Road[source]#

A helper function to get the nearest road.

Args:

coord: The coordinate to find the nearest road to

Returns:

The nearest road to the coordinate

shortest_path(origin: Coordinate, destination: Coordinate, weight: str | Callable | None = None) List[Road][source]#

Computes the shortest path between an origin and a destination

Args:

origin: The origin coordinate destination: The destination coordinate weight: The weight to use for the path, either a string or a function

Returns:

A list of roads that form the shortest path