Friday 25 September 2015

arcgis desktop - Fastest way to count the number of features in a feature class?


With the introduction of the Data Access module in arcpy (30x faster search cursors), I want to know if counting features matching sql criteria is faster than the traditional MakeTableView + GetCount methodology?



Answer




I have tested solution from answer above and on my real world data the difference is negligible. Opposite to results in other answer, my times for arcpy.MakeTableView_management and arcpy.da.SearchCursor within ArcMap are same same.


I have tested variations with and without query, please see the code for query version, and final measured results below:


@staticmethod
def query_features(feature_class, query):

# Method 1
time.sleep(5) # Let the cpu/ram calm before proceeding!
start_time = time.clock()
count = len(list(i for i in arcpy.da.SearchCursor(feature_class, ["OBJECTID"], query)))
end_time = time.clock()

arcpy.AddMessage("Method 1 finished in {} seconds".format((end_time - start_time)))
arcpy.AddMessage("{} features".format(count))

# Method 2
time.sleep(5) # Let the cpu/ram calm before proceeding!
start_time = time.clock()
arcpy.MakeTableView_management(feature_class, "myTableView", query)
count = int(arcpy.GetCount_management("myTableView").getOutput(0))

end_time = time.clock()

arcpy.AddMessage("Method 2 in {} seconds".format((end_time - start_time)))
arcpy.AddMessage("{} features".format(count))

The results below:


    No query:
Method 1 finished in 5.3616442 seconds
804140 features
Method 2 in 4.2843138 seconds
804140 features


Many results query:
Method 1 finished in 12.7124766 seconds
518852 features
Method 2 in 12.1396602 seconds
518852 features

Few results query:
Method 1 finished in 11.1421476 seconds
8 features
Method 2 in 11.2232503 seconds

8 features

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...