Network#

This module contains the class to manage Network

class tracklib.core.Network.Node(id, coord)[source]#

Node / vertice of a network

__init__(id, coord)[source]#

Node constructor

Parameters
__str__()[source]#

Node to string

Return type

str

distanceTo(node)[source]#

Distance to an other node

Parameters

node (Node) – Node to compute the distance

Return type

float

Returns

3d distance

distance2DTo(node)[source]#

2d distance to an other node

Parameters

node (Node) – Node to compute the distance

Return type

float

Returns

2d distance

plot(sym='r+')[source]#

Plot the node

class tracklib.core.Network.Edge(id, track)[source]#

Class to define an edge / arc

DOUBLE_SENS = 0#
SENS_DIRECT = 1#
SENS_INVERSE = -1#
__init__(id, track)[source]#

Edge constructor

Parameters
  • id (int) – unique id of Edge

  • track (Track) – A track

plot(sym='k-')[source]#

Plot the edge

Parameters

sym (str) – TODO

__str__()[source]#

Print the edge

Return type

str

class tracklib.core.Network.Network[source]#

Define a network

ROUTING_ALGO_DIJKSTRA = 0#
ROUTING_ALGO_ASTAR = 1#
AF_WEIGHT = '#weight'#
__init__()[source]#

Network constructor

addNode(node)[source]#

Add a Node to the current Network

Parameters

Node – Node to add

addEdge(edge, source, target)[source]#

Add a Edge to the current Network

Parameters
  • edge (Edge) – Edge to add

  • source (Node) – Source Node

  • target (Node) – Target Node

size()[source]#

Return the number of edges

Return type

int

Returns

Number of edges

setRoutingMethod(method)[source]#

Define the routing algorithm

Parameters

method (int) –

Algorithm of routing. Two values are possible :

  1. ROUTING_ALGO_DIJKSTRA [default].

  2. ROUTING_ALGO_ASTAR.

NB: A* is an efficient approximate version of Dijkstra. It returns exact solution under theoretical assumptions on the metrics used to set weights on graph edges.

setAStarWeight(weight)[source]#

Define the A* weight

toGeoCoords(base)[source]#

Convert network to core.Coords.GeoCoords

Parameters

base (Union[ECEFCoords, ENUCoords]) – Base coordinate for conversion

toENUCoords(base)[source]#

Convert network to core.Coords.ENUCoords

Parameters

base (Union[GeoCoords, ECEFCoords]) – Base coordinate for conversion

getSRID()[source]#

Return the SRID of network

Return type

int

Returns

SRID of current network

totalLength()[source]#

Count the edges of current network

Return type

int

Returns

Number of edges

simplify(tolerance, mode=1)[source]#

Simplification of current network

Parameters
  • tolerance – TODO

  • mode (int) –

    Mode of simplification. The possibles values are:

    1. DOUGLAS_PEUCKER

    2. VISVALINGAM

    3. MINIMIZE_LARGEST_DEVIATION

    4. MINIMIZE_ELONGATION_RATIO

    5. PRECLUDE_LARGE_DEVIATION

    6. FREE

    7. FREE_MAXIMIZE

    See algo.Simplification.simplify() documentation for more infos

createSpatialIndex(resolution=None, margin=0.05, verbose=True)[source]#

Create a spatial index

Parameters
  • resolution – TODO

  • margin (float) – TODO

  • verbose (bool) – Verbose creation

exportSpatialIndex(filename)[source]#

Export the spatial index to a file

Parameters

filename (str) – File to export

importSpatialIndex(filename)[source]#

Import a spatial index from a file

Parma filename

File to import

getNextNodes(node_id)[source]#

Give the XXX of a node

Parameters

node_id (int) – id of a node

getNextEdges(node_id)[source]#

Give the XXX of a node

Parameters

node_id (int) – id of a node

getPrevNodes(node_id)[source]#

Give the XXX of a node

Parameters

node_id (int) – id of a node

getPrevEdges(node_id)[source]#

Give the XXX of a node

Parameters

node_id (int) – id of a node

getAdjacentNodes(node_id)[source]#

Give the XXX of a node

Parameters

node_id (int) – id of a node

getIncidentEdges(node_id)[source]#

Give the XXX of a node

Parameters

node_id (int) – id of a node

degree(node_id)[source]#

Compute the degree of a node

