I am trying to reclassify a raster, using arcpy in Python. My code is below

from osgeo import gdal
import arcpy
from arcpy import env
from arcpy.sa import *


raster_file = 'M:/tgopalakrishna\Data\CONUS_def_001_in_020\CONUS_def_001_in_020.tif'

#Opening the raster file
dataset = gdal.Open(raster_file)
band = dataset.GetRasterBand(1)
print band

#Reading the raster properties
projectionfrom = dataset.GetProjection()
print projectionfrom
geotransform = dataset.GetGeoTransform()
print geotransform

block_sizes = band.GetBlockSize()
x_block_size = block_sizes[0]
print x_block_size
y_block_size = block_sizes[1]
print y_block_size

xsize = band.XSize
print xsize
ysize = band.YSize
print ysize

max_value = band.GetMaximum()
print max_value
min_value = band.GetMinimum()
print min_value

stats= band.GetStatistics(0,1) 
print stats # prints min, max, mean and std

# Set local variables
reclassField = "VALUE"
remap = RemapRange([0,999,1])

# Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")

outReclassify = Reclassify(raster_file, reclassField, remap, "NODATA")

outReclassify.save("M:/tgopalakrishna\Outputs\Defended_state/trial.tif") 

The errors I get are

Traceback (most recent call last): File "*************trial.py", line 41, in remap = RemapRange([0,999,1])

File "D:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\sa\ParameterClasses.py", line 1160, in init _Remap.init(self, remapTable)

File "D:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\sa\ParameterClasses.py", line 1129, in init self.remapTable = [list(record) for record in remapTable]

TypeError: 'int' object is not iterable

So clearly, there is something wrong with the range of the reclassification. How do I solve this?

I think you might need double brackets here:

remap = RemapRange([[0,999,1]])

Because RemapRange expects a list of lists for the remapTable parameter.

  • I think RemapRange requires spatial analyst - maybe check out the extension before hitting that method? – Jvhowube Aug 3 at 15:12
  • I have checked out the Spatial extension (see towards the end of script) – tg110 Aug 3 at 15:16
  • Right but do it before you run RemapRange, as scripting is linear. – Jvhowube Aug 3 at 15:21
  • DId not help. Same 99999 error. – tg110 Aug 3 at 15:28
  • Not sure, but what's going on with your crazy forward/back slashes in your paths? Shouldn't they all be backslashes? – Jvhowube Aug 3 at 15:40

I ultimately ended up using the Con tool with syntax

outRas= Con(Raster(raster_file)>0 & Raster(raster_file)<999,1,0)

Above worked.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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