arcgis pro中批量合并多个数据库中的同名要素
时间: 2025-06-29 14:20:34 浏览: 14
### 批量合并多个 Geodatabase 中的同名 Feature Class
在 ArcGIS Pro 中实现批量合并多个 Geodatabase (GDB) 或数据库中的同名 Feature Class 可通过 Python 脚本自动化完成。此过程涉及遍历源 GDB 数据库,提取特定名称模式下的所有要素类,并将其合并到目标 GDB 中。
#### 方法概述
为了高效处理这一需求,可以编写一段 Python 代码来执行以下操作:
- 遍历指定目录下所有的 .gdb 文件。
- 对于每个 GDB,查找并读取其中具有相同名字的 Feature Classes。
- 将这些 Feature Classes 合并成一个新的 Feature Class 并保存至目标 GDB 中。
下面是一个具体的 Python 实现方案[^3]:
```python
import arcpy
from pathlib import Path
def merge_feature_classes(source_gdbs, target_gdb_path, fc_name_pattern):
"""
Merges all feature classes matching the given name pattern from multiple source geodatabases into one in the target gdb.
:param source_gdbs: List of paths to source geodatabases containing feature classes with same names that need merging.
:type source_gdbs: list[str]
:param target_gdb_path: Full path where merged result will be stored as new feature class inside this geodatabase.
:type target_gdb_path: str
:param fc_name_pattern: Name or partial name used for identifying which feature classes should participate in the merge operation.
:type fc_name_pattern: str
"""
# Ensure workspace environment is set correctly before performing any operations on data within it.
arcpy.env.workspace = None
input_fcs = []
for gdb in source_gdbs:
arcpy.env.workspace = gdb
fcs = [fc for fc in arcpy.ListFeatureClasses() if fc.startswith(fc_name_pattern)]
if not fcs:
continue
full_paths_to_fcs = [Path(gdb).joinpath(fc).__str__() for fc in fcs]
input_fcs.extend(full_paths_to_fcs)
if not input_fcs:
raise ValueError(f"No feature classes found matching '{fc_name_pattern}'")
output_fc_full_path = Path(target_gdb_path).joinpath(fc_name_pattern).__str__()
try:
arcpy.Merge_management(input_fcs, output_fc_full_path)
print(f"Merged {len(input_fcs)} feature classes successfully.")
except Exception as e:
print(e)
if __name__ == "__main__":
sources = ["C:\\SourceData\\GDB1.gdb", "C:\\SourceData\\GDB2.gdb"]
dest = r"C:\Destination\TargetGDB.gdb"
pattern = "Roads"
merge_feature_classes(sources, dest, pattern)
```
这段脚本定义了一个函数 `merge_feature_classes` ,该函数接收三个参数:源文件夹列表、目标文件夹路径以及要匹配的功能类名称模板。它会自动搜索给定的工作空间内的所有功能类,并将它们合并为单一的目标功能类存储在一个新的地理数据库中。
阅读全文
相关推荐












