Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion LinkBikeNet_MVP.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
"closest_pairs = []\n",
"for i in range(to_iterate):\n",
" wcc = [H.subgraph(c).copy() for c in sorted(nx.connected_components(H), key=lambda c: sum([l[-1] for l in H.subgraph(c).copy().edges.data('length')]), reverse=True)]\n",
" pair = pair_between_closest_components(wcc)\n",
" pair = pair_between_largest_components(wcc)\n",
" closest_pairs.append(pair)\n",
" H.add_edge(pair[0], pair[1], length=0)"
],
Expand Down Expand Up @@ -201,6 +201,48 @@
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"cell_type": "code",
"source": "len(gdf)",
"id": "88be0fda819f9012",
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"cell_type": "code",
"source": [
"# reset Graph\n",
"H = G.edge_subgraph(edges).copy()\n",
"\n",
"# calculating connectivity metrics\n",
"print(\"Calculating connectivity metrics...\")\n",
"network_lengths = []\n",
"lcc_lengths = []\n",
"edge_lengths = gdf['geometry'].length\n",
"\n",
"for i in range(len(gdf)):\n",
" H.add_edge(closest_pairs[i][0], closest_pairs[i][1], length=edge_lengths[i])\n",
" total, largest = calculate_network_statistics(H)\n",
" network_lengths.append(total)\n",
" lcc_lengths.append(largest)"
],
"id": "fb40177941e881f0",
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"cell_type": "code",
"source": [
"gdf['network_length'] = network_lengths\n",
"gdf['lcc_length'] = lcc_lengths"
],
"id": "57d9c49a74f60f01",
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"cell_type": "code",
Expand Down
37 changes: 36 additions & 1 deletion linkbikenet/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from . import settings
import re
import osmnx as ox
import networkx as nx
import geopandas as gpd
import numpy as np
from scipy.spatial import cKDTree
Expand Down Expand Up @@ -358,4 +359,38 @@ def slugify(s):
"aeiouaeiouaeiouaeiouaeiouaeiouaaaccilnnoorssssuyyzz"
)
s = s.translate(tab)
return s
return s

def calculate_network_statistics(H):
"""Return total network length and largest component length.

Parameters
----------
H: networkx.Graph
undirected simple graph representing the street network with weighted edges

Returns
-------
total_length: float
total length of the network
largest_length: float
length of the largest connected component
"""

total_length = sum(
data["length"]
for _, _, data in H.edges(data=True)
)
components = nx.connected_components(H)

largest_component = max(components, key=lambda c: sum(
H[u][v]["length"]
for u, v in H.subgraph(c).edges()
))

largest_length = sum(
data["length"]
for _, _, data in H.subgraph(largest_component).edges(data=True)
)

return total_length, largest_length
18 changes: 18 additions & 0 deletions linkbikenet/linkbikenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,24 @@ def linkbikenet(

gdf['ordering'] = gdf.index

# reset Graph
H = G.edge_subgraph(edges).copy()

# calculating connectivity metrics
print("Calculating connectivity metrics...")
network_lengths = []
lcc_lengths = []
edge_lengths = gdf['geometry'].length

for i in range(len(gdf)):
H.add_edge(closest_pairs[i][0], closest_pairs[i][1], length=edge_lengths[i])
total, largest = calculate_network_statistics(H)
network_lengths.append(total)
lcc_lengths.append(largest)

gdf['network_length'] = network_lengths
gdf['lcc_length'] = lcc_lengths

edges_pbi_gdf = edges_gdf[edges_gdf["pbi"] == 1]

# Back to unprojected (potentially). No more calculations after here.
Expand Down
Loading