The script below works in that it does what it's supposed to: creates a single Case feature from selected parcels. But it takes anywhere from 1.5 - 2 minutes to run, which seems a long time for a relatively simple operation. I could do it faster manually, although the people it's intended for probably couldn't :)
Any ideas on why it's so slow, an/or what could be done to speed it up? Thanks.
Any ideas on why it's so slow, an/or what could be done to speed it up? Thanks.
Code:
import arcpy, sys, os
from arcpy import env
env.overwriteOutput = True
temp_lyr_location = r"\Cases_Geodatabase.gdb\temp_parcel_lyr"
parcel = r"Parcel"
try:
lyr = os.getcwd() + temp_lyr_location
mxd = arcpy.mapping.MapDocument("CURRENT")
except Exception as e:
arcpy.AddError('ERROR initializing: /n' + e.message)
#-------------------------------------------------------------------------------
def MakeCaseFeature():
''' Dissolve selected parcel features into one temporary lyr file.
Append it to the Cases feature class.
Delete the temporary lyr file. '''
clear = "CLEAR_SELECTION"
target = "Cases"
schemaType = "NO_TEST"
fld = "CENTRACT" # no data is stored in this field
selectType = "HAVE_THEIR_CENTER_IN"
try:
# make temporary parcel lyr file from selected parcels, dissolving them
# into one feature
arcpy.Dissolve_management(parcel, lyr, fld)
# add case feature in temporary layer to Cases
arcpy.Append_management(lyr, target, schemaType, "", "")
# select new case feature
arcpy.SelectLayerByLocation_management(target, selectType, lyr)
# delete temporary layer
arcpy.Delete_management(lyr)
# clear selection on parcels
arcpy.SelectLayerByAttribute_management(parcel, clear)
except Exception as e:
arcpy.AddError("ERROR in MakeCaseFeature: \n" + e.message)
#-------------------------------------------------------------------------------
def main():
''' Check how many parcels are selected. Count returns all parcels if none are
selected, or only selected ones if there are any.
'''
try:
count = int(arcpy.GetCount_management(parcel).getOutput(0))
arcpy.AddMessage(str(count) + " parcels selected")
# make sure parcels are selected before running rest of script, otherwise exit
if count >= 0 and count <= 10:
MakeCaseFeature() # create the case feature
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
else:
arcpy.AddError("No features selected! \n Please select at least one parcel feature. \n")
arcpy.AddError("Quitting the Create Case tool \n")
except Exception as e:
arcpy.AddError('ERROR in main: /n' + e.message)
#-------------------------------------------------------------------------------
if __name__ == '__main__':
main()