3

I'm attempting to automate the process of clipping a larger feature layer into specific enumeration units. My code starts off with a fairly simple logical test to determine if new feature layer needs to be made for a particular county. If there are no features in a given county then the test is supposed to recognize this and move on to the next county. I've been using arcpy.SelectLayerByLocation_management() and arcpy.GetCount_management() in order to determine if there are in fact given features within a specific county. Both arcpy modules seem to be working, but when I use the value that GetCount_management spits out the logic doesn't. Here's my code...

    arcpy.SelectLayerByLocation_management(layer, "INTERSECT", County)
    count = arcpy.GetCount_management(layer)

    if count > 0:
        print "There are features in this county"
    else:
        print "There aren't features in this county"

Where I run into a problem is the logical test. For instance, the count will be zero but the logical test will print the if statement rather than the else statement. I've done something like this before and had similar issues but I don't know why, the test is fairly simple.

  • 1
    If you're just checking for selection, I'd suggest using if arcpy.Describe (layer).FIDSet:instead of the get count method. FIDSet returns a string of selected OIDs, and will return an empty string ("") if no selection is applied. It's a faster method. – Emil Brundage Dec 18 '17 at 21:24
4

To get the count value try:

result = arcpy.GetCount_management(layer)
count = int(result.getOutput(0))

GetCount_management() returns an object which you need to grab the 0 index value and convert it to an int using int(result.getOutput(0)) to get the count number.

3

The GetCount_manangement does not return a count like you would think. It returns a result. You will need to call getOutput on the result to find the number of features.

From the help:

result = arcpy.GetCount_management(layer)
count = int(result.getOutput(0))
print(count)
  • @Zeppelinfaktor then accept the answer with the checkbox – BERA Dec 19 '17 at 8:12
0

All of the following approaches should yield an integer value you can use in your logic. Out of curiosity I set up a time trial for three methods used to count selected features:

  1. Method 1: Get Count
  2. Method 2: FIDSet method
  3. Method 3: Search Cursor

Surprisingly, the Get Count method appears to be the most efficient approach.

Here are the results of three trials:

*** Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on win32. ***
>>> 
Method 1 yielded 500000 features in 0.0160000324249 seconds
Method 2 yielded 500000 features in 0.358999967575 seconds
Method 3 yielded 500000 features in 0.171999931335 seconds
>>> 
Method 1 yielded 500000 features in 0.0160000324249 seconds
Method 2 yielded 500000 features in 0.422000169754 seconds
Method 3 yielded 500000 features in 0.125 seconds
>>> 
Method 1 yielded 500000 features in 0.0149998664856 seconds
Method 2 yielded 500000 features in 0.375 seconds
Method 3 yielded 500000 features in 0.110000133514 seconds
>>>

The following is reproducible code with the benchmark test:

import arcpy, time
arcpy.CheckOutExtension("Spatial")

# Create some random points to test
outFolder = r'C:\temp'
numExtent = "0 0 100000 100000"
numPoints = 1000000
outName = "random_points.shp"
arcpy.CreateRandomPoints_management(outFolder, outName, "", numExtent, numPoints)

# Convert to layer following the OP's question
shp = r'C:\temp\random_points.shp'
arcpy.MakeFeatureLayer_management(shp, 'layer')
arcpy.SelectLayerByAttribute_management('layer', "", ' "FID" > 499999 ')

# Method 1: Get Count
start1 = time.time()
result = arcpy.GetCount_management('layer')
count = int(result.getOutput(0))
end1 = time.time()

clock1 = end1-start1

# Method 2: FIDSet
start2 = time.time()
result2 = len(arcpy.Describe('layer').FIDSet.split(';'))
end2 = time.time()

clock2 = end2-start2

# Method 3: Search Cursor
start3 = time.time()
result3 = len([r[0] for r in arcpy.da.SearchCursor('layer', ["OID@"])])
end3 = time.time()

clock3 = end3-start3

arcpy.Delete_management('layer')

# Print the results
print "Method 1 yielded %s features in %s seconds" % (result, clock1)
print "Method 2 yielded %s features in %s seconds" % (result2, clock2)
print "Method 3 yielded %s features in %s seconds" % (result3, clock3)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.