我有一个看起来像这样的数组(该数组是通过转换 TIF 斜率栅格创建的)
[[ 15.26072407 16.01073837 27.81685448 24.49266815 32.94010162]
[ 26.0834446 21.01086426 30.02531815 25.44641304 19.48436928]
[ 21.98579979 19.06574059 24.44838905 26.67520905 26.61030769]
[ 20.42549133 28.58900261 17.00629425 31.25236893 32.34066772]
[ 8.33120537 24.303339 7.85025167 35.50911713 29.56856346]]
我将此数组转换为图表。数组中的第一个节点如下所示:
>>> G[1]
{0: {'weight': 26.083444595336914}, 1: {'weight': 21.0108642578125}, 2: {'weight': 19.06574058532715}, 3: {'weight': 28.58900260925293}, 4: {'weight': 24.3033390045166}}
现在我想找到节点 1、边缘 3 和节点 4、边缘 1 之间的最短路径。这可能吗,还是我必须以不同的方式设置图表?我有可能,我该怎么做。我只能找到节点之间的路径(例如nx.shortest_path(G,source=0,target=4)
,不在边缘之间。
TIF 首先使用 GDAL 转换为数组:
import gdal, numpy
import networkx as nx
# Open data
input_raster = "slope5.tif"
raster = gdal.Open(input_raster)
# read raster as arrays
bandraster = raster.GetRasterBand(1)
arr = bandraster.ReadAsArray()
G = nx.DiGraph(arr)