Source

dem_waters_extractor / demfunctions.py

Full commit
#!/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"