Hide Unselected Features

704
6
01-20-2013 04:11 PM
babakkasraei
New Contributor II
I have used the following codes to delete the selected features:


if int(arcpy.GetCount_management(layer).getOutput(0)) > 0:                 

       arcpy.DeleteFeatures_management(layer)



I have two questions

1- What if I want to deleted unselected features? Can I use the following syntax?


 if int(arcpy.GetCount_management(layer).getOutput(0)) = 0:             

       arcpy.DeleteFeatures_management(layer)



2- I don't want to delete the features. I only want to hide them. What syntax should I use instead of

            

       arcpy.DeleteFeatures_management(layer)

 


3- How about if I want to show them again. What syntax must be used?

I will appreciate if anybody help me.

Best Regards

Babak
Tags (2)
0 Kudos
6 Replies
KimOllivier
Occasional Contributor III
The selected features definition is part of the layer properties.
All you are doing is counting the number selected before you delete them.
Selected features from a featureclass can be defined by a definition expression and a selection set that is arbitrary

There is an arcpy.Describe() property FIDSet that will enable you to get a list of selected FID's that you can build into a set
then invert the set and make a new layer definition, but it is a bit geeky. You  have to manipulate a list of FIDs and use an SQL query with a list.
0 Kudos
babakkasraei
New Contributor II
The selected features definition is part of the layer properties.
All you are doing is counting the number selected before you delete them.
Selected features from a featureclass can be defined by a definition expression and a selection set that is arbitrary

There is an arcpy.Describe() property FIDSet that will enable you to get a list of selected FID's that you can build into a set
then invert the set and make a new layer definition, but it is a bit geeky. You  have to manipulate a list of FIDs and use an SQL query with a list.


Thank you Kim I will work on it.

Best Regards
0 Kudos
babakkasraei
New Contributor II
Using the describe function will be something like this.


# describe the feature layer to access the the selected set
desc = arcpy.Describe(layer)

# FIDSet will contain the selected features
selectedFids = desc.FIDSet

# If there are selectedFids (a selection set), write them to a new feature
# class in the current workspace.
if len(selectedFids) > 0:
 do something



Please let me know how you want to invert it now.

Thanks
0 Kudos
MathewCoyle
Frequent Contributor
Using the describe function will be something like this.


# describe the feature layer to access the the selected set
desc = arcpy.Describe(layer)

# FIDSet will contain the selected features
selectedFids = desc.FIDSet

# If there are selectedFids (a selection set), write them to a new feature
# class in the current workspace.
if len(selectedFids) > 0:
 do something



Please let me know how you want to invert it now.

Thanks


You'll have to create a layer object to reference the definitionQuery parameter if you haven't already. Then something like this.

oidField = desc.OIDFieldName
queryList = selectedFids.replace(';', ',')
oidDelim = arcpy.AddFieldDelimiters(layer, oidField)
layerObject.definitionQuery = '{0} in ({1})'.format(oidDelim, queryList)
arcpy.RefreshActiveView()
0 Kudos
babakkasraei
New Contributor II
So It will finally become something like this.


# describe the feature layer to access the the selected set
desc = arcpy.Describe(layer)

# FIDSet will contain the selected features
selectedFids = desc.FIDSet

# If there are selectedFids (a selection set), write them to a new feature
# class in the current workspace.
if len(selectedFids) > 0:

    # Here I create a new FC from my selected FIDS
 arcpy.CopyFeatures_management(layer,"AnewFC")

queryList = selectedFids.replace(';', ',')
oidDelim = arcpy.AddFieldDelimiters(layer, oidField)
layerObject.definitionQuery = '{0} not in ({1})'.format(oidDelim, queryList)
arcpy.RefreshActiveView() 



It is amazing Thank you. However, the last lines is a little bit confusing for me. Would you please add a little descriptions to them.

For instance I still don't know how it happens to hide the unselected features.

I appreciate your help.
0 Kudos
babakkasraei
New Contributor II
You'll have to create a layer object to reference the definitionQuery parameter if you haven't already. Then something like this.

oidField = desc.OIDFieldName
queryList = selectedFids.replace(';', ',')
oidDelim = arcpy.AddFieldDelimiters(layer, oidField)
layerObject.definitionQuery = '{0} in ({1})'.format(oidDelim, queryList)
arcpy.RefreshActiveView()


Hi Kim

I have created the following codes for my "Hide Unselected Features" button function in My Tkinter as you instructed me, but they don't do anything.

Would you have a look on them. Why don't they work?


def button3Click(self, selection):
        index = self.listbox.curselection()               
        label = self.listbox.get(index) 

        # Local variables:
        layer = label

        # describe the feature layer to access the the selected set
        desc = arcpy.Describe(layer)

        # FIDSet will contain the selected features
        selectedFids = desc.FIDSet

        # If there are selectedFids (a selection set), write them to a new feature
        # class in the current workspace.
        if len(selectedFids) > 0:
           oidField = desc.OIDFieldName
           queryList = selectedFids.replace(';', ',')
           oidDelim = arcpy.AddFieldDelimiters(layer, oidField)
           layerObject.definitionQuery = '{0} in ({1})'.format(oidDelim, queryList)
           arcpy.RefreshActiveView()



ln these codes layer is an object layer which is chosen from a listbox.

Thank you for your help for doing my first button. It was a great help.

Please help me for this button too.

Best Regards
0 Kudos