ArcMap批次附色操作,並儲存mxd

Sherioc發表於2024-09-07

ArcMap批次附色操作,並儲存mxd

1、對單檔案操作

1、儲存當前ArcMap中開啟的shp檔案為mxd檔案

開啟label_shp_root中的任意一個shp資料夾

image-20240905223954234

儲存成mxd檔案

image-20240905224127626

2、對當前在arcmap中開啟的shp檔案應用color配色

color配色是手動設定好一個shp資料夾的配色方案並儲存成mxd檔案

image-20240905224334797

應用color.mxd的配色方案後

image-20240905224520580

2、批次操作

1、shp檔案儲存為mxd檔案(遞迴遍歷資料夾及其子資料夾)

這裡我設定num_folders_to_process = 5,儲存了前5個

image-20240905224802435

image-20240905224920335

2、批次應用配色方案(對result資料夾中的mxd應用color配色)

image-20240905225424894

函式儲存在ipynb檔案。

核心函式

arcpy.mapping.UpdateLayer

image-20240905230345668

label_shp_root資料夾中的全部shp檔案進行批次配色結果:

image-20240906124549152

程式碼

點選檢視程式碼
#### 1、shp檔案儲存為mxd檔案(遞迴遍歷資料夾及其子資料夾)
import os
import arcpy
main_workspace = r"D:/desktop/label_shp_root"
result_folder = r"D:/desktop/label_shp_root_mxd"

if not os.path.exists(result_folder):
    os.makedirs(result_folder)

# 指定要處理的子資料夾數量
num_folders_to_process = 50


folders_processed = 0 # 初始化計數器
subfolders = [os.path.join(main_workspace, d) for d in os.listdir(main_workspace) if os.path.isdir(os.path.join(main_workspace, d))]

for subfolder in subfolders:
    if folders_processed >= num_folders_to_process:
        break  # 如果達到指定數量,停止遍歷
    
    # 獲取子資料夾名稱
    subfolder_name = os.path.basename(subfolder)
    print("[processing subfolder]: ", subfolder_name)  # 列印正在處理的子資料夾名稱
    
    # 構建對應的mxd檔案路徑
    mxd_path = os.path.join(result_folder, "{}.mxd".format(subfolder_name))
    mxd = arcpy.mapping.MapDocument("CURRENT")
    df = arcpy.mapping.ListDataFrames(mxd)[0]

    # 遍歷子資料夾中的所有shp檔案
    for filename in os.listdir(subfolder):
        if filename.endswith(".shp"):
            # 構建完整的檔案路徑
            shp_path = os.path.join(subfolder, filename)
            # 新增shp檔案到地圖文件
            layer = arcpy.mapping.Layer(shp_path)
            arcpy.mapping.AddLayer(df, layer)  
    # 儲存mxd檔案
    mxd.saveACopy(mxd_path)

    # 清空所有圖層
    for lyr in arcpy.mapping.ListLayers(mxd, "", df):
        arcpy.mapping.RemoveLayer(df, lyr)

    folders_processed += 1  # 更新計數器

# 提示完成
print("success processed {} subfolders, saved in {}".format(folders_processed, result_folder))


####  2、批次應用配色方案(對result資料夾中的mxd應用color配色)
import os
import arcpy
# PATH
ROOT_PATH = r'D:/desktop/label_shp_root'
RESULT_MXD_PATH = r'D:/desktop/label_shp_root_mxd'
RESULT_MXD_COLORED_PATH = r'D:/desktop/label_shp_root_mxd_colored'  # 新的資料夾路徑
MXD_PATH = r"D:/desktop/color.mxd" # color.mxd的路徑

if not os.path.exists(RESULT_MXD_COLORED_PATH):
    os.makedirs(RESULT_MXD_COLORED_PATH)

# 獲取color圖層
color_GT_room_poly = "GT_room_poly"   
color_label_poly = "label_poly"
color_poly = "poly"
color_edges = "edges"
color_vertexes = "vertexes"
color_achor = "achor"
color_GT_bound = "GT_bound"
mxd_path = MXD_PATH


