ArcMap批量附色操作,并保存mxd

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

posted @ 2024-09-07 16:24  Sherioc  阅读(42)  评论(0编辑  收藏  举报