写一个arcgis工具箱脚本:除Object ID,Shape 和 Shape_Length/Shape_Area 字段外,用来删除数据的其他所有字段
时间: 2023-12-14 15:35:41 浏览: 128
下面是一个简单的ArcGIS工具箱脚本,可以实现删除指定要素类中除Object ID、Shape和Shape_Length/Shape_Area字段外的所有字段:
```python
import arcpy
class Toolbox(object):
def __init__(self):
self.label = "Delete Fields Except OID and Shape"
self.alias = "Delete Fields Except OID and Shape"
self.tools = [DeleteFields]
class DeleteFields(object):
def __init__(self):
self.label = "Delete Fields Except OID and Shape"
self.description = "Deletes all fields except Object ID, Shape, and Shape_Length/Shape_Area."
self.canRunInBackground = False
def getParameterInfo(self):
fc_param = arcpy.Parameter(
displayName="Feature Class",
name="fc",
datatype="DEFeatureClass",
parameterType="Required",
direction="Input")
return [fc_param]
def execute(self, parameters, messages):
fc = parameters[0].valueAsText
# Get a list of all fields in the feature class
fields = arcpy.ListFields(fc)
# Create a list of field names to delete
field_names = []
for field in fields:
if field.name not in ["OBJECTID", "SHAPE", "SHAPE_Length", "SHAPE_Area"]:
field_names.append(field.name)
# Delete the fields
arcpy.DeleteField_management(fc, field_names)
messages.addMessage("Fields deleted: {}".format(", ".join(field_names)))
```
使用方法:
1. 将上述代码保存为一个.py文件,例如`DeleteFieldsExceptOIDandShape.py`;
2. 在ArcGIS中创建一个新的工具箱(或打开一个现有的工具箱);
3. 右键单击工具箱,选择“添加工具”;
4. 在“添加新工具”对话框中,选择“脚本”类型,输入一个名称,例如“Delete Fields Except OID and Shape”;
5. 在“脚本”选项卡中,选择上述保存的.py文件;
6. 在“参数”选项卡中,添加一个“Feature Class”参数,并设置为“DEFeatureClass”类型,必填;
7. 保存工具箱。
现在你可以在ArcGIS中使用该工具箱,选择要素类并运行工具来删除除Object ID、Shape和Shape_Length/Shape_Area字段之外的所有字段。
阅读全文