Select to view content in your preferred language

User parameters in script not recognized

1568
2
09-14-2010 05:59 AM
MikeHouts
Emerging Contributor
I created a select by attributes process in model builder where the selection criteria were parameters entered by the user to select on a series of attributes to narrow down the selection to a single polygon (township, range, section).  It work fine in model builder, but when I exported it as a Py script, it does not work right.  When run, it ignores the user/parameter input, and uses the default values I added when making the model. 

How do I get it to accept the user entries? Thanks.
Here is the code:


# Import arcpy module
import arcpy

# Script arguments
Section = arcpy.GetParameterAsText(0)
Section = "\"SECTION\" = 5" # provide a default value if unspecified

Township = arcpy.GetParameterAsText(1)
Township = "\"TOWNSHIP\" = 5" # provide a default value if unspecified

Range = arcpy.GetParameterAsText(2)
Range = "\"RANGE\" = 5" # provide a default value if unspecified

Range_dir = arcpy.GetParameterAsText(3)
Range_dir = "\"RANGE_DIR\" = 'E'" # provide a default value if unspecified

# Local variables:
PLSS = "PLSS"
PLSS__1_ = Section
PLSS__2_ = Township
PLSS__4_ = Range
PLSS__5_ = Range_dir

# Process: Select Layer By Attribute
arcpy.SelectLayerByAttribute_management(PLSS, "NEW_SELECTION", Section)

# Process: Select Layer By Attribute (2)
arcpy.SelectLayerByAttribute_management(PLSS, "SUBSET_SELECTION", Township)

# Process: Town_dir
arcpy.SelectLayerByAttribute_management(PLSS, "SUBSET_SELECTION", "\"TOWNSHIP_D\" = 'S'")

# Process: Select Layer By Attribute (4)
arcpy.SelectLayerByAttribute_management(PLSS, "SUBSET_SELECTION", Range)

# Process: Select Layer By Attribute (5)
arcpy.SelectLayerByAttribute_management(PLSS, "SUBSET_SELECTION", Range_dir)
0 Kudos
2 Replies
MikeHouts
Emerging Contributor
No solution yet, but Im updating my original post with a simplified version.  If I run the below model as is, it wont run and I get the folowing error:

Running script selecttestvariable...
<class 'arcgisscripting.ExecuteError'>: ERROR 000358: Invalid expression
Failed to execute (SelectLayerByAttribute).
Failed to execute (selecttestvariable).
  
But, if comment out the next to last line and activate the last line (changing from user defined parameter to hard coded select section 1) it runs fine.

Is there a problem with my "GetParameterAsText" or could it be in the script properties in the toolbox (currently parameters defined as data type = long, required, input...


Thanks.



# Import arcpy module
import arcpy

# set workspace
arcpy.env.workspace = "G:/temp/"

# Script arguments
SelCondition = arcpy.GetParameterAsText(0)

# Local variables:
PLSS = "PLSS"

#### Use the SelectLayerByAttribute tool to select a Section
arcpy.SelectLayerByAttribute_management(PLSS, "NEW_SELECTION", SelCondition)
# arcpy.SelectLayerByAttribute_management(PLSS, "NEW_SELECTION", "\"SECTION\" = 1")
0 Kudos
MikeHouts
Emerging Contributor
Got it figured out.  I had to combine the attribute name with the variable parameter.


#Use the SelectLayerByAttribute tool to select a Section
arcpy.SelectLayerByAttribute_management(PLSS, "NEW_SELECTION", "\"SECTION\" = "+SelCondition)

# the above is the variable version of this
# arcpy.SelectLayerByAttribute_management(PLSS, "NEW_SELECTION", "\"SECTION\" = 1")
0 Kudos