写一个Python2.7的arcgis工具箱脚本:删除数据除Object ID,Shape 和 Shape_Length/Shape_Area 字段外的所有字段
时间: 2023-12-14 17:36:18 浏览: 87
下面是一个Python 2.7的ArcGIS工具箱脚本,它会删除除Object ID、Shape和Shape_Length/Shape_Area字段外的所有字段:
```python
import arcpy
class Toolbox(object):
def __init__(self):
self.label = "Delete Extra Fields"
self.alias = "Delete Extra Fields"
# List of tool classes associated with this toolbox
self.tools = [DeleteExtraFields]
class DeleteExtraFields(object):
def __init__(self):
self.label = "Delete Extra Fields"
self.description = "Deletes all fields except for Object ID, Shape, and Shape_Length/Shape_Area."
self.canRunInBackground = False
def getParameterInfo(self):
params = []
# Input feature class parameter
input_fc = arcpy.Parameter(
name="input_fc",
displayName="Input Feature Class",
datatype="GPFeatureLayer",
parameterType="Required",
direction="Input")
params.append(input_fc)
return params
def execute(self, parameters, messages):
# Get the input feature class
input_fc = parameters[0].valueAsText
# Get a list of all the fields in the input feature class
field_names = [field.name for field in arcpy.ListFields(input_fc)]
# Loop through the fields and delete any fields that are not Object ID, Shape, or Shape_Length/Shape_Area
for field_name in field_names:
if not (field_name == "OBJECTID" or field_name == "SHAPE" or field_name == "SHAPE_Length" or field_name == "SHAPE_Area"):
arcpy.DeleteField_management(input_fc, field_name)
messages.addMessage("Extra fields deleted.")
return
```
要使用此工具箱脚本,请按照以下步骤操作:
1. 在ArcGIS中创建一个新的工具箱。
2. 右键单击工具箱,并选择“添加脚本”。
3. 输入上面的代码,并保存脚本。
4. 在工具箱中,右键单击脚本,并选择“属性”。
5. 在“参数”选项卡上,添加一个“GPFeatureLayer”类型的参数,命名为“input_fc”。
6. 保存属性并关闭窗口。
7. 现在您可以在工具箱中使用此脚本。只需选择要处理的要素类,然后运行脚本即可删除所有除Object ID、Shape和Shape_Length/Shape_Area字段外的其他字段。
阅读全文