Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- store_buffs = r"H:DMLANTASMapShapefiles2013_A30min_weekday.shp"
- density_ras = r"D:Downloadsblock_clipped_Raster1.tif"
- table_list = []
- with arcpy.da.SearchCursor(store_buffs, ["FacilityID"]) as cursor:
- for row in cursor:
- exp = '"FacilityID" = ' + str(row[0])
- temp_table10 = r"in_memorytemp_table10" + str(row[0])
- temp_shp10 = r'in_memorytemp_shp10'
- arcpy.Select_analysis(store_buffs, temp_shp10, exp)
- arcpy.sa.ZonalStatisticsAsTable(temp_shp10, 'FacilityID', density_ras, temp_table10, 'DATA', 'SUM')
- table_list.append(temp_table10)
- del row
- final_table = r"D:Downloads2013_A30min_weekday.dbf"
- arcpy.Merge_management(table_list, final_table)
- import arcpy
- store_buffs = r"D:ScratchA30min_weekday.shp"
- density_ras = r"D:ScratchRaster1.tif"
- table_list = []
- with arcpy.da.SearchCursor(store_buffs, ["FacilityID"]) as cursor:
- for row in cursor:
- arcpy.AddMessage(row[0])
- exp = '"FacilityID" = ' + str(row[0])
- temp_table10 = r"in_memorytemp_" + str(row[0])
- temp_shp10 = r'in_memorytemp_shp10'
- arcpy.Select_analysis(store_buffs, temp_shp10, exp)
- arcpy.sa.ZonalStatisticsAsTable(temp_shp10, 'FacilityID', density_ras, temp_table10, 'DATA', 'SUM')
- table_list.append(temp_table10)
- del row
- final_table = r"D:scratchall_rows.dbf"
- arcpy.Merge_management(table_list, final_table)
- import arcpy, traceback, os, sys
- from arcpy import env
- env.overwriteOutput = True
- mxd = arcpy.mapping.MapDocument("CURRENT")
- store_buffs=arcpy.mapping.ListLayers(mxd,'A30min_weekday')[0]
- density_ras = r"D:ScratchRaster1.tif"
- parID="FacilityID"
- parID2="FacilityID_1"
- env.workspace = "in_memory"
- dbf="stat"
- joinLR="SD"
- try:
- def showPyMessage():
- arcpy.AddMessage(str(time.ctime()) + " - " + message)
- def Get_V(aKey):
- try:
- return smallDict[aKey]
- except:
- return (-1)
- arcpy.AddMessage("Defining neighbours...")
- arcpy.SpatialJoin_analysis(store_buffs, store_buffs, joinLR,"JOIN_ONE_TO_MANY")
- arcpy.AddMessage("Creating empty dictionary")
- dictFeatures = {}
- with arcpy.da.SearchCursor(store_buffs, parID) as cursor:
- for row in cursor:
- dictFeatures[row[0]]=()
- del row, cursor
- arcpy.AddMessage("Assigning neighbours...")
- with arcpy.da.SearchCursor(joinLR, (parID,parID2)) as cursor:
- for row in cursor:
- aKey=row[0]
- aList=dictFeatures[aKey]
- aList+=(row[1],)
- dictFeatures[aKey]=aList
- del row, cursor
- arcpy.AddMessage("Defining non-overlapping subsets...")
- runNo=0
- while (True):
- toShow,toHide=(),()
- nF=len(dictFeatures)
- for item in dictFeatures:
- if item not in toShow and item not in toHide:
- toShow+=(item,)
- toHide+=(dictFeatures[item])
- m=len(toShow)
- quer='"FacilityID" IN '+str(toShow)
- if m==1:
- quer='"FacilityID" = '+str(toShow[0])
- store_buffs.definitionQuery=quer
- runNo+=1
- arcpy.AddMessage("Run %i, %i polygon(s) found" % (runNo,m))
- arcpy.AddMessage("Running Statistics...")
- arcpy.gp.ZonalStatisticsAsTable_sa(store_buffs, parID, density_ras, dbf, "DATA", "SUM")
- arcpy.AddMessage("Data transfer...")
- smallDict={}
- with arcpy.da.SearchCursor(dbf, (parID,"SUM")) as cursor:
- for row in cursor:
- smallDict[row[0]]=row[1]
- del row, cursor
- with arcpy.da.UpdateCursor(store_buffs, (parID,"SUM")) as cursor:
- for row in cursor:
- aKey=row[0]
- row[1]=Get_V(aKey)
- cursor.updateRow(row)
- del row, cursor
- for item in toShow:
- del dictFeatures[item]
- m=len(dictFeatures)
- if m==0:
- break
- store_buffs.definitionQuery=""
- except:
- message = "n*** PYTHON ERRORS *** "; showPyMessage()
- message = "Python Traceback Info: " + traceback.format_tb(sys.exc_info()[2])[0]; showPyMessage()
- message = "Python Error Info: " + str(sys.exc_type)+ ": " + str(sys.exc_value) + "n"; showPyMessage()
Add Comment
Please, Sign In to add comment