Arcpy导出ArcGIS全部坐标系及目录结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import arcpy
import os
import json

def checkDir(dir):
if not os.path.exists(dir):
os.makedirs(dir)

# 获取所有内置的坐标系
spatial_references = arcpy.ListSpatialReferences()

# 输出坐标系文件的目录
output_directory = r"D:\file\program\py\ExportSpatialReference\output"

# 遍历所有坐标系并导出为.prj文件
for spatial_reference in spatial_references:
try:
# 创建一个空间参考对象
spatial_ref_obj = arcpy.SpatialReference(spatial_reference)

# 获取坐标系的WKT(Well-Known Text)表示
wkt = spatial_ref_obj.exportToString()

# 构建.prj文件的完整路径
prj_file_name = f"{spatial_reference}.prj"
prj_file_path = os.path.join(output_directory, prj_file_name).replace('/','\\')
checkDir(os.path.dirname(prj_file_path))
# 将WKT写入.prj文件
with open(prj_file_path, 'w') as prj_file:
prj_file.write(wkt)

print(f"坐标系文件 {prj_file_path} 已成功导出")
except Exception as e:
print(str(e))

def generate_folder_structure(folder_path):
folder_structure = {
"type": "folder",
"name": os.path.basename(folder_path),
"content": []
}

for item in os.listdir(folder_path):
item_path = os.path.join(folder_path, item)
if os.path.isdir(item_path):
# 递归处理子文件夹
folder_structure["content"].append(generate_folder_structure(item_path))
else:
# 处理文件
file_info = {
"type": "file",
"name": item
}
folder_structure["content"].append(file_info)

return folder_structure

# 生成文件夹结构的JSON
folder_structure_json = generate_folder_structure(output_directory)

# 将JSON保存到文件
with open(r"D:\file\program\py\ExportSpatialReference\folder_structure.json", "w") as json_file:
json.dump(folder_structure_json, json_file, indent=4)

print("文件夹结构已保存为folder_structure.json")