# 在ArcGIS中创建Python工具（二）

ArcGIS Help 中脚本工具的帮助过于枯燥，在这里，我以一个具体的实例来总结构建脚本工具的过程，我要实现的需求是做个快速实现羽化边界效果的小工具，预期得到如下的效果：

# 1 准备Python脚本文件

__author__ = 'kikita'

# FileName: EasyFeathering.py

import arcpy
# arcpy.env.workspace = "D:\something\Data.gdb"

# Script Tool Parameters
InputFeature = "InterestArea"
OutputFeature = "OutFeathering"
SingleRingWidth = 10000

# Some Predefined Parameters
distances = []
level = 9
bufferUnit = "meters"
NewField = "Percent"

# My Easy Feathering function
for i in range(level):
distances.append(SingleRingWidth*(i+1))
i = i+1

print  str(distances)
print  "Distance Complete!"

arcpy.MultipleRingBuffer_analysis(InputFeature, OutputFeature, distances, bufferUnit, "", "ALL","OUTSIDE_ONLY")
print  "Success to execute Multi Ring Buffer."

print "Success to add Transparency Percent Field."

arcpy.CalculateField_management(OutputFeature, NewField, "!OBJECTID! *10", "PYTHON", "")
print "Success to Calculate Transparency Percent Field."

# 2 脚本工具参数配置

# Script Tool Parameters
InputFeature = arcpy.GetParameterAsText(0)
SingleRingWidth = arcpy.GetParameterAsText(1)
OutputFeature = arcpy.GetParameterAsText(2)

• 工具对话框中的参数顺序必须与脚本中的参数顺序一致
• 每个脚本工具参数都有关联的数据类型。ArcGIS的地理处理不会将值发送给数据类型不正确的脚本，从这点上看，脚本工具比下一篇要说到的脚本工具箱多了一个优势，就是，在参数值发送给脚本之前会有数据类型检验。

# 3 消息

__author__ = 'kikita'

# FileName: EasyFeathering.py

import arcpy

#arcpy.env.workspace = "D:\IncidentSupport2015\something\Data.gdb"

# Get the input values from tool UI
InputFeature = arcpy.GetParameterAsText(0)
SingleRingWidth = arcpy.GetParameterAsText(1)
OutputFeature = arcpy.GetParameterAsText(2)

# Some Predefined Parameters
distances = []
level = 9
bufferUnit = "meters"
NewField = "Percent"

# My Easy Feathering function
for i in range(level):
distances.append(int(SingleRingWidth)*(i+1))
i = i+1

arcpy.MultipleRingBuffer_analysis(InputFeature, OutputFeature, distances, bufferUnit, "", "ALL","OUTSIDE_ONLY")
arcpy.AddMessage("Step2 Success to execute Multi Ring Buffer.")

arcpy.CalculateField_management(OutputFeature, NewField, "!OBJECTID! *10", "PYTHON", "")

InputFeatureCount = int(arcpy.GetCount_management(OutputFeature).getOutput(0))
if InputFeatureCount == 0:
else:
arcpy.AddMessage("Step4 Success to Calculate Transparency Percent Field.")


# 4 显示结果图层

1. 为输出参数配置模板图层。

1. 在处理设置中，激活设置

# 5 配置路径

PS：但是不要想太多，这个设置仅仅会将脚本文件所在位置按照相对路径存储，而不会将脚本内部的路径进行转换。

# Layer files are located in same folder as the .py file
PythonFilePath = os.path.dirname(__file__)
params = arcpy.GetParameterInfo()
params[2].symbology = os.path.join(PythonFilePath, "FeatheringEffectTemplate.lyr")

# Pass message
arcpy.AddMessage("Finding Feathering Effect Template Layer ..." +"/n"+ os.path.join(PythonFilePath, "FeatheringEffectTemplate.lyr"))



OK，路径的问题就解决了。

• 写评论