初始化

This commit is contained in:
2026-04-22 12:27:49 +08:00
commit 4857cb6e45
73 changed files with 20927 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
import gc
import sys
import arcpy
# 临时文件清理
def clean_up_temp_files(temp_files, workspace=None):
"""安全清理临时文件和内存工作空间"""
try:
if temp_files:
for temp_file in temp_files:
if arcpy.Exists(temp_file):
try:
arcpy.management.Delete(temp_file)
# print_status(f"已删除临时文件: {temp_file}")
except Exception as delete_err:
sys.stderr.write(f"CleanupError:无法删除临时文件 {temp_file}: {str(delete_err)}\n")
# 清理内存工作空间 (确保在 in_memory 工作空间中操作,而不是删除其他地方的同名项)
try:
# 切换到内存工作空间进行清理
if arcpy.Exists("in_memory"):
arcpy.env.workspace = "in_memory"
# 删除内存工作空间中的所有内容
for item in arcpy.ListDatasets() + arcpy.ListFeatureClasses() + arcpy.ListRasters():
try:
arcpy.management.Delete(item)
# print_status(f"已清理内存项: in_memory/{item}")
except Exception as delete_mem_item_err:
sys.stderr.write(f"CleanupError:无法清理内存项 in_memory/{item}: {str(delete_mem_item_err)}\n")
except Exception as delete_in_memory_err:
sys.stderr.write(f"CleanupError:清理 in_memory 工作空间时发生错误: {str(delete_in_memory_err)}\n")
# 恢复原始工作空间
if workspace and arcpy.Exists(workspace):
try:
arcpy.env.workspace = workspace
arcpy.management.ClearWorkspaceCache()
except Exception as restore_ws_err:
sys.stderr.write(f"CleanupError:无法恢复原始工作空间 {workspace}: {str(restore_ws_err)}\n")
except Exception as cleanup_err:
# 外层异常捕获
sys.stderr.write(f"CleanupError:清理临时文件过程中发生未预料的错误: {str(cleanup_err)}\n")
# 强制垃圾回收
gc.collect()