Placeholder Variables in Schript

511
5
11-10-2010 11:04 AM
UlrichStroetz
New Contributor II
hi,

i wrote the following script. it is supposed to select feature classes out of a featuredataset, based on the feature class stored in another geodatabase. it works fine so far. but i want to make it more flexible.
in the following line for example:
if FeatureToPoint == "in_memory\\A_FeatureToPoint": arcpy.Select_analysis(locationstands, outfeatureclass, "\"identify\" = 'A'"); arcpy.AddMessage("worksA")


"in_memory\\A_FeatureToPoint" shall be somehow get connected to "\"identify\" = 'A'". so the two "A"s shall get connected.

i have a hard time expressing what i want. i hope you guys understands me and can help me.

thanks,

uli


here is the full code:
# Import:
import arcpy
import sys
import os
from arcpy import env



# Variables:

FeatureToPoint = arcpy.GetParameterAsText(0)
locationstands = arcpy.GetParameterAsText(1)
outfeatureclass = arcpy.GetParameterAsText(2)


if FeatureToPoint == "in_memory\\A_FeatureToPoint": arcpy.Select_analysis(locationstands, outfeatureclass, "\"identify\" = 'A'"); arcpy.AddMessage("worksA")
elif FeatureToPoint == "in_memory\\B_FeatureToPoint": arcpy.Select_analysis(locationstands, outfeatureclass, "\"identify\" = 'B'"); arcpy.AddMessage("worksB")
elif FeatureToPoint == "in_memory\\C_FeatureToPoint": arcpy.Select_analysis(locationstands, outfeatureclass, "\"identify\" = 'C'"); arcpy.AddMessage("worksC")
elif FeatureToPoint == "in_memory\\D_FeatureToPoint": arcpy.Select_analysis(locationstands, outfeatureclass, "\"identify\" = 'D'"); arcpy.AddMessage("worksD")
elif FeatureToPoint == "in_memory\\E_FeatureToPoint": arcpy.Select_analysis(locationstands, outfeatureclass, "\"identify\" = 'E'"); arcpy.AddMessage("worksE")
elif FeatureToPoint == "in_memory\\F_FeatureToPoint": arcpy.Select_analysis(locationstands, outfeatureclass, "\"identify\" = 'F'"); arcpy.AddMessage("worksF")
elif FeatureToPoint == "in_memory\\G_FeatureToPoint": arcpy.Select_analysis(locationstands, outfeatureclass, "\"identify\" = 'G'"); arcpy.AddMessage("worksG")
elif FeatureToPoint == "in_memory\\H_FeatureToPoint": arcpy.Select_analysis(locationstands, outfeatureclass, "\"identify\" = 'H'"); arcpy.AddMessage("worksH")
elif FeatureToPoint == "in_memory\\I_FeatureToPoint": arcpy.Select_analysis(locationstands, outfeatureclass, "\"identify\" = 'I'"); arcpy.AddMessage("worksI")
elif FeatureToPoint == "in_memory\\J_FeatureToPoint": arcpy.Select_analysis(locationstands, outfeatureclass, "\"identify\" = 'J'"); arcpy.AddMessage("worksJ")
elif FeatureToPoint == "in_memory\\AA_FeatureToPoint": arcpy.Select_analysis(locationstands, outfeatureclass, "\"identify\" = 'AA'"); arcpy.AddMessage("worksAA")


else: arcpy.AddMessage("doesntwork")
0 Kudos
5 Replies
NiklasNorrthon
Occasional Contributor III
Perhaps something like this:
feature_label = arcpy.GetParameterAsText(0) # String
locationstands = arcpy.GetParameterAsText(1)
outfeatureclass = arcpy.GetParameterAsText(2)

if FeatureToPoint == "in_memory\\%s_FeatureToPoint" % feature_label: 
    arcpy.Select_analysis(locationstands, outfeatureclass, "\"identify\" = '%s'" % feature_label)
    arcpy.AddMessage("works%s" % feature_label)
else:
    arcpy.AddError("No match for feature label %s" % feature_label)
0 Kudos
UlrichStroetz
New Contributor II
thanks, i will give it a try.
0 Kudos
ChrisSnyder
Regular Contributor III
Also, I bet you can drop the "\" stuff in the SQL. I think this confusing \ notation is a holdover formatting thing for shapefiles/dbf tables from v9.0 - 9.2, but not necessary in v9.3+

#so instead of this:
arcpy.Select_analysis(locationstands, outfeatureclass, "\"identify\" = '%s'" % feature_label

#try this:
arcpy.Select_analysis(locationstands, outfeatureclass, "identify = '%s'" % feature_label)
0 Kudos
UlrichStroetz
New Contributor II
okay, i am quite knew to this. just started using python last week.

i changed the code to the following
FeatureToPoint = arcpy.GetParameterAsText(0)
feature_label = arcpy.GetParameterAsText(1)
locationstands = arcpy.GetParameterAsText(2)
outfeatureclass = arcpy.GetParameterAsText(3)



if FeatureToPoint == "U:\\Intern\\Ulrich\\fairbanks_biomass\\test.gdb\\%s_FeatureToPoint" % feature_label: 
    arcpy.Select_analysis(locationstands, outfeatureclass, "identify = '%s'" % feature_label)
    arcpy.AddMessage("works%s" % feature_label)

else: arcpy.AddError("doesntwork")


and i made the "feature_label" parameter to string. but what shall i now put in there?

here is the part of my model, it might help to understand the process
0 Kudos
UlrichStroetz
New Contributor II
arcpy.Select_analysis(locationstands, outfeatureclass, "identify = '%s'" % feature_label)


it seems to work. thank you guys!
0 Kudos