我有一个河网的shapefile,它的属性是流的顺序和权重(与这条流相关的垃圾的总和)。如何使用python和networkx模块计算下游权重的累积?
我在networkx中寻找合适的工具,但没有找到,或者我在参考文献中有误解。
发布于 2019-03-24 18:22:51
我已经使用算法祖先解决了这个问题。它似乎与节点一起工作,并返回节点集,我们可以对其进行总结。这不是很方便,因为我们需要从流到节点获得权重,然后将它们从节点关联回流。但它真的起作用了。
import networkx as nx
import os
dir = r'd:\YandexDisk\Projects\Bali'
os.chdir(dir)
G = nx.read_shp(r'Rivers_network.shp')
nodes = nx.nodes(G)
edges = nx.edges(G)
total_dict = nx.get_edge_attributes(G, 'Total')
for node in nodes:
ancestors = list(nx.ancestors(G, node))
sum_weight = 0
for coords in ancestors:
for edge in edges:
if tuple([edge[0][0], edge[0][1]]) == tuple([coords[0], coords[1]]):
sum_weight += int(total_dict[edge])
print(sum_weight)
for edge in edges:
if edge[0] == node:
accum_edge = {edge: sum_weight}
nx.set_edge_attributes(G, accum_edge, 'Accum')
nx.write_shp(G, dir)https://stackoverflow.com/questions/55311963
复制相似问题