写一个Python2.7的arcgis工具箱脚本:删除数据除Object ID,Shape 和 Shape_Length/Shape_Area 字段外的所有格式字段
时间: 2023-12-14 14:36:11 浏览: 33
以下是一个Python 2.7的ArcGIS工具箱脚本,用于删除数据除Object ID,Shape和Shape_Length/Shape_Area字段外的所有格式字段:
```python
import arcpy
class Toolbox(object):
def __init__(self):
self.label = "Delete Fields"
self.alias = "Delete Fields"
# List of tool classes associated with this toolbox
self.tools = [DeleteFields]
class DeleteFields(object):
def __init__(self):
self.label = "Delete Fields"
self.description = "Deletes all fields of a feature class except for Object ID, Shape, and Shape_Length/Shape_Area."
self.canRunInBackground = False
def getParameterInfo(self):
# Input feature class parameter
fc = arcpy.Parameter(
displayName="Input Feature Class",
name="in_features",
datatype="DEFeatureClass",
parameterType="Required",
direction="Input")
# Output feature class parameter
out_fc = arcpy.Parameter(
displayName="Output Feature Class",
name="out_feature_class",
datatype="DEFeatureClass",
parameterType="Required",
direction="Output")
# Set parameter properties
out_fc.schema.clone = True
# Return the parameters
return [fc, out_fc]
def execute(self, parameters, messages):
# Get input feature class and output feature class parameters
in_fc = parameters[0].valueAsText
out_fc = parameters[1].valueAsText
# Get list of field names to delete
fields = arcpy.ListFields(in_fc)
field_names = [field.name for field in fields]
keep_fields = ["OBJECTID", "Shape", "Shape_Length", "Shape_Area"]
delete_fields = [field_name for field_name in field_names if field_name not in keep_fields]
# Delete fields
arcpy.DeleteField_management(in_fc, delete_fields)
# Copy feature class to output feature class
arcpy.CopyFeatures_management(in_fc, out_fc)
# Add message
messages.addMessage("Fields deleted successfully.")
```
该脚本包含一个工具类“Delete Fields”,该类使用getParameterInfo方法定义输入和输出参数,并使用execute方法执行实际的操作。该脚本使用arcpy模块中的ListFields和DeleteField_management函数来获取和删除要删除的字段。最后,使用CopyFeatures_management函数将结果复制到输出要素类中。