Select to view content in your preferred language

arcpy function to test for selected features

5689
8
05-18-2011 08:15 AM
JoshuaJohnson1
Deactivated User
Hello!

I am searching for a function that would return a Boolean value if there are selected features in an mxd.

I know you can use arcpy.SelectLayerByAttribute_management(Layer, "CLEAR_SELECTION") to clear selections for a layer but I don't want to clear just test.  The maps were exported to pdf and may have the selected features showing on the pdf docs.  I need a function to  test which mxd's have selected features and I've got about 1600 maps.

Any help would be appreciated.

Thanks!
Josh
Tags (2)
0 Kudos
8 Replies
TedCronin
MVP Honored Contributor
You could try GetCount.  This was at 9.3.1 with arcgisscripting


result = gp.GetCount(ParcelInfo) #Total Anno Count
    gp.SelectLayerByAttribute_management(SpecAnno, SubSel, AC[0])
    result = gp.GetCount(ACParcelInfo)
    getSel = result.GetOutput(0)
    if getSel >= 1:
         do something
    elif getSel == 0:
        do something else
0 Kudos
ChrisSnyder
Honored Contributor
Two methods I use (v9.3 code BTW, but easy to convert to arcpy):

For actual selections (must be a selection made by SelectByAttributes or SelectByLoactions and not a definition query):

selectionCount = len(gp.describe(lyr).fidset.split(";"))

But for layers that might have a differenet feature count than the actual feature class they are based off (via a definition query or some SQL used by the MakeFeatuerLayer tool):

selectionCount = int(gp.GetCount_management(gp.describe(lyr).catalogpath).getoutput(0)) - int(gp.GetCount_management(lyr).getoutput(0))
0 Kudos
JoshuaJohnson1
Deactivated User
Thanks to you both.  Does arcgisscripting even work with v10.0?

Thanks!
Josh
0 Kudos
ChrisSnyder
Honored Contributor
Does arcgisscripting even work with v10.0?


It does if you use the default version of Python that is auto-installed with v10 (and you don't try to monkey with or upgrade it).

Although I think(?) in this case all you have to do is change "gp" to "arcpy":

selectionCount = len(arcpy.describe(lyr).fidset.split(";"))
#or
selectionCount = int(arcpy.GetCount_management(gp.describe(lyr).catalogpath).getoutput(0)) - int(arcpy.GetCount_management(lyr).getoutput(0)) 
0 Kudos
TedCronin
MVP Honored Contributor
arcgisscripting.create(10.0).  You can migrate when you want to, basically at 10, which is pretty cool.  I have lots of pre 10 scripts in production, and just port when I have the time or need arcpy.
0 Kudos
GreggPitts
Deactivated User
Hi everyone, I've got a similar situation... I've tried the above mentioned feature selection detection scripts but they do not work on my ArcMap 10.2. Or maybe I'm not puting them in right?

I've got a button that when pressed performs two field calculations - one on a polygon layer then another on a arc layer.
However I want to put in a failsafe so it will not do anything if nothing is selected since this morning I accidentally had a polygon selected and there was no arc selected so it field calc'ed the entire Kerb_Channel arc feature class with one value!!!
Basically they're polygon traffic islands with arc kerb lines around them, however the prefab traffic islands must not have a kerb line because there is no seperate kerb. I've used the ArcGIS AddIn Assistant to make the button with this Arcpy script which I copied and pasted from an exported to Python Model Builder model:

import arcpy
import pythonaddins

# Local variables:
Traffic_Islands = "Island+Kerb Topology\\Traffic_Islands"
Kerb_Channel = "Island+Kerb Topology\\Kerb_Channel"

class ButtonClass1(object):
    """Implementation for SetKerbType_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
       # Some kind of selection detection code here for the Kerb_Channel layer since a polygon will always be selected
        arcpy.CalculateField_management(Traffic_Islands, "KERB_TYPE", "\"SM3\"", "VB", "")
        arcpy.CalculateField_management(Kerb_Channel, "KERBTYPE", "\"SM3\"", "VB", "")


Also, is there a PDF or site somewhere that has the Arcpy procedures and functions and what they do etc like there is for normal Python? I've googled it and I can't find anything with a list of Arcpy procedures/functions with what they do and how to use them.  Something like SharpDev's inline help would be awesome.



Thank you,
Gregg
0 Kudos
ChrisSnyder
Honored Contributor
How about something like:

Traffic_Islands = "Island+Kerb Topology\\Traffic_Islands"
Kerb_Channel = "Island+Kerb Topology\\Kerb_Channel"
class ButtonClass1(object):
   """Implementation for SetKerbType_addin.button (Button)"""
   def __init__(self):
   self.enabled = True
   self.checked = False
   def onClick(self):
      if len(arcpy.Describe(Traffic_Islands).fidset.split(";")) > 0:
         arcpy.CalculateField_management(Traffic_Islands, "KERB_TYPE", "\"SM3\"", "VB", "")
      else:
         #tell the user they are doing something wrong
      if len(arcpy.Describe(Kerb_Channel).fidset.split(";")) > 0:
         arcpy.CalculateField_management(Kerb_Channel, "KERBTYPE", "\"SM3\"", "VB", "")
      else:
         #tell the user they are doing something wrong
0 Kudos
ChrisSnyder
Honored Contributor
Okay your right... a better way:

selectedCount = len([int(fid) for fid in arcpy.Describe(layerName).fidset.split(";") if fid != ''])
0 Kudos