Can't save Zonal Statistics As Table

287
1
01-18-2012 08:19 AM
RameshGautam
New Contributor
I am trying to save Zonal Statistics Table, which is a mean of a raster images based on fields identified using Field_ID. Although, the code runs well, but it does not produce output result. Can you please find what???s wrong with this code?

Here is the code:

import arcpy
import sys, os, string
from arcpy.sa import *
from arcpy import env


#Get an specific raster image file from the folder
arcpy.env.workspace = "D:\Project\Stanislaus\Timeseries\NDVI"
LstRst = arcpy.ListRasters("*", "img")
fileprefix = "aug07_ndvi"
for Rst in LstRst:
    Img1 = string.find(Rst, fileprefix + "_stanislaus.img")
    if Img1 > -1:
        print Rst
            
#Get an specific field from a feature class file

arcpy.env.workspace = "D:\Project\Stanislaus\Timeseries"
LstFC = arcpy.ListFeatureClasses("*")
for fc in LstFC:
    print fc
   
inputFC = "StanislausAgFields.shp"
field = "Field_ID"
lstFlds = arcpy.ListFields(inputFC, field)
if len(lstFlds) == 0:
    arcpy.AddField_management(inputFC, field, "Text")
else:
    print "field already exists"
       
arcpy.CheckOutExtension("Spatial")
arcpy.env.overwriteOutput = 1

try:
   arcpy.AddMessage("Processing zonal means...")
   OutZsaT1 = ZonalStatisticsAsTable(inputFC, field, Rst, "outTable1", "DATA", "MEAN")
   outTable1.save("D:\Project\Stanislaus\Timeseries\aug07_zonalstat.dbf")

except:
    arcpy.AddMessage("Problem in attempting to calculate NDVI means. Check for existence of rasters")
    print arcpy.GetMessages()
    arcpy.AddMessage(arcpy.GetMessages())
    print arcpy.GetMessages()[/SIZE]

I tried both ways to save zonal statistics using the followings:

outTable1.save("D:\Project\Stanislaus\Timeseries\aug07_zonalstat.dbf")
OutZsaT1.save("D:\Project\Stanislaus\Timeseries\aug07_zonalstat.dbf")

However, I don't see "aug07_zonalsat.dbf" table in that directory.

Would appreciate for your help. thanks

RG
0 Kudos
1 Reply
DanPatterson_Retired
MVP Esteemed Contributor
For every file path listed in the script, eg.
"D:\Project\Stanislaus\Timeseries\NDVI"
you need to replace the backslashes with forward slashes or double backslashes ( \\ ) or place an r before the string ie.
r"D:\Project\Stanislaus\Timeseries\NDVI"
check the python documentation on string requirements and escape codes.
0 Kudos