Select to view content in your preferred language

Error looping Zonal Statistics as Table

07-31-2019 03:23 PM
New Contributor

What is the bug in this scenario? I have used the following bit of code (last chunk in a long script) 5 times successfully (on 5 different batches of rasters) and now the 6th time it refuses to work, alternately giving me error 000354: "The name contains invalid characters", or 99999: "Something unexpected caused the tool to fail", depending on how I try to fix it.

Aim: Run Zonal Statistics as Table on time series batches of binary threshold rasters to observe growth of plants. 

I got it to run smoothly following this post:

The only change I can see in this latest batch of rasters it that my "zones" shapefile exceeds 36KB. There are no Null values in "Id", the fieldname is not longer than 17 characters, there are only 2 values present in the rasters (binary otsu threshold), and the feature zone dataset is comprised of 276 zones, far less than the 170,000 features at which problems are supposed to present themselves. I tried all the solutions and workarounds found here:

Any suggestions are more than welcome.

The code snippet: 

import os, glob, shutil, arcpy, cv2
import numpy as np
from arcpy import env
from import *
from arcpy import UpdateCursor
env.overwriteOutput = True

#Create table of stats for each threshold using vectorized tray zones
    env.workspace = "O:/NeilsonJ/Image_Processing/Working/150719/6.Scaled"
    for raster in arcpy.ListRasters():
        print (raster)
        outDir = "O:/NeilsonJ/Image_Processing/Working/150719/7.Tables"
        inZoneData = "O:/NeilsonJ/Image_Processing/Working/150719/150719_tray.shp"
        raster_name = os.path.basename(raster).rstrip(os.path.splitext(raster)[1])
        outTable = os.path.join(outDir, raster_name +".dbf"), "Id", raster, outTable, "NODATA", "ALL")
    print ("Script failed to complete")
    print (arcpy.GetMessages(2))

Script failed to complete
ERROR 000354: The name contains invalid characters
Failed to execute (ZonalStatisticsAsTable).

##WHEN IT WORKS (19513_195514 has already been completed)
env.workspace = "O:/NeilsonJ/Image_Processing/Working/19513_19514/7.Tables"
for table in arcpy.ListTables():
    print (table)
0 Kudos
2 Replies
MVP Emeritus

Anne, it would be useful to throw in a couple of print statements so people can see the actual names for the zonalstats input/output

New Contributor

Hi Dan, good idea, put them in there. Just noticed - none of the completed projects have dashes in the file names but the new one does. I just tried replacing all the hyphens with underscore and it worked like a charm. Thanks for pointing me in the right direction!

0 Kudos