测绘中 dat格式和xyz格式互转软件

该文章已生成可运行项目,

   在测绘行业工作的朋友们都知道,数据处理往往占据了我们大部分的时间。面对大量的DAT文件需要转换成XYZ格式时,手动操作不仅耗时而且容易出错,我的解决之道就是——“懒”。没错,正是这份“懒”才动手编写了一个Python脚本,有了转换软件我更懒了。

软件界面 如此简单 还是懒得缘故 

   开源是好习惯代码在此

import tkinter as tk
from tkinter import filedialog, messagebox

def convert_dat_to_xyz(dat_content):
    lines = dat_content.strip().split('\n')
    converted_lines = [
        f"{parts[2].strip()} {parts[3].strip()} {parts[4].strip()}"
        for line in lines
        if (parts := line.split(',')) and len(parts) == 5
    ]
    return '\n'.join(converted_lines), len(converted_lines)

def convert_xyz_to_dat_combined(xyz_content):
    lines = xyz_content.strip().split('\n')
    converted_lines = [
        f"{i+1},,{parts[0].strip()} ,{parts[1].strip()} ,{parts[2].strip()}"
        for i, line in enumerate(lines)
        if (parts := line.split()) and len(parts) == 3 or (parts := line.split(',')) and len(parts) == 3
    ]
    return '\n'.join(converted_lines), len(converted_lines)

def open_file(file_type):
    file_path = filedialog.askopenfilename(filetypes=[(f"{file_type.upper()} files", f"*.{file_type}")])
    if not file_path:
        return None
    with open(file_path, 'r', encoding='utf-8') as file:
        return file.read()

def save_file(content, extension):
    file_path = filedialog.asksaveasfilename(defaultextension=extension, filetypes=[(f"{extension.upper()} files", f"*.{extension}"), ("All files", "*.*")])
    if file_path:
        with open(file_path, 'w', encoding='utf-8') as file:
            file.write(content)

def convert_and_save(conversion_function, open_type, save_extension):
    if content := open_file(open_type):
        converted_content, line_count = conversion_function(content)
        save_file(converted_content, save_extension)
        messagebox.showinfo("转换完成", f"成功转换了 {line_count} 行数据。")

def create_gui():
    root = tk.Tk()
    root.title("数据转换工具")

    frame = tk.Frame(root)
    frame.pack(padx=80, pady=80)

    dat_to_xyz_button = tk.Button(frame, text="DAT 转 XYZ", command=lambda: convert_and_save(convert_dat_to_xyz, "dat", "xyz"))
    dat_to_xyz_button.grid(row=0, column=0, padx=5, pady=5)

    xyz_to_dat_combined_button = tk.Button(frame, text="XYZ 转 DAT ", command=lambda: convert_and_save(convert_xyz_to_dat_combined, "xyz", "dat"))
    xyz_to_dat_combined_button.grid(row=0, column=1, padx=5, pady=5)

    author_label = tk.Label(frame, text="作者: 浅修 联系方式: 16699988885")
    author_label.grid(row=2, column=0, columnspan=2, pady=10)
    author_label = tk.Label(frame, text="区域公司: 第二区域公司")
    author_label.grid(row=3, column=0, columnspan=2, pady=10)
    root.mainloop()

if __name__ == "__main__":
    create_gui()

    

本文章已经生成可运行项目
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值