3

I have created a python script that runs from an ArcMap 10.1 session; however, I would like to modify it to run as a stand alone script, if possible. The problem is I don't see a workaround for prompting the user for the parameters when executed outside ArcMap.

Can this even be reasonably done? If so, how would I approach it? Below is a sample of my script. How can I modify this to prompt the user at the command line for the path names of parameters 0 and 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)

This is as much a philosophical question as it is a programmatic one. I am interested in whether this can be done and the amount of effort to do it this way. Is the effort worth the convenience of not opening up a map document?

user12059
  • 733
  • 2
  • 13
  • 27

2 Answers2

1

Check to see if the parameters were specified. If they were not specified, do one of the following:

  • Use Python's raw_input() method to prompt the user (see this question).
  • Print a "usage" message, which instructs the user to enter parameters on the command line, and then exit.

Prompting the user could look like this:

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()

Printing a usage message could look like this:

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

If you prompt for input with raw_input(), make sure to make all parameters required when adding to your toolbox in ArcGIS for Desktop. Otherwise, you'll get this error from raw_input() when running in Desktop:

EOFError: EOF when reading a line
Community
  • 1
  • 1
Gary Sheppard
  • 4,764
  • 3
  • 25
  • 35
1

hell yeah its worth the convenience of not opening up arcmap. I like to use the optparse module to create command line tools. arcpy.GetParameter(0) is only useful for Esri GUI integration (e.g. script tools). Here is a nice example of a python commandline tool:

http://www.jperla.com/blog/post/a-clean-python-shell-script

I include a unittest class in my tools to testing and automation. I also keep all arcpy.GetParameterAsText statements outside of any real business logic. I like to include at the bottom:

if __name__ == '__main__':
    if arcpy.GetParameterAsText(0):
        params = parse_arcpy_parameters()
        main_business_logic(params)
    else:
        unittest.main()
bcollins
  • 3,379
  • 4
  • 19
  • 35