0

When we have a shapefile we can add a field (e.g. "X") and then calculate it by using a Python code block inside the Calculate Field tool.

A visual example could be this one:

def x(w, v):
 if w ==2: return 6
 elif v >= 0.420001 : return 5
 elif  v >= 0.340001 and v<0.420001 : return 4
 elif  v >= 0.260001 and v< 0.340001 : return 3
 elif  v >= 0.180001 and v< 0.260001 : return 2
 else: return 1 

However, if I want to do something similar (see the code below) for a raster dataset (with two bands), the Raster Calculator and Con tools cannot provide such an option.

def reclassify (band1, band2):
    if band1 == 3:return 3
    elif band2 == 1: return 3
    elif band2 == 2: return 2
    elif band1 ==1 and band2 == 3:return 1
    elif band1 == 2 and band2 == 3:return 2
    else: return 0

Do you have any idea or plausible tool to do this in a raster?

P.S. A possible solution could be to convert the raster to polygon and then use the calculate field tool as shown in the example. However, I wanted to know if there is an other way by avoiding convert it into a feature.

4
  • 2
    You need to edit your question and show what it is you want to do with the rasters, because "I want to do something similar" can be interpreted in all sorts of ways...
    – Hornbydd
    Commented Aug 19, 2021 at 11:46
  • 1
    Look at the last example of Con
    – Bera
    Commented Aug 19, 2021 at 13:18
  • @Hornbydd Ok, you are right. I will edit it and make it more concrete. Commented Aug 19, 2021 at 14:00
  • 1
    @BERA Ok, I will give it a shot and see what I can do. Thank you Commented Aug 19, 2021 at 14:01

1 Answer 1

0

I finally found the answer which definitely works for my case and quite similar cases after checking all layers in ArcMap.

In particular, instead of two bands in one raster dataset, I used two raster datasets where raster dataset 1 == band1 and raster dataset 2 == band2 and I found the solution through the Raster Calculator tool.

So to implement the following function for raster datasets,

def reclassify (band1, band2):
    if band1 == 3:return 3
    elif band2 == 1: return 3
    elif band2 == 2: return 2
    elif band1 ==1 and band2 == 3:return 1
    elif band1 == 2 and band2 == 3:return 2
    else: return 0

if band1 and band2 are two different raster datasets as described above, you could write the following lines inside the Raster Calculator tool:

Con(("%band1.tif%" == 3),3, Con(("%band2.tif%" == 1), 3, Con(("%band2.tif%"== 2), 2, Con(("%band1.tif%" == 1) & ("%band2.tif (2)%" == 3), 1, Con(("%band1.tif%" == 2) & ("%band2.tif%" == 3), 1, 0)))))

P.S. "%......%" are being written automatically inside the Raster Calculator box when you select the raster dataset (band1 and/or band2) which should be open in ArcMap.

Hope this would be helpful.

Your Answer

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

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