Source

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
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#!/usr/bin/env python
#-*- encoding:utf-8 -*-

import math
import copy

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])):
            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:
                s0 = data[r][c] <= data[r-1][c+1]
                print 'x0'
                print data[r-1][c+1]
            if c+1<=c_max-1:
                s1 = data[r][c] <= data[r][c+1]
                print 'x1'
                print data[r][c+1]
            if r+1<=r_max-1 and c+1<=c_max-1:
                s2 = data[r][c] <= data[r+1][c+1]
                print 'x2'
                print data[r+1][c+1]
            if r+1<=r_max-1:
                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:
                s4 = data[r][c] <= data[r+1][c-1]
                print 'x4'
                print data[r+1][c-1]
            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:
                s6 = data[r][c] <= data[r-1][c-1]
                print 'x6'
                print data[r-1][c-1]
            if r-1>=0:
                s7 = data[r][c] <= data[r-1][c]
                print 'x7'
                print data[r-1][c]
            if False not in [s0,s1,s2,s3,s4,s5,s6,s7]: # sink?
                # window size default to 2*2
                print '==================================='
                print 'center point:r:%s,c:%s'%(r,c)
                print '==================================='
                len_ = 0
                marks = [[r,c]]
                hidden_outset_min = None
                border = []
                while True:
                    border = []
                    print '___'
                    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]
                            if tem not in border:
                                border.append(tem)
                        if r+len_ <= r_max-1 and c-i >=0:
                            tem= [r+len_,c-i]
                            if tem not in border:
                                border.append(tem)
                        if r-len_ >= 0 and c+i <=c_max-1:
                            tem = [r-len_,c+i]
                            if tem not in border:
                                border.append(tem)
                        if r-len_ >= 0 and c-i >=0:
                            tem = [r-len_,c-i]
                            if tem not in border:
                                border.append(tem)
                        if c+len_ <= c_max-1 and r+i <=r_max-1:
                            tem = [r+i,c+len_]
                            if tem not in border:
                                border.append(tem)
                        if c+len_ <= c_max-1 and r-i >=0:
                            tem = [r-i,c+len_]
                            if tem not in border:
                                border.append(tem)
                        if c-len_ >= 0 and r+i <=r_max-1:
                            tem = [r+i,c-len_]
                            if tem not in border:
                                border.append(tem)
                        if c-len_ >=0 and r-i >=0:
                            tem = [r-i,c-len_]
                            if tem not in border:
                                border.append(tem)
                    print 'border:%s'%border
                    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 = []
                    for node in border:
                        print 'which node:%s'%node
                        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(8)]
                        print 'marks:%s'%marks
                        if loc0 not in marks:
                            if loc0[0]>=0 and loc0[1]>=0 and loc0[0]<=r_max-1 and loc0[1]<=c_max-1:
                                print 'loc0:%s'%loc0
                                t0 = data[loc0[0]][loc0[1]]
                        if loc1 not in marks:
                            if loc1[0]>=0 and loc1[1]>=0 and loc1[0]<=r_max-1 and loc1[1]<=c_max-1:
                                print 'loc1:%s'%loc1
                                t1 = data[loc1[0]][loc1[1]]
                        if loc2 not in marks:
                            if loc2[0]>=0 and loc2[1]>=0 and loc2[0]<=r_max-1 and loc2[1]<=c_max-1:
                                print 'loc2:%s'%loc2
                                t2 = data[loc2[0]][loc2[1]]
                        if loc3 not in marks:
                            if loc3[0]>=0 and loc3[1]>=0 and loc3[0]<=r_max-1 and loc3[1]<=c_max-1:
                                print 'loc3:%s'%loc3
                                t3 = data[loc3[0]][loc3[1]]
                        if loc4 not in marks:
                            if loc4[0]>=0 and loc4[1]>=0 and loc4[0]<=r_max-1 and loc4[1]<=c_max-1:
                                print 'loc4:%s'%loc4
                                t4 = data[loc4[0]][loc4[1]]
                        if loc5 not in marks:
                            if loc5[0]>=0 and loc5[1]>=0 and loc5[0]<=r_max-1 and loc5[1]<=c_max-1:
                                print 'loc5:%s'%loc5
                                t5 = data[loc5[0]][loc5[1]]
                        if loc6 not in marks:
                            if loc6[0]>=0 and loc6[1]>=0 and loc6[0]<=r_max-1 and loc6[1]<=c_max-1:
                                print 'loc6:%s'%loc6
                                t6 = data[loc6[0]][loc6[1]]
                        if loc7 not in marks:
                            if loc7[0]>=0 and loc7[1]>=0 and loc7[0]<=r_max-1 and loc7[1]<=c_max-1:
                                print 'loc7:%s'%loc7
                                t7 = data[loc7[0]][loc7[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:
                        print 'current hidden_outset number:%s'%len(hidden_outset)
                    else:
                        continue
                    hidden_outset_min = hidden_outset[0]
                    for node in (hidden_outset):
                        if hidden_outset_min > data[node[0]][node[1]]:
                            hidden_outset_min = data[node[0]][node[1]]
                    print 'hidden_outset:%s'%hidden_outset
                    print 'hidden_outset_min:%s'%hidden_outset_min
                    next_border_e = 0 # escape while loop
                    for node in border:
                        if data[node[0]][node[1]] < hidden_outset_min:
                            next_border_e = 1
                            break
                    if next_border_e == 0:
                        break
                if len(border) == 8*len_:
                    if hidden_outset_min > data[r][c]:
                        print "sink"
                        for node in marks:
                            if data[node[0]][node[1]] < hidden_outset_min:
                                data[node[0]][node[1]] = hidden_outset_min
                    else:
                        print "plane"
    return data

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])):
            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]
            if r+1<=r_max-1 and c+1<=c_max-1:
                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:
                s4 = data[r][c] <= data[r+1][c-1]
            if c-1>=0:
                s5 = data[r][c] <= data[r][c-1]
            if r-1>=0 and c-1>=0:
                s6 = data[r][c] <= data[r-1][c-1]
            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 or plane
                lifted[r][c] += p
                modified = True
    if modified == False:
        return lifted
    print 'life return'
    return lift(lifted,p)

