从iGraph集中性设置边缘属性时出现问题

时间:2019-04-14 10:39:49

标签: python networkx igraph osmnx

我正在尝试使用iGraph为来自osmnx的图计算边缘接近度的中心度,但是值正确附加。

我可以计算中心度,但是在将边缘属性设置为原始图形时,有些边缘没有值,而另一些边缘的属性(osmid)发生了变化,如在QGIS中显示边缘属性时所见。

https://imgur.com/a/JrL6znI

我认为这是我的代码的错误,该代码无法根据图形的边键和紧密度值创建字典。

我是Python的新手,所以无法弄清楚。任何帮助表示赞赏。

import igraph as ig
import networkx as nx
import osmnx as ox

ox.config(use_cache=True, log_console=True)
weight = 'length'

G = ox.graph_from_address('Chippendale, New South Wales', network_type='drive')
G_nx = nx.relabel.convert_node_labels_to_integers(G_nx)

G_ig = ig.Graph(directed=True)
G_ig.add_vertices(list(G_nx.nodes()))
G_ig.add_edges(list(G_nx.edges()))
G_ig.vs['osmid'] = list(nx.get_node_attributes(G_nx, 'osmid').values())
G_ig.es[weight] = list(nx.get_edge_attributes(G_nx, weight).values())

cc = G_ig.closeness(vertices=None, mode='ALL', cutoff=None, weights=weight, normalized=True)

cc_dict = dict(zip(G.edges, cc))

nx.set_edge_attributes(G, cc_dict, 'closeness')
ox.save_graph_shapefile(G)

更新 对于任何有兴趣的人,我都可以解决此问题。我使用igraph评估了节点的紧密度中心性,然后将边缘属性添加为“到/来自节点”值的平均值。

cc = dict(zip(G.nodes, closeness))

nx.set_node_attributes(G, cc, 'closeness')

nodes = list(G.nodes)
data_nodes = list(G.nodes(data=True))
for edge in G.edges(data=True):
    node_from = data_nodes[nodes.index(edge[0])][1][att_name]
    node_to = data_nodes[nodes.index(edge[1])][1][att_name]
    average = (((node_from + node_to) / 2))
    edge[2][att_name] = average

0 个答案:

没有答案
相关问题