Select to view content in your preferred language

Select from user input variable

2945
25
Jump to solution
09-10-2012 08:27 AM
NoahHuntington
Deactivated User
Please help.  Unable to make selection.

Here is what I have....

# Model selects parcel from user input import arcpy  # Assign variable to the feature class featureClass = "G:\\PRAD\\PRAD_v10.1.gdb\\tax_acct"  # Assign variable to users input input = arcpy.GetParameterAsText(0)  # Make a feature layer from feature class arcpy.MakeFeatureLayer_management(featureClass, "layer")       # Apply a selection to the feature layer  arcpy.SelectLayerByAttribute_management("layer", "NEW_SELECTION", '"'+str(input)+'"') 
Tags (2)
0 Kudos
25 Replies
NoahHuntington
Deactivated User
Try adding these lines at the end of your script. This will print how many features are selected, and then refresh the view to make those selections visible.

selection = int(arcpy.GetCount_management("layer").getOutput(0))
arcpy.AddMessage("%s features selected" % (selection))
arcpy.RefreshActiveView()


I can see 1 selected in results window but still no effect on dataframe? See attached...
0 Kudos
NoahHuntington
Deactivated User
[ATTACH=CONFIG]17579[/ATTACH]
Try adding these lines at the end of your script. This will print how many features are selected, and then refresh the view to make those selections visible.

selection = int(arcpy.GetCount_management("layer").getOutput(0))
arcpy.AddMessage("%s features selected" % (selection))
arcpy.RefreshActiveView()


I can see 1 selected in results window but still no effect on dataframe? See attached...
0 Kudos
MathewCoyle
Honored Contributor
[ATTACH=CONFIG]17579[/ATTACH]

I can see 1 selected in results window but still no effect on dataframe? See attached...


That is because you create a temporary layer called "layer" that you are basing your selections on. I don't see that layer in your TOC.

Maybe you should describe what you want to accomplish since it looks like you are going about this the wrong way.
0 Kudos
NoahHuntington
Deactivated User
That is because you create a temporary layer called "layer" that you are basing your selections on. I don't see that layer in your TOC.

Maybe you should describe what you want to accomplish since it looks like you are going about this the wrong way.


I am trying to create a simple tool to collect user input and zoom to selected feature.  ideally would like to select from fc in toc.  Any suggestions?

...maybe a search cursor?
0 Kudos
MathewCoyle
Honored Contributor
I am trying to create a simple tool to collect user input and zoom to selected feature.  ideally would like to select from fc in toc.  Any suggestions?


Well first of all you don't have feature classes in your TOC, you have layers pointing to your feature classes on disk. These are the objects you make selections on, you cannot make selections on feature classes.

Try running this as your script. You could also add another input parameter to select the layer manually.
import arcpy

def main():
    arcpy.AddMessage("Starting")

    # Get and set variables
    job_num = arcpy.GetParameterAsText(0)
    layer_name = "tax_acct"
    df_name = "Layers"

    arcpy.AddMessage(job_num)

    # Define mxd, df and lyr objects
    mxd = arcpy.mapping.MapDocument("CURRENT")
    df = arcpy.mapping.ListDataFrames(mxd, df_name)[0]
    lyr = arcpy.mapping.ListLayers(mxd, layer_name, df)[0]

    arcpy.AddMessage(lyr.name)

    # Create where clause for selection
    query = "Job_Num = '%s'" % (job_num)

    arcpy.AddMessage(query)

    arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", query)

    # Get number of selected features
    selection = int(arcpy.GetCount_management(layer_name).getOutput(0))

    arcpy.AddMessage("%s features selected" % (selection))

    # Set df extent to layer selection
    df.extent = lyr.getSelectedExtent()
    arcpy.RefreshActiveView()

    arcpy.AddMessage("Completed")

main()
0 Kudos
NoahHuntington
Deactivated User
Well first of all you don't have feature classes in your TOC, you have layers pointing to your feature classes on disk. These are the objects you make selections on, you cannot make selections on feature classes.

