供地坐标格式转换

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')  # 290 160为窗口大小,+1000 +10 定义窗口弹出时的默认展示位置
window['background'] = 'LightPink'
v = tk.StringVar()
listbox = tk.Listbox(window, height=20, width=90, listvariable=v)


# 处理单击选项
def show(event):
    # nearest可以传回最接近y坐标在Listbox的索引
    # 传回目前选项的索引
    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  # 地块号为1时,在列表中的位置为0
                    if n == 0:  #
                        textk[n].append("\n")
                        for c in contentsw[13:]:
                            textk[n].append(c)  # 添加扣除地块,扣除地块不需要前面属性值,contentsw取第13个位置
                        k0 = textk[n][12].split(",")  # 第一个列表第12个值切片

                        k2 = k0[0]  # 取到列表12的位置第1个值,原地块的jzd个数
                        k22 = k0[1]  # 取到列表12的位置第2个值,原地块的面积
                        k1 = (contentsw[12].split(","))[0]  # 取到循环contentsw的第十二个位置切片的第1个位置--扣除地块的jzd个数
                        k11 = (contentsw[12].split(","))[1]  # 取到循环contentsw的第十二个位置切片的第2个位置--扣除地块的面积
                        k3 = int(k2) + int(k1)  # jzd个数相减
                        k33 = Decimal(k22) - Decimal(k11)  # 地块面积相减
                        k0[0] = str(k3)  # 列表k0的第一个值替换为相减的jzd
                        k0[1] = str(k33)  # 列表k0的第二个值替换为相减的面积
                        textk[n][12] = (",".join(k0))  # 列表第n(0)的第12个位置替换为k0--替换的的相减jzd个数,相减的面积
                        n += 1  # 因为0+1还是0所以+1让列表继续循环
                    elif n > 0:  # 地块号为2时,在列表中的位置为1
                        textk[n].append("\n")
                        for c in contentsw[13:]:
                            textk[n].append(c)  # 添加扣除地块,扣除地块不需要前面属性值,contentsw取第13个位置
                        k0 = textk[n][0].split(",")  # 地块2在列表中位置为1,取位置1的第12个值切片
                        k2 = k0[0]  # 取到列表12的位置第1个值,原地块的jzd个数
                        k22 = k0[1]  # 取到列表12的位置第2个值,原地块的面积
                        k1 = (contentsw[12].split(","))[0]  # 取到循环contentsw的第十二个位置切片的第1个位置--扣除地块的jzd个数

                        k11 = (contentsw[12].split(","))[1]  # 取到循环contentsw的第十二个位置切片的第2个位置--扣除地块的面积

                        k3 = int(k2) + int(k1)  # jzd个数相减
                        k33 = Decimal(k22) - Decimal(k11)  # 地块面积相减
                        k0[0] = str(k3)  # 列表k0的第一个值替换为相减的jzd
                        k0[1] = str(k33)  # 列表k0的第二个值替换为相减的面积
                        textk[n][0] = (",".join(k0))  # 列表第n(0)的第12个位置替换为k0--替换的的相减jzd个数,相减的面积
                        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:])):  # 循环列表位置13开始的值
                    gh[13:][gg][1] = str(mc)  # 第二个位置的1改为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


# 文本框1-------------------------
lable1 = tk.Label(window, text="[属性描述]", width=15).grid(row=0, column=0)
text1_var = tk.StringVar()  # 获取text_1输入的值
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()  # 获取text_1输入的值
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()  # 获取text_1输入的值
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()  # 获取text_1输入的值
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()  # 获取text_1输入的值
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()  # 获取text_1输入的值
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()  # 获取text_1输入的值
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()  # 获取text_1输入的值
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()  # 获取text_1输入的值
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()  # 获取text_1输入的值
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()  # 获取text_1输入的值
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()  # 获取text_1输入的值
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()  # 获取text_1输入的值
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()  # 获取text_1输入的值
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()  # 获取text_1输入的值
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()  # 获取text_1输入的值
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()  # 获取text_1输入的值
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()  # 获取text_1输入的值
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()  # 获取text_1输入的值
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()

你可能感兴趣的:(python)