Select Layer By Attribute User Input problem - Please help

Discussion created by scottaidh on Apr 8, 2013
Latest reply on Apr 8, 2013 by scottaidh

I'm writing a script for a toolbox that gets user input to run a select layer by attribute and location tool on a cities shapefile that contains two fields of interest, cime index (CRIME_IND) and university status (UNIVERSITY), both integers.  The 1st part, select layer by attribute, results in a layer being saved (citiesL) with those attributes and is shown in the code below.

I'm having problems in getting the user to define which attributes to select, none, one or more than one.  If the user chooses to select only crime attributes ("CRIME_INDE" <= 0.02) then only those ones would be saved and if the user chooses to select crime and university status attributes ("CRIME_INDE" <=0.02 AND "UNIVERSITY" = 1) then both those sets of attributes would be saved to the layer file.

Similarily, if the user does not fill in the required fields (by changing their default to nothing) then all the attributes of the cities layer would be effectively be saved to the layer file i.e. no selection by attribute.

When I run the code in the toolbox it completes successfuly but no layer is saved (citiesL).  I'm not sure where I'm going wrong with this and would appreciate any help.



# script for user input tool based on select layer by location and attribute
# parameters within feature classes are optional
# scottaidh 8/4/13

# import system modules
import arcpy, os, traceback, arcpy
from arcpy import env
arcpy.env.overwriteOutput = True

# get user supplied path, layers and fields
path = arcpy.GetParameterAsText(0)

try :
 citiesFC = arcpy.GetParameterAsText(1) # cities Layer is cities Feature Layer
 crimefieldindex = arcpy.GetParameterAsText(2) # crime index is CRIME_INDE and is a Double integer
 whereClause1 = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(citiesL, crimeField), crimefieldindex)
 crimeField = arcpy.GetParameterAsText(3) # crimeField is fieldname 'CRIME_INDE' SQL expression
 universityfieldindex = arcpy.GetParameterAsText(4) # universityfieldindex is the UNIVERSITY field and is DOUBLE integer
 whereClause2 = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(citiesL, universityField), universityfieldindex)
 universityField = arcpy.GetParameterAsText(5) # universityField is fieldname 'UNIVERSITY' SQL expression
 outputlayer = arcpy.GetParameterAsText(6) # output layer name
 # make a layer from the cities feature class
 arcpy.MakeFeatureLayer_management(citiesFC, "citiesL")
 # select the attributes from citiesL from user input
 prj = ""
 prj2 = ""
 if prj.find("CRIME_INDE")>=0:
  crimecount = 1
  print 'using crime index'
 elif prj2.find("UNIVERSITY")>=1:
  unicount = 2
  print 'using university status'
 # set field to determine where clause variables 1=crime 2=university 3=crime and university else: none selected
 usecityfield = crimecount + unicount
 if usecityfield == 1:
  arcpy.SelectLayerByAttribute_management("citiesL", "SUBSET_SELECTION", crimefieldindex)
  arcpy.CopyFeatures_management("citiesL", "citycrime")
 elif usecityfield == 2:
  arcpy.SelectLayerByAttribute_management("citiesL", "SUBSET_SELECTION", universityfieldindex)
  arcpy.CopyFeatures_management("citiesL", "cityuni")
 elif usecityfield == 3:
  arcpy.SelectLayerByAttribute_management("citiesL", "SUBSET_SELECTION", crimefieldindex + universityfieldindex)
  arcpy.CopyFeatures_management("citiesL", "citycrimeuni")
  # write selected features to a new featureclass
  arcpy.CopyFeatures_management("citiesL", "SUBSET_SELECTION")

 print arcpy.GetMessages()