0

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?

2 Answers 2

2

I think you might need double brackets here:

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

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

6
  • I think RemapRange requires spatial analyst - maybe check out the extension before hitting that method?
    – Jvhowube
    Aug 3, 2018 at 15:12
  • I have checked out the Spatial extension (see towards the end of script)
    – tg110
    Aug 3, 2018 at 15:16
  • Right but do it before you run RemapRange, as scripting is linear.
    – Jvhowube
    Aug 3, 2018 at 15:21
  • DId not help. Same 99999 error.
    – tg110
    Aug 3, 2018 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, 2018 at 15:40
0

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

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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