AnsweredAssumed Answered

Error 999999 whilst calculating zonal statistics as a table for overlapping polygons

Question asked by HBranson@fauna_flora.org_FFI on Nov 11, 2019
Latest reply on Nov 11, 2019 by HBranson@fauna_flora.org_FFI

Adapting esri tech python code for zonal stats for overlapping polygons. Continual error 999999 - not sure if issues transferring from arcmap, as I am working in arcpro.

Have tried many remedies, including changing unique value rendering, arcpro file extensions, building raster attribute table, changing zone feature ID column to integer; all filenames less than 13 chars. Not sure what the root of the issue is, so wanted to ask a wider audience if they'd experienced anything similar.

Usually when I run the code I get to 'print saved layer file', and then the code breaks.

Sometimes it works and then states that the iterated layer file does not exist - it does, its in the folder directory and in the arcpro document.


import arcpy, os, sys, string

from arcpy import env

# Set the environmental variable to overwrite output
arcpy.env.overwriteOutput = True

def CreateDirectory(DBF_dir):
    if not os.path.exists(DBF_dir):
    print ("created directory {0}".format(DBF_dir))

def ZonalStatsAsTable_OL(fc,DBF_dir,raster,zoneField):
    arcpy.AddField_management(fc, "ID2", "LONG", field_is_nullable="NON_NULLABLE")
    arcpy.CalculateField_management(fc, "ID2", "!ID!")
    for row in arcpy.SearchCursor(fc):
       lyr = "Zone_{0}_lyr".format(row.ID2)
       tempTable = DBF_dir + os.path.sep + "zone_{0}.dbf".format(row.ID2)
       arcpy.MakeFeatureLayer_management(fc, lyr, "\"ID2\" = {0}".format(row.ID2))
       arcpy.BuildRasterAttributeTable_management(raster, "overwrite")
       print ("Creating layer {0}".format(lyr))
       out_layer = DBF_dir + os.sep + lyr + ".lyrx"
       arcpy.SaveToLayerFile_management(lyr, out_layer)
       print ("Saved layer file"), zoneField, raster, tempTable, "DATA", "ALL")
       print ("Populating zonal stats for {0}").format(lyr)
    del row, lyr

def MergeTables(DBF_dir,zstat_table):
    arcpy.env.workspace = DBF_dir
    tableList = arcpy.ListTables()
    print ("Merged tables. Final zonalstat table {0} created. Located at {1}".format(zstat_table,DBF_dir))
    del tableList

if __name__ == "__main__":
    ws = "D:\TEMP"
    DBF_dir = ws + os.path.sep + "DBFile"
    fc = "D:\iteratortest\ZoneFeatures.shp"
    zoneField = "ID2"
    raster = r"D:\iteratortest\Hansen_GFCv16.tif"
    zstat_table = DBF_dir + os.path.sep + "Zonalstat.dbf"