写一个arcgis工具箱脚本自动删除图层的所有字段
时间: 2023-12-10 19:42:42 浏览: 90
清理图层工具
下面是一个arcgis工具箱脚本,可以自动删除指定图层的所有字段:
```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 in a specified feature class or table."
self.canRunInBackground = False
def getParameterInfo(self):
fc_param = arcpy.Parameter(
displayName="Input Feature Class or Table",
name="fc",
datatype="DEFeatureClass",
parameterType="Required",
direction="Input")
return [fc_param]
def execute(self, parameters, messages):
fc = parameters[0].valueAsText
# Get list of fields in feature class or table
fields = arcpy.ListFields(fc)
# Loop through fields and delete them
for field in fields:
arcpy.DeleteField_management(fc, field.name)
messages.addMessage("All fields have been deleted from " + fc)
```
将以上代码保存为一个.py文件,然后通过ArcGIS工具箱将其添加为工具。运行该工具时,输入要删除字段的图层即可。
阅读全文