def get_vect_martrix(data,x,y):
    '''
    方向代码阵列
    x: x distance of each node on column
    y: y distance of each node on row
    '''
    r_max = len(data) # exactly len(data) - 1
    c_max = len(data[0]) # exactly len(data[0]) - 1
    vect_matrix = [[0 for c in range(c_max)] for r in range(r_max)]
    for r in xrange(len(data)):
        for c in xrange(len(data[r])):
            if r == 0:
                vect_matrix[r][c] = 7
                continue
            if r == r_max-1:
                vect_matrix[r][c] = 3
                continue
            if c == 0:
                vect_matrix[r][c] = 5
                continue
            if c == c_max-1:
                vect_matrix[r][c] = 1
                continue
            v0,v1,v2,v3,v4,v5,v6,v7 = [0 for i in range(8)]
            if r-1>=0 and c+1<=c_max-1:
                v0 = (data[r][c]-data[r-1][c+1])/math.pow(x**2+y**2,0.5)
            if c+1<=c_max-1:
                v1 = (data[r][c]-data[r][c+1])/x
            if r+1<=r_max-1 and c+1<=c_max-1:
                v2 = (data[r][c]-data[r+1][c+1])/math.pow(x**2+y**2,0.5)
            if r+1<=r_max-1:
                v3 = (data[r][c]-data[r+1][c])/y
            if r+1<=r_max-1 and c-1>0:
                v4 = (data[r][c]-data[r+1][c-1])/math.pow(x**2+y**2,0.5)
            if c-1>=0:
                v5 = (data[r][c]-data[r][c-1])/x
            if r-1>=0 and c-1>=0:
                v6 = (data[r][c]-data[r-1][c-1])/math.pow(x**2+y**2,0.5)
            if r-1>=0:
                v7 = (data[r][c]-data[r-1][c])/y
            which = 0
            v_max = max([v0,v1,v2,v3,v4,v5,v6,v7])
            print 'v_max:%s'%v_max
            for i in [v0,v1,v2,v3,v4,v5,v6,v7]:
                if v_max == i:
                    break
                which += 1
            if which == None:
                raise 'error'
            vect_matrix[r][c] = which
    return vect_matrix

def trackpath(data,r,c,r_ax,c_ax):
    '''
    跟踪水流方向
    data: vect_matrix
    '''
    if r > r_ax-1 or r < 0 or c>c_ax-1 or c < 0:
        return []
    #if r>5 or r<0 or c>5 or c<0:
    #    return[]
    node = data[r][c]
    path = [[r,c],]
    if node == 0:
        temp = trackpath(data,r-1,c+1,r_ax,c_ax)
        if len(temp) >0:
            path.extend(temp)
    if node == 1:
        temp = trackpath(data,r,c+1,r_ax,c_ax)
        if len(temp) >0:
            path.extend(temp)
    if node == 2:
        temp = trackpath(data,r+1,c+1,r_ax,c_ax)
        if len(temp) >0:
            path.extend(temp)
    if node == 3:
        temp = trackpath(data,r+1,c,r_ax,c_ax)
        if len(temp) >0:
            path.extend(temp)
    if node == 4:
        temp = trackpath(data,r+1,c-1,r_ax,c_ax)
        if len(temp) >0:
            path.extend(temp)
    if node == 5:
        temp = trackpath(data,r,c-1,r_ax,c_ax)
        if len(temp) >0:
            path.extend(temp)
    if node == 6:
        temp = trackpath(data,r-1,c-1,r_ax,c_ax)
        if len(temp) >0:
            path.extend(temp)
    if node == 7:
        temp = trackpath(data,r-1,c,r_ax,c_ax)
        if len(temp) >0:
            path.extend(temp)
    return path

