【ArcPy】批量删除shp属性表中的字段

需求

先前批量操作添加了很多字段,重新调试想要清空,只留下原有的必要字段

代码

point_shp_file = r"E:\IrrigationFinal\00SHP\HNfish.shp"
fieldList = arcpy.ListFields (point_shp_file)
for field in fieldList :
     if field . name != 'FID' and field . name != 'Shape' and field . name !='Id' :
       arcpy . DeleteField_management ( point_shp_file , field . name )

注意

操作时要关闭在ArcGIS打开的该shp,否则会提示schema lock

在ArcGIS Pro或Desktop环境中,你可以使用Pythonarcpy模块来处理Shapefile (shp) 文件并批量按某个字段进行分组导出为KML文件格式。这里是一个基本步骤: 1. **安装和导入所需的库**: 首先,确保已经安装了`arcpy`, `os`, 和`datetime`等库,如果没有可以使用下面命令安装: ``` !pip install arcgis ``` 2. **加载数据和确定分组字段**: ```python import arcpy # 加载Shapefile input_shp = "path_to_your_shapefile.shp" feature_class = arcpy.mp.FeatureClass.to_featureclass(input_shp) # 确定你要使用的分组字段 group_field = "your_grouping_field" # 替换为实际字段名 ``` 3. **创建临时表和对数据进行分组**: ```python # 创建临时表以便于处理 temp_table = r"C:\temp\temp_table.gdb\temp_group_table" arcpy.management.CopyRows(feature_class, temp_table) # 对数据按指定字段进行分组 arcpy.management.GroupBy(temp_table, group_field) ``` 4. **遍历分组结果,分别导出每个组到KML**: ```python output_folder = "path_to_output_kmls_folder" os.makedirs(output_folder, exist_ok=True) for group in arcpy.da.SearchCursor(temp_table, ["FID", group_field]): group_id = group[0] group_name = group[1] # 构建KML文件名 kml_file = f"{output_folder}\\{group_name}.kml" # 使用arcpy.CreateFeatureclass_management创建KML arcpy.management.CreateFeatureclass(output_folder, f"{group_name}", "POINT", spatial_reference=feature_class.spatialReference) # 将当前组内的点添加到新KML文件 with arcpy.da.UpdateCursor(temp_table, ["SHAPE@"]) as cursor: for feat in cursor: if feat[0].FID == group_id: feat[0].shape.name = group_name arcpy.InsertCursor(kml_file).insertRow(feat) ``` 5. **清理工作**: ```python # 删除临时表 arcpy.management.Delete(temp_table) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值