1

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.

Can anyone help me, please?

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
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jun 25, 2015 at 14:26
1
  • 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. Commented Jun 29, 2015 at 3:11

1 Answer 1

1

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--------------------------------------")
answered Jun 26, 2015 at 1:48
5
  • 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(). Commented Jun 26, 2015 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). Commented Jun 26, 2015 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... Commented Jun 26, 2015 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. Commented Jun 26, 2015 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. Commented Jun 28, 2015 at 15:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.