import tkinter as tk
from tkinter import filedialog, messagebox
from decimal import Decimal
'''打开选择文件夹对话框'''
window = tk.Tk()
window.title("供地坐标提取与合并1.1 by: 放放风")
window.geometry('1050x555+500+200')
window['background'] = 'LightPink'
v = tk.StringVar()
listbox = tk.Listbox(window, height=20, width=90, listvariable=v)
def show(event):
listbox.index = listbox.nearest(event.y)
def showInfo(event):
newIndex = listbox.nearest(event.y)
if newIndex < listbox.index:
x = listbox.get(newIndex)
listbox.delete(newIndex)
listbox.insert(newIndex + 1, x)
listbox.index = newIndex
elif newIndex > listbox.index:
x = listbox.get(newIndex)
listbox.delete(newIndex)
listbox.insert(newIndex - 1, x)
listbox.index = newIndex
def listbox_delete():
listbox.delete(0, tk.END)
def open_hebing():
Filer_paths = filedialog.askopenfilenames(filetypes=[("文本", "txt")], title="供地坐标合并")
if not Filer_paths:
tk.messagebox.showinfo('警告', "请选择需要合并的的供地坐标!")
return
return Filer_paths
def list_save2():
for x in open_hebing():
listbox.insert(tk.END, x)
listbox.bind('', show)
listbox.bind('', showInfo)
def get_hebing():
n = 0
textk = []
save_text = filedialog.asksaveasfilename(defaultextension='.txt', filetypes=[("TXT", ".txt")], title="请输入保存的文件名")
if not save_text:
tk.messagebox.showinfo('警告', "请输入保存的文件名!")
return
plan = open(save_text, "w")
plan2 = open("list1.txt", "w")
lxo = v.get().split("'")
xxo = [xxj for xxj in lxo if len(xxj) > 3]
for x in xxo:
plan2.write(x)
plan2.write("\n")
n += 1
with open(x) as f:
contentsw = f.readlines()
if n == 1:
textk.append(contentsw)
else:
if "扣除地块" in x:
n -= 2
if n == 0:
textk[n].append("\n")
for c in contentsw[13:]:
textk[n].append(c)
k0 = textk[n][12].split(",")
k2 = k0[0]
k22 = k0[1]
k1 = (contentsw[12].split(","))[0]
k11 = (contentsw[12].split(","))[1]
k3 = int(k2) + int(k1)
k33 = Decimal(k22) - Decimal(k11)
k0[0] = str(k3)
k0[1] = str(k33)
textk[n][12] = (",".join(k0))
n += 1
elif n > 0:
textk[n].append("\n")
for c in contentsw[13:]:
textk[n].append(c)
k0 = textk[n][0].split(",")
k2 = k0[0]
k22 = k0[1]
k1 = (contentsw[12].split(","))[0]
k11 = (contentsw[12].split(","))[1]
k3 = int(k2) + int(k1)
k33 = Decimal(k22) - Decimal(k11)
k0[0] = str(k3)
k0[1] = str(k33)
textk[n][0] = (",".join(k0))
n += 1
else:
textk.append(contentsw[12:])
for xa in textk:
plan.writelines(xa)
plan.write("\n")
plan2.close()
plan.close()
tk.messagebox.showinfo("提示!", "合并成功!")
return
def Polygon_area(file_name):
s = 0
with open(file_name, "r") as f2:
plan2 = [[Decimal(new2_lines_col2), Decimal(new2_lines_col1) + 40000000] for new2_lines_col1, new2_lines_col2 in
[lines2.split()[:-2] for lines2 in f2.readlines()[1:]]]
point_num = len(plan2)
for i in range(point_num):
s += (plan2[i][1] * (plan2[i - 1][0] - plan2[(i + 1) % point_num][0]))
mj = abs((s / Decimal(2)) / 10000)
mj = round(mj, 4)
return [mj, plan2]
def Land_supply_coordinates(file_names):
n = 0
mm = []
t1 = text1_var.get()
t2 = text2_var.get()
t3 = text3_var.get()
t4 = text4_var.get()
t5 = text5_var.get()
t6 = text6_var.get()
t7 = text7_var.get()
t8 = text8_var.get()
t9 = text9_var.get()
t10 = text10_var.get()
t11 = text11_var.get()
t12 = text12_var.get()
t13 = text13_var.get()
t14 = str(Polygon_area(file_names)[0])
t15 = text15_var.get()
t16 = text16_var.get()
t17 = text17_var.get()
t18 = text18_var.get()
t19 = text19_var.get()
Text_default_format = [["[属性描述]\n"],
["格式版本号=" + str(t2) + "\n"],
["数据生产单位 = " + str(t3) + "\n"],
["数据生产日期 = " + str(t4) + "\n"],
["坐标系=" + str(t5) + "\n"],
["几度分带 = " + str(t6) + "\n"],
["投影类型 = " + str(t7) + "\n"],
["计量单位 = " + str(t8) + "\n"],
["带号 = " + str(t9) + "\n"],
["精度 = " + str(t10) + "\n"],
["转换参数=" + str(t11) + "\n"],
["[地块坐标]\n"],
[t13, t14, t15, t16, t17, t18, t19, "", "@" + "\n"]]
accuracy = len(t10) - 2
for x in Polygon_area(file_names)[1]:
n += 1
if n < len(Polygon_area(file_names)[1]):
mm.append(["J" + str(n), "1", str(round(x[0], accuracy)), str(round(x[1], accuracy)) + "\n"])
elif n == len(Polygon_area(file_names)[1]):
mm.append(["J" + str(n), "1", str(round(x[0], accuracy)), str(round(x[1], accuracy)) + "\n"])
mm.append(["J" + str(1), "1", mm[0][2], mm[0][3].replace("\n", "")])
Text_default_format[12][0] = str(n + 1)
text_var = Text_default_format + mm
return text_var
def get_Land_supply_coordinates():
Filer_paths = filedialog.askopenfilenames(filetypes=[("文本", "txt")], title="选择需要转换的文本")
if not Filer_paths:
tk.messagebox.showinfo('警告', "请选择需要转换的文本!")
return
return Filer_paths
def list_save():
for x in get_Land_supply_coordinates():
listbox.insert(tk.END, x)
listbox.bind('', show)
listbox.bind('', showInfo)
def get_save():
n = 0
mc = 1
Save_path = filedialog.askdirectory()
if not Save_path:
tk.messagebox.showinfo('警告', "请选择需要保存的路径!")
return
else:
lo = v.get().split("'")
xo = [xj for xj in lo if len(xj) > 3]
for Filer_path in xo:
n += 1
Filer_name = Filer_path.split("/")[-1].replace(".txt", "(供地坐标).txt")
new_filer = open(Save_path + "/" + Filer_name, "w")
gh = Land_supply_coordinates(Filer_path)
if "扣除地块" in Filer_name:
mc += 1
n -= 1
gh[12][2] = str(n)
for gg in range(len(gh[13:])):
gh[13:][gg][1] = str(mc)
else:
gh[12][2] = str(n)
mc = 1
for new_lines in gh:
new_filer.writelines(",".join(new_lines))
new_filer.close()
tk.messagebox.showinfo('结果', "供地坐标转换成功!")
return
lable1 = tk.Label(window, text="[属性描述]", width=15).grid(row=0, column=0)
text1_var = tk.StringVar()
text1_var.set(r'默认')
text1 = tk.Entry(window, textvariable=text1_var, bd=5).grid(row=0, column=1)
lable2 = tk.Label(window, text="格式版本号", width=15).grid(row=1, column=0)
text2_var = tk.StringVar()
text2_var.set(r'1.01版本')
text2 = tk.Entry(window, textvariable=text2_var, bd=5).grid(row=1, column=1)
lable3 = tk.Label(window, text="数据生产单位", width=15).grid(row=2, column=0)
text3_var = tk.StringVar()
text3_var.set(r'杭州通泰测绘有限公司')
text3 = tk.Entry(window, textvariable=text3_var, bd=5).grid(row=2, column=1)
lable4 = tk.Label(window, text="数据生产日期", width=15).grid(row=3, column=0)
text4_var = tk.StringVar()
text4_var.set(r'2021-01-01')
text4 = tk.Entry(window, textvariable=text4_var, bd=5).grid(row=3, column=1)
lable5 = tk.Label(window, text="坐标系", width=15).grid(row=4, column=0)
text5_var = tk.StringVar()
text5_var.set(r'2000国家大地坐标系')
text5 = tk.Entry(window, textvariable=text5_var, bd=5).grid(row=4, column=1)
lable6 = tk.Label(window, text="几度分带", width=15).grid(row=5, column=0)
text6_var = tk.StringVar()
text6_var.set(r'3')
text6 = tk.Entry(window, textvariable=text6_var, bd=5).grid(row=5, column=1)
lable7 = tk.Label(window, text="投影类型", width=15).grid(row=6, column=0)
text7_var = tk.StringVar()
text7_var.set(r'高斯克吕格')
text7 = tk.Entry(window, textvariable=text7_var, bd=5).grid(row=6, column=1)
lable8 = tk.Label(window, text="计量单位", width=15).grid(row=7, column=0)
text8_var = tk.StringVar()
text8_var.set(r'米')
text8 = tk.Entry(window, textvariable=text8_var, bd=5).grid(row=7, column=1)
lable9 = tk.Label(window, text="带号", width=15).grid(row=8, column=0)
text9_var = tk.StringVar()
text9_var.set(r'40')
text9 = tk.Entry(window, textvariable=text9_var, bd=5).grid(row=8, column=1)
lable10 = tk.Label(window, text="精度", width=15).grid(row=9, column=0)
text10_var = tk.StringVar()
text10_var.set(r'0.001')
text10 = tk.Entry(window, textvariable=text10_var, bd=5).grid(row=9, column=1)
lable11 = tk.Label(window, text="转换参数", width=15).grid(row=10, column=0)
text11_var = tk.StringVar()
text11_var.set(r',,,,,,')
text11 = tk.Entry(window, textvariable=text11_var, bd=5).grid(row=10, column=1)
lable12 = tk.Label(window, text="[地块坐标]", width=15).grid(row=11, column=0)
text12_var = tk.StringVar()
text12_var.set(r'默认')
text12 = tk.Entry(window, textvariable=text12_var, bd=5).grid(row=11, column=1)
lable13 = tk.Label(window, text="界址点个数", width=15).grid(row=12, column=0)
text13_var = tk.StringVar()
text13_var.set(r'默认')
text13 = tk.Entry(window, textvariable=text13_var, bd=5).grid(row=12, column=1)
lable14 = tk.Label(window, text="地块面积", width=15).grid(row=13, column=0)
text14_var = tk.StringVar()
text14_var.set(r'默认(公顷),保留四位小数')
text14 = tk.Entry(window, textvariable=text14_var, bd=5).grid(row=13, column=1)
lable15 = tk.Label(window, text="地块编号", width=15).grid(row=14, column=0)
text15_var = tk.StringVar()
text15_var.set(r'默认(1,2,3....)')
text15 = tk.Entry(window, textvariable=text15_var, bd=5).grid(row=14, column=1)
lable16 = tk.Label(window, text="项目名称", width=15).grid(row=15, column=0)
text16_var = tk.StringVar()
text16_var.set(r'')
text16 = tk.Entry(window, textvariable=text16_var, bd=5).grid(row=15, column=1)
lable17 = tk.Label(window, text="面", width=15).grid(row=16, column=0)
text17_var = tk.StringVar()
text17_var.set(r'面')
text17 = tk.Entry(window, textvariable=text17_var, bd=5).grid(row=16, column=1)
lable18 = tk.Label(window, text="分幅号", width=15).grid(row=17, column=0)
text18_var = tk.StringVar()
text18_var.set(r'')
text18 = tk.Entry(window, textvariable=text18_var, bd=5).grid(row=17, column=1)
lable19 = tk.Label(window, text="用地性质", width=15).grid(row=18, column=0)
text19_var = tk.StringVar()
text19_var.set(r'其他用地')
text19 = tk.Entry(window, textvariable=text19_var, bd=5).grid(row=18, column=1)
text20 = tk.Text(window, width=20, height=15)
text20.grid(row=1, column=8, rowspan=13, columnspan=7)
text20.tag_config("tag_1", backgroun="yellow", foreground="red")
text20.insert("insert",
"1.Cass 中输入 units ,建议单位精度保留五位小数!\n2.使用Getpl插件提取坐标(检查提取坐标精度是否与Cass中一致)。\n3"
".窗口中精度为输出精度,不建议高于输入坐标精度(默认保留三位小数)。\n")
text20.insert("end", "4.更多功能请联系:\[email protected]", "tag_1")
cjip = tk.PhotoImage(file="D:\\a5.gif")
tk.Label(window, image=cjip, height=80, width=140).grid(row=16, column=2, rowspan=7, columnspan=5)
tk.Button(window, text="打开", width=20, height=1, command=list_save, bg="Green").grid(row=1, column=2)
tk.Button(window, text="打开", width=20, height=1, command=list_save2, bg="Cyan").grid(row=2, column=2)
tk.Button(window, text="合并", width=20, height=1, command=get_hebing, bg="Cyan").grid(row=2, column=3)
tk.Button(window, text="转换", width=20, height=1, command=get_save, bg="Green").grid(row=1, column=3)
tk.Button(window, text="清空列表", width=20, height=1, command=listbox_delete, bg="Red").grid(row=16, column=6)
listbox.grid(row=3, column=2, rowspan=13, columnspan=6)
window.mainloop()