Parameters

node_id (int) – id of a node

Return type

int

Returns

in degree

degreeIn(node_id)[source]#

Compute the in degree of a node

Parameters

node_id (int) – id of a node

Return type

int

Returns

in degree

degreeOut(node_id)[source]#

Compute the out degree of a node

Parameters

node_id (int) – id of a node

Return type

int

Returns

out degree

getIndexNodes()[source]#

Return the list of seted nodes

Return type

list[int]

Returns

A list of nodes id

getNumberOfNodes()[source]#

Number of nodes in the network

Return type

int

Returns

Number of nodes

getNumberOfEdges()[source]#

Number of edges in the network

Return type

int

Returns

Number of edges

getNumberOfVertices()[source]#

Number of vertices in the network

Return type

int

Returns

Number of vertices

getNodeId(n)[source]#

The return the id of the n-est Node

Parameters

n (int) – Position of the node

Return type

int

Returns

Id of the node

getEdgeId(n)[source]#

The return the id of the n-est Edge

Parameters

n (int) – Position of the Edge

Return type

int

Returns

Id of the Edge

getNodesId()[source]#

Return a list of all nodes Id

Return type

list[int]

Returns

A list of id

getEdgesId()[source]#

Return a list of all edges Id

Return type

list[int]

Returns

A list of id

getRandomNode()[source]#

Return a random node of network

Return type

Node

Returns

A node randomly choosed

hasNode(id)[source]#

Check if a node with a given id exist

Parameters

id (int) – node id to test

Return type

bool

hasEdge(id)[source]#

Check if a node with a given id exist

Parameters

id (int) – edge id to test

Return type

bool

getNode(id)[source]#

Return the node with a give id

Parameters

id (int) – node id to get

Return type

Node

getEdge(id)[source]#

Return the edge with a give id

Parameters

id (int) – edge id to get

Return type

Edge

getAllEdgeGeoms()[source]#

Return a TrackCollection of all edges

Return type

TrackCollection

Returns

All edges of Network

bbox()[source]#

Return the Bbox of network

Return type

Bbox

plot(edges='k-', nodes='', direct='k--', indirect='k--', size=0.5, append=<module 'matplotlib.pyplot' from '/home/marie-dominique/.pyenv/versions/3.9.1/lib/python3.9/site-packages/matplotlib/pyplot.py'>, v=None)[source]#
__correctInputNode(node)#

TODO

sub_network(source, cut, mode='TOPOLOGIC', verbose=True)[source]#

Extracts sub-network from routing forward search

An edge is added to the output network if bot its ends have been visited during the search process (TOPOGRAPHIC mode) or if one of its ends are located in a circle centered on source with radius = cut

Parameters
  • source (Union[int, Node, GeoCoords, ENUCoords, ECEFCoords]) – a source node (id or node object) or a coord

  • cut (float) – a maximal distance for search

  • mode (Literal[‘TOPOLOGIC’, ‘GEOMETRIC’]) – “TOPOLOGIC” (default) or “GEOMETRIC”

  • verbose (bool) – Verbose creation

Result

a network containing only visited nodes reachable from source with a cost lower than cut distance.

Return type

Network

__sub_network_routing(verbose)#

Sub network generation

Parameters

verbose (bool) – Verbose creation

Return type

Network

Returns

A sub part of the current network

__sub_network_geometric(source, cut, verbose)#

Sub network generation, based on geometry

Parameters
  • source (Union[Node, str]) – Source for the sub-network

  • cut (float) – TODO

  • verbose (bool) – Verbose generation

Return type

Network

Returns

A sub part of the current network

__resetFlags()#

Internal function to call before dijkstra forward step

run_routing_forward(source, target=None, cut=1e+300, output_dict=None)[source]#

Executes forward pass of routing algorithm to find shortest distance between source node and all other nodes in graph.

Execution may be stopped prematurely by specifying target node and/or cut distance. For each nodes reached (with a distance d < cut) during the search process, an entry {key=(source, n), value=d} is added to an output dictionnary (if specified as input)

Parameters
  • source (Union[Node, int]) – a source node

  • target (Union[Node, int, None]) – a target node

  • cut (float) – a maximal distance for search

  • output_dict (Optional[dict]) – output structure for retrieved distance

shortest_distance(source, target=None, cut=1e+300, output_dict=None)[source]#

