diff --git a/__pycache__/urls.cpython-310.pyc b/__pycache__/urls.cpython-310.pyc index dca75e5..4227ce1 100644 Binary files a/__pycache__/urls.cpython-310.pyc and b/__pycache__/urls.cpython-310.pyc differ diff --git a/__pycache__/views.cpython-310.pyc b/__pycache__/views.cpython-310.pyc index 7f83fd0..8d7cdb4 100644 Binary files a/__pycache__/views.cpython-310.pyc and b/__pycache__/views.cpython-310.pyc differ diff --git a/templates/pxy_city_digital_twins/city_digital_twin.html b/templates/pxy_city_digital_twins/city_digital_twin.html index a657d97..da426db 100644 --- a/templates/pxy_city_digital_twins/city_digital_twin.html +++ b/templates/pxy_city_digital_twins/city_digital_twin.html @@ -19,13 +19,27 @@ } }); + + - + + + {% endfor %} + + + + + + + + + + + + + + + + + + + + + diff --git a/urls.py b/urls.py index a594400..85d593d 100644 --- a/urls.py +++ b/urls.py @@ -7,4 +7,5 @@ urlpatterns = [ # Pattern to accept string words path('city/digital/twin//', views.city_digital_twin, name='city_digital_twin_str'), + ] diff --git a/views.py b/views.py index f1f1edc..ed589a8 100644 --- a/views.py +++ b/views.py @@ -298,22 +298,8 @@ def generate_com_con_city_data(lat, long): }) # Fiber paths connect neighboring towers - fiber_paths = [] - for i in range(len(towers) - 1): - fiber_paths.append({ - 'id': i + 1, - 'start_x': towers[i]['position_x'], - 'start_z': towers[i]['position_z'], - 'end_x': towers[i + 1]['position_x'], - 'end_z': towers[i + 1]['position_z'], - 'mid_x': (towers[i]['position_x'] + towers[i + 1]['position_x']) / 2, - 'mid_y': 0.1, - 'mid_z': (towers[i]['position_z'] + towers[i + 1]['position_z']) / 2, - 'length': ((towers[i + 1]['position_x'] - towers[i]['position_x'])**2 + (towers[i + 1]['position_z'] - towers[i]['position_z'])**2)**0.5, - 'angle': random.uniform(0, 360), - 'status': 'Connected' if random.random() > 0.1 else 'Broken', - 'color': '#4682b4' - }) + # Compute optimized fiber paths using MST + fiber_paths = compute_mst_fiber_paths(towers) # Wi-Fi Hotspots scattered nearby but within grid bounds wifi_hotspots = [] @@ -330,8 +316,76 @@ def generate_com_con_city_data(lat, long): 'color': '#32cd32' }) + network_summary = compute_network_summary(towers, fiber_paths, wifi_hotspots) + return { 'towers': towers, 'fiber_paths': fiber_paths, - 'wifi_hotspots': wifi_hotspots + 'wifi_hotspots': wifi_hotspots, + 'network_summary': network_summary, } + + +import networkx as nx +import math + +def compute_distance(t1, t2): + """ + Compute Euclidean distance between two towers in the horizontal plane. + """ + dx = t1['position_x'] - t2['position_x'] + dz = t1['position_z'] - t2['position_z'] + return math.sqrt(dx**2 + dz**2) + +def compute_mst_fiber_paths(towers): + """ + Given a list of tower dictionaries, compute a Minimum Spanning Tree (MST) + and return a list of fiber paths connecting the towers. + """ + G = nx.Graph() + # Add towers as nodes + for tower in towers: + G.add_node(tower['id'], **tower) + + # Add edges: compute pairwise distances + n = len(towers) + for i in range(n): + for j in range(i+1, n): + d = compute_distance(towers[i], towers[j]) + G.add_edge(towers[i]['id'], towers[j]['id'], weight=d) + + # Compute MST + mst = nx.minimum_spanning_tree(G) + + fiber_paths = [] + for edge in mst.edges(data=True): + id1, id2, data = edge + # Find towers corresponding to these IDs + tower1 = next(t for t in towers if t['id'] == id1) + tower2 = next(t for t in towers if t['id'] == id2) + + fiber_paths.append({ + 'id': len(fiber_paths) + 1, + 'start_x': tower1['position_x'], + 'start_z': tower1['position_z'], + 'end_x': tower2['position_x'], + 'end_z': tower2['position_z'], + 'mid_x': (tower1['position_x'] + tower2['position_x']) / 2, + 'mid_y': 0.1, # Slightly above the ground + 'mid_z': (tower1['position_z'] + tower2['position_z']) / 2, + 'length': data['weight'], + # Optionally, compute the angle in degrees if needed: + 'angle': math.degrees(math.atan2(tower2['position_x'] - tower1['position_x'], + tower2['position_z'] - tower1['position_z'])), + 'status': 'Connected', + 'color': '#4682b4' + }) + return fiber_paths + +def compute_network_summary(towers, fiber_paths, wifi_hotspots): + total_fiber = sum(fiber['length'] for fiber in fiber_paths) + return { + 'num_towers': len(towers), + 'total_fiber_length': total_fiber, + 'num_wifi': len(wifi_hotspots), + } \ No newline at end of file