如何在NetworkX图中将坐标转换为indice数组(反之亦然)?

时间:2017-05-13 21:58:01

标签: python networkx dijkstra a-star geotiff

我想在NetworkX中使用栅格进行A *和双向Dijkstra路径分析。我在这个项目中使用Python。

Raster示例(它是上传时转换的png文件,但真正的问题是TIFF): [Here](https://i.stack.imgur.com/fz9CM.png)

首先,我使用GDAL读取栅格

input_raster = "raster.tif"
raster = gdal.Open(input_raster)

接下来,我将栅格作为数组读取

bandraster = raster.GetRasterBand(1)
arr = bandraster.ReadAsArray()

因此,我将使用函数转换坐标:

def coord2pixelOffset(rasterfn, x, y):
    raster = gdal.Open(rasterfn)
    geotransform = raster.GetGeoTransform()
    originX = geotransform[0]
    originY = geotransform[3]
    pixelWidth = geotransform[1]
    pixelHeight = geotransform[5]
    xOffset = int((x - originX)/pixelWidth)
    yOffset = int((y - originY)/pixelHeight)
    return xOffset, yOffset

CostSurfacefn = 'raster.tif'
source_coord = (-41.1823753163, -13.83393276)
target_coord = (-40.3726182077, -14.2361991946)

# coordinates to array index
source = coord2pixelOffset(CostSurfacefn, source_coord[0], source_coord[1])
target = coord2pixelOffset(CostSurfacefn, target_coord[0], target_coord[1])

数组就像这样(例子):

# Grid with 2x2. The float numbers are the pixel values 
[[  1.83781120e+08   1.90789248e+08]
[  1.83781120e+08   1.90789248e+08]]

# array[0][0] is 1.83781120e+08
# array[0][1] is 1.90789248e+08
# array[1][0] is 1.83781120e+08
# array[1][1] is 1.90789248e+08

接下来,加载图形并调用bi-dijkstra函数(但我希望例如从array [0] [0]到array [1] [1]):

G = nx.from_numpy_matrix(np.array(arr))
length, path = nx.bidirectional_dijkstra(G, source, target)

如何通过数组获取源和目标的节点ID?

0 个答案:

没有答案