import tkinter as tk
from tkinter import *
from tkinter import filedialog
import numpy as np
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,NavigationToolbar2Tk
from tkinter.messagebox import *
class GIS_GUI:
def __init__(self):
self.root=tk.Tk() #创建主窗体
self.root.title('GIS地理信息系统') #加标题
#self.root.attributes('-fullscreen', True) #全屏显示
self.root.geometry('1500x1000') #设置分辨率
self.label_set() #定义label
self.text_set()
self.button_set()
self.canvas_set()
#定义全局变量
self.Judge_Slope_Aspect = False
self.Judge_data_read = False
self.root.mainloop()
def Slope_Aspect(self):
'''
方法功能:计算坡度坡向,并输出坡度坡向矩阵
'''
if self.Judge_data_read == False:
showwarning('数据缺失', '请先读取文件')
else:
#得出cellsize数据,number为数值数据
cellsize = float(self.data[4][1])
number = self.data[6:]
#将列表数据转换为浮点型数组数据
number_arr = np.matrix(number,dtype=np.float64)
#矩阵的扩充,调整参数为边缘补充,并得出行列
number_matrix_0 = np.pad(number_arr,(1,1),'edge')
row,column = np.shape(number_matrix_0)
#利用坡度坡向计算公式,计算出每一点的坡度坡向,并保存在矩阵当中
Slope = np.ones((int(self.data[1][1]),int(self.data[0][1])))*-9999
Aspect = np.ones((int(self.data[1][1]),int(self.data[0][1])))*-9999
for i in range(1,row-1):
for j in range(1,column-1):
flag1=[0]*9
flag1[0]=1 if abs(number_matrix_0[i][j]-int(self.data[5][1])) < 1 else 0
flag1[1]=1 if abs(number_matrix_0[i][j-1]-int(self.data[5][1]))<1 else 0
flag1[2]=1 if abs(number_matrix_0[i][j+1]-int(self.data[5][1]))<1 else 0
flag1[3]=1 if abs(number_matrix_0[i-1][j]-int(self.data[5][1]))<1 else 0
flag1[4]=1 if abs(number_matrix_0[i-1][j-1]-int(self.data[5][1]))<1 else 0
flag1[5]=1 if abs(number_matrix_0[i-1][j+1]-int(self.data[5][1]))<1 else 0
flag1[6]=1 if abs(number_matrix_0[i+1][j]-int(self.data[5][1]))<1 else 0
flag1[7]=1 if abs(number_matrix_0[i+1][j-1]-int(self.data[5][1]))<1 else 0
flag1[8]=1 if abs(number_matrix_0[i+1][j+1]-int(self.data[5][1]))<1 else 0
if sum(flag1)<1 :
slope_we = ((2*number_matrix_0[i][j-1] + number_matrix_0[i-1][j-1] + number_matrix_0[i+1][j-1] ) - ( 2*number_matrix_0[i][j+1] + number_matrix_0[i-1][j+1] + number_matrix_0[i+1][j+1] ))/(8*cellsize)
slope_sn = ((2*number_matrix_0[i+1][j] + number_matrix_0[i+1][j-1] + number_matrix_0[i+1][j-1] ) - ( 2*number_matrix_0[i-1][j] + number_matrix_0[i-1][j-1] + number_matrix_0[i-1][j+1] ))/(8*cellsize)
Slope[i-1][j-1] = np.sqrt(slope_we**2+slope_sn**2)*180/np.pi
if slope_we != 0:
Aspect[i-1][j-1] = (slope_sn/slope_we)*180/np.pi
if (slope_we > 0) & (slope_sn > 0):
Aspect[i-1][j-1] = np.arctan(Aspect[i-1][j-1])
if (slope_we > 0) & (slope_sn <= 0):
Aspect[i-1][j-1] = np.arctan(Aspect[i-1][j-1]*-1) + 90
if (slope_we < 0) & (slope_sn >= 0):
Aspect[i-1][j-1] = np.arctan(Aspect[i-1][j-1]*-1) + 270
if (slope_we < 0) & (slope_sn < 0):
Aspect[i-1][j-1] = np.arctan(Aspect[i-1][j-1]) + 180
if (slope_we == 0) & (slope_sn < 0):
Aspect[i-1][j-1] = 180
if (slope_we == 0) & (slope_sn > 0):
Aspect[i-1][j-1] = 0
self.Slope = Slope
self.Aspect = Aspect
self.var.set('坡度坡向计算完毕')
showinfo(title = "提示",
message = "坡度坡向计算完毕")
self.Judge_Slope_Aspect = True
#统计量计算
def compute(self):
if self.Judge_data_read == False:
showwarning('数据缺失', '请先读取文件')
else:
self.t.delete('1.0','end')
#转换为数值数据
data = self.data[6:]
#将数据转换为np矩阵
data = np.matrix(data,dtype=np.float64)
#获取非填充值
real_data = data[data!=-9999]
#输出高程数学统计
statistic_data = '高程最小值:{}\n高程最大值:{}\n高程均值:{}\n高程分布:{}'.format(np.min(real_data),np.max(real_data),np.mean(real_data),np.sort(real_data))
self.var7.set('平均值:{}'.format(np.mean(real_data)))
self.var8.set('最大值:{}'.format(np.max(real_data)))
self.var9.set('最小值:{}'.format(np.min(real_data)))
self.t.insert('end',statistic_data )
self.var.set('统计量计算成功')
#输出aspect文件
def Write_Aspect(self):
if self.Judge_Slope_Aspect == False:
showwarning('数据缺失', '请先计算坡度坡向')
else:
#数据写入
a=filedialog.asksaveasfilename()
f1 = open('{}'.format(a),'w',encoding='UTF-8')
i=0
for line in self.content:
if i<=5:
f1.writelines(line)
i +=1
#数据写入,并设置输出格式
for i in range(int(self.data[1][1])):
for j in range(int(self.data[0][1])):
if self.Aspect[i][j] == -9999:
f1.write('%10.2d'%(self.Aspect[i][j]))
else:
f1.write('%10.2f'%(self.Aspect[i][j]))
f1.write('\n')
f1.close()
self.var.set('输出坡向成功')
showinfo(title = "提示",
message = "坡向输出成功")
#输出slope文件
def Write_Slope(self):
if self.Judge_Slope_Aspect == False:
showwarning('数据缺失', '请先计算坡度坡向')
else:
#数据写入
a=filedialog.asksaveasfilename()
f1 = open('{}'.format(a),'w',encoding='UTF-8')
i=0
for line in self.content:
if i<=5:
f1.writelines(line)
i +=1
#数据写入,并设置输出格式
for i in range(int(self.data[1][1])):
for j in range(int(self.data[0][1])):
if self.Slope[i][j] == -9999:
f1.write('%10.2d'%(self.Slope[i][j]))
else:
f1.write('%10.2f'%(self.Slope[i][j]))
f1.write('\n')
f1.close()
self.var.set('输出坡度成功')
showinfo(title = "提示",
message = "坡度输出成功")
#画dem
def draw_dem(self):
if self.Judge_data_read == False:
showwarning('数据缺失', '请先读取文件')
else:
#清空画布
self.f.clear()
self.Figure = self.f.add_subplot(111)
#转换为数值数据
data = self.data[6:]
#将数据转换为np矩阵
data = np.array(data,dtype=np.float64)
real_data = data[data!=-9999]
dem = self.Figure.imshow(data,cmap='hsv',vmin=0,vmax=np.max(real_data))
self.Figure.set_title('高程图',fontproperties="SimHei")
self.f.colorbar(dem)
self.toolbar.update()
self.c.draw()
self.var.set('高程图像绘制完毕')
#画slope
def draw_slope(self):
if self.Judge_Slope_Aspect == False:
showwarning('数据缺失', '请先计算坡度坡向')
else:
#清空画布
self.f.clear()
self.Figure = self.f.add_subplot(111)
#转换为数值数据
data = self.Slope[6:]
#将数据转换为np矩阵
data = np.array(data,dtype=np.float64)
real_data = data[data!=-9999]
slope = self.Figure.imshow(data,cmap='hsv',vmin=0,vmax=np.max(real_data))
self.Figure.set_title('坡度图',fontproperties="SimHei")
self.f.colorbar(slope)
self.c.draw()
self.toolbar.update()
self.var.set('坡度图像绘制完毕')
#画aspect
def draw_aspect(self):
if self.Judge_Slope_Aspect == False:
showwarning('数据缺失', '请先计算坡度坡向')
else:
#清空画布
self.f.clear()
self.Figure = self.f.add_subplot(111)
#转换为数值数据
data = self.Aspect[6:]
#将数据转换为np矩阵
data = np.array(data,dtype=np.float64)
real_data = data[data!=-9999]
aspect = self.Figure.imshow(data,cmap='hsv',vmin=0,vmax=np.max(real_data))
self.Figure.set_title('坡向图',fontproperties="SimHei")
self.f.colorbar(aspect)
self.c.draw()
self.toolbar.update()
self.var.set('坡向图像绘制完毕')
#画直方图
def draw_hist(self):
if self.Judge_data_read == False:
showwarning('数据缺失', '请先读取文件')
else:
#清空画布
self.f.clear()
self.Figure = self.f.add_subplot(111)
#转换为数值数据
data = self.data[6:]
#将数据转换为np矩阵
data = np.array(data,dtype=np.float64)
real_data = data[data!=-9999]
self.Figure.hist(real_data)
self.Figure.set_title('高程分布直方图',fontproperties="SimHei")
self.Figure.set_xlabel('高程值',fontproperties="SimHei")
self.Figure.set_ylabel('频数',fontproperties="SimHei")
self.c.draw()
self.toolbar.update()
self.var.set('坡度图像绘制完毕')
#画线图
def draw_string(self):
if self.Judge_data_read == False:
showwarning('数据缺失', '请先读取文件')
else:
#清空画布
self.f.clear()
self.Figure = self.f.add_subplot(111)
#转换为数值数据
data = self.data[6:]
#将数据转换为np矩阵
data = np.array(data,dtype=np.float64)
real_data = data[data!=-9999]
real_data_sort = np.sort(real_data)
self.Figure.plot(real_data_sort)
self.Figure.set_title('高程分布线图',fontproperties="SimHei")
self.Figure.set_xlabel('排序',fontproperties="SimHei")
self.Figure.set_ylabel('高程值',fontproperties="SimHei")
self.c.draw()
self.toolbar.update()
self.var.set('线图绘制完毕')
#清空画布
def draw_clean(self):
self.f.clear()
self.c.draw()
self.var.set('画布清空成功')
#文件打开功能
def open_file(self):
#调用askopenfile方法获取单个打开的文件
sfname =None
sfname = filedialog.askopenfilename(title='打开单个文件',
filetypes=[('asc文件', '*.asc')], # 处理的文件类型
initialdir='g:/')# 初始目录
return sfname
#数据读取功能,text内容显示,
def data_read(self):
#按行全部打开文件
name=self.open_file()
if name == '':
showwarning(title = "警告",
message = "未成功读取文件")
else:
self.data=[]
with open(name) as c:
self.content = c.readlines()
i = 0
for line in self.content:
if i == 0:
self.var1.set(line)
if i == 1:
self.var2.set(line)
if i == 2:
self.var3.set(line)
if i == 3:
self.var4.set(line)
if i == 4:
self.var5.set(line)
if i == 5:
self.var6.set(line)
self.t.insert('end',line)
line = line.split()
i=i+1
self.data.append(line)
self.Judge_data_read = True
self.var.set('读取{}成功'.format(name))
#button清除文本框
def move(self):
self.t.delete('1.0','end')
#定义label框(程序运行结果显示)
def label_set(self):
self.var=tk.StringVar()
self.l = tk.Label(self.root,bg='green',textvariable=self.var,text='result',height=5,width=15,font=('Arial',12)).pack(side='top', anchor='ne',fill=X,expand=1)
#定义画布
def canvas_set(self):
self.f = Figure(figsize=(2.5, 2.56),dpi=330)
self.Figure = self.f.add_subplot(111)
self.c = FigureCanvasTkAgg(self.f, self.root)
self.c.get_tk_widget().pack(side=BOTTOM, fill=BOTH, expand=1)
self.toolbar =NavigationToolbar2Tk(self.c, self.root)
#定义文本框,以及元数据显示
def text_set(self):
self.fm1 = tk.Frame(self.root)
self.fm2 = tk.Frame(self.fm1)
self.fm2.pack()
self.var1=tk.StringVar()
self.var2=tk.StringVar()
self.var3=tk.StringVar()
self.var4=tk.StringVar()
self.var5=tk.StringVar()
self.var6=tk.StringVar()
self.var7=tk.StringVar()
self.var8=tk.StringVar()
self.var9=tk.StringVar()
self.l1_1 = tk.Label(self.fm2,textvariable=self.var1,height=2,font=('Arial',12)).grid(row = 1, column = 0)
self.l2_1 = tk.Label(self.fm2,textvariable=self.var2,height=2,font=('Arial',12)).grid(row = 2, column = 0)
self.l3_1 = tk.Label(self.fm2,textvariable=self.var3,height=2,font=('Arial',12)).grid(row = 3, column = 0)
self.l4_1 = tk.Label(self.fm2,textvariable=self.var4,height=2,font=('Arial',12)).grid(row = 4, column = 0)
self.l5_1 = tk.Label(self.fm2,textvariable=self.var5,height=2,font=('Arial',12)).grid(row = 5, column = 0)
self.l6_1 = tk.Label(self.fm2,textvariable=self.var6,height=2,font=('Arial',12)).grid(row = 6, column = 0)
self.l7_1 = tk.Label(self.fm2,textvariable=self.var7,height=2,font=('Arial',12)).grid(row = 7, column = 0)
self.l8_1 = tk.Label(self.fm2,textvariable=self.var8,height=2,font=('Arial',12)).grid(row = 8, column = 0)
self.l9_1 = tk.Label(self.fm2,textvariable=self.var9,height=2,font=('Arial',12)).grid(row = 9, column = 0)
self.t = tk.Text(self.fm1,width=50,height=10,)
self.t.pack(side=TOP,expand=1,anchor='w',fill=BOTH)
self.fm1.pack(side='left',expand=1,anchor='w',fill=BOTH)
#定义按钮
def button_set(self):
self.fm = tk.Frame(self.root)
self.b1 =tk.Button(self.fm,text='DEM文件读取',command=self.data_read).pack(side=LEFT)
self.b2 = tk.Button(self.fm,text='绘制高程图',command=self.draw_dem).pack(side=LEFT)
self.b10 = tk.Button(self.fm,text='计算统计量',command=self.compute).pack(side=LEFT)
self.b11 = tk.Button(self.fm,text='绘制直方图',command=self.draw_hist).pack(side=LEFT)
self.b12 = tk.Button(self.fm,text='绘制线图',command=self.draw_string).pack(side=LEFT)
self.b3 = tk.Button(self.fm1, text = "清空文本框", command = self.move).pack(side=BOTTOM,fill=BOTH)
self.b4 = tk.Button(self.fm,text='清空画板',command=self.draw_clean).pack(side=RIGHT,ipadx=50,padx=10)
self.b5 = tk.Button(self.fm,text='计算坡度坡向',command=self.Slope_Aspect).pack(side=LEFT)
self.b6 = tk.Button(self.fm,text='绘制坡度图',command=self.draw_slope).pack(side=LEFT)
self.b7 = tk.Button(self.fm,text='绘制坡向图',command=self.draw_aspect).pack(side=LEFT)
self.b8 = tk.Button(self.fm,text='输出坡向文件',command=self.Write_Aspect).pack(side=LEFT)
self.b9 = tk.Button(self.fm,text='输出坡度文件',command=self.Write_Slope).pack(side=LEFT)
self.fm.pack(side=TOP,padx=10)
if __name__=="__main__":
window=GIS_GUI()
测试数据地址:
链接:https://pan.baidu.com/s/1k1arl-2WO51Op6tS1KdmVg
提取码:mg4v