2

I am running this Zonal Statistics as a Table code for a single raster and it works:

import arcpy, os 
destination_path= r'\\Mac\Home\Desktop\arc_stuff' 
shapefile= r'\\Mac\Home\Desktop\arc_stuff\subfolder1\adm_shp_all_selection.shp'
arcpy.env.workspace=r'\\Mac\Home\Desktop\arc_stuff' 
raster='lights_1992.tif'
outZSaT = arcpy.gp.ZonalStatisticsAsTable_sa(shapefile, "unique_id", raster,'lights_1992.dbf',"DATA","SUM")

I'll have to process many rasters, hence I tried building the loop below, but I always get this error:

ERROR 999999: Error executing function. Create output table failed Failed to execute (ZonalStatisticsAsTable).

This is the loop:

import os
import arcpy, os
destination_path=r'\\Mac\Home\Desktop\arc_stuff\output' 
shapefile= r'\\Mac\Home\Desktop\arc_stuff\subfolder1\adm_shp_all_selection.shp'
arcpy.env.workspace=r'\\Mac\Home\Desktop\arc_stuff'
rasters = arcpy.ListRasters("lights_*.tif")
for raster in rasters:
    print('Destination Path: %s\nRaster: %s\nDestination File: %s\n' % (destination_path, raster, destination_file))
    destination_file = os.path.join(destination_path, raster + ".dbf")
    outZSaT = arcpy.gp.ZonalStatisticsAsTable_sa(shapefile, "unique_id", raster, destination_file,"DATA","SUM")

I am aware of this related thread: Batch Zonal Statistics as table?, but I think the issues pointed out there do not apply to my problem.

Who can help?

4
  • Does your code work if you copy your data to a local drive?
    – PolyGeo
    Commented Oct 20, 2016 at 20:36
  • For the single raster yes, for the loop no.
    – cecefuss
    Commented Oct 21, 2016 at 9:10
  • I have a couple things for you to try: "from arcpy.sa import *"; "arcpy.CheckOutExtension("Spatial")"; "outZSat = ZonalStatisticsAsTable(shapefile, "unique_id", raster, destination_file,"DATA","SUM")"
    – jbalk
    Commented Oct 24, 2016 at 4:47
  • Also try creating a feature layer from the shapefile and using that as your 'In_zone_data". It specifies that it only accepts a raster layer or feature layer.
    – jbalk
    Commented Oct 24, 2016 at 4:49

1 Answer 1

1

You may need to check out the extension.

Also, the in_zone_data only accepts a raster layer or feature layer. I've edited your code to include these changes.

Try changing your code to this:

import os
import arcpy
from arcpy.sa import *
arcpy.CheckOutExtension("Spatial")
destination_path=r'\\Mac\Home\Desktop\arc_stuff\output' 
shapefile= r'\\Mac\Home\Desktop\arc_stuff\subfolder1\adm_shp_all_selection.shp'
arcpy.MakeFeatureLayer_management(shapefile,"layer")
layer = arcpy.mapping.Layer("layer")
arcpy.env.workspace=r'\\Mac\Home\Desktop\arc_stuff'
rasters = arcpy.ListRasters("lights_*.tif")
for raster in rasters:
    print('Destination Path: %s\nRaster: %s\nDestination File: %s\n' % (destination_path, raster, destination_file))
    destination_file = os.path.join(destination_path, raster + ".dbf")
    outZSat = ZonalStatisticsAsTable(layer, "unique_id", raster, destination_file,"DATA","SUM")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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