# HG changeset patch # User luoboiqingcai # Date 1350992560 -28800 # Node ID 1daef4852c41df08f4556816125c2c694db948dd # Parent 003385d7939220e0cde2410f0d373e99fd99a57e finish testing lift diff --git a/demfunctions.py b/demfunctions.py --- a/demfunctions.py +++ b/demfunctions.py @@ -2,6 +2,7 @@ #-*- encoding:utf-8 -*- import math +import copy def fill_sinks(data): r_max = len(data) @@ -11,7 +12,7 @@ if r == 0 or r == r_max-1 or c == 0 or c == c_max-1: continue s0,s1,s2,s3,s4,s5,s6,s7 = [True for i in range(8)] # all the border is highter than this node - if r-1>0 and c+1<=c_max-1: + if r-1>=0 and c+1<=c_max-1: s0 = data[r][c] <= data[r-1][c+1] print 'x0' print data[r-1][c+1] @@ -27,19 +28,19 @@ s3 = data[r][c] <= data[r+1][c] print 'x3' print data[r+1][c] - if r+1<=r_max-1 and c-1>0: + if r+1<=r_max-1 and c-1>=0: s4 = data[r][c] <= data[r+1][c-1] print 'x4' print data[r+1][c-1] - if c-1>0: + if c-1>=0: s5 = data[r][c] <= data[r][c-1] print 'x5' print data[r][c-1] - if r-1>0 and c-1>0: + if r-1>=0 and c-1>=0: s6 = data[r][c] <= data[r-1][c-1] print 'x6' print data[r-1][c-1] - if r-1>0: + if r-1>=0: s7 = data[r][c] <= data[r-1][c] print 'x7' print data[r-1][c] @@ -48,7 +49,7 @@ print '===================================' print 'center point:r:%s,c:%s'%(r,c) print '===================================' - len_ = 1 + len_ = 0 marks = [[r,c]] hidden_outset_min = None border = [] @@ -58,6 +59,7 @@ print 'center point:r:%s,c:%s'%(r,c) print 'len_:%s'%len_ print '___' + len_ += 1 for i in range(len_+1): if r+len_ <= r_max-1 and c+i <=c_max-1: tem = [r+len_,c+i] @@ -92,7 +94,7 @@ if tem not in border: border.append(tem) print 'border:%s'%border - if len(border) == 0: + if len(border) < 8*len_: print 'center node: %s.\n the end of dem for this center node.\n this node is assumed to be a biggggggg sink'%[r,c] break hidden_outset = [] @@ -146,10 +148,8 @@ # mark belonging to sink area marks.append(node) if len(hidden_outset) > 0: - #enlarge window print 'current hidden_outset number:%s'%len(hidden_outset) else: - len_ += 1 continue hidden_outset_min = hidden_outset[0] for node in (hidden_outset): @@ -160,7 +160,6 @@ next_border_e = 0 # escape while loop 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: @@ -173,19 +172,19 @@ data[node[0]][node[1]] = hidden_outset_min else: print "plane" - elif len(border) > 0: - print 'sink on the border of the dem file' - else: - print 'too large sink' return data -def lift(data,data_modified,p): +def lift(data,p): r_max = len(data) c_max = len(data[0]) + lifted = copy.deepcopy(data) + modified = False for r in xrange(len(data)): for c in xrange(len(data[r])): - s0,s1,s2,s3,s4,s5,s6,s7 = [True for i in range(8)] # all the border is highter than this node - if r-1>0 and c+1<=c_max-1: + if r == 0 or r == r_max-1 or c == 0 or c == c_max-1: + continue + s0,s1,s2,s3,s4,s5,s6,s7 = [True for i in range(8)] # all the border is highter than or equal to this node + if r-1>=0 and c+1<=c_max-1: s0 = data[r][c] <= data[r-1][c+1] if c+1<=c_max-1: s1 = data[r][c] <= data[r][c+1] @@ -193,18 +192,21 @@ s2 = data[r][c] <= data[r+1][c+1] if r+1<=r_max-1: s3 = data[r][c] <= data[r+1][c] - if r+1<=r_max-1 and c-1>0: + if r+1<=r_max-1 and c-1>=0: s4 = data[r][c] <= data[r+1][c-1] - if c-1>0: + if c-1>=0: s5 = data[r][c] <= data[r][c-1] - if r-1>0 and c-1>0: + if r-1>=0 and c-1>=0: s6 = data[r][c] <= data[r-1][c-1] - if r-1>0: + if r-1>=0: s7 = data[r][c] <= data[r-1][c] - if False not in [s0,s1,s2,s3,s4,s5,s6,s7]: # sink - data_modified[r][c] += p + if False not in [s0,s1,s2,s3,s4,s5,s6,s7]: # sink or plane + lifted[r][c] += p + modified = True + if modified == False: + return lifted print 'life return' - return data_modified + return lift(lifted,p) def get_vect_martrix(data,x,y): ''' diff --git a/test.py b/test.py --- a/test.py +++ b/test.py @@ -35,7 +35,7 @@ [1,2,3,3,3,3,4,8,6], [1,1,2,3,3,4,5,7,8]] data_modified = fill_sinks(data) - return lift(data,data_modified,0.01) + return lift(data,0.1) print test_lift()