1

I want to use a raster for a A* and bidirectional Dijkstra path analysis in NetworkX. I am using Python for this project.

Raster example (it's a png file converted when uploaded, but the real problem is TIFF): Here

First I read in the raster with GDAL

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

Next I read the raster as an array

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

So, I'll transform coords using a function:

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])

Next, the graph is loaded and function is called:

G = nx.DiGraph(arr)
length, path = nx.bidirectional_dijkstra(G, source, target)

But, the function return a error:

raise NetworkXError("The node %s is not in the digraph."%(n,))

networkx.exception.NetworkXError: The node (2612, -352) is not in the digraph.

How to do this in correct way?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.