"Failed to open raster dataset" in zonal statistics

2825
13
07-11-2012 07:11 AM
RPatarasuk
New Contributor II
Hi,

I am trying to use zonal statistics to calculate the average amount of rainfall for each county. Here is my code:

# Import arcpy module
import arcpy
from arcpy import env
from arcpy.sa import *

# Check out any necessary licenses
arcpy.CheckOutExtension("spatial")

# Input data source
arcpy.env.workspace = "S:/Work/Risa/Trials_Errors/InputFiles02"
arcpy.env.overwriteOutput = True

ZoneShapefile = "S:/Work/Risa/USA Boundary_AlbersNAD83/PINEMAP_Counties_USCensus.shp"

# Output File
OutputFolder = "S:/Work/Risa/Trials_Errors/ZonalStats"

# Loop through a list of files in the workspace
RasterFiles = arcpy.ListRasters()

# Local variables:
for filename in RasterFiles:
    print("Processing: " + filename)
    inRaster = arcpy.env.workspace + "/" + filename
    fileroot = filename
    outRaster = OutputFolder + "/" + "Avg_" + fileroot + ".tif"
    
    # Process: Zonal Statistics
    arcpy.gp.ZonalStatistics_sa(ZoneShapefile, "FID", inRaster, outRaster, "MEAN", "DATA")

print " "
print ":o) End Processing :)"



The problem is that when I run it, it creates this error:


Processing: ppt_1970_01.tif

Traceback (most recent call last):
File "S:/Work/Risa/Python codes/zonalStats_test.py", line 36, in <module>
arcpy.gp.ZonalStatistics_sa(ZoneShapefile, "FID", inRaster, outRaster, "MEAN", "DATA")
File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing\_base.py", line 474, in <lambda>
return lambda *args: val(*gp_fixargs(args))
ExecuteError: ERROR 999999: Error executing function.
Failed to open raster dataset
Failed to execute (ZonalStatistics).

What is weird is that every time when I run the program, it creates a raster file in the input folder, which I didn't specify. e.g. "t_t126". If I hit run again, will create another file and so on. These files don't give me the average value by the county either. It gave me a weird number. If the program runs properly, it should create "Avg_ppt_1970_01.tif" in the output folder.

However, if I run one file at a time (exported from Model Builder), it works fine. Here's the code that works.

# Import arcpy module
import arcpy

# Check out any necessary licenses
arcpy.CheckOutExtension("spatial")


# Local variables:
PINEMAP_Counties_USCensus = "S:/Work/Risa/USA Boundary_AlbersNAD83/PINEMAP_Counties_USCensus.shp"
arcpy.env.overwriteOutput = True
ppt_1970_01_tif = "S:/Work/Risa/Trials_Errors/InputFiles02/ppt_1970_01.tif"
ZonalSt_shp2 = "S:/Work/Risa/Trials_Errors/ZonalStats/ZonalSt_shp.tif"

# Process: Zonal Statistics
arcpy.gp.ZonalStatistics_sa(PINEMAP_Counties_USCensus, "FID", ppt_1970_01_tif, ZonalSt_shp2, "MEAN", "DATA")

print "Done!"



So, I don't get it why it says "Failed to open raster dataset".

Here are the sample files (see the attachments)


Any thoughts and help would be pretty much appreciated.

Thank you.
Tags (2)
0 Kudos
13 Replies
MathewCoyle
Frequent Contributor
I'm not sure what your issue would be here. This method works for me. I came across this old forum thread with a similar issue, could it be related?
http://forums.esri.com/Thread.asp?c=93&f=995&t=283135
0 Kudos
RPatarasuk
New Contributor II
Dear Matt,

Yep, I saw that post. I will change the computer and see if it works. Wow..ESRI bugging problems are a waste of time!! I will let you know if it works or not. Thank you so much.

Risa
0 Kudos
RPatarasuk
New Contributor II
HI Matt,

yeah...i changed the computer and it is still gives me the same error. Hopefully, someone else will  comment on my post.

I tried to specify the individual filename and it doesn't read the file name, it reads as 'band 1', which is weird. I changed this line:

arcpy.env.workspace = "S:/Work/Risa/Trials_Errors/InputFiles02/ppt_1971_07.tif"


and the output is:

[u'Band_1']
 
Processing: Band_1

Traceback (most recent call last):
  File "S:\Work\Risa\Python codes\zonalStats_test02.py", line 39, in <module>
    saveRaster = arcpy.sa.ZonalStatistics(ZoneData, "ZoneField", inRaster, "MEAN", "DATA")
  File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\sa\Functions.py", line 5787, in ZonalStatistics
    ignore_nodata)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\sa\Utils.py", line 47, in swapper
    result = wrapper(*args, **kwargs)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\sa\Functions.py", line 5780, in wrapper
    ignore_nodata)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing\_base.py", line 474, in <lambda>
    return lambda *args: val(*gp_fixargs(args))
ExecuteError: ERROR 999999: Error executing function.
Failed to open raster dataset
Failed to execute (ZonalStatistics).
0 Kudos
RPatarasuk
New Contributor II
Dear Matt,

The code works but I had to move the files to the local drive. I think there might be something wrong with the program reading from our server. Thank you very much for your help.

Risa
0 Kudos