Try running this as your script. You could also add another input parameter to select the layer manually.
import arcpy  def main():     arcpy.AddMessage("Starting")      # Get and set variables     job_num = arcpy.GetParameterAsText(0)     layer_name = "tax_acct"     df_name = "Layers"      arcpy.AddMessage(job_num)      # Define mxd, df and lyr objects     mxd = arcpy.mapping.MapDocument("CURRENT")     df = arcpy.mapping.ListDataFrames(mxd, df_name)[0]     lyr = arcpy.mapping.ListLayers(mxd, layer_name, df)[0]      arcpy.AddMessage(lyr.name)      # Create where clause for selection     query = "Job_Num = '%s'" % (job_num)      arcpy.AddMessage(query)      arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", query)      # Get number of selected features     selection = int(arcpy.GetCount_management(layer_name).getOutput(0))      arcpy.AddMessage("%s features selected" % (selection))      # Set df extent to layer selection     df.extent = lyr.getSelectedExtent()     arcpy.RefreshActiveView()      arcpy.AddMessage("Completed")  main()



That was perfect!!!  Your fast too!  Just a couple questions?  What was going on with the addmessages methods? Also some of these features have 1, 2, or 3 possible job numbers associated with them, as in fields Job_Num2, Job_Num3, so on.... Would there be a way add multiple fields to the query?  Job_Num, or Job_Num2, or Job_Num3..
0 Kudos
MathewCoyle
Honored Contributor
That was perfect!!!  Your fast too!  Just a couple questions?  What was going on with the addmessages methods? Also some of these features have 1, 2, or 3 possible job numbers associated with them, as in fields Job_Num2, Job_Num3, so on.... Would there be a way add multiple fields to the query?  Job_Num, or Job_Num2, or Job_Num3..


The addmessages are there just to check your variables to make sure they are what you are expecting. They are unnecessary for the script itself and more for trouble shooting.

If you are selecting from multiple fields, are they all the same value from these fields? Or do you want unique values from each field?
Eg
Job_Num = "first" and Job_Num2 = "first"
# or
Job_Num = "first" and Job_Num2 = "second"
0 Kudos
NoahHuntington
Deactivated User
The addmessages are there just to check your variables to make sure they are what you are expecting. They are unnecessary for the script itself and more for trouble shooting.

If you are selecting from multiple fields, are they all the same value from these fields? Or do you want unique values from each field?
Eg
Job_Num = "first" and Job_Num2 = "first"
# or
Job_Num = "first" and Job_Num2 = "second"


They exist b/c we have revisited parcels sometimes multiple times each with a new job number.  Added field to table to handle new job number.  I am not sure the following is correct, where should this query take place.


    # Get and set variables
    Job_Num = "first" and Job_Num2 = "second" = arcpy.GetParameterAsText(0)
    layer_name = "tax_acct"
    df_name = "Layers"
0 Kudos
MathewCoyle
Honored Contributor
For each field you will need a new parameter added to your tool. There are a couple ways to go about it, and it gets a little more complicated if you want it to be dynamic (eg a user can select any number of fields to put in the query).
    job_num = arcpy.GetParameterAsText(0)
    job_num2 = arcpy.GetParameterAsText(1)
    job_num3 = arcpy.GetParameterAsText(2)
    ...
    # Create where clause for selection
    query = ""
    query_list = [("Job_Num", job_num), ("Job_Num2", job_num2), ("Job_Num3", job_num3)]
    for field_name, variable in query_list:
        if query == "":                                              
            query = "%s = '%s'" % (field_name, variable)            
        else:                                                        
            query = query + " and %s = '%s'" % (field_name, variable)


Something like that should get you on track. May require some tinkering to make it work for you.
0 Kudos
NoahHuntington
Deactivated User
For each field you will need a new parameter added to your tool. There are a couple ways to go about it, and it gets a little more complicated if you want it to be dynamic (eg a user can select any number of fields to put in the query).
    job_num = arcpy.GetParameterAsText(0)
    job_num2 = arcpy.GetParameterAsText(1)
    job_num3 = arcpy.GetParameterAsText(2)
    ...
    # Create where clause for selection
    query = ""
    query_list = [("Job_Num", job_num), ("Job_Num2", job_num2), ("Job_Num3", job_num3)]
    for field_name, variable in query_list:
        query = query + "%s = '%s' " % (field_name, variable)


Something like that should get you on track. May require some tinkering to make it work for you.


Ok. I will see what I can do with that.  Thanks for all your help!
0 Kudos