How to improve speed in my arcpy script

460
2
06-30-2019 03:14 AM
SuryaNarayana
New Contributor

Hi every one,

I want to update the symbology of selected feature from a feature layer. For this I have created a table of feature layer and joined these two. Then updating the attribute value in the table to update the symbology of the respect feature in feature layer. I wrote the below python script. By using this script the feature selection is taking more time. Can any one suggest me, how to improve speed in my script.

Tags (1)
0 Kudos
2 Replies
DanPatterson_Retired
MVP Emeritus
with arcpy.da.UpdateCursor(tbl, updateFieldsList) as updateCursor:
     for updateRow in updateCursor:
         if updateRow[0] == selFeature:
              updateRow[1] = "YES"
              updateCursor.updateRow(updateRow)
              del updateCursor

why do you 'del' the updatecursor within the updateCursor block?  There is no need within a 'with' statement, and in any event, it is placed wrong if it was (should be at the same level as the 'with' line, if it were.

You are also using python 2.7?

and you have an indentation block error as well.

Why don't you separate out the selection process from the processing steps... hard to pinpoint what lines since they seem to be happening all at one go.  Select everything you need and accumulate the selection, then process the selection, rather than the deep nesting you have

LukeWebb
Occasional Contributor III

When you write this code:

				    for selFeature in lyr.getSelectionSet():
					with arcpy.da.UpdateCursor(tbl, updateFieldsList) as updateCursor:
					    for updateRow in updateCursor:
						if updateRow[0] == selFeature:
						    updateRow[1] = "YES"
						    updateCursor.updateRow(updateRow)
						    del updateCursor‍‍‍‍‍‍‍

If you have more than 1 feature selected, the Cursor is running on all of the data and updating it.   It will then update all of the data each time for every selected feature. You probable need something more like:

if   len(lyr.getSelectionSet()) > 0:

       update cursor stuff

You have this in 2 places in your script, so it may be running your script many times more than is needed.

Edit: 

Revised after re-reading the code more thoroughly!, you probably need something more along the lines of this, to stop the cursor going through all of your datasets:

                     for selFeature in lyr.getSelectionSet():
                        with arcpy.da.UpdateCursor(tbl, updateFieldsList, where_clause="FID = '%s'" % selFeature) as updateCursor:
                            for updateRow in updateCursor:
                            if updateRow[0] == selFeature:
                                updateRow[1] = "YES"
                                updateCursor.updateRow(updateRow)
                                del updateCursor‍‍‍‍‍‍‍‍‍‍‍‍‍‍