Saturday, 19 March 2016

arcgis desktop - Iterating using search cursor and layer selection performance?


I'm trying to use the results of a search cursor to iteratively make selections on a feature class and then create new feature classes (so you get a new feature class or shp for each unique value in a field... kind of an explode by attribute tool).


As an example - if you had a feature class containing 14763 records and one of the fields had 18 unique values in it, after running the script you would have 18 new feature classes in your gdb.


So far I have this:


def unique_values(table, field):
with arcpy.da.SearchCursor(table, [field]) as cursor:
dict = sorted({x[0] for x in cursor})

print type(dict)
for i in range(len(dict)):

whereD = dict[i]
fname = whereD[0:5] + "_" + whereD[-4:]
arcpy.MakeFeatureLayer_management(table, fname)
query = "relatedBirds_Species = '" + whereD + "'"
arcpy.SelectLayerByAttribute_management(fname, "NEW_SELECTION", query)
arcpy.CopyFeatures_management(fname,fname+ "_c")


This works but it's incredibly slow when it runs. Does anyone know of a quicker way to get the same results?


Requested edits below


The Data


Geodatabase workspace The feature class I'm running this with has 18 unique values in the field in question. Total number of records in the original feature class is 14,673


Timings


As the code is above = 2' 35.04"


With the {where clause} added to MakeFeatureLayer (w/out copy feature either)= 1' 27.75" (thanks Stephen)


With Select_analysis = 2' 10.58"


I think most of the time was being taken to actually draw these in Arc as I was running this directly from the python window so I ran it again from cmd with arc closed.


Stephen wins at 57.93" (with copy feature put back in) running from cmd.




Answer



If you do decide to use Make Feature Layer, note that it allows you to specify a where clause:


MakeFeatureLayer_management (in_features, out_layer, {where_clause}...

I haven't tested to see whether this improves performance, but you could potentially save some time by running the query while creating the layer, thus omitting the Select By Attributes.


You could also step through your code in a debugger to determine which lines are slow.


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...