AnsweredAssumed Answered

checking the connectivity of cells in a raster

Question asked by stashaestha1 on Dec 14, 2017
Latest reply on Dec 17, 2017 by xander_bakker

Hello, can anybody help with a python script that checks if cell A is connected to its neighboring cells and if it flooded then the cell connecting cells should be flooded too. I tried the code below but i cant seem to figure out how to in-cooperate my data into the script. My data include a DEM from which i will check inundated zone:

 


import heapq
import numpy as np
from scipy import ndimage


def flood_fill(test_array, four_way=False):
""""""
input_array = np.copy(test_array)
input_rows, input_cols = input_array.shape

# Set h_max to a value larger than the array maximum to ensure
#   that the while loop will terminate
h_max = np.max(input_array*2.0)

# Build mask of cells with data not on the edge of the image
# Use 3x3 square structuring element
inside_mask = ndimage.binary_erosion(
    np.isfinite(input_array),
structure=ndimage.generate_binary_structure(2, 2).astype(np.int))

# Initialize output array as max value test_array except edges
output_array = np.copy(input_array)
output_array[inside_mask] = h_max

# Build priority queue and place edge pixels into priority queue
# Last value is flag to indicate if cell is an edge cell
put = heapq.heappush
get = heapq.heappop
fill_heap = [
    (output_array[t_row, t_col], int(t_row), int(t_col), True)
    for t_row, t_col in np.transpose(np.where(edge_mask))]
heapq.heapify(fill_heap)

def neighbors(row, col, four_way=False):
    """Return indices of adjacent cells"""
    if four_way:
        return [
            (row - 1, col), (row, col + 1),
            (row + 1, col), (row, col - 1)]
    else:
        return [
            (row - 1, col), (row - 1, col + 1),
            (row, col + 1), (row + 1, col + 1),
            (row + 1, col), (row + 1, col - 1),
            (row, col - 1), (row - 1, col - 1)]

# Iterate until priority queue is empty
while True:
    try:
    h_crt, t_row, t_col, edge_flag = get(fill_heap)
except IndexError:
    break
for n_row, n_col in neighbors(t_row, t_col, four_way):
    # Skip cell if outside array edges
    if edge_flag:
        try:
            if not inside_mask[n_row, n_col]:
        continue
except IndexError:







Outcomes