Finds shortest distance between source node and either a target node or a list of target nodes.

In every case, time complexity is \(O((n+m) \cdot \log(n))\) with \(n\) the number of node and \(m\) the number of edges. That amounts roughly to \(O(n \cdot \log(n))\) for a typical planar road network, and to \(O(n^2 \cdot \log(n))\) for a general network

Parameters
  • source (Union[int, Node]) – A source node (id or node object)

  • target (Union[Node, int, None]) – A target node (id or node object, optional)

  • cut (float) – A maximal distance for search (optional)

  • output_dict (Optional[dict]) – output structure for retrieved distance

Return type

Union[float, list[float]]

Returns

The distance between source and target nodes If target node is not specified, returns a list of distances between source node and all other nodes in the network (non-reachable nodes are associated to 1e300 distance).

all_shortest_distances(cut=1e+300, output_dict=None)[source]#

Computes all shortest distances between pairs of nodes

The results are saved in a dictionnary {key=(source, n), value=d}.

If output dictionnary structure is provided as input, it is directly incremented during the process, otherwise, it is created before begining to search for shortest distances.

Time complexity is \(O(n \cdot (n+m) \cdot \log(n))\) with \(n\) the number of nodes and \(n\) the number of edges. That amounts roughly to \(O(n^2.log(n))\) for a typical planar road network, and to \(O(n^3.log(n))\) for a general network.

Parameters
  • cut (float) – a maximal distance for search (optional)

  • output_dict (Optional[dict]) – output structure for retrieved distances

Return type

Dict[Tuple[int, int], float]

Returns

The distances between all pairs of nodes. Nodes separated by a distance > cut are not registered in the output dictionnary. If dictionnary is provided as input it is incremented during the process, hence it is possble tocall ‘all_shortest_distances’ successively with the same output dictionnary structure.

run_routing_backward(target)[source]#

Computes shortest path between the source node used in forward step run_routing_forward() and any target node.

If target node has not been reached during forward search, a None object is returned by the function.

Parameters

target (Union[int, Node]) – A target node

Return type

Optional[Track]

Returns

A track between the source node specified in run_routing_forward() and a target node. The track contains topologic and non-topologic vertices. If the node target has not been reached during forward step, None object is output

shortest_path(source, target, cut=1e+300, output_dict=None)[source]#

Computes shortest path between source and target nodes

Parameters
  • source (Union[int, Node]) – A source node

  • target (Union[int, Node]) – a target node

  • cut (float) – a maximal distance for search

  • output_dict (Optional[dict]) – output structure for retrieved distances

Return type

Optional[Track]

Returns

A track between source and target node. If target is not reachable during forward step, None object is output

distanceBtwPts(edge1, abs_curv_1, edge2, abs_curv_2)[source]#

Distance between 2 points

Each points being described by:

  • Edge id number

  • Curvilinear abscissa on edge geometry from source

Parameters
  • edge1 (int) – edge 1 id number

  • abs_curv_1 (float) – curvilinear abscissa on edge 1 geometry from source

  • edge2 (int) – edge 2 id number

  • abs_curv_2 (float) – curvilinear abscissa on edge 2 geometry from source

Return type

float

Returns

Distance between points

prepare(cut=1e+300)[source]#

Precomputes shortest distances between all pairs of nodes and saves the result in DISTANCES attribute.

Parameters

cut (Optional[float]) – A maximal distance for search

has_prepared_shortest_distance(source, target)[source]#

Tests if a shortest distance has been precomputed

Parameters
  • source (Union[int, Node]) – A source node

  • target (Union[int, Node]) – A target node

Return type

bool

Returns

True if a shortest distance between source and target has already been computed.

prepared_shortest_distance(source, target)[source]#

Finds shortest distance from the precomputation. May be called only after prepare() or load_prep.()

Parameters
  • source (Union[int, Node]) – A source node

  • target (Union[int, Node]) – A target node

Return type

float

Returns

Shortest distance between source and targer. Returns 1e300 if shortest distance has not been precomputed.

save_prep(filename)[source]#

Saves DISTANCES attribute in a npy file for further use

Parameters

filename (str) – Path to save precomputed structure

load_prep(filename)[source]#

Imports DISTANCES attribute from an npy file

Parameters

filename (str) – Path where precomputed structure is saved