Then there is the requisite numpy/scipy solution... it looks the same as the above picture.
It takes a *.tif (exported raster if it is in another format), does the fill and returns a tif as output
def num_121():
""" Fill an array's zeros in with their nearest value
simple sample data
a = np.random.randint(0, 3, size=(20, 20))
"""
from scipy import ndimage as nd
import arcpy
r = r"C:\path\to\thefile\poly.tif"
a = arcpy.RasterToNumPyArray(r)
m = np.where(a==0,1, 0)
ind = nd.distance_transform_edt(m, return_distances=False, return_indices=True)
b = a[tuple(ind)]
r0 = arcpy.NumPyArrayToRaster(b, arcpy.Point(300000, 5025000), 1)
r0.save(r"C:\path\to\thefile\polyfilled.tif")
return a, b
For a simple random sample you can explore the result if you don't have and image.
'a' is the raster who's zeros need to be filled … 'b' is the result
from scipy import ndimage as nd
a = np.random.randint(0, 3, size=(20, 20))
m = np.where(a==0,1, 0)
ind = nd.distance_transform_edt(m, return_distances=False, return_indices=True)
b = a[tuple(ind)]
a
Out[74]:
array([[1, 1, 0, 2, 2, 2, 0, 1, 1, 2, 2, 2, 2, 2, 0, 1, 0, 2, 1, 0],
[1, 1, 2, 1, 0, 2, 0, 0, 0, 2, 2, 2, 0, 1, 2, 0, 2, 2, 0, 1],
[2, 0, 2, 1, 1, 2, 0, 0, 0, 2, 2, 1, 1, 0, 2, 1, 0, 0, 0, 0],
[2, 0, 1, 1, 0, 2, 0, 0, 2, 1, 0, 0, 0, 1, 2, 0, 2, 2, 2, 2],
[2, 1, 1, 0, 0, 0, 2, 1, 0, 1, 1, 1, 0, 0, 2, 2, 0, 2, 1, 1],
[2, 1, 1, 0, 0, 0, 0, 0, 0, 2, 1, 2, 1, 0, 1, 1, 0, 2, 2, 2],
[0, 2, 1, 0, 2, 0, 1, 1, 1, 0, 1, 2, 1, 2, 2, 2, 1, 2, 0, 2],
[1, 0, 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 0, 0, 1, 2, 2, 0, 2, 1],
[0, 2, 2, 2, 2, 2, 2, 0, 2, 0, 1, 0, 1, 2, 1, 2, 0, 2, 0, 2],
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