
分类 | 面积(公顷) | 面积(亩) |
国土总面积 | | |
农用地 | | |
水田(0101) | | |
水浇地(0102) | | |
旱地(0103) | | |
果园(0201) | | |
茶园(0202) | | |
其他园地(0204) | | |
乔木林地(0301) | | |
竹林地(0302) | | |
灌木林地(0305) | | |
其他林地(0307) | | |
其他草地(0404) | | |
农村道路(1006) | | |
水库水面(1103) | | |
坑塘水面(1104) | | |
沟渠(1107) | | |
设施农用地(1202) | | |
田坎(1203) | | |
建设用地 | | |
城市(201) | | |
建制镇(202) | | |
村庄(203) | | |
采矿用地(204) | | |
风景名胜及特殊用地(205) | | |
铁路用地(1001) | | |
轨道交通用地(1002) | | |
公路用地(1003) | | |
港口码头用地(1008) | | |
管道运输用地(1009) | | |
水工建筑用地(1109) | | |
未利用地 | | |
内陆滩涂(1106) | | |
河流水面(1101) | | |
湖泊水面(1102) | | |
裸土地(1206) | | |
裸岩石砾地(1207) | | |
# -*- coding:utf-8 -*-
import os
import arcpy
import xlwt
from Tkinter import *
import Tkinter, tkFileDialog
def se1():#这是获取路径函数
g1.delete(0, "end")
#path=tkFileDialog.askopenfilename(filetypes=[("GDB file", "*.gdb"),("all","*.*")])
path=tkFileDialog.askdirectory()
path=path.replace("/","\\\\")#通过replace函数替换绝对文件地址中的/来使文件可被程序读取 #注意:\\转义后为\,所以\\\\转义后为\\
g1.insert('insert',path)
# 处理文件选择的函数
def on_select_file():
file_path = tkFileDialog.askopenfilename()
directory = os.path.dirname(file_path) # 获取文件的目录路径
if directory:
g1.delete(0, END)
g1.insert(0, directory)
window = Tkinter.Tk()
window.title(u"图斑核定地类面积汇总")
#window.rowconfigure(1, weight=1)
#window.columnconfigure(0, weight=1)
window.geometry('500x250')
menu = Menu(window);
window.config(menu=menu);
setupmenu = Menu(menu);
menu.add_cascade(label="选项", menu=setupmenu); #主菜单1
setupmenu.add_command(label="开机自启", command=se1);#分菜单1
setupmenu.add_command(label="皮肤更换", command=se1); #分菜单2
setupmenu.add_command(label="代码模板", command=se1);#分菜单3
def path_name():
#path_1=ur"C:\Users\wwc21\Desktop\三上工程库.gdb\Dataset\DLTB_HDK"
try:
# 自定义输入文件路径
#e2 = Label(window, text=u"-----开始运行-----",bg="green")#这是标签
#e2.grid(row=7,column=1,padx=10, pady=10)
if g1.get()=='':
e2 = Label(window, text=u"-----路径不能为空-----",bg="red")#这是标签
e2.grid(row=7,column=1, pady=10)
else:
f = xlwt.Workbook()
sheet1 = f.add_sheet(ur'地类统计',cell_overwrite_ok=True)
col_width=250*20
sheet1.col(0).width=400*20
sheet1.col(1).width=col_width
sheet1.col(2).width=col_width
for dirpath, dirnames, filenames in arcpy.da.Walk(g1.get(), topdown=True, datatype="FeatureClass", type='Polygon'):#path:路径
for filename in filenames:
if g2.get() == filename:
#print os.path.join(dirpath, filename)
#feature_classes.append(os.path.join(dirpath, filename))
# 设置工作空间
arcpy.env.workspace = dirpath
# 输入要素类
input_feature_class = g2.get()
style1 = xlwt.XFStyle()
#为样式创建字体(font)
font1 = xlwt.Font()
# 指定字体的一些常用属性
font1.name = u'Times New Roman' # 指定字体
font1.height = 200 # 和excel字体大小比例是1:20
style1.font = font1
style11 = xlwt.XFStyle()
#为样式创建字体(font)
font11 = xlwt.Font()
# 指定字体的一些常用属性
font11.name = u'Times New Roman' # 指定字体
font11.bold = True # 字体是否为粗体
font11.height = 200 # 和excel字体大小比例是1:20
style11.font = font11
style2 = xlwt.XFStyle()
#为样式创建字体(font)
font2 = xlwt.Font()
# 指定字体的一些常用属性
font2.name = u'仿宋' # 指定字体
font2.height = 200 # 和excel字体大小比例是1:20
style2.font = font2
style22 = xlwt.XFStyle()
#为样式创建字体(font)
font22 = xlwt.Font()
# 指定字体的一些常用属性
font22.name = u'仿宋' # 指定字体
font22.bold = True # 字体是否为粗体
font22.height = 200 # 和excel字体大小比例是1:20
style22.font = font22
# 设置单元格对齐方式
alignment = xlwt.Alignment()
# 0x01(左端对齐)、0x02(水平方向上居中对齐)、0x03(右端对齐)
alignment.horz = 0x02
# 0x00(上端对齐)、 0x01(垂直方向上居中对齐)、0x02(底端对齐)
alignment.vert = 0x01
style1.alignment = alignment
style2.alignment = alignment
style11.alignment = alignment
style22.alignment = alignment
style1.num_format_str = '0.00' # 设置数字格式为小数点后两位
style11.num_format_str = '0.00' # 设置数字格式为小数点后两位
# 设置边框
borders = xlwt.Borders() # Create Borders
# DASHED虚线
# NO_LINE没有
# THIN实线
borders.left = xlwt.Borders.THIN
borders.right = xlwt.Borders.THIN
borders.top = xlwt.Borders.THIN
borders.bottom = xlwt.Borders.THIN
style1.borders = borders
style2.borders = borders
style11.borders = borders
style22.borders = borders
DLMC={'0303':u'红树林地','0304':u'森林沼泽','0306':u'灌丛沼泽','0402':u'沼泽草地','0101':u'水田','0102':u'水浇地',
'0103':u'旱地', '0201':u'果园','0202':u'茶园', '0203':u'橡胶园', '0204':u'其他园地','0301':u'乔木林地','0302':u'竹林地',
'0305':u'灌木林地', '0307':u'其他林地','0401':u'天然牧草地','0403':u'人工牧草地', '0404':u'其他草地','1006':u'农村道路',
'1103':u'水库水面', '1104':u'坑塘水面','1107':u'沟渠', '1202':u'设施农用地', '1203':u'田坎',
'201':u'城市', '202':u'建制镇', '203':u'村庄','204':u'采矿用地','205':u'风景名胜及特殊用地','1001':u'铁路用地',
'1002':u'轨道交通用地', '1003':u'公路用地','1007':u'机场用地','1008':u'港口码头用地','1009':u'管道运输用地','1109':u'水工建筑用地',
'1105':u'沿海滩涂', '1106':u'内陆滩涂','1108':u'沼泽地', '1101':u'河流水面','1102':u'湖泊水面','1110':u'冰川及永久积雪',
'1204':u'盐碱地','1205':u'沙地', '1206':u'裸土地', '1207':u'裸岩石砾地',
}
# 设定要统计的分类
classifications_to_sum = [
'0303','0304','0306','0402','0101','0102','0103','0201','0202','0203','0204',
'0301','0302','0305','0307','0401','0403','0404','1006','1103','1104','1107','1202','1203',
'201','202','203','204','205','1001','1002','1003','1007','1008','1009','1109',
'1105','1106','1108','1101','1102','1110','1204','1205','1206','1207']
# 创建一个字典来存储分类和它们的面积
area_dict = {}
# 使用SearchCursor遍历要素类
with arcpy.da.SearchCursor(input_feature_class, [g3.get(), g4.get()]) as cursor:
for row in cursor:
# 假设"土地分类字段名"是存储分类代码的字段,"面积字段名"是存储面积的字段
classification_code = row[0]
area_hectares = row[1]
# 检查当前分类是否在我们的列表中
if str(classification_code) in classifications_to_sum:
# 如果在列表中,则添加到字典中
if classification_code in area_dict:
area_dict[classification_code] += area_hectares/10000
else:
area_dict[classification_code] = area_hectares/10000
# 打印结果
#for classification, area in area_dict.items():
# print classification
# print(u"分类 {}: {} 公顷".format(classification, area))
# 如果需要转换为亩,可以乘以15(1公顷 = 15亩)
#area_mu = area * 15
#print(u"分类 {}: {} 亩".format(classification, area_mu))
# 如果需要将结果保存到文件中,可以使用Python的文件操作功能
a=2
b=0
c=0
sheet1.write(0,0,u"分类",style22)
sheet1.write(0,1,u"面积(公顷)",style22)
sheet1.write(0,2,u"面积(亩)",style22)
NYD_1=0
JSYD_1=0
WYD_1=0
JS=0
WY=0
for m in range(len(classifications_to_sum)):
if m==24:
a+=1
JS=a
if m==36:
a+=1
WY=a
for classification, area in area_dict.items():
if classifications_to_sum[m]==classification:
sheet1.write(a+1,0,DLMC[classifications_to_sum[m]]+"("+classifications_to_sum[m]+")",style2)
sheet1.write(a+1,1,area,style1)
b+=area
sheet1.write(a+1,2,area*15,style1)
c+=area*15
a+=1
if m<=23:
NYD_1+=area
elif m<=35:
JSYD_1+=area
else:
WYD_1+=area
sheet1.write(1,0,u"国土总面积",style22)
sheet1.write(1,1,b,style11)
sheet1.write(1,2,c,style11)
sheet1.write(2,0,u"农用地",style22)
sheet1.write(2,1,NYD_1,style11)
sheet1.write(2,2,NYD_1*15,style11)
sheet1.write(JS,0,u"建设用地",style22)
sheet1.write(JS,1,JSYD_1,style11)
sheet1.write(JS,2,JSYD_1*15,style11)
sheet1.write(WY,0,u"未利用地",style22)
sheet1.write(WY,1,WYD_1,style11)
sheet1.write(WY,2,WYD_1*15,style11)
f.save(u'核定地类面积统计表.xls') #文件名
e2 = Text(window,bg="green",width=40)#这是标签
e2.grid(row=7,column=1,padx=10, pady=10)
e2.insert('insert',u"-----全部完成-----")
#open_new_window()
except Exception as e:
e2 = Text(window,bg="red",width=40)#这是标签
e2.grid(row=7,column=1,padx=10, pady=10)
e2.insert('insert',e)
e1 = Label(window, text=u"GDB路径:")#这是标签
e1.grid(row=1,column=0, sticky=Tkinter.W)
g1 = Entry(window,width=45)#这是输入框
g1.grid(row=1, column=1,columnspan=1)
e2 = Label(window, text=u"DLTB名称:")#这是标签
e2.grid(row=2,column=0)
g2 = Entry(window,width=45)#这是输入框
g2.grid(row=2, column=1,columnspan=1)
g2.insert(0,'DLTB_HDK')
e3 = Label(window, text=u"字段名称:")#这是标签
e3.grid(row=3,column=0)
g3 = Entry(window,width=45)#这是输入框
g3.grid(row=3, column=1,columnspan=1)
g3.insert(0,'DLBMX')
e4 = Label(window, text=u"面积字段:")#这是标签
e4.grid(row=4,column=0)
g4 = Entry(window,width=45)#这是输入框
g4.grid(row=4, column=1,columnspan=1)
g4.insert(0,'TQMJ')
#g5 = Entry(window,width=40)#这是输入框
#g5.grid(row=6, column=1,columnspan=1)
b1 = Button(window, text=u"打开路径", command=on_select_file)#这是按键
b1.grid(row=1, column=3,padx=5, pady=5)
b5 = Button(window, text=u"开始运行", command=path_name)#这是按键
b5.grid(row=5, column=1,padx=2, pady=2)
window.mainloop()