2

I'm trying to run code to automate getting zonal statistics from a folder with almost 50 shapefiles and 6 rasters from another folder. So, each shapefile needs to do the zonal statistics on each raster in another folder. The code runs without error but the output is not created. What's happening? This is the code:

import arcpy
import os
arcpy.env.overwriteOutput = True
output = "D:\pontos_modelagem\buffer\dbf"
arcpy.env.workspace = (r'D:\slope')
rasters = arcpy.ListRasters()
arcpy.env.workspace = (r'D:\pontos_modelagem\buffers')
shapefiles = arcpy.ListFeatureClasses()
listshps = []
for shp in listshps:
    if shp.startswith('.shp'):
        for rst in rasters:  
            table_out = output + "\\" + shp + "_Zonal_Est"   
            arcpy.gp.ZonalStatisticsAsTable_sa(shp,'FID',rst, table_out,"NODATA","MEAN")
0

1 Answer 1

5

You still have logical errors in your code.

  1. You list the feature classes and do nothing with the data.

    shapefiles = arcpy.ListFeatureClasses()

  2. You initialize an empty list and then try to iterate. There is nothing to iterate, nothing below this point is executed.

    listshps = []

    for shp in listshps:

  3. The if statement has the wrong logic. Shapefiles end with '.shp'

    if shp.startswith('.shp'):

  4. The raster iterator will only give you the raster name. You will need to pass in the full path to zonal statitics as you changed your workspace on line 7

Next items are not fatal erors but you might want to consider them

  1. The table_out is going to have odd extension as you are not removing the .SHP extension

  2. You are assuming you have a Spatial Analyst license available. You might want to consider checking a license in and out if you are running from a standalone script.

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.