Guest User

Untitled

a guest
Nov 18th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. store_buffs = r"H:DMLANTASMapShapefiles2013_A30min_weekday.shp"
  2. density_ras = r"D:Downloadsblock_clipped_Raster1.tif"
  3. table_list = []
  4. with arcpy.da.SearchCursor(store_buffs, ["FacilityID"]) as cursor:
  5. for row in cursor:
  6. exp = '"FacilityID" = ' + str(row[0])
  7. temp_table10 = r"in_memorytemp_table10" + str(row[0])
  8. temp_shp10 = r'in_memorytemp_shp10'
  9. arcpy.Select_analysis(store_buffs, temp_shp10, exp)
  10. arcpy.sa.ZonalStatisticsAsTable(temp_shp10, 'FacilityID', density_ras, temp_table10, 'DATA', 'SUM')
  11. table_list.append(temp_table10)
  12. del row
  13. final_table = r"D:Downloads2013_A30min_weekday.dbf"
  14. arcpy.Merge_management(table_list, final_table)
  15.  
  16. import arcpy
  17. store_buffs = r"D:ScratchA30min_weekday.shp"
  18. density_ras = r"D:ScratchRaster1.tif"
  19. table_list = []
  20. with arcpy.da.SearchCursor(store_buffs, ["FacilityID"]) as cursor:
  21. for row in cursor:
  22. arcpy.AddMessage(row[0])
  23. exp = '"FacilityID" = ' + str(row[0])
  24. temp_table10 = r"in_memorytemp_" + str(row[0])
  25. temp_shp10 = r'in_memorytemp_shp10'
  26. arcpy.Select_analysis(store_buffs, temp_shp10, exp)
  27. arcpy.sa.ZonalStatisticsAsTable(temp_shp10, 'FacilityID', density_ras, temp_table10, 'DATA', 'SUM')
  28. table_list.append(temp_table10)
  29. del row
  30. final_table = r"D:scratchall_rows.dbf"
  31. arcpy.Merge_management(table_list, final_table)
  32.  
  33. import arcpy, traceback, os, sys
  34. from arcpy import env
  35. env.overwriteOutput = True
  36. mxd = arcpy.mapping.MapDocument("CURRENT")
  37. store_buffs=arcpy.mapping.ListLayers(mxd,'A30min_weekday')[0]
  38. density_ras = r"D:ScratchRaster1.tif"
  39. parID="FacilityID"
  40. parID2="FacilityID_1"
  41. env.workspace = "in_memory"
  42. dbf="stat"
  43. joinLR="SD"
  44.  
  45. try:
  46. def showPyMessage():
  47. arcpy.AddMessage(str(time.ctime()) + " - " + message)
  48. def Get_V(aKey):
  49. try:
  50. return smallDict[aKey]
  51. except:
  52. return (-1)
  53. arcpy.AddMessage("Defining neighbours...")
  54. arcpy.SpatialJoin_analysis(store_buffs, store_buffs, joinLR,"JOIN_ONE_TO_MANY")
  55. arcpy.AddMessage("Creating empty dictionary")
  56. dictFeatures = {}
  57. with arcpy.da.SearchCursor(store_buffs, parID) as cursor:
  58. for row in cursor:
  59. dictFeatures[row[0]]=()
  60. del row, cursor
  61. arcpy.AddMessage("Assigning neighbours...")
  62. with arcpy.da.SearchCursor(joinLR, (parID,parID2)) as cursor:
  63. for row in cursor:
  64. aKey=row[0]
  65. aList=dictFeatures[aKey]
  66. aList+=(row[1],)
  67. dictFeatures[aKey]=aList
  68. del row, cursor
  69. arcpy.AddMessage("Defining non-overlapping subsets...")
  70. runNo=0
  71. while (True):
  72. toShow,toHide=(),()
  73. nF=len(dictFeatures)
  74. for item in dictFeatures:
  75. if item not in toShow and item not in toHide:
  76. toShow+=(item,)
  77. toHide+=(dictFeatures[item])
  78. m=len(toShow)
  79. quer='"FacilityID" IN '+str(toShow)
  80. if m==1:
  81. quer='"FacilityID" = '+str(toShow[0])
  82. store_buffs.definitionQuery=quer
  83. runNo+=1
  84. arcpy.AddMessage("Run %i, %i polygon(s) found" % (runNo,m))
  85. arcpy.AddMessage("Running Statistics...")
  86. arcpy.gp.ZonalStatisticsAsTable_sa(store_buffs, parID, density_ras, dbf, "DATA", "SUM")
  87. arcpy.AddMessage("Data transfer...")
  88. smallDict={}
  89. with arcpy.da.SearchCursor(dbf, (parID,"SUM")) as cursor:
  90. for row in cursor:
  91. smallDict[row[0]]=row[1]
  92. del row, cursor
  93. with arcpy.da.UpdateCursor(store_buffs, (parID,"SUM")) as cursor:
  94. for row in cursor:
  95. aKey=row[0]
  96. row[1]=Get_V(aKey)
  97. cursor.updateRow(row)
  98. del row, cursor
  99. for item in toShow:
  100. del dictFeatures[item]
  101. m=len(dictFeatures)
  102. if m==0:
  103. break
  104. store_buffs.definitionQuery=""
  105. except:
  106. message = "n*** PYTHON ERRORS *** "; showPyMessage()
  107. message = "Python Traceback Info: " + traceback.format_tb(sys.exc_info()[2])[0]; showPyMessage()
  108. message = "Python Error Info: " + str(sys.exc_type)+ ": " + str(sys.exc_value) + "n"; showPyMessage()
Add Comment
Please, Sign In to add comment