-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEdgeCalculation.py
36 lines (29 loc) · 1.15 KB
/
EdgeCalculation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import networkx as nx
from geopy.distance import great_circle
# 读取GraphML文件
G = nx.read_graphml('updated_Chinanet.graphml')
# 创建一个字典存储节点坐标
pos = {}
for node in G.nodes(data=True):
node_id = node[0]
lat = node[1].get('Latitude') # 使用正确的键名
lon = node[1].get('Longitude') # 使用正确的键名
if lat is not None and lon is not None:
pos[node_id] = (float(lat), float(lon))
else:
print(f"Warning: Node {node_id} is missing latitude or longitude.")
# 计算并添加边的距离
for u, v, data in G.edges(data=True):
if u in pos and v in pos:
coord1 = pos[u]
coord2 = pos[v]
distance = great_circle(coord1, coord2).kilometers # 计算距离(公里)
# 将距离转换为长整型(以米为单位)
weight = int(distance * 1000) # 转换为米并取整
# 添加更新weight属性
data['weight'] = weight
else:
print(f"Warning: One of the nodes {u} or {v} is missing coordinates.")
# 保存更新后的GraphML文件
nx.write_graphml(G, 'updated_Chinanet.graphml')
print("GraphML file updated successfully.")