for mxd_file in os.listdir(RESULT_MXD_PATH):


    mxd = arcpy.mapping.MapDocument(mxd_path)
    df = arcpy.mapping.ListDataFrames(mxd)[0]

    origin_GT_room_poly_Layer = arcpy.mapping.ListLayers(mxd, color_GT_room_poly)[0]
    origin_label_poly_Layer = arcpy.mapping.ListLayers(mxd, color_label_poly)[0]
    origin_poly_Layer = arcpy.mapping.ListLayers(mxd, color_poly)[0]
    origin_edges_Layer = arcpy.mapping.ListLayers(mxd, color_edges)[0]
    origin_vertexes_Layer = arcpy.mapping.ListLayers(mxd, color_vertexes)[0]
    origin_achor_Layer = arcpy.mapping.ListLayers(mxd, color_achor)[0]
    origin_GT_bound_Layer = arcpy.mapping.ListLayers(mxd, color_GT_bound)[0]
    print('======get all origin color layers========')

    print(mxd_file)
    if mxd_file.endswith(".mxd"):
        # 構建完整的mxd檔案路徑
        full_mxd_path = os.path.join(RESULT_MXD_PATH, mxd_file)
        # 開啟mxd檔案
        mxd2 = arcpy.mapping.MapDocument(full_mxd_path)
        df2 = arcpy.mapping.ListDataFrames(mxd2)[0]

        GT_room_poly_Layer = arcpy.mapping.ListLayers(mxd2, "GT_room_poly", df2)[0]
        label_poly_Layer = arcpy.mapping.ListLayers(mxd2, "label_poly", df2)[0]
        poly_Layer = arcpy.mapping.ListLayers(mxd2, "poly", df2)[0]
        edges_Layer = arcpy.mapping.ListLayers(mxd2, "edges", df2)[0]
        vertexes_Layer = arcpy.mapping.ListLayers(mxd2, "vertexes", df2)[0]

        arcpy.mapping.UpdateLayer(df, GT_room_poly_Layer, origin_GT_room_poly_Layer, True)
        arcpy.mapping.UpdateLayer(df, label_poly_Layer, origin_label_poly_Layer, True)
        arcpy.mapping.UpdateLayer(df, poly_Layer, origin_poly_Layer, True)
        arcpy.mapping.UpdateLayer(df, edges_Layer, origin_edges_Layer, True)
        arcpy.mapping.UpdateLayer(df, vertexes_Layer, origin_vertexes_Layer, True)
        
        # 將修改後的mxd檔案儲存到新資料夾中
        new_mxd_path = os.path.join(RESULT_MXD_COLORED_PATH, mxd_file)
        print(new_mxd_path)
        mxd2.saveACopy(new_mxd_path)

        # 清空地圖文件中的所有圖層
        mxd = arcpy.mapping.MapDocument("CURRENT")
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        for lyr in arcpy.mapping.ListLayers(mxd):
            arcpy.mapping.RemoveLayer(df, lyr)

    print('============success process =========================')
    
print('All mxd files in result folder have been updated with color layers and saved to the new folder.')

data資料增強

1、在這裡進行資料載入(但是感覺程式碼入侵了)

image-20240907000105735

或者是直接對label_shp_root進行資料變換,將資料夾大小變為原來6倍

2、但是發現實際上就是對所有點vertices進行同樣操作(x,y 變換,也完全可以只x+1這樣就是平移操作)

所以實際上只需要在這裡進行資料複製5份(然後對vertieces進行簡單操作)即可

image-20240907003251665

總感覺怪怪的,好像沒有什麼實際的操作

執行train.py

到第六個epoch 8%進度時候,記憶體爆了

image-20240907072832322

使用增強後的資料集

image-20240907125116939

bug。。。

vscode不能用了

image-20240907161258670

pycharm也出問題了

image-20240907161323891

相關文章