Being not the most savvy python user, I've been trying to find a solution to speed up my code. I have 2 raster files, both have the same dimension and extent. One raster is a river, extracted from a LIDAR image, with a narrow range of elevation values, all other values in the river file are 0. So basically, with the exception of the river width (the pixels with elevation data), 99% of the river raster has the value 0. The other raster file is extracted from a LIDAR image and has elevation data throughout its entire extent.
My plan is to go over each pixel in the river file that does not have a 0 value, add 1 to it (if the river water level rises by 1 meter) and compare it to the neighboring pixel elevation data (basically the pixel location of the LIDAR elevation). If it is higher, give it a value of 1 (which means that this pixel is flooded). The code that I came up with works, but it looks horrible, and extremely slow (paint dries faster than the code runs..) So I'm looking for a way to speed things up. I looked at vectorizing, numpy, etc, but can't make sense of it yet (could be because I've been at work for over 9 hours..)
So here it is, any suggestions would be very much appreciated:
river = arcpy.Raster(r'C:\Flood.gdb\Clip_River1')
lidar = arcpy.Raster(r'C:\Flood.gdb\Clip_Lidar_1')
arrayRiver = arcpy.RasterToNumPyArray(river,nodata_to_value=0)
(rHeight, rWidth)=arrayRiver.shape
arrayLidar = arcpy.RasterToNumPyArray(lidar,nodata_to_value=0)
(lHeight, lWidth)=arrayLidar.shape
# extent of the 2 rasters: columns 3822, rows 10129
flood = 1
while flood == 1:
flood = 0
for row in range(0,rHeight-1):
for col in range(0,rWidth-1):
if arrayRiver.item(row,col) <> 0 and arrayRiver[row,col] <> 1:
if arrayLidar[row,col-1] <> 1 and arrayLidar.item(row,col-1) < arrayRiver.item(row,col)+1:
arrayLidar[row,col-1] = 1
arrayRiver[row,col-1] = arrayRiver.item(row,col)
flood = 1
[....doing the same concept for each possible neighboring pixel]
if arrayLidar[row+1,col+1] <> 1 and arrayLidar.item(row+1,col+1) < arrayRiver.item(row,col)+1:
arrayLidar[row+1,col+1] = 1
arrayRiver[row+1,col+1] = arrayRiver.item(row,col)
flood = 1
arrayRiver[row,col] = 1
newRaster = arcpy.NumPyArrayToRaster(arrayLidar,lowerLeft,cellSize,value_to_nodata=0)
newRaster.save(r"C:\Flood.gdb\newRastaRiver_small")
newEmptyRaster = arcpy.NumPyArrayToRaster(arrayEmpty,lowerLeft,cellSize,value_to_nodata=0)
newEmptyRaster.save(r"C:\Flood.gdb\newEmptyRastaRiver_small")