2

I have a TIFF format file and want to automate some tasks. I have a Python script for Zonal Statistics as Table but it results in empty table output. However, the Zonal Statistics As Table Tool at ArcMap runs successfully! What is the problem with the Script?

import os
import arcpy
from arcpy import env
from arcpy.sa import *

arcpy.env.overwriteOutput = True
arcpy.CheckOutExtension('Spatial')

arcpy.env.workspace = "D:/Data/Hydro/"

raster_list = arcpy.ListRasters("*.tif")
lisTable = arcpy.ListTables()

zone = "D:/Data/Catch.shp"

# Loop through each file and perform the zonal statistics processing
for raster in raster_list:
    raster_name = os.path.basename(raster).rstrip(os.path.splitext(raster)[1])
    outTable = raster_name + ".dbf"
    arcpy.gp.ZonalStatisticsAsTable(zone, "NAME", raster, outTable, "NODATA", "MEAN_STD")

enter image description here

3
  • I recreated your script using my own data and it worked fine for me. Maybe double check that the input data matches what you used when running the Tool in ArcMap? I could not reproduce your error of the blank table either.
    – JamiRae
    Jun 13 '20 at 18:52
  • Thanks @JamiRae The inputs for the ArcMap and the python script are the same. The same script works for me for other similar TIFF files and not for these files. I am not clear why! I even changed the working directory, however the result is the same; empty table!
    – Daniel
    Jun 15 '20 at 6:49
  • @ JamiRae You can find a subset of the original TIFF file for your reference here: drive.google.com/open?id=1i4TlS4ycuIsVp4UxBqjmoWTVs1ZnIiz6
    – Daniel
    Jun 15 '20 at 7:38
2

The script worked fine when I removed the "NODATA" value I was passing to the ignore_nodata parameter and simply put the double quotation instead. This switches the functionality from ignoring all zones that contain NODATA values to the default behavior of excluding NODATA values from the calculations of a zone's statistics and not skipping zones that contain NODATA values.

import os
import arcpy
from arcpy import env
from arcpy.sa import *

arcpy.env.overwriteOutput = True
arcpy.CheckOutExtension('Spatial')

arcpy.env.workspace = "D:/Data/Hydro/"

raster_list = arcpy.ListRasters("*.tif")
lisTable = arcpy.ListTables()

zone = "D:/Data/Catch.shp"

# Loop through each file and perform the zonal statistics processing
for raster in raster_list:
    raster_name = os.path.basename(raster).rstrip(os.path.splitext(raster)[1])
    outTable = raster_name + ".dbf"
    arcpy.gp.ZonalStatisticsAsTable(zone, "NAME", raster, outTable, "", "MEAN_STD")
    
0

I downloaded your files and it worked just fine. Pretty sure your issue is that your files are ADF (Esri Grid files, not a TIF) so simply change this line

raster_list = arcpy.ListRasters("*.tif")

to this

raster_list = arcpy.ListRasters("*")

And it should work. I had no problem with it working.

2
  • I checked it again, no output! I tried to build an attribute table using Copy Raster Tool and run the script and still no output in the dbf.
    – Daniel
    Jun 17 '20 at 13:32
  • @Daniel - that is strange! I just ran it again and it works fine for me. Here's the script I used: gisdiva.com/downloads/stackx/Zonal !screenshot
    – JamiRae
    Jun 18 '20 at 14:40

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.