1

The script below is used to update a shp which is missing values from another .shp using a spatial reference. It's taking about 19 minutes to run through ~53,000 rows. Any ideas on how I can make it quicker?

# import arc module and datetime
import arcpy, datetime

# Get start time 
start = datetime.datetime.now()

#Create an error class
class NoExist(Exception):
    pass

#Get Dataset locations  (need to turn this into a toolbox) 
dataSetFolder = arcpy.GetParameterAsText(0)
propertyDataset = arcpy.GetParameterAsText(1)
suburbsDataset = arcpy.GetParameterAsText(2)

#Test locations exist
try:

    if not arcpy.Exists(dataSetFolder):
        raise NoExist
    elif not arcpy.Exists(propertyDataset):
        raise NoExist
    elif not arcpy.Exists(suburbsDataset):
        raise NoExist

    arcpy.env.workspace = dataSetFolder

# if data doesnt exist display error message        
except NoExist:
    print "No data available"
    arcpy.AddMessage("No data available")   

# construct variable list
fc_p = propertyDataset
fc_s = suburbsDataset
fields = arcpy.ListFields(fc_p)
LOCALITYList = []

# build query (blank string) and assign fields to variables
query = '"LOCALITY" = \'\''
field_p = ['FID','LOCALITY']
field_s = ['FID', 'ADMINAREA']

# Make feature layers of each of the files
arcpy.MakeFeatureLayer_management(fc_s,'suburbs_lyr')
arcpy.MakeFeatureLayer_management(fc_p,'properties_lyr')


# use update cursor to identify properties without associated locality
with arcpy.da.UpdateCursor('properties_lyr', field_p,query) as Urows:
    for Urow in Urows:
        # second query for FID while using cursor through rows
        query2 = '"FID" = ' + str(Urow[0])
        # make a temp layer with the results and then select properties which are contained in suburbs layer
        arcpy.MakeFeatureLayer_management(fc_p,'Temp_properties_lyr', query2)
        arcpy.SelectLayerByLocation_management('suburbs_lyr', "CONTAINS", 'Temp_properties_lyr')
        # Search cursor to scroll through suburbs
        for Srow in arcpy.da.SearchCursor('suburbs_lyr',field_s):
            #variable to record result of ADMINAREA in search cursor
            Suburb = Srow[1]
        # set value of temp properties layer LOCALITY to Suburbs ADMINAREA    
        Urow[1] = Suburb
        print Urow
        # continue through rows
        Urows.updateRow(Urow)
        # place reults into list
        LOCALITYList.append(Suburb)
        # delete temp properties layer
        arcpy.Delete_management('Temp_properties_lyr')

#Delete cursors
del Urows

closed as primarily opinion-based by Vince, whyzar, csk, BBG_GIS, ahmadhanb Apr 22 at 6:03

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Select by Location for each row in the updatecursor will be slow. I would try using one Spatial Join before the updatecursor then read/write the results from this to properties_lyr. – BERA Apr 19 at 8:20
  • im trying not to make another feature class if i can help it. – Brett Apr 19 at 9:17
  • 1
    If I spent an hour or so, I could make this run in seconds, certainly under a minute. How many times do you have to run this that it's worth the effort? Code Review offers code reviews; GIS SE generally does not. – Vince Apr 19 at 11:19
  • For future reference do you know whether Code Review can handle arcpy/GIS specific code? – smiller Apr 19 at 16:05
  • hey vince, need to ru nthis through a few times and also change it up to incorporate additional arguments for the select layer. thanks for the tip about code review, i'll have a look – Brett Apr 20 at 5:54