I am working on an assignment that requires us to reclass a raster(via python) from two rasters. We need to compare the power received at one cell(on raster1) and compare it to the same cell on raster 2.

If the value of that cell in raster1 is greater than the value in raster2, the cell would be assigned a value of "1" while a value in raster2 greater than raster1 would get a "2". We also need to reclassify certain values as "0" since they are considered out of range.

So the final raster would have three values: 0-out of range/no service 1-better value in raster1 2-better value in raster2

I can try to clarify further if needed.


1 Answer 1


Assuming you have spatial analyst you can create a difference raster and then follow with a nested Con statement to produce your result.

Activate spatial analyst

if arcpy.CheckExtension("Spatial") == "Available":
    print "Checking out Spatial"
    print "Unable to get spatial analyst extension"

First load your rasters as raster objects:

Ras2 = raster(r"d:\Some\path\raster.tif")
Ras1 = raster(r"d:\Some\Other\path\OtherRaster.tif")

then subtract and conditional

Diff = Ras2-Ras1
PreResult = Con((Ras2 <= -80) | (Ras1 <= -80) | IsNull(Ras2) | IsNull(Ras1),0,Diff)
Result = Con(PreResult == 0,0,Con(PreResult < 0,1,Con(PreResult > 0,2)))
Result.save(r"d:\Out\Path\OutRaster.tif") # save the raster as a file

The first two IsNull statements in the con cover areas of nodata in both input rasters so if one or the other is nodata the result is 0 then if the difference is 0 it is also 0, less than 0 is 1 and greater than 0 is 2.

Don't forget to free up spatial analyst when you're done: arcpy.CheckInExtension("Spatial")


Subtracting rasters http://resources.arcgis.com/en/help/main/10.1/index.html#//005m000000mn000000

isnull http://resources.arcgis.com/en/help/main/10.1/index.html#//009z000000m8000000

con http://resources.arcgis.com/en/help/main/10.1/index.html#//009z00000005000000

  • This works mostly. I also need to have it if there are values that are under a certain value to be given zero. In this case, if any of the cells have a value below -80 then they are zero May 9, 2014 at 0:55
  • Perhaps a pre-con to get rid of the wild values.. PreResult = Con(Diff > 80 | Diff < -80, 0, Diff) puts 0 into the preresult for values outside of range, then change the Diff in the result= line to PreResult. Or extend the con statement after the IsNulls and before the first Con. May 9, 2014 at 1:01
  • I needed to edit the previous comment. The values below -80 are in the ras2 and ras1 files. May 9, 2014 at 1:12
  • See edits, I've moved the IsNulls into the outlier to PreResult and then testing the PreResult for less than, greater than or equal to 0. This could all be done on one line but it looks nicer on two - though it will run slower. Put as many conditions on the PreResult line as you need separated by a | (pipe) which means or, I think writing 'or' works too. May 9, 2014 at 1:19
  • Got this for your preresult variable: Runtime error Traceback (most recent call last): File "<string>", line 4, in <module> ValueError: The truth value of a raster is ambiguous. Invalid use of raster with Boolean operator or function. Check the use of parentheses where applicable. May 9, 2014 at 1:52

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.