Loader Module

class wayfarer.loader.UniqueDict[source]

A subclass of dict to ensure all keys are unique

wayfarer.loader.add_edge(net, properties)[source]

Add a new edge to a network based on a dict containing the required wayfarer fields

Parameters:
  • net (MultiGraph | MultiDiGraph) – A network

  • properties (dict) – A dictionary containing values for the edge

Return type:

str | int

Returns:

The key of the edge

>>> net = MultiGraph()
>>> add_edge(net, {"EDGE_ID": 1, "NODEID_FROM": 1, "NODEID_TO": 1})
1
wayfarer.loader.create_graph(use_reverse_lookup=True, graph_type=<class 'networkx.classes.multigraph.MultiGraph'>)[source]

Create a new networkx graph, with an optional dictionary to store unique keys for fast edge lookups

>>> net = create_graph()
Parameters:
  • use_reverse_lookup (bool) – Create a dictionary for fast key lookup

  • graph_type (MultiGraph | MultiDiGraph) – The type of network

Return type:

MultiGraph | MultiDiGraph

Returns:

The new network

wayfarer.loader.distance(p1, p2)[source]

Return the Euclidean distance between two points. Any z-values associated with the points are ignored.

Parameters:
  • p1 (tuple[int | float, int | float]) – The first point

  • p2 (tuple[int | float, int | float]) – The second point

Return type:

float

Returns:

The distance between the two points

>>> distance((0, 0), (10, 10))
14.142135623730951
wayfarer.loader.load_network_from_file(filename)[source]

Return a network previously saved to a pickle file

Parameters:

filename (str) – The filename to the pickle file containing the network

Return type:

MultiGraph | MultiDiGraph

Returns:

The network

wayfarer.loader.load_network_from_geometries(recs, use_reverse_lookup=True, graph_type=<class 'networkx.classes.multigraph.MultiGraph'>, skip_errors=False, strip_properties=False, keep_geometry=False, key_field='EDGE_ID', length_field='LEN_', rounding=5, use_integer_keys=True)[source]

Create a new networkX graph using a list of recs of type __geo_interface__ This allows networks to be created using libraries such as Fiona. Any MultiLineString geometries in the network will be ignored, geometries should be converted to LineString prior to creating the network.

>>> rec = {"geometry": {"type": "LineString", "coordinates": [(0, 0), (0, 1)]}, "properties": {"EDGE_ID": 1}}
>>> net = load_network_from_geometries([rec])
>>> str(net)  
"MultiGraph named 'Wayfarer Generated MultiGraph (version ...)' with 2 nodes and 1 edges"
Parameters:
  • recs (Iterable) – An iterable of dictionary objects The filename to the pickle file containing the network

  • use_reverse_lookup (bool) – Create a dictionary as part of the graph that stores unique edge keys for fast lookups

  • graph_type (MultiGraph | MultiDiGraph) – The type of network to create

  • skip_errors (bool) – Ignore any geometries that are not of type LineString

  • strip_properties (bool) – Reduce the size of the network file by only retaining the properties required to run routing. Any other properties in the records will be ignored

  • key_field (str) – A key field containing a unique Id for each feature must be provided

  • length_field (str) – The field in the geometry properties that contains a length. If not provided then the length will be calculated from the geometry. It is assumed the geometry is projected, as a simple planar distance will be calculated.

  • rounding (int) – If no node fields are used then nodes will be created based on the start and end points of the input geometries. As node values must match exactly for edges to be connected rounding to a fixed number of decimal places avoids unconnected edges to to tiny differences in floats e.g. (-9.564484483347517, 52.421103202488965) and (-9.552925853749544, 52.41969110706263)

  • use_integer_keys (bool) – Using Integer keys makes using wayfarer faster. By default keys will be attempted to be converted to integers. Set this to False to leave keys unconverted (for example when using String keys)

Return type:

MultiGraph | MultiDiGraph

Returns:

A new network

wayfarer.loader.load_network_from_records(recs, use_reverse_lookup=True, graph_type=<class 'networkx.classes.multigraph.MultiGraph'>, key_field='EDGE_ID', length_field='LEN_', from_field='NODEID_FROM', to_field='NODEID_TO')[source]

Create a new networkX graph based on a list of dictionary objects containing the required wayfarer properties

>>> recs = [{"EDGE_ID": 1, "NODEID_FROM": 1, "NODEID_TO": 2, "LEN_": 5, "PROP1": "A"}]
>>> net = load_network_from_records(recs)
>>> str(net)  
"MultiGraph named 'Wayfarer Generated MultiGraph (version ...)' with 2 nodes and 1 edges"
Parameters:
  • recs (Iterable) – An iterable of dictionary objects The filename to the pickle file containing the network

  • use_reverse_lookup (bool) – Create a dictionary as part of the graph that stores unique edge keys for fast lookups

  • graph_type (MultiGraph | MultiDiGraph) – The type of network to create

Return type:

MultiGraph | MultiDiGraph

Returns:

A new network

wayfarer.loader.save_network_to_file(net, filename)[source]

Save a network to a Python pickle file Note these cannot be shared between different versions of Python

Parameters:
  • net (MultiGraph | MultiDiGraph) – The network

  • filename (str) – The filename to save the network as a pickle file

Return type:

None