0

I created a for loop to perform a (simplified here) series of operations over tiles from a larger dataset.

My initial code looked like this:

ext_template = bingmean_odd
totalextent = arcpy.Describe(ext_template).Extent
n_2d = 20 #Number of side tiles (total number of tiles = n_2d^2)

xpl = range(int(totalextent.XMin), int(math.ceil(totalextent.XMax)), int(math.ceil((totalextent.XMax - totalextent.XMin)/n_2d))+1) + [int(math.ceil(totalextent.XMax))]
ypl = range(int(totalextent.YMin), int(math.ceil(totalextent.YMax)), int(math.ceil((totalextent.YMax - totalextent.YMin)/n_2d))+1) + [int(math.ceil(totalextent.YMax))]
x=0
for xmin, xmax in zip(xpl[:-1], xpl[1:]):
    for ymin, ymax in zip(ypl[:-1], ypl[1:]):
        tilext = arcpy.Extent(xmin, ymin, xmax, ymax)
        print(tilext)
        arcpy.env.extent = tilext
        arcpy.env.snapRaster = ext_template

        outeuc = bingeuc + str(x)
        if not arcpy.Exists(outeuc):
            print(outeuc)
            clean_mean = Int(1000*(Raster(bingmean_odd) + Raster(bingmean_even))/2)
            euc = arcpy.sa.EucAllocation(clean_mean, maximum_distance=50)
            euc.save(bingeuc)
            arcpy.Delete_management(euc)
            arcpy.Delete_management(clean_mean)
        x+=1

This led to the deletion of both the temporary variable (euc) and the main output (bingeuc). So, I tried to rename the output variable.

euc = arcpy.sa.EucAllocation(clean_mean, maximum_distance=50)
euc.save(bingeuc)
arcpy.Rename_management(bingeuc, outeuc)
arcpy.Delete_management(euc)
arcpy.Delete_management(clean_mean)
x+=1

But it still deleted the main output. If I took out Delete_management or tried to include the operation in a function (as in this example), I would get the infamous:

Traceback (most recent call last): File "", line 40, in File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\sa\Functions.py", line 890, in EucAllocation out_direction_raster) File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\sa\Utils.py", line 53, in swapper result = wrapper(*args, **kwargs) File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\sa\Functions.py", line 881, in Wrapper out_direction_raster) File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\geoprocessing_base.py", line 510, in return lambda *args: val(*gp_fixargs(args, True)) RuntimeError: ERROR 999998: Unexpected Error.

However, when I magically moved the deleting prior to the map algebra. It all worked (as follows)... any idea why?

ext_template = bingmean_odd
totalextent = arcpy.Describe(ext_template).Extent
n_2d = 20 #Number of side tiles (total number of tiles = n_2d^2)

xpl = range(int(totalextent.XMin), int(math.ceil(totalextent.XMax)), int(math.ceil((totalextent.XMax - totalextent.XMin)/n_2d))+1) + [int(math.ceil(totalextent.XMax))]
ypl = range(int(totalextent.YMin), int(math.ceil(totalextent.YMax)), int(math.ceil((totalextent.YMax - totalextent.YMin)/n_2d))+1) + [int(math.ceil(totalextent.YMax))]
x=0
for xmin, xmax in zip(xpl[:-1], xpl[1:]):
    for ymin, ymax in zip(ypl[:-1], ypl[1:]):
        tilext = arcpy.Extent(xmin, ymin, xmax, ymax)
        print(tilext)
        arcpy.env.extent = tilext
        arcpy.env.snapRaster = ext_template

        outeuc = bingeuc + str(x)
        try:
            arcpy.Delete_management(clean_mean)
        except:
            pass        
        try:
            arcpy.Delete_management(euc)
        except:
            pass

        if not arcpy.Exists(outeuc):
            print(outeuc)
            clean_mean = Int(1000*(Raster(bingmean_odd) + Raster(bingmean_even))/2)
            euc = arcpy.sa.EucAllocation(clean_mean, maximum_distance=50)
            euc.save(bingeuc)
            arcpy.Rename_management(bingeuc, outeuc)
        x+=1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.