Select to view content in your preferred language

Check if FeaturLayer has Selection FAIL

814
3
11-02-2010 10:58 AM
GregCorradini
Emerging Contributor
Hello,
I'm trying to find a FeatureLayer property or function that returns the number of features selected after I run a gp.SelectLayerByAttribute() or gp.SelectLayerByLocation() using the Python geoprocessing API.

This should be a simple thing to achieve (It should be!) with the geoprocessing API (like it is with C# ArcObjects API). But now I'm starting to wonder if there's not an easy way to do this after reading the geoprocessing object model diagram. Hopefully someone can show me the light and where I've strayed.

I was hoping that the selection functions would return an integer giving feedback on what just happened (like 0 for nothing was selected would be useful). Instead they just return the name of the layer even when I know I'm selecting something that doesn't exist in the Layer. Then I thought the Layer or Describe object would have a .HasSelected property somewhere. Nope.

If nothing exists, then the one known laborious/ridiculous workaround to test if something was selected would be as follows:

1) run a SelectLayerByAttribute or SelectLayerByLocation on a Layer
2) take that Layer (hopefully with a selection) and create a new Layer with gp.MakeFeatureLayer(). The goal here being to create a temp layer with our selected feature
3) put a SearchCursor() on the temp Layer and run through it to see if there are any rows that are not None types.

Ideas? I'm using ArcGIS 9.3.1
0 Kudos
3 Replies
LoganPugh
Frequent Contributor
result = gp.GetCount_management(featureLayer)
count = int(result)
0 Kudos
ChrisSnyder
Honored Contributor
Or even:

if int(gp.GetCount_management(featureLayer).getoutput(0)) > 0:
   do this
else:
   do that


Sometimes it's usefull to compare the selection count with the total count:

if int(gp.GetCount_management(gp.describe(featureLayer).catalogpath).getoutput(0)) > int(gp.GetCount_management(featureLayer).getoutput(0)):
   do this
else:
   do that


And then its sometimes usefull to get the OBJECTIDs of the selected features (BTW there has to be a selection in the featurelayer not just a SQL definition):

selectedOidList = [int(oid) for oid in gp.describe(featureLayer).fidset.split(";")]
0 Kudos
GregCorradini
Emerging Contributor
Thanks Logan,
That's the ticket! It's not on the object model diagram. Thanks for the heads up
0 Kudos