Thanks ahead of time for the help. I am having trouble with appending an array index to a list. I Have an array counterStack which contains the starting point (1366, 1264) I run a neighborhood search on that starting point and for every new index that satisfies the conditions set the index should be appended to the counterStack. The strange thing is indices get properly appended to the list stack, but not list counterStack.
Is this because of my if statement (if ([nx, ny] not in counterStack):)? I know it enters the if statement because the counter count gets plus one through each iteration. Indices will get added to stack twice. So I set up the counterStack to only account for an index once so I can limit the overall edited pixels. By the end of the run there should be 121 indeces appended to counterStack.
This is an example of the results:
count = 1
[(1366, 1264), (1365, 1263)]
count = 2
[(1366, 1264), (1365, 1264)]
count = 3
[(1366, 1264), (1365, 1265)]
count = 4
[(1366, 1264), (1366, 1265)]
count = 5
[(1366, 1264), (1367, 1265)]
count = 6
[(1366, 1264), (1367, 1264)]
count = 7
[(1366, 1264), (1367, 1263)]
count = 8
[(1366, 1264), (1366, 1263)]
count = 9
[(1366, 1264), (1365, 1262)]
Here is some of my code:
neighbors = [(-1,-1), (-1,0), (-1,1), (0,1), (1,1), (1,0), (1,-1), (0,-1)]
mask = np.zeros_like(dem_arr, dtype = bool)
stack = [(1366, 1264)] # push start coordinate on stack
if (dem_arr[1366, 1264] > -100):
count = 0
while count <= 121:
x, y = stack.pop()
mask[x, y] = True
for dx, dy in neighbors:
nx, ny = x + dx, y + dy
if (0 <= nx < dem_arr.shape[0] and 0 <= ny < dem_arr.shape[1] and dem_arr[x, y] > -100 and dem_arr[nx, ny] > -100 and not mask[nx, ny] and abs(dem_arr[nx, ny] - dem_arr[x, y]) <= 5): #set elevation differnce
stack.append((nx, ny)) #if point is selected (true) array position gets added to stack and process runs over again
counterStack = [(1366, 1264)]
if ([nx, ny] not in counterStack):
counterStack.append((nx, ny))
dem_copy[(nx, ny)] = 8888
#dem_copy[randx, randy] = 8888
count += 1
print 'count = ', count
print counterStack
else:
print 'Point chosen has no data'
randx = random.randint(0, row-1)
randy = random.randint(0, col-1)
Thanks again for the help!
-R
dem_arr?