1

I have three rasters of soil granulometric fractions (clay, sand and silt), all in %. I want to make a single raster of the soil texture classes. For that, I need to make some conditionals (i. e.: when sand > 90 and clay < 10, so the class in the new raster must have the value 1).

Any suggestion? I'm using Arcgis 10.6.

  • Do they have the same extent and resolution? – BERA Feb 15 at 12:55
  • 1
    Did you take a look at the raster calculator of arcgis ? – nmatton Feb 15 at 12:58
  • @BERA yes. All the rasters hava the same resolution. – Diêgo Araújo Feb 15 at 13:00
3

Try converting your rasters to numpy arrays, build a classifying function and convert resulting Array back to raster. (This way you dont need Spatial Analyst extension)

Example with two random rasters with values from 0-1. Modify the function etc:

import numpy as np
import arcpy

raster1 = "randraster_1" #Change
raster2 = "randraster_2" #Change
output_raster = r'C:\Default.gdb\resultraster123' #Change

arr1 = arcpy.RasterToNumPyArray(raster1)
arr2 = arcpy.RasterToNumPyArray(raster2)

def whatsoil(a,b):
    if a>b:
        return 1
    elif a<b:
        return 2
    else:
        return 3
vwhatsoil = np.vectorize(whatsoil)

arr3 = vwhatsoil(arr1, arr2)

desc = arcpy.Describe(raster1+r'/Band_1')
arcpy.env.outputCoordinateSystem = desc.spatialReference

resultraster = arcpy.NumPyArrayToRaster(in_array=arr3, lower_left_corner=desc.extent.lowerLeft, x_cell_size=desc.meanCellHeight, y_cell_size=desc.meanCellWidth)
resultraster.save(output_raster)

enter image description here

  • if you're using numpy, you'll likely get much better performance with numpy.where over your whatsoil function – Paul H Feb 15 at 17:59
  • Ok! I did not know how to do it with 3 input arrays and many if elses so i used a function instead – BERA Feb 15 at 18:01
  • 1
    You should be able to numpy.where(a > b, 1, numpy.where(a < b, 2, 3)). numpy.select works in this case too. – Paul H Feb 15 at 23:13
2

you can also use the raster calculator, but it quickly creates long statement

Map algebra > raster calculator, for example :

Con(("sand" > 90) and ("clay" <10 ), 1, Con(("silt">50) and ("clay"<30),2,3)

The syntax is Con (condition, value if true, value if false)

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.