4

Is there a better way to do this?

I'm trying to create a script that will change classifications of a raster to:

0 for: 0,7-9,15-20,40,62-63,73,78-87,89-130,132-151,153-175,177-203,215,228,251-253

1 for: all others

#RECLASSIFY LANDCOVER
#Considers 0 for:  63,81-83,87,111-112,121-124,141-143,190,195,     
#Considers 1 for:  All others.
#Considers "NODATA" for:  background, blank
import arcpy
from arcpy import env  
from arcpy.sa import *
env.workspace = r'C:\Erosion\LandCover'
outReclass1 = Reclassify("CDL_2014.tif", "Value", 
                         RemapRange([[0,0,"NODATA"],[7,9,"NODATA"],[15,20,"NODATA"],[40,40,"NODATA"],
                                     [62,62,"NODATA"],[63,63,0],[73,73,"NODATA"],[78,80,"NODATA"],[81,83,0],
                                     [84,86,"NODATA"],[87,87,0],[89,91,"NODATA"],[93,110,"NODATA"],[111,112,0],
                                     [113,120,"NODATA"],[121,124,0],[125,130,"NODATA"],[132,140,"NODATA"],
                                     [141,143,0],[144,151,"NODATA"],[153,175,"NODATA"],[177,189,"NODATA"],
                                     [190,190,0],[191,194,"NODATA"],[195,195,0],[196,203,"NODATA"],
                                     [215,215,"NODATA"],[228,228,"NODATA"],[251,253,"NODATA"]]), 1)
outReclass1.save(r'C:\Erosion\Output\CDL_reclass.tif')

I was attempting to modify the script from the ArcGIS Resource Center Desktop Help.

  • 4
    Define better: is the listed method slow, does it not work, are the values wrong? You could also use Reclassify by Table: resources.arcgis.com/en/help/main/10.1/index.html#//…, your values go into a table so you can reuse it. – Michael Stimson Mar 24 '15 at 4:46
  • @Michael Says invalid syntax on the last line. – rnr0004 Mar 24 '15 at 6:19
  • 1
    An attractive alternative to consider is InList. The syntax is clearer and more congenial. I haven't tested the performance relative to the other reclassification methods. – whuber Mar 24 '15 at 14:58
  • outReclass1.save(r'C:\output.gdb\RasterName'), you don't just need to tell it where you must also supply a dataset name. If you're saving to a folder location then you should include file extension, if you don't it will try to save as GRID which has severe limitations. – Michael Stimson Mar 24 '15 at 21:01
  • 2
    It's not bad you just can't save a file source (like tif, jpg, ecw, shp...) in a geodatabase. Everything that's stored in a geodatabase is a geodatabase type: Geodatabase Feature class, Geodatabase Raster... those types don't have extensions and you need ArcCatalog etc. just to see them. – Michael Stimson Mar 24 '15 at 21:32
2

You should save the output raster to a raster format, not to a geodatabase, e.g.:

outReclass1.save(r"C:\output\reclass.tif")

EDIT: And there is a missing bracket in the Reclassify line (RemapRange is enclosed but not Reclassify). I've updated my code as well.

You can also simplify the remap parameter a little bit by using the missing_values parameter for all other values that must be remapped to 1:

outReclass1 = Reclassify("raster.tif", "Value", 
                         RemapRange([[0,0,0],[7,9,0],[15,20,0],
                                     [40,40,0],[62,63,0],[73,73,0],[78,87,0],
                                     [89,130,0],[132,151,0],[153,175,0],
                                     [177,203,0],[215,215,0],[228,228,0],[251,253,0]]), 1)

See the Reclassify help page for more details and examples.

  • Still getting, "Parsing error SyntaxError: invalid syntax (line 10)" - which is for the outReclass1.save(r"C:\output\reclass.tif") – rnr0004 Mar 24 '15 at 12:12
  • Running it in IDLE shows outReclass1 in red. @GISGe – rnr0004 Mar 24 '15 at 13:44
  • What do you mean? If you have a closing bracket missing, the error will show up farther in the code, where it is expected to be found (not where it is actually missing). – GISGe Mar 24 '15 at 13:48
  • I got it working. Thanks. Only now it says, "The input raster minimum (1) is out of range. To ensure a correct reclassification, edit ranges or use a different precision settings in the Reclassification dialog box. The input raster maximum (238) is out of range. To ensure a correct reclassification, edit ranges or use a different precision settings in the Reclassification dialog box." – rnr0004 Mar 24 '15 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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