I have a collection of about 1300 points and for each point I want to get the average of a raster layer within a 50 m buffer. I have overlapping polygons. I've been scouring the forums for days now and I still haven't been able to come up with a solution that works. Here's an idea of what my data looks like
My current workflow is to to run Zonal Statistics as Table, digest it into a dictionary, and then use that to populate a field in my buffer layer. The following is my code (modified from something I found on a forum):
out = output workspace
features = arcpy.Buffer_analysis(pointfc, out + '\\name', '50 Meters')
zone_field = "OBJECTID"
input_raster = raster file
field_name = "Average"
field_type = "DOUBLE"
stat = "MEAN"
import arcpy
from arcpy.sa import *
arcpy.AddField_management(features, field_name, field_type)
if arcpy.Exists("zonal_table"):
try:
arcpy.Delete_management("zonal_table")
except:
arcpy.AddError("Unable to clear temp table")
sys.exit(-1)
print("Performing zonal statistics: " + field_name)
zonal_table = ZonalStatisticsAsTable(features,zone_field, input_raster, out + "\\zonal_table", "DATA", stat)
print("Digesting " + stat + " from zonal table")
stat_dict = {}
with arcpy.da.SearchCursor(zonal_table,[zone_field,stat]) as cursor:
for row in cursor:
stat_dict[row[0]] = row[1]
print("dictionary done")
print("updating field")
arcpy.AddMessage("Calculating " + field_name)
with arcpy.da.UpdateCursor(features, [zone_field, field_name]) as cursor2:
for row2 in cursor2:
row2[1] = stat_dict[row2[0]]
cursor2.updateRow(row2)
The process works; however, my results are mostly different than what I get from running Zonal Statistics and comparing the point to the underlying raster. All of my data is in the same projected coordinate system. I've seen that there are issues with the way the snap rasters are implemented and have tried converting my buffer to raster (outside of the code on a small subset to test) without any better results. I also saw that the underlying bug was resolved in 10.5, but I still have differing results between Zonal Statistics and Zonal Statistics as Table in both 10.6 and ArcPro 2.3.
Is the issue with the Zonal Statistics or the Zonal Statistics as Table tool? Or is there another hole in my workflow somewhere I'm not considering? I'm about out of stuff to check, so any ideas are greatly appreciated.