scottaidh

Generic non hard coded select by attribute and location query - Help!

Discussion created by scottaidh on Apr 8, 2013
Hi,

I've written a script to import into Arc Tools (Toolbox) that finds suitable cities using a select by attribute and location query that is non hard coded and takes user input.  All paths, shapefiles etc. are user input obtained when run as a tool in Arc Toolbox. In case it makes it clearer, the workspaces and shapefiles navigated to in GetParameterAsText are as follows-

workspace H:\working\FindSites.gdb
feature classes are "cities.shp", "counties.shp" and "interstates.shp"

I can't get the script to generate the output table, CityList.shp as required and can't seem to spot why not. I get the following message after it has ran-

Executing: EFLAgeneric H:\working\FindSites.gdb cities.shp 1 "CRIME_INDE" 1 "UNIVERSITY" counties.shp 2500 "AGE_18_64" 500 "NO_FARMS87" interstates.shp "30 Kilometers" CityList.shp
Start Time: Tue Apr 09 00:34:29 2013
Running script EFLAgeneric...
<type 'exceptions.NameError'>: name 'crimeField' is not defined
Failed to execute (EFLAgeneric).
Failed at Tue Apr 09 00:34:29 2013 (Elapsed Time: 0.00 seconds)

The crimeField (and others) are defined in the script and don't know why the error message says it's not defined.

Any help would be appreciated as I think I've run out of things to try.

Thanks

Scottaidh

[CODE]
# extract features by attrtibute generic non hard coded version
# scottaidh 9/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) # path is H:\working\Findsites.gdb


cities = arcpy.GetParameterAsText(1) # cities Layer is cities Feature Layer cities.shp
citiesL = "citiesL"
crimefieldindex = arcpy.GetParameterAsText(2) # crime index is CRIME_INDE and is a Double integer 0.02
whereClause = "{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 1
whereClause2 = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(citiesL, universityField), universityfieldindex)
universityField = arcpy.GetParameterAsText(5) # universityField is fieldname 'UNIVERSITY' SQL expression
counties = arcpy.GetParameterAsText(6) # countiesL is counties Feature Layer counties.shp
countiesL = "countiesL"
workforceindex = arcpy.GetParameterAsText(7) # workforce index is attribute of AGE_18_64 field and is a Double and is 25000
whereClause3 = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(countiesL, workforceField), workforcefieldindex)
workforceField = arcpy.GetParameterAsText(8) # workforceField is fieldname 'AGE_18_64' SQL expression
farmfieldindex = arcpy.GetParameterAsText(9) # farmfieldindex is the NO_FARMS87 field and is Double integer is 500
whereClause4 = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(countiesL, farmField), farmfieldindex)
farmField = arcpy.GetParameterAsText(10) # farmField is fieldname 'NO_FARMS87' SQL expression
interstates = arcpy.GetParameterAsText(11) # interstatesL Layer is the interstates Feature Layer interstates.shp
# interstatesL = "interstatesL"
nearestInterstate = arcpy.GetParameterAsText(12) # interstate WITHIN_A_DISTANCE linear unit
# set output location
outputLayer = arcpy.GetParameterAsText(13) # output layer CityList.shp
targetFeatures = "cityListL"
joinFeatures = "countiesL"

# error trapping measures
try:
# make a layer from the cities feature class
arcpy.MakeFeatureLayer_management(cities, citiesL)
 
# select layer by location to interstates
arcpy.SelectLayerByLocation_management(citiesL, "WITHIN_A_DISTANCE", interstates, nearestInterstate, "NEW_SELECTION")

# from selection above select layer by attribute select "CRIME_IND" <= 0.02 AND "UNIVERSITY" = 1
arcpy.SelectLayerByAttribute_management(citiesL, "SUBSET_SELECTION", whereClause + " <= " + crimefieldindex + " AND " + whereClause2 + " = " + universityfieldindex)
 
# write selected features to a new featureclass
arcpy.CopyFeatures_management(citiesL, "SelectionCityLayer")

# make counties feature layer
arcpy.MakeFeatureLayer_management(counties,"countiesL")

# select workforce and number of farms
# new selection on counties layer countiesL  "AGE_18_64" >= 25000 AND "NO_FARMS87" >= 500")
arcpy.SelectLayerByAttribute_management("countiesL", "NEW_SELECTION", whereClause3 + " >= " + workforceindex + " AND " + whereClause4 + " >= " + farmfieldindex)
 
# from selection above select cities intersecting counties
arcpy.SelectLayerByLocation_management("citiesL", "INTERSECT", "countiesL", "", "SUBSET_SELECTION")
 
# save selected features
arcpy.CopyFeatures_management("citiesL", "tempCityList")

# make temp cities list feature layer so that the output can be spatially joined to counties
arcpy.MakeFeatureLayer_management("tempCityList",targetFeatures)

# Generate NearTable_analysis to find closest interstate distance
arcpy.GenerateNearTable_analysis(targetFeatures, interstatesFC, NearestInterstate, nearestInterstate)

# join new city list layer to generated Near Table
arcpy.AddJoin_management(targetFeatures, "OBJECTID", NearestInterstate, "IN_FID")

#Run the Spatial Join tool, using the defaults for the join operation and join type
arcpy.SpatialJoin_analysis(targetFeatures, joinFeatures, outputLayer, "#", "#", "#")
# add field mappings later
 
except:
print arcpy.GetMessages()
[\CODE]

Outcomes