我创建了一个从ArcMap 10.1会话运行的python脚本;但是,如果可能的话,我想将其修改为独立脚本运行。问题是我没有看到在ArcMap外部执行时提示用户输入参数的解决方法。
这可以合理地完成吗?如果是这样,我将如何处理它?以下是我的脚本示例。如何修改此选项以在命令行中提示用户输入参数0和1的路径名?
import arcpy
arcpy.env.overwriteOutput = True
siteArea = arcpy.GetParameterAsText(0)
tempGDB_Dir = arcpy.GetParameterAsText(1)
tempGDB = tempGDB_Dir + "\\tempGDB.gdb"
# Data from which records will be extracted
redWoods = "D:\\Data\\GIS\\Landforms\\Tress.gdb\\Redwoods"
# List of tree names that will be used in join
treesOfInterest = "C:\\Data\\GIS\\Trees\\RedwoodList.dbf"
inFeature = [redWoods, siteArea]
tempFC = tempGDB_Dir + "\\TempFC"
tempFC_Layer = "TempFC_Layer"
output_dbf = tempGDB_Dir + "\\Output.dbf"
# Make a temporaty geodatabase
arcpy.CreateFileGDB_management(tempGDB_Dir, "tempGDB.gdb")
# Intersect trees with site area
arcpy.Intersect_analysis([redWoods, siteArea], tempFC, "ALL", "", "INPUT")
# Make a temporary feature layer of the results
arcpy.MakeFeatureLayer_management(tempFC, tempFC_Layer)
# Join redwoods data layer to list of trees
arcpy.AddJoin_management(tempFC_Layer, "TreeID", treesOfInterest, "TreeID", "KEEP_COMMON")
# Frequency analysis - keeps only distinct species values
arcpy.Frequency_analysis(tempFC_Layer, output_dbf, "tempFC.TreeID;tempFC.TreeID", "")
# Delete temporary files
arcpy.Delete_management(tempFC_Layer)
arcpy.Delete_management(tempGDB)
这是一个哲学问题,因为它是一个程序化的问题。我感兴趣的是这是否可以做到以及这样做的努力程度。是否值得方便不打开地图文档?
答案 0 :(得分:1)
检查参数是否已指定。如果未指定,请执行以下操作之一:
提示用户可能如下所示:
siteArea = arcpy.GetParameterAsText(0)
tempGDB_Dir = arcpy.GetParameterAsText(1)
if (not siteArea):
arcpy.AddMessage("Enter the site area:")
siteArea = raw_input()
if (not tempGDB_Dir):
arcpy.AddMessage("Enter the temp GDB dir:")
tempGDB_Dir = raw_input()
打印使用信息可能如下所示:
siteArea = arcpy.GetParameterAsText(0)
tempGDB_Dir = arcpy.GetParameterAsText(1)
if (not (siteArea and tempGDB_Dir)):
arcpy.AddMessage("Usage: myscript.py <site area> <temp GDB dir>")
else:
# the rest of your script goes here
如果使用raw_input()提示输入,请确保在ArcGIS for Desktop中添加到工具箱时生成所需的所有参数。否则,在Desktop中运行时,您将从raw_input()获得此错误:
EOFError: EOF when reading a line
答案 1 :(得分:1)
地狱是的,值得不开放弧光图的便利。我喜欢使用optparse模块来创建命令行工具。 arcpy.GetParameter(0)仅对Esri GUI集成(例如脚本工具)有用。以下是python命令行工具的一个很好的例子:
http://www.jperla.com/blog/post/a-clean-python-shell-script
我在我的工具中加入了一个单元测试类来测试和自动化。我还将所有arcpy.GetParameterAsText语句保留在任何实际业务逻辑之外。我想在底部加入:
if __name__ == '__main__':
if arcpy.GetParameterAsText(0):
params = parse_arcpy_parameters()
main_business_logic(params)
else:
unittest.main()