Use Con with two rasters

1297
6
06-30-2021 06:35 AM
mody_buchbinder
Occasional Contributor III

Hello all

I am trying to write a python program that will create a new raster by condition of two rasters.

I can do it with the con function like this:

res = arcpy.sa.Con((Raster1 == 0 & Raster2 == 0),1,0)

For the con function you need to create a Raster() before you can use it and then save the results.

From the other hand arcpy.gp.con_sa  can get a path of tif as parameter and the final result name is parameter.

I could not find the way to put two rasters in the condition (it is usually "value == 0" of the raster in the new parameter.

Any ideas?

 

0 Kudos
6 Replies
JayantaPoddar
MVP Esteemed Contributor

Not sure if I am able to understand the concern.

Are you looking for an expression like?

 

arcpy.sa.Con(((Raster1 == 0) & (Raster2 == 0)), 1, 0)

 

 

 



Think Location
0 Kudos
DavidPike
MVP Frequent Contributor

What do you mean by python program? If you mean as a python script tool, can't the output filename when using .save() just be used as a parameter?  If you mean as a GP tool/task, then the result object of .save() would just be put as arcpy.SetParameter(x) and the output set as derived (a while since I've done that so may not be exactly correct).

0 Kudos
DanPatterson
MVP Esteemed Contributor

Did you try something like this... pick one or the other to see if it works... they lead to the same code in any event

 

r0 = "c:/path/some.tif"
r1 = "c:/path/another.tif"
arcpy.gp.con_sa((Raster(r0) == 0) & (Raster(r1) == 0), 1, 0)
# ---- or!!
arcpy.sa.Con((Raster(r0) == 0) & (Raster(r1) == 0), 1, 0)

 


... sort of retired...
0 Kudos
mody_buchbinder
Occasional Contributor III

Hi Dan

Please read my previous question here: https://community.esri.com/t5/python-questions/what-is-the-way-to-call-spatial-analysis-tools-in-arc...

The arcpy.sa.con_sa have different parameters and the first parameter is the input raster so you cannot gives two rasters.

I will look into the numpy

Thanks

0 Kudos
DanPatterson
MVP Esteemed Contributor

Mody, if you want to bring the raster back in, check the optional parameters for rastertonumpyarray and numpyarraytoraster

RasterToNumPyArray—ArcGIS Pro | Documentation

RasterToNumPyArray (in_raster, {lower_left_corner}, {ncols}, {nrows}, {nodata_to_value})

NumPyArrayToRaster—ArcGIS Pro | Documentation

NumPyArrayToRaster (in_array, {lower_left_corner}, {x_cell_size}, {y_cell_size}, {value_to_nodata}, {mdinfo})

 

This allows you to maintain the spatial location and nodata values.

the lower left corner has examples in the help eg

lowerLeft = arcpy.Point(in_raster.extent.XMin, in_raster.extent.YMin)


... sort of retired...
0 Kudos
DanPatterson
MVP Esteemed Contributor

or a numpy lesson... verbose, showing that arcpy plays nice with numpy

a1 = arcpy.RasterToNumPyArray("c:/temp/a0.tif")
b1 = arcpy.RasterToNumPyArray("c:/temp/b0.tif")
whr = np.logical_and((a1==0), (b1==0))
out = np.where(whr, 1, 0)

a1 
array([[1, 0, 3, 0],
       [3, 2, 1, 1],
       [4, 3, 0, 3],
       [2, 0, 0, 1],
       [2, 4, 2, 1]])

b1 
array([[1, 1, 2, 4],
       [3, 1, 0, 3],
       [4, 2, 0, 1],
       [3, 4, 1, 2],
       [1, 0, 2, 0]])

whr 
array([[False, False, False, False],
       [False, False, False, False],
       [False, False,  True, False],
       [False, False, False, False],
       [False, False, False, False]])

out
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 1, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]])

my favorite raster goto 😉


... sort of retired...
0 Kudos