Select to view content in your preferred language

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

983
4
11-11-2019 04:35 AM
HarrietBranson
Occasional Contributor

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
arcpy.CheckOutExtension("spatial")

# Set the environmental variable to overwrite output
arcpy.env.overwriteOutput = True
 
def CreateDirectory(DBF_dir):
    if not os.path.exists(DBF_dir):
    os.mkdir(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")
       arcpy.gp.ZonalStatisticsAsTable_sa(out_layer, 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() 
    arcpy.Merge_management(tableList,zstat_table)
    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"
    CreateDirectory(DBF_dir)
    ZonalStatsAsTable_OL(fc,DBF_dir,raster,zoneField)
    MergeTables(DBF_dir,zstat_table)
0 Kudos
4 Replies
DanPatterson_Retired
MVP Emeritus

/blogs/dan_patterson/2016/08/14/script-formatting  would help readers refer to line numbers.

Sometimes, I have found a nap is in order for Arc* to catch up. so add

import time to your imports then take a rest at the location where you think things might be failing

import time

time.sleep(1)

Other than that you filenames seem to concatenate well but I would also enforce 'r' encoding all paths just to be safe.

0 Kudos
HarrietBranson
Occasional Contributor

Sorry about the code; I have updated it!

Unfortunately, the error still runs even with the nap added at several points where arc may be overrunning itself.

Thank you, though

0 Kudos
DanPatterson_Retired
MVP Emeritus

And if the print statements are still working prior to using an arcpy method, 

maybe check your cursor.  You are using the legacy versions.  The data access module is usually use in ArcGIS Pro 

SearchCursor—Data Access module | ArcGIS Desktop 

0 Kudos
HarrietBranson
Occasional Contributor

I would ideally use the arcpy.da.SearchCursor, but it completely disregards the fact I created a new column (ID2) so that it was integer, stating it doesn't exist/attribute error tuple object has no attribute 'ID2', which I cannot find a workaround for.

0 Kudos