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()
Solved! Go to Solution.
Chris:
Thanks for the tip on in-memory. I used that, not much time difference, though.
env.workspace = "IN-MEMORY"
env.workspace = "IN_MEMORY"
import time
t0 = time.clock()
# some tool/code block
arcpy.AddMessage("Elapsed time: {0} seconds".format(int(time.clock() - t0)))import arcpy, sys, time #, os
from arcpy import env
env.overwriteOutput = True
env.workspace = "IN-MEMORY"
#temp_lyr_location = r"\Cases_Geodatabase.gdb\temp_parcel_lyr"
parcel = r"Parcel"
try:
#lyr = os.getcwd() + temp_lyr_location
lyr = r"temp_parcel_lyr"
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
t0 = time.clock()
arcpy.Dissolve_management(parcel, lyr, fld)
arcpy.AddMessage("Elapsed time dissolving: {0} seconds".format(int(time.clock() - t0)))
# add case feature in temporary layer to Cases
t0 = time.clock()
arcpy.Append_management(lyr, target, schemaType, "", "")
arcpy.AddMessage("Elapsed time appending: {0} seconds".format(int(time.clock() - t0)))
# select new case feature
t0 = time.clock()
arcpy.SelectLayerByLocation_management(target, selectType, lyr)
arcpy.AddMessage("Elapsed timeselecting case layer: {0} seconds".format(int(time.clock() - t0)))
# delete temporary layer
#arcpy.Delete_management(lyr)
# clear selection on parcels
t0 = time.clock()
arcpy.SelectLayerByAttribute_management(parcel, clear)
arcpy.AddMessage("Elapsed time clearing parcel selection: {0} seconds".format(int(time.clock() - t0)))
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:
t0 = time.clock()
count = int(arcpy.GetCount_management(parcel).getOutput(0))
arcpy.AddMessage(str(count) + " parcels selected")
arcpy.AddMessage("Elapsed time counting parcels: {0} seconds".format(int(time.clock() - t0)))
# make sure parcels are selected before running rest of script, otherwise exit
if count >= 0 and count <= 10:
MakeCaseFeature() # create the case feature
t0 = time.clock()
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
arcpy.AddMessage("Elapsed time refreshing view: {0} seconds".format(int(time.clock() - t0)))
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)
env.workspace = "in_memory"
Chris:
Thanks for the tip on in-memory. I used that, not much time difference, though.
env.workspace = "IN-MEMORY"
env.workspace = "IN_MEMORY"