-
使用 ListFeatureClasses 函数获取需要处理的所有要素类的路径名(这些要素类必须是 ArcGIS 支持的 Feature Class 格式),并遍历要素类列表。
-
在循环体内,使用 AddField_management 函数向当前加载的要素类添加一个新字段,用于保存名称信息。此处我们以“Name”为字段名称,数据类型采用文本类型(text)。
import arcpy
# Set input geodatabase path and output feature class name
in_gdb = "D:/path/to/input.gdb"
out_fc_name = "merged_features"
# Create an empty list to hold all the feature layers
all_fcs = []
# Use ListFeatureClasses function to get a list of all feature classes in the input geodatabase
for fc in arcpy.ListFeatureClasses("*", "", in_gdb):
# Create a new field in the feature class to store the layer name
arcpy.AddField_management(fc, "Name", "TEXT")
# Append the feature layer to the list
all_fcs.append(fc)
3、继续在循环体内部,使用 CalculateField 函数计算当前要素类中每个要素的 Name 字段值,将其设为该要素所属的要素类名称。
import arcpy
# Set input geodatabase path and output feature class name
in_gdb = "D:/path/to/input.gdb"
out_fc_name = "merged_features"
# Create an empty list to hold all the feature layers
all_fcs = []
# Use ListFeatureClasses function to get a list of all feature classes in the input geodatabase
for fc in arcpy.ListFeatureClasses("*", "", in_gdb):
# Create a new field in the feature class to store the layer name
arcpy.AddField_management(fc, "Name", "TEXT")
# Calculate the Name field for each feature in the layer
arcpy.CalculateField_management(fc, "Name", "'{}'".format(fc), "PYTHON")
# Append the feature layer to the list
all_fcs.append(fc)
4、接下来,将所有图层合并到一个新的要素类中。可以使用 Merge 函数来实现此操作。
import arcpy
# Set input geodatabase path and output feature class name
in_gdb = "D:/path/to/input.gdb"
out_fc_name = "merged_features"
# Create an empty list to hold all the feature layers
all_fcs = []
# Use ListFeatureClasses function to get a list of all feature classes in the input geodatabase
for fc in arcpy.ListFeatureClasses("*", "", in_gdb):
# Create a new field in the feature class to store the layer name
arcpy.AddField_management(fc, "Name", "TEXT")
# Calculate the Name field for each feature in the layer
arcpy.CalculateField_management(fc, "Name", "'{}'".format(fc), "PYTHON")
# Append the feature layer to the list
all_fcs.append(fc)
# Use Merge function to merge all feature classes into a single feature class
outs = arcpy.Merge_management(all_fcs, out_fc_name)
注意:在执行上述操作时,请确保对应文件的路径信息是否正确,并检查计算机性能和资源使用情况等问题。此外,将所有要素合并到一个新的要素类中,可能会引起空间参考或字段映射相关问题,需要视需要进行进一步调整。