-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
192 lines (149 loc) · 5.33 KB
/
test.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
from func import *
import time
from osgeo import ogr, gdal
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.backend_bases import key_press_handler
from tkinter import messagebox
import matplotlib
matplotlib.use('TkAgg')
from tkinter import *
from tkinter.ttk import *
from tkinter import filedialog
from matplotlib.figure import Figure
from tkinter.filedialog import askopenfile
import sys
if sys.version_info[0] < 3:
import Tkinter as Tk
else:
import tkinter as Tk
root = Tk.Tk()
root.geometry('900x900')
root.wm_title("Flow Direction and Accumulation")
root.configure(background="light blue")
equation = StringVar()
expression_field = Entry(root, textvariable=equation)
f = ""
canvas = ""
toolbar = ""
# Testing File
dempath = 'test.tiff'
ds = gdal.Open(dempath)
nodata = 0
demdata = ds.GetRasterBand(1).ReadAsArray()
fdir = flowDirectionTestInward(demdata, nodata)
flowto = flowsTo(fdir, nodata)
group1 = firstGroup(flowto, nodata)
fac, group = flowaccum(flowto, group1, nodata)
#Plots
def plotelevation():
global canvas,toolbar,f
if canvas != "":
canvas.get_tk_widget().pack_forget()
if toolbar != "":
toolbar.pack_forget()
if f != "":
f.destroy()
fig = Figure(figsize=(8, 6), dpi=100)
a = fig.add_subplot(111)
grid,grid.dem=getdem(inputfile_entry.get())
im=a.imshow(grid.dem, extent=grid.extent, cmap='cubehelix', zorder=1)
fig.colorbar(im,label='Elevation (m)')
a.set_title('Digital Elevation Map')
a.set_xlabel('Longitude')
a.set_ylabel('Latitude')
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
fig.savefig('result/elevationplot.png')
toolbar = NavigationToolbar2Tk(canvas, root)
toolbar.update()
canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
def plotdirection():
global canvas,toolbar,f
if canvas != "":
canvas.get_tk_widget().pack_forget()
if toolbar != "":
toolbar.pack_forget()
if f != "":
f.destroy()
fig = Figure(figsize=(8, 6), dpi=100)
a = fig.add_subplot(111)
dirmap = (64, 128, 1, 2, 4, 8, 16, 32)
grid,grid.dem=getdem(inputfile_entry.get())
grid, fdir=flowdirection(dirfile_entry.get(),grid)
im=a.imshow(fdir, extent=grid.extent, cmap='viridis', zorder=1)
boundaries = ([0] + sorted(list(dirmap)))
fig.colorbar(im,boundaries= boundaries, values=sorted(dirmap))
a.set_title('Flow Direction Grid')
a.set_xlabel('Longitude')
a.set_ylabel('Latitude')
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
fig.savefig('result/plotdirection.png')
toolbar = NavigationToolbar2Tk(canvas, root)
toolbar.update()
canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
def plotaccumulation():
global canvas,toolbar,f
if canvas != "":
canvas.get_tk_widget().pack_forget()
if toolbar != "":
toolbar.pack_forget()
if f != "":
f.destroy()
fig = Figure(figsize=(8, 6), dpi=100)
a = fig.add_subplot(111)
dirmap = (64, 128, 1, 2, 4, 8, 16, 32)
x, y = -97.294167, 32.73750
grid,grid.dem=getdem(inputfile_entry.get())
grid, fdir=flowdirection(dirfile_entry.get(),grid)
grid, catch=dilineatecatchmant(dirmap,x,y,grid)
grid, facc=flowaccumulation(grid,dirmap)
im=a.imshow(facc, extent=grid.extent, zorder=1,cmap='cubehelix', norm=colors.LogNorm(1, grid.acc.max()))
boundaries = ([0] + sorted(list(dirmap)))
fig.colorbar(im, label='Upstream Cells')
a.set_title('Flow Accumulation')
a.set_xlabel('Longitude')
a.set_ylabel('Latitude')
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
fig.savefig('result/plotaccumulation.png')
toolbar = NavigationToolbar2Tk(canvas, root)
toolbar.update()
canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
def _clear():
global f,canvas,toolbar
if canvas != "":
canvas.get_tk_widget().pack_forget()
if toolbar != "":
toolbar.pack_forget()
if f != "":
f.destroy()
f = ""
canvas = ""
toolbar = ""
def _quit():
root.quit()
root.destroy()
inputfile_label = Tk.Label(root, text = 'Dem Data File/Folder',font=('calibre',10, 'bold'))
inputfile_entry = Tk.Entry(root,font=('calibre',10,'normal'))
inputfile_label.pack(side = Tk.TOP, pady = 0)
inputfile_entry.pack(side = Tk.TOP, pady = 5)
dirfile_label = Tk.Label(root, text = 'Dir Data File/Folder',font=('calibre',10, 'bold'))
dirfile_entry = Tk.Entry(root,font=('calibre',10,'normal'))
dirfile_label.pack(side = Tk.TOP, pady = 0)
dirfile_entry.pack(side = Tk.TOP, pady = 5)
btn = Button(root, text ='Plot Elevation Data', command = lambda:plotelevation())
btn.pack(side = Tk.TOP, pady = 10)
btn = Button(text='Plot Flow Direction', command= lambda: plotdirection())
btn.pack(side = Tk.TOP, pady = 10)
btn = Button(text='Plot Flow Accumulation', command= lambda: plotaccumulation())
btn.pack(side = Tk.TOP, pady = 10)
btn = Button(text='Clear', command=lambda:_clear())
btn.pack(side = Tk.BOTTOM)
button = Tk.Button(master=root, text='Quit', command=_quit)
button.pack(side=Tk.BOTTOM)
Tk.mainloop()