I try to make an A-star Path Finding program base on tile-graph (image raster data as graph) where each pixel values represent as cost and elevation.. In my case, DEM raster data was called by using gdal, then converting to numpy array to be processed on A* path finding implementation.This similar with Least Cost-Path analysis in GIS.
This is my standalone script i've been made so far :
import gdal from gdalconst import * import numpy as np from numpy import savetxt import math as mt def Input_Dem(): filename = "F:\dem.tif" dataset = gdal.Open( filename, GA_ReadOnly ) if dataset is None: print "failed" return dataset def Input_Cost(): filename = "F:\cost.tif" dataset = gdal.Open( filename, GA_ReadOnly ) if dataset is None: print "failed" return dataset def Weight(): W = 1 return W def RasterMatrix(): dem_surface = Input_Dem() cost_surface = Input_Cost() DEM_Matrix = np.array(dem_surface.GetRasterBand(1).ReadAsArray()) COST_Matrix = np.array(cost_surface.GetRasterBand(1).ReadAsArray()) geotransform_dem = dem_surface.GetGeoTransform() geotransform_cost = cost_surface.GetGeoTransform() col_dem = dem_surface.RasterXSize row_dem = dem_surface.RasterYSize col_cost = dem_surface.RasterXSize row_cost = cost_surface.RasterYSize cell_id_elevation = dict(enumerate(DEM_Matrix.flatten(),1)) cell_id_cost = dict(enumerate(COST_Matrix.flatten(),1)) cell_size = geotransform_dem[1] col = col_dem row = row_dem return cell_id_elevation, cell_id_cost, col, row, cell_size, DEM_Matrix, COST_Matrix def Var(): start_id = 1 end_id = 151044 open_list = [ ] close_list = [ ] current_cell_id = 0 accumulated_cost = { start_id : 0 } parent_list = { } heuristic_type = 'M' return start_id, end_id, open_list, close_list, parent_list, accumulated_cost, current_cell_id, heuristic_type def adjacent_cell(o,dx,dy): adjacent_square = [o-(dx+1),o-dx,o-(dx-1),o-1,o+1,o+(dx-1),o+dx,o+(dx+1)] for i in range(dx,dy*dx+dx,dx) : if o == i : adjacent_square = filter(lambda n: n != o-(dx-1) and n != o+1 and n != o+(dx+1) ,adjacent_square) for i in range(1,dy*dx,dx) : if o == i : adjacent_square = filter(lambda n: n != o-(dx+1) and n != o-1 and n != o+(dx-1) ,adjacent_square) adjacent_square = filter(lambda n: n > 0 and n
This is my standalone script i've been made so far :
import gdal from gdalconst import * import numpy as np from numpy import savetxt import math as mt def Input_Dem(): filename = "F:\dem.tif" dataset = gdal.Open( filename, GA_ReadOnly ) if dataset is None: print "failed" return dataset def Input_Cost(): filename = "F:\cost.tif" dataset = gdal.Open( filename, GA_ReadOnly ) if dataset is None: print "failed" return dataset def Weight(): W = 1 return W def RasterMatrix(): dem_surface = Input_Dem() cost_surface = Input_Cost() DEM_Matrix = np.array(dem_surface.GetRasterBand(1).ReadAsArray()) COST_Matrix = np.array(cost_surface.GetRasterBand(1).ReadAsArray()) geotransform_dem = dem_surface.GetGeoTransform() geotransform_cost = cost_surface.GetGeoTransform() col_dem = dem_surface.RasterXSize row_dem = dem_surface.RasterYSize col_cost = dem_surface.RasterXSize row_cost = cost_surface.RasterYSize cell_id_elevation = dict(enumerate(DEM_Matrix.flatten(),1)) cell_id_cost = dict(enumerate(COST_Matrix.flatten(),1)) cell_size = geotransform_dem[1] col = col_dem row = row_dem return cell_id_elevation, cell_id_cost, col, row, cell_size, DEM_Matrix, COST_Matrix def Var(): start_id = 1 end_id = 151044 open_list = [ ] close_list = [ ] current_cell_id = 0 accumulated_cost = { start_id : 0 } parent_list = { } heuristic_type = 'M' return start_id, end_id, open_list, close_list, parent_list, accumulated_cost, current_cell_id, heuristic_type def adjacent_cell(o,dx,dy): adjacent_square = [o-(dx+1),o-dx,o-(dx-1),o-1,o+1,o+(dx-1),o+dx,o+(dx+1)] for i in range(dx,dy*dx+dx,dx) : if o == i : adjacent_square = filter(lambda n: n != o-(dx-1) and n != o+1 and n != o+(dx+1) ,adjacent_square) for i in range(1,dy*dx,dx) : if o == i : adjacent_square = filter(lambda n: n != o-(dx+1) and n != o-1 and n != o+(dx-1) ,adjacent_square) adjacent_square = filter(lambda n: n > 0 and n