2

I need to reclassify certain values to different values in a raster using Python in ArcGIS Pro 2.2.2. I can't use reclassify because I don't want to change all values and I can't go through each one of them because there is a lot. When I use Raster calculator I get an attribute error.

Is there a way to do that?

4

You can use this approach Esri provide for ArcGIS Pro Reclassify

Reclassifies (or changes) the values in a raster.

I would suggest using the code samples they provide as guidance as to how you can build your python code to reclassify the values in your raster

import arcpy
from arcpy import env  
from arcpy.sa import *
env.workspace = "C:/sapyexamples/data"

outReclass1 = Reclassify("landuse", "Value", 
                    RemapValue([[1,9],[2,8],[3,1],[4,6],[5,3],[6,3],[7,1]]))
outReclass1.save("C:/sapyexamples/output/landuse_rcls")

outReclass2 = Reclassify("slope_grd", "Value", 
                     RemapRange([[0,10,"NODATA"],[10,20,1],[20,30,2],
                                 [30,40,3],[40,50,4],[50,60,5],[60,75,6]]))
outReclass2.save("C:/sapyexamples/output/slope_rcls")

outReclass3 = Reclassify("pop_density", "Value", 
                     RemapRange([[10,10,1],[10,20,2],[20,25,3],
                                 [25,50,4],[50,]]), "NODATA")
outReclass3.save("C:/sapyexamples/output/popden_rcls")
  • Thank you @whyzar for your help! What happens to the values that I want to keep them as they are without reclassification? – MJA Sep 20 '18 at 17:32
  • Reclassify worked. However, I think for ArcGIS Pro the above syntax did not work or maybe because I am using arcpy. I had to use arcpy.reclassify_3d (InputRaster, field, "from to new value;from to new value", Output). Sorry I am still a beginner. – MJA Sep 20 '18 at 18:24
0

Edit: I added this solution before the ArcGIS Pro tag, but here's a GDAL/Numpy solution:

You can could use gdal and numpy to reclassify your raster.

First open the file and read as array:

arr = gdal.Open("/path/to/your/file").ReadAsArray()

Then use numpy to classify certain pixel ranges:

Example:

>>> arr = np.random.randint(100, size = (5,5))
>>> arr
array([[30, 74,  6,  4, 94],
       [45, 13, 15,  5, 35],
       [26, 48, 17, 30, 24],
       [56, 24, 19, 11, 39],
       [70, 18, 31, 59, 24]])
>>> arr[arr<50]= 0
>>> arr
array([[ 0, 74,  0,  0, 94],
       [ 0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0],
       [56,  0,  0,  0,  0],
       [70,  0,  0, 59,  0]])

Then write the array back to file:

def array2raster(rasterfn,newRasterfn,array):
    raster = gdal.Open(rasterfn)
    geotransform = raster.GetGeoTransform()
    proj = raster.GetProjection()
    originX = geotransform[0]
    originY = geotransform[3]
    pixelWidth = geotransform[1]
    pixelHeight = geotransform[5]
    cols = raster.RasterXSize
    rows = raster.RasterYSize

    driver = gdal.GetDriverByName('GTiff')
    outRaster = driver.Create(newRasterfn, cols, rows, 1, gdal.GDT_Float32) # Change dtype here
    outRaster.SetGeoTransform((originX, pixelWidth, 0, originY, 0, pixelHeight))
    outband = outRaster.GetRasterBand(1)
    outband.WriteArray(array)

    outRaster.SetProjection(proj)
    outband.FlushCache()

array2raster("/path/to/raster.tif", "/path/to/newraster.tif", array)

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.