1

I have some troubles to understand the behaviour of my code below. Obviously, GetCount_Management doesn't match the rows in my layer selection.

This code is part of a long for loop. I have hidden the rest for clarity purposes.

#...
# tmpThi is a in_memory output of CreateThiessenPolygons_analysis
# evtSts is a pandas Serie containing the name of rainfall stations of interest
arcpy.MakeFeatureLayer_management(tmpThi, "thLyr") # creating a layer
evtStsNames = [name for name in evtSts] # creating a list of station of interest
# select the Thiessen polygons where station name 'Nom' is in the list
arcpy.SelectLayerByAttribute_management("thLyr", "NEW_SELECTION", ' "Nom" IN (\'' + '\',\''.join(evtStsNames) + '\')' ) 
rows = arcpy.SearchCursor("thLyr") # Counting selected features with Cursor
rowCount = 0
for row in rows:
 rowCount += 1
print len(evtStsNames), arcpy.GetCount_management("thLyr").getOutput(0), rowCount # This should print the same result

This print a lower GetCount_management as follow:

3 2 3
4 3 4
7 5 7
1 1 1
3 1 3
4 2 4
1 1 1
6 2 6
14 6 14
...

This is quite confusing to me.

It seems that if I store the Thiessen polygons not in "in_memory" getCount match rowCount. I am still trying to understand what really happens

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Nov 6, 2015 at 8:56
2
  • When you add thLyr to the map can you verify that arcpy.SelectLayerByAttribute_management() is indeed selecting features? Commented Nov 6, 2015 at 9:07
  • @JamesSLC if I save my selections using arcpy.CopyFeatures_management("thLyr", "\\tmp\\thSel_{0}".format(i)), the number of feature saved is the one given by GetCount_management. I expect it to be rather the rowCount. Commented Nov 6, 2015 at 9:29

1 Answer 1

3

For the searchCursor, the first parameter is a dataset. The selection is thus not taken into account and rowCount will take all features from the feature class.

For CopyFeature() and GetCount(), the first parameter is a layer which will take the selection into account.

If you want to use a query on your feature class inside the cursor, you should write in directly into the cursor :

rows = arcpy.SearchCursor("thLyr", ' "Nom" IN (\'' + '\',\''.join(evtStsNames) + '\')' ) 

by the way, you should consider arcpy.da.SearchCursor instead of arcpy.SearchCursor because it is often more efficient.

answered Nov 6, 2015 at 12:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.