# dem_waters_extractor / demfunctions.py

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83``` ```#!/usr/bin/env python #-*- encoding:utf-8 -*- def fill_sinks(data): r_max = len(data) c_max = len(data[0]) for r in xrange(len(data)): for c in xrange(len(data[r])): s0 = data[r][c] <= data[r-1][c+1] s1 = data[r][c] <= data[r][c+1] s2 = data[r][c] <= data[r+1][c+1] s3 = data[r][c] <= data[r+1][c] s4 = data[r][c] <= data[r+1][c-1] s5 = data[r][c] <= data[r][c-1] s6 = data[r][c] <= data[r-1][c-1] s7 = data[r][c] <= data[r-1][c] if False not in [s0,s1,s2,s3,s4,s5,s6,s7]: # sink # window size default to 2*2 len_ = 1 marks = [] hidden_outset_min = None while True: border = [] for i in range(len_): if r+len_ <= r_max-1: border.append([r+len_,c+i]) if r-len_ >= 1: border.append([r-len_,c+i]) if c+len_ <= c_max-1: border.append([r+i,c+len_]) if c-len_ >= 1: border.append([r+i,c-len_]) hidden_outset = [] for node in border: loc0 = [node[0]+1,node[1]+1] loc1 = [node[0],node[1]+1] loc2 = [node[0]-1,node[1]+1] loc3 = [node[0]-1,node[1]] loc4 = [node[0]-1,node[1]-1] loc5 = [node[0],node[1]-1] loc6 = [node[0]+1,node[1]-1] loc7 = [node[0]+1,node[1]] t0,t1,t2,t3,t4,t5,t6,t7 = [None for i in range(7)] if loc0 not in marks: t0 = data[loc0[0],loc0[1]] if loc1 not in marks: t1 = data[loc0[0],loc0[1]] if loc2 not in marks: t2 = data[loc0[0],loc0[1]] if loc3 not in marks: t3 = data[loc0[0],loc0[1]] if loc4 not in marks: t4 = data[loc0[0],loc0[1]] if loc5 not in marks: t5 = data[loc0[0],loc0[1]] if loc6 not in marks: t6 = data[loc0[0],loc0[1]] if loc7 not in marks: t7 = data[loc0[0],loc0[1]] if True in [data[node[0],node[1]]>i for i in[t0,t1,t2,t3,t4,t5,t6,t7] if i!=None]: # is a hidden output node hidden_outset.append(node) # mark belonging to sink area marks.append(node) if len(hidden_outset) == 0: #enlarge window len_ += 1 continue for node in (hidden_outset): if hidden_outset_min < data[node[0]][node[1]]: hidden_outset_min = data[node[0]][node[1]] next_border_e = 0 for node in border: if data[node[0]][node[1]] < hidden_outset_min: len_ += 1 next_border_e = 1 break if next_border_e == 0: break if hidden_outset_min > data[r][c]: print "sink" else: print "plane" ```