Select Records

382
4
09-07-2012 01:30 PM
NoahHuntington
Occasional Contributor
I am attempting to prepare script which will be used for script tool.
First, I am only interested in selecting features then intend on appending zoom to selected routine to the end of code shown here. 
Having said that I may not even be using the correct tool (select_analysis) but am unsure of one that allows for selection only.

Regardless if I could nail down the syntax I am confident I could switch tools effectively.  Could someone suggest a better tool and

help with debugging.

Thanks in advance...


# This script runs the Select tool. The user supplies the input
import arcpy

# Get the input parameters for the Select tool
infeatures = arcpy.GetParameterAsText(0)
userinput = arcpy.GetParameterAsText(1)
    
# Run the Select tool
arcpy.Select_analysis(infeatures, "NEW_SELECTION", '\Job_Num\" = %s' % userinput)
Tags (2)
0 Kudos
4 Replies
ChrisSnyder
Regular Contributor III
It depends on what you want to do with the selected set, but typically I use the MakeFeatureLayer tool to apply rapid selections (runs faster than the SelectLayerByAttribute tool). The Select tool actually creates a new featureclass on disk, so has the additional overhead of writting to disk...

The MakeFeatureLayer tool creates a "feature layer" that is an in memory reference to the selcted features on disk (but does not actualy store the geometry/attributes in RAM). Then you can use the feature layer for input into other tools just as you would a feature class.

For example:

arcpy.MakeFeatureLayer_management(infeatures, "my_feature_layer", "Job_Num = " + str(userinput))
arcpy.Clip_analysis("my_feature_layer", ....)
0 Kudos
NoahHuntington
Occasional Contributor
It depends on what you want to do with the selected set, but typically I use the MakeFeatureLayer tool to apply rapid selections (runs faster than the SelectLayerByAttribute tool). The Select tool actually creates a new featureclass on disk, so has the additional overhead of writting to disk...

The MakeFeatureLayer tool creates a "feature layer" that is an in memory reference to the selcted features on disk (but does not actualy store the geometry/attributes in RAM). Then you can use the feature layer for input into other tools just as you would a feature class.

For example:

arcpy.MakeFeatureLayer_management(infeatures, "my_feature_layer", "Job_Num = " + str(userinput))
arcpy.Clip_analysis("my_feature_layer", ....)


Thanks, this seems to be the kind of selection I am looking for.  How would I then zoom to the temp feature layer using python?
0 Kudos
ChrisSnyder
Regular Contributor III
See this Help topic: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//000v00000051000000
Try using the featurelayer's SQL 1st... You may "have to" use the SelectLayerByAttribute tool though... I am not totally familiar with the arcpy.mapping module.
0 Kudos
NoahHuntington
Occasional Contributor
See this Help topic: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//000v00000051000000
Try using the featurelayer's SQL 1st... You may "have to" use the SelectLayerByAttribute tool though... I am not totally familiar with the arcpy.mapping module.


You have been quite helpful, thanks again.  How should the variable syntax (type string?) look in the selectbyattribute tool; similar to what you did in the makefeaturelayer where clause?  still not quite there.

Here is what I have...

# This script runs the Select tool. The user supplies the input
import arcpy

#Set the workspace
arcpy.env.workspace = "G:\\PRAD\\PRAD_v10.1.gdb"

#Retrieve input
userinput = arcpy.GetParametersAsText(0)

# Make a layer from the feature class based on whereclause from input
arcpy.MakeFeatureLayer_management ("tax_acct", "lyr", "Job_Num = " + str(userinput))

# Select that layer
arcpy.SelectLayerByAttribute_management("lyr", "NEW_SELECTION")

#Zoom to selected
mxd = arcpy.mapping.MapDocument('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
df.zoomToSelectedFeatures()
df.scale = df.scale * 1.1
arcpy.RefreshActiveView()

# Clear the selection and refresh the active view
arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION")
arcpy.RefreshActiveView()
0 Kudos