Select to view content in your preferred language

Error looping Zonal Statistics as Table

1536
2
07-31-2019 03:23 PM
AnneFarineau1
Emerging 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: https://gis.stackexchange.com/questions/206559/python-script-zonal-stats-as-table-loop-question

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: https://support.esri.com/en/technical-article/000012343.

Any suggestions are more than welcome.

The code snippet: 

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

#Create table of stats for each threshold using vectorized tray zones
try:
    env.workspace = "O:/NeilsonJ/Image_Processing/Working/150719/6.Scaled"
    for raster in arcpy.ListRasters():
        print (raster)
        run9_1_2019-15-28-10-52_dist_NXG_OTSU_scaled.tif
        run9_1_2019-15-28-11-52_dist_NXG_OTSU_scaled.tif
        run9_1_2019-15-28-12-52_dist_NXG_OTSU_scaled.tif
        run9_1_2019-15-28-13-52_dist_NXG_OTSU_scaled.tif
        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")
        arcpy.gp.ZonalStatisticsAsTable(inZoneData, "Id", raster, outTable, "NODATA", "ALL")
except:
    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)
run7_27__dist_NXG_OTSU_scaled.dbf
run7_21__dist_NXG_OTSU_scaled.dbf
run7_7__dist_NXG_OTSU_scaled.dbf
run7_33__dist_NXG_OTSU_scaled.dbf
0 Kudos
2 Replies
DanPatterson_Retired
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

AnneFarineau1
Emerging 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