Select to view content in your preferred language

sys.argv/raw input question

1894
7
07-24-2013 11:15 AM
DonalCasey
Emerging Contributor
Hi all,

I have a piece of code where I want the user to be able to specify the rasters that will be used in the mosaic tool etc but I'm having a bit of trouble. Could anybody please point me in the right direction regarding what I'm doing wrong and how to get the user to be able to input the rasters below. Thank you.

# Prints the length of sys.argv and the first argument
print 'Number of Arguments = ', len(sys.argv)
print 'First Argument sys.argv[0] = ', sys.argv[0]
if len(sys.argv)==1:
    Workspace = raw_input("Please enter the folder path of your workspace")
    Raster_1 = raw_input("Please enter the path to the first raster")
    Raster_2 = raw_input("Please enter the path to the second raster")
    Raster_3 = raw_input("Please enter the path to the third raster")
    Raster_4 = raw_input("Please enter the path to the fourth raster")
    BufferDistance = raw_input("Please enter the required buffer distance")
else:
    Workspace = sys.argv[1]
    Raster_1 = sys.argv[2]
    Raster_2 = sys.argv[3]
    Raster_3 = sys.argv[4]
    Raster_4 = sys.argv[5]
    BufferDistance = sys.argv[6]
#User enters the workspace path
arcpy.env.workspace = out_path = Workspace
print 'Workspace Set'
# Create Raster Dataset
out_name = "R_Dataset"
# Run the Create Raster Dataset Script
arcpy.CreateRasterDataset_management (out_path, out_name, "", "8_BIT_UNSIGNED", "", "1", "", "PYRAMIDS -1 NEAREST DEFAULT 75 NO_SKIP", "128 128", "LZ77", "")
# Mosaic Rasters into the Raster Dataset
Raster_inputs = (sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5])
# Set the target to the Raster Dataset that was already created
Tags (2)
0 Kudos
7 Replies
JamesCrandall
MVP Alum
1. What exactly is the problem you are having?  error?  nothing happens? 

2. This doesn't look right (maybe it is, but I have never set a workspace like this):

arcpy.env.workspace = out_path = Workspace


3. Why raw input?  What if they type in an incorrect path or raster?  Seems like this is much better suited for a Toolbox that you can add input parameters for the individual rasters and the workspace itself.
0 Kudos
DanPatterson_Retired
MVP Emeritus
what is the error?  Why not just use the syntax of the mosaic tool rather than coding rawinput stuff?
0 Kudos
DonalCasey
Emerging Contributor
I'm getting an error that says:

"Index Error: list index out of range'

Any ideas?

I don't know how to do it besides using raw_inputs? and I also don't know how to have an alternative scenario if they type the wrong file name, apologies for my ignorance, I'm very new to Python.
0 Kudos
JamesCrandall
MVP Alum
I'm getting an error that says:

"Index Error: list index out of range'

Any ideas?

I don't know how to do it besides using raw_inputs? and I also don't know how to have an alternative scenario if they type the wrong file name, apologies for my ignorance, I'm very new to Python.


Maybe have a look at some great resources in the ESRI library to get you started:

http://resources.arcgis.com/en/help/main/10.1/index.html#/A_quick_tour_of_creating_custom_tools/0015...

I picked this because it appears you need to start by making a choice between developing with Model Builder or jump into creating Toolbox components.  Entirely up to you.

Sorry I don't have a direct answer to your OP.
0 Kudos
AlisonMontgomery
Deactivated User
It's my understanding that sys.argv begins with 1, so the sys.argv[0] might be throwing the "out of range" error. Though I may be misunderstanding how that sys.argv[0] line is being used.

Additionally, are you using PythonWin to be using the raw_input? While it is nice to use in PythonWin, raw_input won't work elsewhere. When I am running scripts with lots of sys.argv in PythonWin, I write it all out in a text file that I keep on hand to copy/paste. Though, as mentioned, user input errors will cause it to error as well. If this will ultimately be a script in ArcToolbox, I would make them sys.argv.

I would also set the out_path as such:

out_path = arcpy.env.workspace = Workspace

If that doesn't work, break it to two lines
arcpy.env.workspace = Workspace
out_path = arcpy.env.workspace

Hope this helps, but I am pretty new to Python too. Good luck!
0 Kudos
DanPatterson_Retired
MVP Emeritus
sys.argv[0] is the scripts name
sys.argv[1] is equivalent to arcpy.GetParameterAsText(1)
0 Kudos
RhettZufelt
MVP Notable Contributor
You might want to look into the Tkinter module as well.  That will allow you to browse for file/folder using the windows explorer type interface.

Not sure what your rasters are as it won't see inside FGDB's, but if they are stand alone files, you may be able to use Tkinter to browse to and select.

R_
0 Kudos