Skip to main content
`Ctrl`+`K`

# Network

This module contains the class to manage Network

class tracklib.core.Network.Node(id, coord)

  Node / vertice of a network

  __init__(id, coord)

    `Node` constructor

    Parameters

      - id (`int`) – unique id of Node

      - coord (`Union`[`ECEFCoords`, `ENUCoords`, `GeoCoords`]) – A coordinate object

  __str__()

    Node to string

    Return type

      `str`

  distanceTo(node)

    Distance to an other node

    Parameters

      node (`Node`) – Node to compute the distance

    Return type

      `float`

    Returns

      3d distance

  distance2DTo(node)

    2d distance to an other node

    Parameters

      node (`Node`) – Node to compute the distance

    Return type

      `float`

    Returns

      2d distance

  plot(sym='r+')

    Plot the node

class tracklib.core.Network.Edge(id, track)

  Class to define an edge / arc

  DOUBLE_SENS = 0
  SENS_DIRECT = 1
  SENS_INVERSE = -1
  __init__(id, track)

    `Edge` constructor

    Parameters

      - id (`int`) – unique id of Edge

      - track (`Track`) – A track

  plot(sym='k-')

    Plot the edge

    Parameters

      sym (`str`) – TODO

  __str__()

    Print the edge

    Return type

      `str`

class tracklib.core.Network.Network

  Define a network

  ROUTING_ALGO_DIJKSTRA = 0
  ROUTING_ALGO_ASTAR = 1
  AF_LINK = '#link'
  AF_WEIGHT = '#weight'
  __init__()

    `Network` constructor

  addNode(node)

    Add a `Node` to the current `Network`

    Parameters

      Node – Node to add

  addEdge(edge, source, target)

    Add a `Edge` to the current `Network`

    Parameters

      - edge (`Edge`) – Edge to add

      - source (`Node`) – Source Node

      - target (`Node`) – Target Node

  size()

    Return the number of edges

    Return type

      `int`

    Returns

      Number of edges

  setRoutingMethod(method)

    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)

    Define the A* weight

  toGeoCoords(base)

    Convert network to `core.Coords.GeoCoords`

    Parameters

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

  toENUCoords(base)

    Convert network to `core.Coords.ENUCoords`

    Parameters

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

  getSRID()

    Return the SRID of network

    Return type

      `int`

    Returns

      SRID of current network

  totalLength()

    Count the edges of current network

    Return type

      `int`

    Returns

      Number of edges

  simplify(tolerance, mode=1)

    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)

    Create a spatial index

    Parameters

      - resolution – TODO

      - margin (`float`) – TODO

      - verbose (`bool`) – Verbose creation

  exportSpatialIndex(filename)

    Export the spatial index to a file

    Parameters

      filename (`str`) – File to export

  importSpatialIndex(filename)

    Import a spatial index from a file

    Parma filename

      File to import

  getNextNodes(node_id)

    Give the XXX of a node

    Parameters

      node_id (`int`) – id of a node

  getNextEdges(node_id)

    Give the XXX of a node

    Parameters

      node_id (`int`) – id of a node

  getPrevNodes(node_id)

    Give the XXX of a node

    Parameters

      node_id (`int`) – id of a node

  getPrevEdges(node_id)

    Give the XXX of a node

    Parameters

      node_id (`int`) – id of a node

  getAdjacentNodes(node_id)

    Give the XXX of a node

    Parameters

      node_id (`int`) – id of a node

  getIncidentEdges(node_id)

    Give the XXX of a node

    Parameters

      node_id (`int`) – id of a node

  degree(node_id)

    Compute the degree of a node

    Parameters

      node_id (`int`) – id of a node

    Return type

      `int`

    Returns

      in degree

  degreeIn(node_id)

    Compute the in degree of a node

    Parameters

      node_id (`int`) – id of a node

    Return type

      `int`

    Returns

      in degree

  degreeOut(node_id)

    Compute the out degree of a node

    Parameters

      node_id (`int`) – id of a node

    Return type

      `int`

    Returns

      out degree

  getIndexNodes()

    Return the list of seted nodes

    Return type

      `list`[`int`]

    Returns

      A list of nodes id

  getNumberOfNodes()

    Number of nodes in the network

    Return type

      `int`

    Returns

      Number of nodes

  getNumberOfEdges()

    Number of edges in the network

    Return type

      `int`

    Returns

      Number of edges

  getNumberOfVertices()

    Number of vertices in the network

    Return type

      `int`

    Returns

      Number of vertices

  getNodeId(n)

    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)

    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()

    Return a list of all nodes Id

    Return type

      `list`[`int`]

    Returns

      A list of id

  getEdgesId()

    Return a list of all edges Id

    Return type

      `list`[`int`]

    Returns

      A list of id

  getRandomNode()

    Return a random node of network

    Return type

      `Node`

    Returns

      A node randomly choosed

  hasNode(id)

    Check if a node with a given id exist

    Parameters

      id (`int`) – node id to test

    Return type

      `bool`

  hasEdge(id)

    Check if a node with a given id exist

    Parameters

      id (`int`) – edge id to test

    Return type

      `bool`

  getNode(id)

    Return the node with a give id

    Parameters

      id (`int`) – node id to get

    Return type

      `Node`

  getEdge(id)

    Return the edge with a give id

    Parameters

      id (`int`) – edge id to get

    Return type

      `Edge`

  getAllEdgeGeoms()

    Return a TrackCollection of all edges

    Return type

      `TrackCollection`

    Returns

      All edges of `Network`

  bbox()

    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)
  __correctInputNode(node)

    TODO

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

    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)

    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)

    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)

    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)

    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)

    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)

    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)

    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)

    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)

    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)

    Saves DISTANCES attribute in a npy file for further use

    Parameters

      filename (`str`) – Path to save precomputed structure

  load_prep(filename)

    Imports DISTANCES attribute from an npy file

    Parameters

      filename (`str`) – Path where precomputed structure is saved