Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm building a script tool that processes shapefiles ( GPS data collected on weekly basis). The tool is set up to take an input for each shapefile ( in this case Sewer, Storm, Sign). On some days, data for one of these assests may not be collected, so there will be no dataset to provide to the tool. I thought O.K., i'll just set the input parameter to optional, and if no dataset is provided, it'll just continue on to the next dataset. Wrong answer, play again. Is it possible to make this work?

Here's a snippet of the code.

#SEWER#
sewer = arcpy.GetParameterAsText(0)
arcpy.AddField_management(sewer, "UID", "TEXT", 10, "", "", "", "NON_NULLABLE", "", "")
arcpy.AddField_management(sewer, "Phot1_Link", "TEXT", 240, "", "", "", "NON_NULLABLE", "", "")

#Calculate UID field
fieldName = "UID"
expression = "id_gen()"
codeblock = """def id_gen(size = 9, chars = string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for x in range (size))"""
arcpy.CalculateField_management(sewer, fieldName, expression, "PYTHON", codeblock)

enter image description here

share|improve this question
2  
did you include an "if" statement to check if the optional parameter is present or not? Without seeing your code or your tool parameter settings, the most logical explanation is that your script doesn't know to skip a process if the necessary input is missing. –  Jay Guarneri Mar 7 '13 at 18:43
    
I had a feeling it was going to need an if statement. However I wouldn't be sure how to code that. –  dchaboya Mar 7 '13 at 19:01

2 Answers 2

up vote 5 down vote accepted

Like Jay said, you need to add logic to test whether each optional parameter was specified and take the appropriate action (or inaction) for each case.

Modifying your snippet above:

#SEWER#
sewer = arcpy.GetParameterAsText(0)

if sewer and sewer != "#":
    arcpy.AddField_management(sewer, "UID", "TEXT", 10, "", "", "", "NON_NULLABLE", "", "")
    arcpy.AddField_management(sewer, "Phot1_Link", "TEXT", 240, "", "", "", "NON_NULLABLE", "", "")

    #Calculate UID field
    fieldName = "UID"
    expression = "id_gen()"
    codeblock = """def id_gen(size = 9, chars = string.ascii_uppercase + string.digits):
    return ''.join(random.choice(chars) for x in range (size))"""
    arcpy.CalculateField_management(sewer, fieldName, expression, "PYTHON", codeblock)

The hash sign ("#") string is what the geoprocessing framework passes in for unspecified values so you have to look for that in addition to the empty/null string case.

share|improve this answer
    
So, running it as is will return an the error string is not defined. As you stated blah, the empty string needs to be dealt with as well. I'm not sure how to deal with that empty string. –  dchaboya Mar 7 '13 at 19:32
    
It sounds like your sewer variable doesn't exist, which means that there is no string for the if statement to check against try replacing "if sewer and sewer != "#":" with "if sewer in globals():" This will check whether your sewer variable has been defined or not. –  Jay Guarneri Mar 7 '13 at 19:41
    
No, that is something else. Did you forget your import statements? –  blah238 Mar 7 '13 at 19:42
    
I'm not so sure. In either case, I goofed up a little bit: the variable sewer should be written in quotes to see if it is in the globals() list. You could also use vars() instead of globals(). –  Jay Guarneri Mar 7 '13 at 19:55
1  
Sorry my comment was directed at dchaboya. The string module looks like it is being referenced before being imported. –  blah238 Mar 7 '13 at 19:58

I think the easiest approach would be to give all your input parameters a default bogus value in ArcMap. Click on "Default" in your Parameters tab, and set them all to 9999 (or some number you know will be invalid) and then check for this value in your Python script:

if sewer != 9999:

It should skip that input if it matches your default value.

share|improve this answer
2  
Please don't do this. –  blah238 Mar 7 '13 at 21:55
    
I don't mind the downvote, but you could at least elaborate. –  Mintx Mar 7 '13 at 22:02
3  
For starters, what if you don't run it from the script tool dialog but call the script from a command prompt, batch file, or another script? The parameters won't get the dummy values and you'll be right back where you started. What if you anticipate incorrectly and someone actually wants to use your dummy value as a legitimate one? Lastly, it's more work, unintuitive, and better/simpler alternatives exist. It's a simple problem with a simple solution, and this isn't it. –  blah238 Mar 7 '13 at 22:09
    
For one, the question specifically asks about an ArcGIS script tool. For two, he's processing GPS data, so a default value of default or any string is almost guaranteed to not be valid. It's not more work, it's not unintuitive, and... okay, I do concede on that last point. :) –  Mintx Mar 7 '13 at 22:22
    
Honestly, thanks for the explanation though, and you're right. It's just a comment of "please don't do this" doesn't serve anyone... me of all people, since I'm wrong and don't know why. –  Mintx Mar 7 '13 at 22:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.