scottaidh

if else loop problem

Discussion created by scottaidh on Apr 24, 2013
Latest reply on Apr 26, 2013 by curtvprice
Hi,

I have written a script for toolbox that selects cities and outputs them to a table (citylist).  It all works apart from the if....else part. 

if: maximim kilometres to interstates > 0:
    subselect cities within x km of interstates
else:
    use citylist

It works fine if users input a figure > 0 but not if it is <= 0 or text - then the program gets stuck in a loop and doesn't stop.

Any help as to where I'm going wrong would be appreciated. 

[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
cities2 = "cities2"
citiesL = "citiesL"
counties = arcpy.GetParameterAsText(2) # counties Layer is counties Feature Layer counties.shp
counties2 = "counties2"
countiesL ="countiesL"
interstates = arcpy.GetParameterAsText(3) # interstates Layer is Feature Layer interstates.shp
interstates2 = "interstates2"
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
citylist = arcpy.GetParameterAsText(14) # narrowed down cities list temporary
citylistL = "citylistL" # the temporary feature layer of the generated cityList feature class
outputLayer = arcpy.GetParameterAsText(15) # ootput

# make cities feature layer and remove unnecessary fields
arcpy.CopyFeatures_management(cities, cities2)
arcpy.MakeFeatureLayer_management(cities2, citiesL)
arcpy.DeleteField_management(citiesL, ["POPULATION", "TOTAL_CRIM"])

if crimeField == "CRIME_INDE" and universityField == "UNIVERSITY":
arcpy.SelectLayerByAttribute_management(citiesL, "NEW_SELECTION", whereClause + " AND " + whereClause2)
arcpy.AddMessage("Maximum crime index threshold and cities with universities selected")
elif crimeField == "CRIME_INDE" and universityField <> "UNIVERSITY":
arcpy.SelectLayerByAttribute_management(citiesL, "NEW_SELECTION", whereClause)
arcpy.AddMessage("Maximum crime index threshold selected")
arcpy.DeleteField_management(citiesL, "UNIVERSITY")
arcpy.CopyFeatures_management(citiesL, citylist)
elif crimeField <> "CRIME_INDE" and universityField == "UNIVERSITY":
arcpy.SelectLayerByAttribute_management(citiesL, "NEW_SELECTION", whereClause2)
arcpy.AddMessage("Cities with a university selected")
arcpy.DeleteField_management(citiesL, "CRIME_INDE")
arcpy.CopyFeatures_management(citiesL, citylist)
elif crimeField <> "CRIME_INDE" and universityField <> "UNIVERSITY":
arcpy.AddMessage("No crime threshold or cities with universities selected")
arcpy.DeleteField_management(citiesL, ["CRIME_INDE", "UNIVERSITY"])
arcpy.CopyFeatures_management(citiesL, citylist)

# make counties feature layer and remove unnecessary fields
arcpy.CopyFeatures_management(counties, counties2)
arcpy.MakeFeatureLayer_management(counties2, countiesL)
arcpy.DeleteField_management(countiesL, ["AREA", "PERIMETER", "POP1990", "POP_SQMILE", "SQ_MILES", "GAVPRIMARY", "X", "Y", "X_1", "Y_1"])

if workforceField == "AGE_18_64" and farmField == "NO_FARMS87":
arcpy.SelectLayerByAttribute_management(countiesL, "NEW_SELECTION", whereClause3 + " AND " + whereClause4)
arcpy.AddMessage("Minimum workforce and proximity to farms selected")
arcpy.CopyFeatures_management(countiesL, "countylist")
elif workforceField == "AGE_18_64" and farmField <> "NO_FARMS87":
arcpy.SelectLayerByAttribute_management(countiesL, "NEW_SELECTION", whereClause3)
arcpy.AddMessage("Minimum workforce selected")
arcpy.DeleteField_management(countiesL, "NO_FARMS87")
arcpy.CopyFeatures_management(countiesL, "countylist")
elif workforceField <> "AGE_18_64" and farmField == "NO_FARMS87":
arcpy.SelectLayerByAttribute_management(countiesL, "NEW_SELECTION", whereClause4)
arcpy.AddMessage("Proximity to farms selected")
arcpy.DeleteField_management(countiesL, "AGE_18_64")
arcpy.CopyFeatures_management(countiesL, "countylist")
elif workforceField <> "AGE_18_64" and farmField <> "NO_FARMS87":
arcpy.AddMessage("No minimun workforce or proximity to farms selected")
arcpy.DeleteField_management(countiesL, ["AGE_18_64", "NO_FARMS87"])
arcpy.CopyFeatures_management(countiesL, "countylist")

# from selection above select cities intersecting counties
arcpy.SelectLayerByLocation_management(citiesL, "INTERSECT", "countylist", "", "SUBSET_SELECTION")

# create a city list if maximum distance to interstates selected
if maxKmInterstate >0:
arcpy.AddMessage("Maximum distance to interstates selected")
# copy interstates feature class so original is unaffected
arcpy.CopyFeatures_management(interstates, interstates2)
# make interstates feature layer
arcpy.MakeFeatureLayer_management(interstates2, interstatesL)
# further select cities within maximum interstate distance
arcpy.SelectLayerByLocation_management(citiesL, "WITHIN_A_DISTANCE", interstatesL, maxKmInterstate, "SUBSET_SELECTION")
# copy to citylist
arcpy.CopyFeatures_management(citiesL, citylist)
# create a feature layer from new citylist
arcpy.MakeFeatureLayer_management(citylist, citylistL)
# Generate NearTable_analysis to find closest interstate distance
arcpy.GenerateNearTable_analysis(citylistL, interstates, nearestInterstate, maxKmInterstate)
# join nearest interstate column to citylist
arcpy.JoinField_management(citylistL, "FID", nearestInterstate, "IN_FID", "NEAR_DIST")
# divide distance to interstates by 1000 to convert to kilometres
# arcpy.CalculateField_management(citylistL, "NEAR_DIST", '!NEAR_DIST!/1000')
# spatially join the narrowed down city/road and counties layers remove unwanted fields and rename others
arcpy.SpatialJoin_analysis(citylistL, countiesL, outputLayer, "#", "#", "")
else:
arcpy.AddMessage("No maximum distance to interstates selected")
# create city list where no maximum interstate distance selected
arcpy.CopyFeatures_management(citiesL, citylist)
# create a feature layer from new citylist
arcpy.MakeFeatureLayer_management(citylist, citylistL)
# spatially join the narrowed down city/road and counties layers remove unwanted fields and rename others
arcpy.SpatialJoin_analysis(citylistL, countiesL, outputLayer, "#", "#", "")
# update citylist with relevant fields from counties join

# Set data path
intable = "H:/working/CityTable.shp"

# Get the fields from the input
fields= arcpy.ListFields(intable)

# Create a fieldinfo object
fieldinfo = arcpy.FieldInfo()

# Iterate through the fields and set them to fieldinfo
for field in fields:
    if field.name == "NAME":
        fieldinfo.addField(field.name, "CITYNM", "VISIBLE", "")
    elif field.name == "CRIME_INDE":
        fieldinfo.addField(field.name, "CRIMEINDX", "VISIBLE", "")
    elif field.name == "UNIVERSITY":
        fieldinfo.addField(field.name, field.name, "VISIBLE", "")
    elif field.name == "NEAR_DIST":
        fieldinfo.addField(field.name, "INTRSTkm", "VISIBLE", "")
    elif field.name == "NAME_1":
        fieldinfo.addField(field.name, "COUNTYNM", "VISIBLE", "")
    elif field.name == "AGE_18_64":
        fieldinfo.addField(field.name, "WORKFRCE", "VISIBLE", "")
    elif field.name == "NO_FARMS87":
        fieldinfo.addField(field.name, "FARMS", "VISIBLE", "") 
#elif field.name == "Join_Count":
    #    fieldinfo.addField(field.name, field.name, "HIDDEN", "")
    elif field.name == "TARGET_FID":
        fieldinfo.addField(field.name, field.name, "HIDDEN", "")
    elif field.name == "OBJECTID":
        fieldinfo.addField(field.name, field.name, "HIDDEN", "")
    elif field.name == "Shape_Leng":
        fieldinfo.addField(field.name, field.name, "HIDDEN", "")
    elif field.name == "Shape_Le_1":
        fieldinfo.addField(field.name, field.name, "HIDDEN", "")
    elif field.name == "Shape_Area":
        fieldinfo.addField(field.name, field.name, "HIDDEN", "")
    elif field.name == "ID":
        fieldinfo.addField(field.name, field.name, "HIDDEN", "")

# The created crime_view layer will have fields as set in fieldinfo object
arcpy.MakeTableView_management(intable, "CityView", "", "", fieldinfo)

# To persist the layer on disk make a copy of the view
arcpy.CopyRows_management("CityView", "H:/working/CityView.dbf")
 
print arcpy.GetMessages()
[\CODE]

Outcomes