### Install pytntp from GitHub Source: https://github.com/ben-hudson/pytntp/blob/main/README.md Install the pytntp library directly from its GitHub repository using pip. ```bash pip install git+https://github.com/ben-hudson/pytntp ``` -------------------------------- ### Visualize Network Congestion and Flow Source: https://github.com/ben-hudson/pytntp/blob/main/docs/getting-started.md Visualizes the network, coloring edges by congestion (vc_ratio) and traffic flow (Volume). Centroid connectors (link_type == 3) are excluded to improve map clarity. Requires matplotlib and osmnx. ```python import matplotlib.pyplot as plt import osmnx as ox road_edges = [(u, v, k) for u, v, k, d in network.edges(keys=True, data=True) if d.get("link_type") != 3] roads = network.edge_subgraph(road_edges) fig, (ax_vc, ax_flow) = plt.subplots(1, 2, figsize=(16, 8)) ox.plot.plot_graph(roads, ax=ax_vc, edge_color=tntp.quantile_edge_colors(roads, "vc_ratio", "RdYlGn_r"), show=False) ox.plot.plot_graph(roads, ax=ax_flow, edge_color=tntp.quantile_edge_colors(roads, "Volume", "magma"), show=False) ``` -------------------------------- ### Convert to NetworkX Graph Source: https://github.com/ben-hudson/pytntp/blob/main/docs/getting-started.md Converts node and network dataframes into a NetworkX graph, compatible with osmnx. Edge attributes from the network dataframe are preserved. Calculates the volume-to-capacity ratio for each edge. ```python network = tntp.convert_to_networkx(node_df, net_df) for u, v, k, data in network.edges(keys=True, data=True): data["vc_ratio"] = data["Volume"] / data["capacity"] ``` -------------------------------- ### Read TNTP Network Files Source: https://github.com/ben-hudson/pytntp/blob/main/docs/getting-started.md Loads network data including flow, node, and net files from a specified URL. It renames 'From' and 'To' columns for consistency and merges flow data into the network edges. Ensure the root URL and file names are correct. ```python import tntp from urllib.parse import urljoin name = "ChicagoSketch" # Note: the file prefix is "ChicagoSketch" but the directory is "Chicago-Sketch". root = "https://raw.githubusercontent.com/bstabler/TransportationNetworks/refs/heads/master/Chicago-Sketch/" flow_df = tntp.read_flow_file(urljoin(root, f"{name}_flow.tntp")).rename( columns={"From": "init_node", "To": "term_node"} ) node_df = tntp.read_node_file( urljoin(root, f"{name}_node.tntp"), index_col="node", x_col="X", y_col="Y", crs="EPSG:26771" ) net_df = tntp.read_net_file(urljoin(root, f"{name}_net.tntp"), crs="EPSG:26771").merge( flow_df, on=["init_node", "term_node"] ) ``` -------------------------------- ### Read TNTP files and convert to NetworkX graph Source: https://github.com/ben-hudson/pytntp/blob/main/docs/index.md Load node and network data from .tntp files using specified URLs and convert them into a NetworkX graph. Ensure the correct CRS is provided for accurate geospatial representation. ```python import tntp from urllib.parse import urljoin root = "https://raw.githubusercontent.com/bstabler/TransportationNetworks/refs/heads/master/SiouxFalls/" node_df = tntp.read_node_file( urljoin(root, "SiouxFalls_node.tntp"), index_col="Node", x_col="X", y_col="Y", crs="EPSG:4326" ) net_df = tntp.read_net_file(urljoin(root, "SiouxFalls_net.tntp"), crs="EPSG:4326") network = tntp.convert_to_networkx(node_df, net_df) ``` -------------------------------- ### Load and Split TNTP Network Data Source: https://github.com/ben-hudson/pytntp/blob/main/docs/zone-splitting.md Loads network, node, demand, and flow data from specified URLs and then splits the centroid nodes using `tntp.split_zone_nodes`. The network data is merged with flow data before splitting to ensure volume and cost information is preserved. ```python import geopandas as gpd import tntp from urllib.parse import urljoin root = "https://raw.githubusercontent.com/bstabler/TransportationNetworks/refs/heads/master/Anaheim/" node_df = gpd.read_file(urljoin(root, "anaheim_nodes.geojson")).set_index("id").rename_axis("node") net_df = tntp.read_net_file(urljoin(root, "Anaheim_net.tntp"), crs=node_df.crs) demand_df = tntp.read_demand_file(urljoin(root, "Anaheim_trips.tntp")) flow_df = tntp.read_flow_file(urljoin(root, "Anaheim_flow.tntp")).rename( columns={"From": "init_node", "To": "term_node"} ) # Merge flow into net BEFORE splitting so Volume/Cost ride along through the centroid # rename. pd.merge drops attrs, so preserve FIRST THRU NODE manually. attrs = net_df.attrs net_df = net_df.merge(flow_df, on=["init_node", "term_node"]) net_df.attrs = attrs node_df, net_df, demand_df = tntp.split_zone_nodes(node_df, net_df, demand_df) network = tntp.convert_to_networkx(node_df, net_df) ``` -------------------------------- ### Filter Road Edges Using `parent_node` Source: https://github.com/ben-hudson/pytntp/blob/main/docs/zone-splitting.md Identifies actual road edges after zone splitting by filtering edges where both endpoints descend from a through node (i.e., their `parent_node` ID is greater than or equal to the `FIRST THRU NODE`). This creates a subgraph containing only the road network. ```python first_thru_node = int(net_df.attrs["FIRST THRU NODE"]) through_nodes = set(node_df.index[node_df["parent_node"] >= first_thru_node]) road_edges = [(u, v, k) for u, v, k in network.edges(keys=True) if u in through_nodes and v in through_nodes] roads = network.edge_subgraph(road_edges) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.