def count_water(data):
    '''
    水流线等级
    data: vect_matrix
    '''
    r_max = len(data) # exactly len(data) - 1
    c_max = len(data[0]) # exactly len(data[0]) - 1
    water_matrix = [[0 for c in range(c_max)] for r in range(r_max)]
    for r in xrange(len(data)):
        for c in xrange(len(data)):
            path = trackpath(data,r,c,r_max,c_max)
            for node in path:
                water_matrix[node[0]][node[1]] += 1
    return water_matrix

def river_map(data,threshold=5):
    '''
    河流的0-1表示图
    '''
    r_max = len(data) # exactly len(data) - 1
    c_max = len(data[0]) # exactly len(data[0]) - 1
    rivermap_matrix = [[0 for c in range(c_max)] for r in range(r_max)]
    for r in xrange(len(data)):
        for c in xrange(len(data[r])):
            if data[r][c] >=threshold:
                rivermap_matrix[r][c] = 1
    return rivermap_matrix

def river_paths(river_map_matrix,vect_matrix):
    '''
    get river paths
    '''
    paths = []
    cross_sections = []
    r_max = len(river_map_matrix) # exactly len(data) - 1
    c_max = len(river_map_matrix[0]) # exactly len(data[0]) - 1
    for r in xrange(len(river_map_matrix)):
        for c in xrange(len(river_map_matrix[r])):
            if river_map_matrix[r][c]: # node in the river
                input_nodes = []
                if r-1 >=0 and c+1 <= c_max-1:
                    if river_map_matrix[r-1][c+1] == 1 and vect_matrix[r-1][c+1] == 4:
                        input_nodes.append([r-1,c+1])
                if c+1 <= c_max-1:
                    if river_map_matrix[r][c+1] ==1 and vect_matrix[r][c+1] == 5:
                        input_nodes.append([r,c+1])
                if r+1 <= r_max-1 and c+1 <= c_max-1:
                    if river_map_matrix[r+1][c+1] == 1 and vect_matrix[r+1][c+1] == 6:
                        input_nodes.append([r+1,c+1])
                if r+1 <= r_max-1:
                    if river_map_matrix[r+1][c] == 1 and vect_matrix[r+1][c] == 7:
                        input_nodes.append([r+1,c])
                if r+1 <= r_max and c-1 >= 0:
                    if river_map_matrix[r+1][c-1] == 1 and vect_matrix[r+1][c-1] == 0:
                        input_nodes.append([r+1,c-1])
                if c-1 >= 0:
                    if river_map_matrix[r][c-1] == 1 and vect_matrix[r][c-1] == 1:
                        input_nodes.append([r,c-1])
                if r-1 >= 0 and c-1 >= 0:
                    if river_map_matrix[r-1][c-1] == 1 and vect_matrix[r-1][c-1] == 2:
                        input_nodes.append([r-1,c-1])
                if r-1 >= 0:
                    if river_map_matrix[r-1][c] == 1 and vect_matrix[r-1][c] == 3:
                        input_nodes.append([r-1,c])
                if len(input_nodes) == 0:
                    # this node is the start point of the path
                    paths.append(trackpath(vect_matrix,r,c,r_max,c_max))
                else:
                    if len(input_nodes) == 1:
                        # this node is on the path
                        pass
                    else:
                        # this node is the cross-section of path
                        cross_sections.append([[r,c],input_nodes])
    return {'cross_sections':cross_sections,'paths':paths}

def water_grad(cross_sections,paths,r_max,c_max):
    '''
    determine the water grad on the basis of paths generated from river_paths function
    '''
    grads = [[0 for i in range(c_max)] for j in range(r_max)]
    for x in range(len(paths)):
        current_grad = 1
        for y in range(len(paths[x])): # traceing from source of this river path
            if paths[x][y] in [i[0] for i in cross_sections]: # is this node a cross section?
                 if grads[paths[x][y][0]][paths[x][y][1]] < current_grad:
                     current_grad += 1
                     grads[paths[x][y][0]][paths[x][y][1]] = current_grad
                 elif grads[paths[x][y][0]][paths[x][y][1]] >= current_grad:
                     break
            else:
                grads[paths[x][y][0]][paths[x][y][1]] = current_grad
    return grads