scottaidh

User Input Select By Attribute & Location - Desparate urgency - PLEASE HELP!!!!

Discussion created by scottaidh on Apr 9, 2013
Latest reply on Apr 10, 2013 by scottaidh
I have written some generic code to run a select by attribute and location query and then do a spatial join with the narrowed down results. This is for a user input Arc Tool in Toolbox.

The trouble is that it doesn't output ANY of the output files (city_list.shp, foundcities.shp and NearTable.dbf).  When I run the hard coded version of the script it outputs perfect results but when I get user input in the tool using GetParameterAsText it doesn't work.
Even a simple make feature layer and copy features command like below doesn't work when it's user input.
arcpy.MakeFeatureLayer_management(cities, citiesL)
arcpy.CopyFeatures_management("citiesL", cityListFC)

Please can you tell me what I've missed or where I'm going wrong. Why can't I get the output shapefile or dbf file?

I have a Friday deadline and I'm desparate and can't find anyone with an answer.  Help would be highly appreciated and urgently needed.

Thanks Scott
# extract features by attrtibute generic non hard coded version
# scottaidh 9/4/13

# import system modules
import arcpy, os, 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"
crimeField = arcpy.GetParameterAsText(2) # crimeField is fieldname 'CRIME_INDE' SQL expression
crimefieldindex = arcpy.GetParameterAsText(3) # crime index is CRIME_INDE and is a Double integer 0.02
whereClause = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(citiesL, crimeField), crimefieldindex)
universityField = arcpy.GetParameterAsText(4) # universityField is fieldname 'UNIVERSITY' SQL expression
universityfieldindex = arcpy.GetParameterAsText(5) # universityfieldindex is the UNIVERSITY field and is DOUBLE integer 1
whereClause2 = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(citiesL, universityField), universityfieldindex)
counties = arcpy.GetParameterAsText(6) # countiesL is counties Feature Layer counties.shp
countiesL = "countiesL"
workforceField = arcpy.GetParameterAsText(7) # workforceField is fieldname 'AGE_18_64' SQL expression
workforceindex = arcpy.GetParameterAsText(8) # workforce index is attribute of AGE_18_64 field and is a Double and is 25000
whereClause3 = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(countiesL, workforceField), workforceindex)
farmField = arcpy.GetParameterAsText(9) # farmField is fieldname 'NO_FARMS87' SQL expression
farmfieldindex = arcpy.GetParameterAsText(10) # farmfieldindex is the NO_FARMS87 field and is Double integer is 500
whereClause4 = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(countiesL, farmField), farmfieldindex)
interstates = arcpy.GetParameterAsText(11) # interstatesL Layer is the interstates Feature Layer interstates.shp
maxKmInterstate = arcpy.GetParameterAsText(12) # interstate WITHIN_A_DISTANCE linear unit
NearestInterstate = arcpy.GetParameterAsText(13) # Near Table Analysis table generated
cityListFC = arcpy.GetParameterAsText(14) # temp city list CityListFC
outputLayer = arcpy.GetParameterAsText(15) # output layer FoundCities.shp
targetFeatures = "cityListL" # cities spatial join layer
joinFeatures = "countiesL" # counties spatial join layer 


# 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)
  
 # 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, cityListFC)
 
 arcpy.AddField_management(cityListFC, "CITYNAME", "TEXT", "", "", "25")
 arcpy.AddField_management(cityListFC, "CRIMEINDEX", "DOUBLE", "", "", "")
 arcpy.AddField_management(cityListFC, "HAS_UNI", "TEXT", "", "", "3")
 arcpy.CalculateField_management(cityListFC, "CITYNAME", "!NAME!", "PYTHON")
 arcpy.CalculateField_management(cityListFC, "CRIMEINDEX", "!CRIME_INDE!", "PYTHON")
 arcpy.CalculateField_management(cityListFC, "HAS_UNI", "!UNIVERSITY!", "PYTHON")
 arcpy.DeleteField_management(cityListFC,["NAME", "LABEL", "CLASS"])
 arcpy.DeleteField_management(cityListFC,["CRIME_INDE", "LABEL", "CLASS"])
 arcpy.DeleteField_management(cityListFC,["UNIVERSITY", "LABEL", "CLASS"])
 
 # make temp cities list feature layer so that the output can be spatially joined to counties
 arcpy.MakeFeatureLayer_management(cityListFC,"cityListL")
 
 # Generate NearTable_analysis to find closest interstate distance
 arcpy.GenerateNearTable_analysis("cityListL", interstates, NearestInterstate, maxKmInterstate)
 
 # join new city list layer to generated Near Table
 arcpy.AddJoin_management("cityListL", "OBJECTID", NearestInterstate, "IN_FID")
 
 arcpy.CopyFeatures_management("cityListL", ouputLayer) # cityListFC)
 
 # 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()

Outcomes