Source code for nrel.hive.model.roadnetwork.link_id

from typing import Optional, Tuple, Any
from nrel.hive.util.typealiases import LinkId

NodeId = Any





[docs]def extract_node_ids( link_id: LinkId, ) -> Tuple[Optional[Exception], Optional[Tuple[NodeId, NodeId]]]: """ expects the provided string is of the form {src_node_id}-{dst_node_id} :param link_id: a string that is a LinkId :return: an error, or, a tuple containing both node ids """ result = link_id.split("-") if len(result) < 2: return ( Exception(f"LinkId {link_id} does not take the form src_node_id-dst_node_id"), None, ) elif len(result) > 2: return ( Exception( f"LinkId {link_id} can only have one dash (-) character in the form src_node_id-dst_node_id" ), None, ) else: try: src = str(result[0]) dst = str(result[1]) except ValueError: return Exception(f"LinkId {link_id} cannot be parsed."), None return None, (src, dst)
[docs]def extract_node_ids_int(link_id: LinkId) -> Tuple[Optional[Exception], Optional[Tuple[int, int]]]: """node ids can be strings for the haversine case but for networkx graphs they must be integers as they are treated as indices. :param link_id: link id to read :type link_id: LinkId :return: _description_ :rtype: Tuple[Optional[Exception], Optional[Tuple[int, int]]] """ err, ids = extract_node_ids(link_id) if err is not None or ids is None: return err, None else: try: src_str, dst_str = ids src = int(src_str) dst = int(dst_str) return None, (src, dst) except ValueError as e: return e, None