Geographic Information Systems Asked on February 27, 2021
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")
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.
Answered by JamiRae on February 27, 2021
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")
Answered by Daniel on February 27, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP