Hi,
I have written some code for a tool in toolbox that selects a layer by attribute and location from user input. The code works fine when the user chooses (or changes) the fields and values in the tool dialog box but doesn't work if the fields or values are blanked out. The program crashes and is not run successfuly unless values are entered. How can I make it run if no selection is input by the user?
Is there a way to check that the field name or field value exists before running the selection? A bit like
if crimeField and crimeval exist
Select layer by location using crime threshold
I need to be able to disable the selection criteria so that if the user chooses to select cities by just crime threshold then the query selects only that but if the user chooses to select cities by crime threshold AND university then both are selected. Similarily, if the user chooses neither and blanks out the default fields and values in the tool dialog box I'd like it to create a layer of all the cities.
Or do I perhaps need to write more code for each choice i.e. if crimeField and crimeval and universityField and universityval exists then use both of those fields in the select layer by attribute query. Esle if just one of them is chosen just use one, if none use none.
I have pasted the code below and any hints or help would be appreciated. I've been practising with self parameters and if clauses but haven't got any to work properly yet.
Thanks
Scottaidh
[CODE]
# import system modules
import arcpy, os, arcpy, string
from arcpy import env
arcpy.env.overwriteOutput = True
# get user supplied path, layers and fields
path = arcpy.GetParameterAsText(0) # path is H:\working\Findsites.gdb
cities = arcpy.GetParameterAsText(1) # cities Layer is cities Feature Layer cities.shp
citiesL = "citiesL"
counties = arcpy.GetParameterAsText(2) # counties Layer is counties Feature Layer counties.shp
countiesL ="countiesL"
interstates = arcpy.GetParameterAsText(3) # interstates Layer is Feature Layer interstates.shp
interstatesL = "interstatesL"
crimeField = arcpy.GetParameterAsText(4) # crimeField is fieldname 'CRIME_INDE' SQL expression
crimeval = arcpy.GetParameterAsText(5) # crime index is CRIME_INDE and is a Double 0.02
whereClause = "{0} <= {1}".format(arcpy.AddFieldDelimiters(citiesL, crimeField), crimeval)
universityField = arcpy.GetParameterAsText(6) # universityField is fieldname 'UNIVERSITY' SQL expression
universityval = arcpy.GetParameterAsText(7) # universityfieldindex is the UNIVERSITY field and is string integer 1
whereClause2 = "{0} = {1}".format(arcpy.AddFieldDelimiters(citiesL, universityField), universityval)
workforceField = arcpy.GetParameterAsText(8) # workforceField is fieldname 'AGE_18_64' SQL expression
workforceval = arcpy.GetParameterAsText(9) # workforce index is attribute of AGE_18_64 field and is a Double and is 25000
whereClause3 = "{0} >= {1}".format(arcpy.AddFieldDelimiters(countiesL, workforceField), workforceval)
farmField = arcpy.GetParameterAsText(10) # farmField is fieldname 'NO_FARMS87' SQL expression
farmval = arcpy.GetParameterAsText(11) # farmfieldindex is the NO_FARMS87 field and is Double integer is 500
whereClause4 = "{0} >= {1}".format(arcpy.AddFieldDelimiters(countiesL, farmField), farmval)
maxKmInterstate = arcpy.GetParameterAsText(12) # interstate WITHIN_A_DISTANCE linear unit
nearestInterstate = arcpy.GetParameterAsText(13) # Near Table Analysis table generated
#targetFeatures = "cityListL" # cities spatial join layer
#joinFeatures = "countiesL" # counties spatial join layer
citylist = arcpy.GetParameterAsText(14) # narrowed down cities list temporary
citylistL = "citylistL" # the feature layer of the generated cityList feature class
citylistR = "citylistR" # the feature layer of the generated citylist intersecting interstates
outputLayer = arcpy.GetParameterAsText(15) # ootput
#arcpy.MakeFeatureLayer_management(cities, citiesL)
# if distance to nearest interstate selected intersect cities and interstates if not save just cities as layer
if nearestInterstate > 0:
arcpy.MakeFeatureLayer_management(cities, citiesL)
arcpy.AddMessage("Maximum distance to interstates selected")
arcpy.MakeFeatureLayer_management(interstates, interstatesL)
arcpy.SelectLayerByLocation_management(citiesL, "WITHIN_A_DISTANCE", interstatesL, maxKmInterstate, "NEW_SELECTION")
if crimeField >0.098:
arcpy.SelectLayerByAttribute_management(citiesL, "SUBSET_SELECTION", whereClause)
arcpy.AddMessage("maximum crime index threshold selected")
if universityField = 1:
arcpy.SelectLayerByAttribute_management(citiesL, "SUBSET_SELECTION", whereClause2)
arcpy.AddMessage("Maximum crime index threshold and cities with universities selected")
arcpy.CopyFeatures_management(citiesL, citylist)
elif nearestInterstate <= 0:
#arcpy.MakeFeatureLayer_management(cities, citiesL)
arcpy.AddMessage("No cities less than 1km from an interstate")
#arcpy.CopyFeatures_management(citiesL, citylist)
else:
#arcpy.MakeFeatureLayer_management(cities, citiesL)
arcpy.AddMessage("Distance to nearest interstate will not be used")
#arcpy.CopyFeatures_management(citiesL, citylist)
# make a layer from the cities feature class
# arcpy.MakeFeatureLayer_management(cities, citiesL)
# from selection above select layer by attribute select "CRIME_IND" <= 0.02 AND "UNIVERSITY" = 1
#arcpy.SelectLayerByAttribute_management(citiesL, "SUBSET_SELECTION", whereClause + " AND " + whereClause2)
# make counties feature layer
arcpy.MakeFeatureLayer_management(counties,countiesL)
# new selection on counties layer countiesL workforce "AGE_18_64" >= 25000 AND farms "NO_FARMS87" >= 500")
arcpy.SelectLayerByAttribute_management(countiesL, "NEW_SELECTION", whereClause3 + " AND " + whereClause4)
# from selection above select cities intersecting counties
arcpy.SelectLayerByLocation_management(citiesL, "INTERSECT", countiesL, "", "SUBSET_SELECTION")
# save selected features
arcpy.CopyFeatures_management(citiesL, outputLayer)
# Generate NearTable_analysis to find closest interstate distance
#arcpy.GenerateNearTable_analysis(citiesL, interstates, nearestInterstate, maxKmInterstate)
# spatially join the narrowed down city/road and counties layers remove unwanted fields and rename others
#arcpy.SpatialJoin_analysis(citiesL, countiesL, citylist, "#", "#", "")
#arcpy.MakeFeatureLayer_management(citylist,citylistL)
# join new city list layer to generated Near Table
#arcpy.AddJoin_management(citylist, "OBJECTID", nearestInterstate, "IN_FID")
#print arcpy.GetMessages()
#print "\n*** LAST GEOPROCESSOR MESSAGE (may not be source of the error)***"; print arcpy.GetMessages()
#print "Python Traceback Info: " + traceback.format_tb(sys.exc_info()[2])[0]
#print "Python Error Info: " + str(sys.exc_type)+ ": " + str(sys.exc_value)
#print arcpy.GetValue(whereClause)
#print arcpy.GetValue(crimeval)
print arcpy.GetMessages()
[\CODE]
I have written some code for a tool in toolbox that selects a layer by attribute and location from user input. The code works fine when the user chooses (or changes) the fields and values in the tool dialog box but doesn't work if the fields or values are blanked out. The program crashes and is not run successfuly unless values are entered. How can I make it run if no selection is input by the user?
Is there a way to check that the field name or field value exists before running the selection? A bit like
if crimeField and crimeval exist
Select layer by location using crime threshold
I need to be able to disable the selection criteria so that if the user chooses to select cities by just crime threshold then the query selects only that but if the user chooses to select cities by crime threshold AND university then both are selected. Similarily, if the user chooses neither and blanks out the default fields and values in the tool dialog box I'd like it to create a layer of all the cities.
Or do I perhaps need to write more code for each choice i.e. if crimeField and crimeval and universityField and universityval exists then use both of those fields in the select layer by attribute query. Esle if just one of them is chosen just use one, if none use none.
I have pasted the code below and any hints or help would be appreciated. I've been practising with self parameters and if clauses but haven't got any to work properly yet.
Thanks
Scottaidh
[CODE]
# import system modules
import arcpy, os, arcpy, string
from arcpy import env
arcpy.env.overwriteOutput = True
# get user supplied path, layers and fields
path = arcpy.GetParameterAsText(0) # path is H:\working\Findsites.gdb
cities = arcpy.GetParameterAsText(1) # cities Layer is cities Feature Layer cities.shp
citiesL = "citiesL"
counties = arcpy.GetParameterAsText(2) # counties Layer is counties Feature Layer counties.shp
countiesL ="countiesL"
interstates = arcpy.GetParameterAsText(3) # interstates Layer is Feature Layer interstates.shp
interstatesL = "interstatesL"
crimeField = arcpy.GetParameterAsText(4) # crimeField is fieldname 'CRIME_INDE' SQL expression
crimeval = arcpy.GetParameterAsText(5) # crime index is CRIME_INDE and is a Double 0.02
whereClause = "{0} <= {1}".format(arcpy.AddFieldDelimiters(citiesL, crimeField), crimeval)
universityField = arcpy.GetParameterAsText(6) # universityField is fieldname 'UNIVERSITY' SQL expression
universityval = arcpy.GetParameterAsText(7) # universityfieldindex is the UNIVERSITY field and is string integer 1
whereClause2 = "{0} = {1}".format(arcpy.AddFieldDelimiters(citiesL, universityField), universityval)
workforceField = arcpy.GetParameterAsText(8) # workforceField is fieldname 'AGE_18_64' SQL expression
workforceval = arcpy.GetParameterAsText(9) # workforce index is attribute of AGE_18_64 field and is a Double and is 25000
whereClause3 = "{0} >= {1}".format(arcpy.AddFieldDelimiters(countiesL, workforceField), workforceval)
farmField = arcpy.GetParameterAsText(10) # farmField is fieldname 'NO_FARMS87' SQL expression
farmval = arcpy.GetParameterAsText(11) # farmfieldindex is the NO_FARMS87 field and is Double integer is 500
whereClause4 = "{0} >= {1}".format(arcpy.AddFieldDelimiters(countiesL, farmField), farmval)
maxKmInterstate = arcpy.GetParameterAsText(12) # interstate WITHIN_A_DISTANCE linear unit
nearestInterstate = arcpy.GetParameterAsText(13) # Near Table Analysis table generated
#targetFeatures = "cityListL" # cities spatial join layer
#joinFeatures = "countiesL" # counties spatial join layer
citylist = arcpy.GetParameterAsText(14) # narrowed down cities list temporary
citylistL = "citylistL" # the feature layer of the generated cityList feature class
citylistR = "citylistR" # the feature layer of the generated citylist intersecting interstates
outputLayer = arcpy.GetParameterAsText(15) # ootput
#arcpy.MakeFeatureLayer_management(cities, citiesL)
# if distance to nearest interstate selected intersect cities and interstates if not save just cities as layer
if nearestInterstate > 0:
arcpy.MakeFeatureLayer_management(cities, citiesL)
arcpy.AddMessage("Maximum distance to interstates selected")
arcpy.MakeFeatureLayer_management(interstates, interstatesL)
arcpy.SelectLayerByLocation_management(citiesL, "WITHIN_A_DISTANCE", interstatesL, maxKmInterstate, "NEW_SELECTION")
if crimeField >0.098:
arcpy.SelectLayerByAttribute_management(citiesL, "SUBSET_SELECTION", whereClause)
arcpy.AddMessage("maximum crime index threshold selected")
if universityField = 1:
arcpy.SelectLayerByAttribute_management(citiesL, "SUBSET_SELECTION", whereClause2)
arcpy.AddMessage("Maximum crime index threshold and cities with universities selected")
arcpy.CopyFeatures_management(citiesL, citylist)
elif nearestInterstate <= 0:
#arcpy.MakeFeatureLayer_management(cities, citiesL)
arcpy.AddMessage("No cities less than 1km from an interstate")
#arcpy.CopyFeatures_management(citiesL, citylist)
else:
#arcpy.MakeFeatureLayer_management(cities, citiesL)
arcpy.AddMessage("Distance to nearest interstate will not be used")
#arcpy.CopyFeatures_management(citiesL, citylist)
# make a layer from the cities feature class
# arcpy.MakeFeatureLayer_management(cities, citiesL)
# from selection above select layer by attribute select "CRIME_IND" <= 0.02 AND "UNIVERSITY" = 1
#arcpy.SelectLayerByAttribute_management(citiesL, "SUBSET_SELECTION", whereClause + " AND " + whereClause2)
# make counties feature layer
arcpy.MakeFeatureLayer_management(counties,countiesL)
# new selection on counties layer countiesL workforce "AGE_18_64" >= 25000 AND farms "NO_FARMS87" >= 500")
arcpy.SelectLayerByAttribute_management(countiesL, "NEW_SELECTION", whereClause3 + " AND " + whereClause4)
# from selection above select cities intersecting counties
arcpy.SelectLayerByLocation_management(citiesL, "INTERSECT", countiesL, "", "SUBSET_SELECTION")
# save selected features
arcpy.CopyFeatures_management(citiesL, outputLayer)
# Generate NearTable_analysis to find closest interstate distance
#arcpy.GenerateNearTable_analysis(citiesL, interstates, nearestInterstate, maxKmInterstate)
# spatially join the narrowed down city/road and counties layers remove unwanted fields and rename others
#arcpy.SpatialJoin_analysis(citiesL, countiesL, citylist, "#", "#", "")
#arcpy.MakeFeatureLayer_management(citylist,citylistL)
# join new city list layer to generated Near Table
#arcpy.AddJoin_management(citylist, "OBJECTID", nearestInterstate, "IN_FID")
#print arcpy.GetMessages()
#print "\n*** LAST GEOPROCESSOR MESSAGE (may not be source of the error)***"; print arcpy.GetMessages()
#print "Python Traceback Info: " + traceback.format_tb(sys.exc_info()[2])[0]
#print "Python Error Info: " + str(sys.exc_type)+ ": " + str(sys.exc_value)
#print arcpy.GetValue(whereClause)
#print arcpy.GetValue(crimeval)
print arcpy.GetMessages()
[\CODE]