ORIGINAL QUESTION
Im relatively new to python and these fourms but Im am having trouble utilizing for-loops for spatial analyses within ArcGIS. It does not appear to recognize the variable, RasterImage!
Here is a part of the code im having trouble with...
import arcpy, os, sys, string
from arcpy import env
from arcpy.sa import *
arcpy.CheckOutExtension("spatial")
arcpy.env.workspace = r'O:\DATA\test'
InputFolder = arcpy.env.workspace
Temp4 = InputFolder + '\\' + 'temp4'
RasterList = arcpy.ListRasters()
for RasterImage in RasterList:
print (RasterImage)
Temp4 = FocalStatistics('RasterImage', NbrRectangle (9, 9, 'CELL'), "RANGE", "DATA")
print ('Focal Statistics Done')
I receive the following error
"Traceback (most recent call last):
File "O:\test.py", line 67, in
Temp4 = FocalStatistics('RasterImage', NbrRectangle (9, 9, 'CELL'), "RANGE", "DATA")
File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\sa\Functions.py", line 4796, in FocalStatistics
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 4790, in wrapper
ignore_nodata)
File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing\_base.py", line 474, in
return lambda *args: val(*gp_fixargs(args))
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000865: Input raster: RasterImage does not exist.
Failed to execute (FocalStatistics)."
It does not seem to recognize my for in loop of the RasterImage - any ideas? The print statment after the loop shows that it is recognizing the file (in python) but that the spatial analyses used in arcpy are not.
I thought it might have to do something with the workspace used so I thought that adding
RasterImage = InputFolder + '\\' + RasterImage
might help locate the full path of the raster datasets but to no avail! Im lost for ideas and the only help files that utilize spatial analyses on the ArcGIS resource page do not utilize loops in their scripts. ¨
UPDATE
Issue has been resolved by implementing the following:
The original question displaying the error 'RasterImage' does not exist was cause in part due to the '' marks and the spaces around the NbrRectangle. I have implemented part of my new code using the suggestions by nmpeterson whereby the geoprocessing is called through the function
def focalStats(raster):
Temp4 = FocalStatistics(raster, NbrRectangle(9, 9, 'CELL'), "RANGE", "DATA")
return Temp4
While methodologies in which several temporary files rely on one another (i.e. a geoprocess within another geoprocess) i've used the .save function into a temporary directory as shown by om_henners before deleting all those contents afterwards.
Answer
In the line Temp4 = FocalStatistics('RasterImage', NbrRectangle (9, 9, 'CELL'), "RANGE", "DATA")
, you need to remove the single-quotes around RasterImage
-- it is being treated as a string literal and looking for a file called RasterImage.
Edit: Thinking about this some more, I'm wondering if the script is failing because you initialize Temp4 as a string but then redefine it as whatever the return type of FocalStatistics is -- I'm not sure if it's handled as a string or not.
Try this:
import arcpy, os, sys, string
from arcpy import env
from arcpy.sa import *
arcpy.CheckOutExtension("spatial")
arcpy.env.workspace = r'O:\DATA\test'
InputFolder = arcpy.env.workspace
def focalStats(raster):
Temp4 = FocalStatistics(raster, NbrRectangle(9, 9, 'CELL'), "RANGE", "DATA")
return Temp4
RasterList = arcpy.ListRasters()
for RasterImage in RasterList:
print (RasterImage)
focalStats(RasterImage)
print ('Focal Statistics Done')
No comments:
Post a Comment