Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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 <module>
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 <lambda>
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.

share|improve this question
add comment

2 Answers 2

up vote 5 down vote accepted

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')
share|improve this answer
    
Thanks for your suggestion but i've tried to remove the single quotes around the RasterImage with no avail. The reason I added it was due to the suggestion on help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//… –  BJEBN Nov 29 '11 at 17:35
    
The only case on that page using quotes (that I can see) is the elevation example, where "elevation" is the actual filename of the raster in the workspace. Have you also tried removing the space between NbrRectangle and the following (? –  nmpeterson Nov 29 '11 at 17:39
    
I edited my original answer with a new suggestion: creating a new function to avoid having to define Temp4 outside of the for-loop. –  nmpeterson Nov 29 '11 at 17:53
    
Alright I will try that again tomorrow... thanks for your response once more! –  BJEBN Nov 29 '11 at 18:00
    
Did this solve your problem? –  nmpeterson Nov 30 '11 at 15:20
add comment

As @nmpeterson says above, the first issue comes down to the quotes around the RasterImage variable name.

However the big thing is that you are overwriting the variable Temp4 with the contents of the raster. Instead, according to the FocalStatistics documentation from ESRI, the raster object returned has a save(path) method.

I'm assuming you don't want to overwrite your output each time, so one quick way of getting a temporary name is in Python using tempfile.NamedTemporaryFile(dir="working_dir").name - however this won't store a reference to the original Raster, which I'll assume you want, so we can do it slightly differently as follows (assuming you're using Info rasters - if you're using a .tif, .png etc you'll have to strip the file extension before creating the new file):

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

arcpy.CheckOutExtension("spatial")

input_folder = r'O:\DATA\test'
arcpy.env.workspace = input_folder

raster_list = arcpy.ListRasters()
for raster_image in raster_list:
    print (raster_image)
    f_stats = FocalStatistics(raster_image, NbrRectangle(9, 9, 'CELL'), "RANGE", "DATA")
    f_stats_out = input_folder + os.sep + raster_image + "_f_stats.tif"
    f_stats.save(f_stats_out)
    print ('Focal Statistics Done')

One final time - standard Python convention is to use lower case for variable names with underscores (see PEP 8) but in the end it's not a big thing.

share|improve this answer
    
Of course I can't really speak for @BJEBN, but I don't think this is necessary in this case. Since the file is clearly a temp file (as evidenced by the name "temp4"), there is no reason to ever save the file -- whether temporary or not. Rather, the input raster for the next process (which was left out of the example here) can simply be specified by the FocalStatistics() function (or the focalStats() function I wrote above), since the processing has to occur within the for-loop anyway. –  nmpeterson Nov 29 '11 at 23:27
    
Ideally I would like to utilize those as temp files rather than saving the file...however i've found that when one has several temp files that rely on one another - this cannot be done without saving the file first. i.e. def rc(raster): Temp3 = Raster(FocalStats(raster)) / 2 return Temp 3 where FocalStats refers to that suggested by nmpeterson above. As for the original question - it was a combination of the '' around the rasterimage and the space around NbrRectangle –  BJEBN Nov 30 '11 at 11:08
    
Well, one possibility would be to add the saved raster datasets to a list, and then after processing loop through them and delete them with arcpy.Delete_management(dataset) –  om_henners Nov 30 '11 at 12:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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