python-2.7 - 作为独立脚本运行时如何使用 arcpy.GetParameterAsText 更改 python 脚本?

标签 python-2.7 arcgis

我创建了一个从 ArcMap 10.1 session 运行的 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)

这是一个哲学问题,也是一个纲领性问题。我感兴趣的是这是否可以完成以及以这种方式完成的工作量。为了不打开 map 文档而付出的努力值得吗?

最佳答案

检查是否指定了参数。如果未指定,请执行以下操作之一:

  • 使用 Python 的 raw_input() 方法来提示用户(参见 this question)。
  • 打印一条“usage”信息,指示用户在命令行输入参数,然后退出。

提示用户可能是这样的:

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 中的工具箱时生成所需的所有参数。否则,当在桌面上运行时,你会从 raw_input() 得到这个错误:

EOFError: EOF when reading a line

关于python-2.7 - 作为独立脚本运行时如何使用 arcpy.GetParameterAsText 更改 python 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19143395/

相关文章:

ubuntu - 在 Ubuntu 中安装 ArcGIS Server 10

python - 如何捕获字符串中直到第一个元音的字符并从中创建子字符串?

python - 以最 Pythonic 的方式替换字符串的第一个和最后一个单词

python - 使用 Python 2.7 向 Excel 单元格添加注释

python - 从 "List"创建子列表

javascript - 如何在 ArcGIS API for JavaScript 中在两点之间绘制一条线

来自 python 脚本的 Django 转储数据

javascript - 如何使用 XY 坐标向 ArcGIS map 添加标记

python - 如何在不使用arcpy的情况下处理python中不支持的fiona类型?

python - 从栅格中提取形状内的点