大疆智图_空三&二维重建&成果传输-四、代码

复制以下代码或点击下载脚本文件,并修改(注释、删除)以下(或者更多)参数,确保脚本能够正确执行。
#拼接大疆任务目录
dom_path
# 设置 7-Zip 可执行文件路径
seven_zip_path
# 设置 fcp 可执行文件路径
fcp_path
# 设置 rclone 可执行文件路径
rclone_path
# 设置 rclone 上传文件路径
rclone_up_path
#压缩至E盘根目录
compressed_file

import arcpy
import os
import time
import datetime
import subprocess
import concurrent.futures

# 判断金字塔文件是否存在
def check_ovr_file_exists(file_path):
    start_time = time.time()
    if os.path.isfile(file_path + ".ovr"):
        print(f"已经存在:{file_path} 金字塔文件")
    else:
        print(f"开始构建:{file_path} 金字塔文件")
        arcpy.management.BuildPyramids(file_path)
        end_time = time.time()
        execution_time = end_time - start_time
        td = datetime.timedelta(seconds=execution_time)
        hh, mm, ss = str(td).split(":")  # 将时间差转换为时分秒
        print(f"构建用时:{file_path} 构建{hh}小时{mm}分钟{ss}秒")

# 判断目录是否存在
def check_path_exists(dir_path):   
    if not os.path.exists(dir_path):
        print(f"指定目录:{dir_path} 不存在!")
        exit()

# 同步最新日期文件夹
def sync_update_folder(source_path, destination_path):
    print(f"正在同步:{source_path}")
    command = f'"{fcp_path}" /cmd=sync_update /force_close "{source_path}" /to="{destination_path}"'
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
    subprocess.run(command, startupinfo=startupinfo, shell=True)

# 同步文件夹
def sync_folder(source_path, destination_path):
    print(f"正在同步:{source_path}")
    command = f'"{fcp_path}" /cmd=sync /force_close "{source_path}" /to="{destination_path}"'
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
    subprocess.run(command, startupinfo=startupinfo, shell=True)

# 选择目录    
choice = input("待构建金字塔文件DOM路径为:\n1、所在目录\n2、指定目录\n请选择:")
if choice == "1":
    current_file_path = os.path.abspath(__file__)
    folder_path = os.path.dirname(current_file_path)
    #拼接大疆任务目录
    dom_path = os.path.join(folder_path, "PCGSPRO\\XXX")
else:
    dom_path = input("请输入指定目录:")
check_path_exists(dom_path)

# 局域网存放路径
dst_path = input("请输入存放路径:")
check_path_exists(dst_path)

# 多线程处理map目录下dsm.tif与result.tif文件
def process_folder(foldername):
    folder_path = os.path.join(dom_path, foldername)
    if os.path.isdir(folder_path):
        check_ovr_file_exists(os.path.join(folder_path, "map\\dsm.tif"))
        check_ovr_file_exists(os.path.join(folder_path, "map\\result.tif"))

if __name__ == "__main__":
    # 设置 7-Zip 可执行文件路径
    seven_zip_path = r"C:\Program Files\7-Zip\7z.exe"
    # 设置 fcp 可执行文件路径
    fcp_path = r"C:\Program Files\FastCopy5.7.10_x64\fcp.exe"
    # 设置 rclone 可执行文件路径
    rclone_path = r"E:\rclone.exe"
    # 设置 rclone 上传文件路径
    rclone_up_path = "OneDrive:/航飞数据"
    # 压缩文件的列表
    compressed_files = []
    
    # 创建线程池,最大线程为4
    with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
        # 提交任务给线程池
        executor.map(process_folder, os.listdir(dom_path))

    # 局域网同步文件夹
    for foldername in os.listdir(dom_path):
        folder_path = os.path.join(dom_path, foldername)
        if os.path.isdir(folder_path):
            sync_folder(os.path.join(folder_path, "map\\dsm*.*"), os.path.join(dst_path, foldername))
            sync_folder(os.path.join(folder_path, "map\\result*.*"), os.path.join(dst_path, foldername))
            sync_folder(os.path.join(folder_path, "map\\*_质量报告.pdf"), os.path.join(dst_path, foldername, "空三"))
            sync_folder(os.path.join(folder_path, "AT\\report\\POS_residual_of_camera.csv"), os.path.join(dst_path, foldername, "空三"))

    # 压缩文件
    for foldername in os.listdir(dom_path):
        folder_path = os.path.join(dom_path, foldername)
        if os.path.isdir(folder_path):
            compressed_file = f"E:\{foldername}.7z"#压缩至E盘根目录
            compressed_files.append(compressed_file)
            seven_zip_command = [
                seven_zip_path,
                "a",
                compressed_file,
                os.path.join(folder_path, "map", "result.prj"),
                os.path.join(folder_path, "map", "result.tfw"),
                os.path.join(folder_path, "map", "result.tif.ovr"),
                os.path.join(folder_path, "map", "result.tif.xml"),
                os.path.join(folder_path, "map", "result.tif")
            ]
            print(f"正在压缩: {foldername}...")
            subprocess.run(seven_zip_command)

    # 使用 rclone 上传文件的函数
    def upload_file(compressed_file):
        rclone_command = [
            "rclone",
            "sync",
            compressed_file,
            rclone_up_path,
            "--progress"
        ]
        print(f"正在上传: {compressed_file}...")
        subprocess.run(rclone_command)
        print(f"上传成功: {compressed_file}")
    # 异步上传文件
    with concurrent.futures.ThreadPoolExecutor() as executor:
        futures = [executor.submit(upload_file, file) for file in compressed_files]
        concurrent.futures.wait(futures)
    
    # 删除本地 7z 文件
    for file in compressed_files:
        os.remove(file)
        print(f"本地文件: {file} 已删除!")

print("\nOVER!")
input("按任意键继续...")
上一篇:AI推介-多模态视觉语言模型VLMs论文速览(arXiv方向):2024.05.25-2024.05.31


下一篇:P8 oss对象存储