scottaidh

Same Code Different results HELP

Discussion created by scottaidh on Apr 10, 2013
Please can someone look at these two small pieces of code and tell me where I'm going wrong.
It's a simple(?) make feature layer and save feature layer command. 

Code2 (below) works perfectly, but Code 1(below), doesn't and I don't know why.  It seems I can make and save a feature layer when the script is had coded but not when it's user input.

When I run code1 I get the following error -

Executing: code1 H:\working\FindSites.gdb cities interstates counties H:\working\FindSites.gdb\OutList "30 Kilometers" "CRIME_INDE" 0.02 "UNIVERSITY" 1
Start Time: Wed Apr 10 13:20:47 2013
Running script code1...
<class 'arcgisscripting.ExecuteError'>: ERROR 999999: Error executing function.
General function failure
General function failure [interstates]
Failed to execute (SelectLayerByLocation).
Failed to execute (code1).
Failed at Wed Apr 10 13:20:49 2013 (Elapsed Time: 2.00 seconds)



Please look at it and tell me what I'm missing as it it is preventing me from progressing and I'm running out of time.

Thanks

Scottaidh

Code 2 - working perfectly
import os, sys, arcpy
arcpy.env.overwriteOutput = True
points = r"H:\working\Findsites.gdb\cities"
lines = r"H:\working\Findsites.gdb\interstates"
polys = r"H:\working\Findsites.gdb\counties"
outputFC = r"H:\working\Findsites.gdb\Outlist"
NearestInterstate = r"H:\working\Findsites.gdb\NearRoad"
            
arcpy.MakeFeatureLayer_management(points,"pointsL")
arcpy.SelectLayerByLocation_management("pointsL", "WITHIN_A_DISTANCE", lines, "30 KILOMETERS", "NEW_SELECTION")
arcpy.SelectLayerByAttribute_management("pointsL", "SUBSET_SELECTION", "UNIVERSITY = 1 AND CRIME_INDE <= 0.02")
arcpy.MakeFeatureLayer_management(polys,"polysL")
arcpy.SelectLayerByAttribute_management("polysL", "NEW_SELECTION", "AGE_18_64 >= 25000 AND NO_FARMS87 >= 500")
arcpy.SelectLayerByLocation_management("pointsL", "INTERSECT", "polysL", "", "SUBSET_SELECTION")
arcpy.CopyFeatures_management("pointsL", outputFC)
    
print arcpy.GetMessages()


Code 1 - generic version not working
# 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
# arcpy.AddMessage(path + "ok")
points = arcpy.GetParameterAsText(1) # cities Layer is cities Feature Layer cities.shp
lines = arcpy.GetParameterAsText(2) # interstatesL Layer is the interstates Feature Layer interstates.shp
polys = arcpy.GetParameterAsText(3) # countiesL is counties Feature Layer counties.shp
outputLayer = arcpy.GetParameterAsText(4) # OutList.shp
distance = arcpy.GetParameterAsText(5) # min distance to road linear unit 30km
crimeField = arcpy.GetParameterAsText(6) # crimeField is fieldname 'CRIME_INDE' SQL expression
crimefieldindex = arcpy.GetParameterAsText(7) # crime index is CRIME_INDE and is a Double integer 0.02
whereClause = "{0} = '{1}'".format(arcpy.AddFieldDelimiters("pointsL", crimeField), crimefieldindex)
universityField = arcpy.GetParameterAsText(8) # universityField is fieldname 'UNIVERSITY' SQL expression
universityfieldindex = arcpy.GetParameterAsText(9) # universityfieldindex is the UNIVERSITY field and is DOUBLE integer 1
whereClause2 = "{0} = '{1}'".format(arcpy.AddFieldDelimiters("pointsL", universityField), universityfieldindex)

# error trapping measures

arcpy.MakeFeatureLayer_management(points, "pointsL")
# select layer by location to interstates
arcpy.SelectLayerByLocation_management("pointsL", "WITHIN_A_DISTANCE", lines, "30 KILOMETERS", "NEW_SELECTION")
 
# from selection above select layer by attribute select "CRIME_IND" <= 0.02 AND "UNIVERSITY" = 1
arcpy.SelectLayerByAttribute_management("pointsL", "SUBSET_SELECTION", whereClause + " <= " + crimefieldindex + " AND " + whereClause2 + " = " + universityfieldindex)
  
# make counties feature layer
arcpy.MakeFeatureLayer_management(polys,"polysL")
 
# from selection above select cities intersecting counties
arcpy.SelectLayerByLocation_management("pointsL", "INTERSECT", "polysL", "", "SUBSET_SELECTION")
  
# save selected features
arcpy.CopyFeatures_management("pointsL", outputLayer)
 
print arcpy.GetMessages()

Outcomes