Hello,
I have the following script. It works as expected, but it takes around 1.5 seconds to run each time. Doesn't seem to matter if it's running against 20 parcels or 5000. Are there any suggestions as to how I can improve the performance of this script.
Thanks
import arcpy
import numpy as np
try:
arcpy.env.overwriteOutput = True
arcpy.CreateTable_management("in_memory", "tableSelRecs")
arcpy.AddField_management(r'in_memory\tableSelRecs', "USE_CATEGORY", "TEXT", field_length=35)
arcpy.AddField_management(r'in_memory\tableSelRecs', "SHAPE_Area", "Double")
arcpy.CreateTable_management("in_memory", "tableSumRecs")
arcpy.AddField_management(r'in_memory\tableSumRecs', "USE_CATEGORY", "TEXT", field_length=35)
arcpy.AddField_management(r'in_memory\tableSumRecs', "SHAPE_Area", "Double")
mxd = arcpy.mapping.MapDocument('CURRENT')
df = mxd.activeDataFrame
totalArea = 0
totalResArea = 0
totalParcels = 0
totalResParcels = 0
lu_layer = arcpy.mapping.ListLayers(mxd, "LandUse_UseCategory", df)[0]
with arcpy.da.SearchCursor(lu_layer,['USE_CATEGORY','SHAPE@AREA']) as cursor:
insCursor = arcpy.da.InsertCursor(r'in_memory\tableSelRecs', ['USE_CATEGORY','SHAPE_Area'])
for row in cursor:
if row[0] != 'Invalid':
insCursor.insertRow((row[0],row[1]))
totalArea += row[1]
totalParcels += 1
if row[0] in('Residential - Low Density','Residential - Medium Density','Residential - High Density'):
totalResArea += row[1]
totalResParcels += 1
arcpy.Statistics_analysis(r'in_memory\tableSelRecs', r'in_memory\tableSumRecs', [["SHAPE_Area", "SUM"]], "USE_CATEGORY")
with arcpy.da.SearchCursor(r'in_memory\tableSumRecs',['USE_CATEGORY','SUM_SHAPE_Area']) as selCur:
interimValue = [0]
interimResValue = [0]
for sRow in selCur:
arcpy.AddMessage('USE_CATEGORY: {}; AREA: {}'.format(sRow[0],sRow[1]))
interimValue.append(np.square(sRow[1]/totalArea))
if sRow[0] in('Residential - Low Density','Residential - Medium Density','Residential - High Density'):
interimResValue.append(np.square(sRow[1]/totalResArea))
DI = 1 - sum(interimValue)
RDI = 1 - sum(interimResValue)
arcpy.AddMessage('Diversity Index: ' + str(DI))
arcpy.AddMessage('Residential Diversity Index: ' + str(RDI))
arcpy.AddMessage('Total Blocks Selected: ' + str(totalParcels))
arcpy.AddMessage('Total Blocks Area: ' + str(totalArea))
arcpy.AddMessage('Total Res Blocks Selected: ' + str(totalResParcels))
arcpy.AddMessage('Total Res Blocks Area: ' + str(totalResArea))
arcpy.AddMessage('NOTE: Any blocks with a use category of Invalid are not included in any calculations.')
arcpy.Delete_management(r'in_memory\tableSelRecs')
arcpy.Delete_management(r'in_memory\tableSumRecs')
except arcpy.ExecuteError:
print(arcpy.GetMessages(2))