I am quite new to python and programming in general and I am trying to execute some code from a book.
The code I am having troubles with should execute a flood fill function on an elevation raster from a certain source point but it always returns 0 values for all cells.
From here you can download the data I used:https://geospatialpython.googlecode.com/files/FloodFill.zip
Here is the code:
import numpy as npfrom linecache import getlinedef floodFill (c,r,mask): #cells already filled filled = set() #cells to fillfill = set()fill.add((c,r))width = mask.shape[1]-1height = mask.shape [0]-1#our output inundation arrayflood = np.zeros_like(mask, dtype=np.int8)#loop through and modify the cells which need to be checkedwhile fill: #grab a cell x,y = fill.pop() if y == height or x == width or x < 0 or y < 0: #don't fill continue if mask [y] [x] == 1: #do fill flood [y] [x] =1 filled.add((x,y)) #check neighbours for 1 values west = (x-1,y) east = (x+1,y) north = (x,y-1) south = (x,y+1) if not west in filled: fill.add(west) if not east in filled: fill.add(east) if not north in filled: fill.add(north) if not south in filled: fill.add(south) return flood#------------------------------------------------------------source = "C:/Users/A021794/Desktop/FloodFill/terrain.asc"target = "C:/Users/A021794/Desktop/FloodFill/flood.asc"print "Opening image..."img = np.loadtxt(source, skiprows=6)print "Image opened"a = np.where(img
The code I am having troubles with should execute a flood fill function on an elevation raster from a certain source point but it always returns 0 values for all cells.
From here you can download the data I used:https://geospatialpython.googlecode.com/files/FloodFill.zip
Here is the code:
import numpy as npfrom linecache import getlinedef floodFill (c,r,mask): #cells already filled filled = set() #cells to fillfill = set()fill.add((c,r))width = mask.shape[1]-1height = mask.shape [0]-1#our output inundation arrayflood = np.zeros_like(mask, dtype=np.int8)#loop through and modify the cells which need to be checkedwhile fill: #grab a cell x,y = fill.pop() if y == height or x == width or x < 0 or y < 0: #don't fill continue if mask [y] [x] == 1: #do fill flood [y] [x] =1 filled.add((x,y)) #check neighbours for 1 values west = (x-1,y) east = (x+1,y) north = (x,y-1) south = (x,y+1) if not west in filled: fill.add(west) if not east in filled: fill.add(east) if not north in filled: fill.add(north) if not south in filled: fill.add(south) return flood#------------------------------------------------------------source = "C:/Users/A021794/Desktop/FloodFill/terrain.asc"target = "C:/Users/A021794/Desktop/FloodFill/flood.asc"print "Opening image..."img = np.loadtxt(source, skiprows=6)print "Image opened"a = np.where(img