# Getting aspect and reclassifying raster files using Python in a loop

By using Python in PythonWin 2.7.3, I tried to obtain aspect files of a list of raster files in a folder(called database_Dem20) and save it to another folder, database_Dem20/testOutput. In the same loop, after getting the aspect file, i want to reclassify this aspect file by using reclassify toolbox.

The first part, getting aspect, is doing fine. However, the reclassify part has error, ERROR 000622: Failed to execute (Reclassify). Parameters are not valid.

I have read the syntax from Esri website for many times and tried to type in different ways but still couldn't figure out where is wrong.

Here is my Python scripts:

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

# Check out any necessary licenses
arcpy.CheckOutExtension("spatial")
arcpy.env.overwriteOutput = True

# Set Geoprocessing environments
env.workspace = "C:\\database_Dem20"
targetWorkspace = "C:\\database_Dem20\\testOutput"
print "==============================NEW START================================"

# Process: Aspect
# Get and print a list of TIFs from the workspace
rasters = arcpy.ListRasters("*", "TIF")

for raster in rasters:
inRaster = raster
outAspect = Aspect(inRaster)
outAspect.save(targetWorkspace + "\\AA_" + inRaster)

print("------------------------Reclassify---------------------------------------")
# Process: Reclassify
inAspect = str(outAspect)
remap = RemapRange([[-1, -9.9999999999999995e-007, 1],[-9.9999999999999995e-007, 22.5, 2],[22.5, 67.5, 3],[67.5, 112.5, 4],[112.5, 157.5, 5],[157.5, 202.5, 6],[202.5, 247.5, 7],[247.5, 292.5, 8],[292.5, 337.5, 9],[337.5, 360, 10]])
outReclassify = Reclassify(inAspect, "Value", remap ,"NODATA")
outReclassify.save(targetWorkspace + "\\BB_" + inAspect)
print("-------------------------Reclassify--------------------------------------")
del raster
del rasters

• I find out where is the problem today. The problem is remap. I changed [-1, -9.9999999999999995e-007, 1] into [-1, -0.00001, 1]. Then, this script works. I guess arcpy couldn't read -9.9999999999999995e-007. On the other hand, there is no need to write "inAspect = str(outAspect)", just type outAspect into outReclassify, instead. – NewHere Jun 29 '15 at 3:11

New to ArcGis 10+ is the Raster object... this needs a bit of an idiom shift to get used to it. To turn a file path into a raster use arcpy.Raster("d:\\path\\to\\raster.ext") or just "raster.ext" if it's in your current arcpy.env.workspace. This also means that you need to get rid of these objects using del.

Some tools will work with either a path or a raster object as input but the arcpy.sa outputs are now pretty much exclusively raster objects.

As for your script I see that there is a problem with trying to access a raster that's in a different workspace (folder).. but still I'm not entirely sure what str() of a raster object gives you.. the data itself is in your %TMP% directory, in a folder called arcXXX which is unique for the session (if the string of a raster object returns anything at all).

How about trying it this way:

for raster in rasters:
inRaster = raster
outAspect = Aspect(arcpy.Raster(inRaster)) # would still work fine as a string
outAspect.save(targetWorkspace + "\\AA_" + inRaster)

print("------------------------Reclassify---------------------------------------")
# Process: Reclassify
# inAspect = str(outAspect) # here is your problem, it's in a different workspace so it can't be found

remap = RemapRange([[-1, -9.9999999999999995e-007, 1],[-9.9999999999999995e-007, 22.5, 2],[22.5, 67.5, 3],[67.5, 112.5, 4],[112.5, 157.5, 5],[157.5, 202.5, 6],[202.5, 247.5, 7],[247.5, 292.5, 8],[292.5, 337.5, 9],[337.5, 360, 10]])
outReclassify = Reclassify(outAspect, "Value", remap ,"NODATA") # why not use the raster object here..
del outAspect # finished with this now
outReclassify.save(targetWorkspace + "\\BB_" + inAspect) # no extension, will save as GRID
del outReclassify # finished with this now
print("-------------------------Reclassify--------------------------------------")

• Thank you for your response. Yes, I have tried to use outAspect in Reclassify directly, just like what you have typed here. But it doesn't work, that why i tried to use str(). – NewHere Jun 26 '15 at 2:46
• perhaps it's getting a bit confused, try putting arcpy.sa. in front of the Reclassify, unless there's a problem in your RemapRange. Consider doing the Remap in model builder, exporting to python script and open that to see how it's done in model builder (sometimes what works and what's in the help is slightly different). – Michael Stimson Jun 26 '15 at 2:56
• mmm...it is really confusing. After it worked successfully in Model Builder, i export the python scripts and tried to run it in PythonWin 2.7.3 and Python 2.7.3. It failed. I am pretty sure that the model builder fully works. What is wrong with PythonWin and Python... – NewHere Jun 26 '15 at 4:08
• It might be one of those rare cases where even model builder gets it wrong. Your range object looks fine, perhaps then change to arcpy.sa.Reclassify(targetWorkspace + "\\AA_" + inRaster,....) and hope that the string will work. – Michael Stimson Jun 26 '15 at 4:12
• Thank you for all your assistance. It still doesn't work. But i can use model builder to run it at least. – NewHere Jun 28 '15 at 15:34