Select to view content in your preferred language

Get Count Equal Zero

2300
5
07-19-2017 02:08 AM
DaveMarkey
Regular Contributor

I am wanting to execute a simple piece of Python code.

If selected features is one or more --> copy selected features to a predefined geodatabase

Else Print "No Features Selected"

But I am finding the GetCount selects all features within a featureclass when there is none pre-selected. How do I do this in Python? I am running the script from within ArcMap so the selected features within the data view window is processed.

Example script below:

# Import arcpy module
import arcpy
from arcpy import env
import sys
import os

arcpy.SetProgressorLabel("Processing Annotation Owner Layer")
arcpy.MakeFeatureLayer_management(r"Annotation\AnnotationOwner", "Layer")
selCountResult = arcpy.GetCount_management("Layer")
selCount = int(selCountResult.getOutput(0))
arcpy.AddMessage ("{0} Features Selected from Annotation Owner Layer".format(str(selCount)))
if selCount > 0:
    arcpy.CopyFeatures_management(r"Annotation\Maritime.DBO.AnnotationMisc", os.path.join(GDBName, "AnnotationMisc"))
    arcpy.AddMessage ("{0} Features Copied From Annotation Owner Layer".format(str(selCount)))
else:
    arcpy.AddMessage("Nothing Copied from Annotation Owner Layer")
arcpy.Delete_management("Layer","")

0 Kudos
5 Replies
XanderBakker
Esri Esteemed Contributor

First of all the arcpy.GetCount_management tool will count the selected features in a table if you have a selection or return the total number of features in case you don't have a selection.

There are two reasons that I can think of. The first would be to check if you have an output extent defined in the geoprocessing environment settings: 

This tool honors the Output Extent environment. Only those features that are within or intersect the Output Extent environment setting will be counted.

The other aspect is related to using annotations. However, I just did a test with a selection of feature-linked annotation and the the GetCount tools returns the correct result.

DaveMarkey
Regular Contributor

Hi.

As the GetCount gets all records when none are preselected I looked to achieve this a different way. I looked into the getSelectionSet() option but couldn't understand how it works. I successfully used the FIDSet in arcpy.Describe to get my script to work how I want. Below is of my script. I run this script from within ArcMap so selected features can be processed.

arcpy.MakeFeatureLayer_management(r"Annotation\Maritime.DBO.AnnotationMisc", "Layer")
desc = arcpy.Describe("Layer")
desc.FIDSet
if desc.FIDSet <> "":
    selCountResult = arcpy.GetCount_management("Layer")
    selCount = int(selCountResult.getOutput(0))
    arcpy.AddMessage ("{0} Features Selected from Annotation Misc Layer".format(str(selCount)))
    arcpy.CopyFeatures_management(r"Annotation\Maritime.DBO.AnnotationMisc", os.path.join(GDBName, "AnnotationMisc"))
    arcpy.AddMessage ("{0} Features Copied From Annotationn Misc Layer".format(str(selCount)))
else:
    arcpy.AddMessage("Nothing Selected from Annotation Misc Layer")
arcpy.Delete_management("Layer","")

0 Kudos
RhettZufelt
MVP Notable Contributor

Take a look at the FIDset in Describe.

lyrs_NET = arcpy.mapping.ListLayers(mxd, "*_NET", df)
for lay in lyrs_NET:
    selCount = len([int(fid) for fid in arcpy.Describe(lay).fidset.split(";") if fid != ''])‍‍

If selCount ==0, no selection.  Otherwise, selCount is equal to the number of items selected.

R_

JoshuaBixby
MVP Esteemed Contributor

In addition to the other responses, which are both helpful comments, you can use getSelectionSet () from Layer—Help | ArcGIS Desktop  to determine selected records of a layer.

RhettZufelt
MVP Notable Contributor

Apparently, this was added at 10.3.  We are still on the Utilities/Telecom model (10.2.1), and doesn't seem to be available.

Something to look forward to

R_

0 Kudos