Raster Calculator help

2766
6
Jump to solution
09-24-2014 07:40 AM
BenLeslie1
Occasional Contributor III

Is it me or is the Raster Calculator help rather vague?  I'm looking at the Conditional oporators and I can't see anywhere that tells me what they do and what syntax they want.

Anyway what I'm trying to do is take my 3 band colour tiff and say

     if the colour is white make the value 1 and make everything else 0

so I've guessed that Con means Conditional and have written this in the raster calculator:

     Con(("mytif.tif.Band1"==255) & ("mytif.tif.Band2"==255) & ("mytif.tif.Band3"==255), 1, 0)

can anyone help?

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

Hi Ben,

I tried it with a screen dump saved as TIFF and it worked for me:

Executing: RasterCalculator Con(("mytif.tif - Band_1"==255) & ("mytif.tif - Band_2"==255) & ("mytif.tif - Band_3"==255), 1, 0) D:\Xander\tmp\NIData.gdb\bla

Start Time: Wed Sep 24 14:28:01 2014

Con((Raster(r"mytif.tif - Band_1")==255) & (Raster(r"mytif.tif - Band_2")==255) & (Raster(r"mytif.tif - Band_3")==255), 1, 0)

Succeeded at Wed Sep 24 14:28:08 2014 (Elapsed Time: 7,00 seconds)

If you look closely the bands in my TIFF are called differently. I did not add the TIFF to the TOC, but the bands inside the TIFF to the TOC. There is no need to use spaces right around the equal sings (that used to be necessary when using float rasters, but the bands are integer).

Kind regards, Xander

View solution in original post

6 Replies
DanPatterson_Retired
MVP Emeritus

don't have an image to test, but did you get an error message?  Did you type this in manually? I would expect a space on either side of the equality check

0 Kudos
IanMurray
Frequent Contributor

You might do better looking at the Con Spatial Analyst tool help, since that is the tool that is being used inside raster calculator.

But yes, your syntax looks correct, you are evaluating  if the value of the cell of Band1 is 255 and Band2 is 255 and Band 3 is 255, make it a 1, otherwise zero.  I might put all the & statements in parentheses as well if it is not working properly.

XanderBakker
Esri Esteemed Contributor

Hi Ben,

I tried it with a screen dump saved as TIFF and it worked for me:

Executing: RasterCalculator Con(("mytif.tif - Band_1"==255) & ("mytif.tif - Band_2"==255) & ("mytif.tif - Band_3"==255), 1, 0) D:\Xander\tmp\NIData.gdb\bla

Start Time: Wed Sep 24 14:28:01 2014

Con((Raster(r"mytif.tif - Band_1")==255) & (Raster(r"mytif.tif - Band_2")==255) & (Raster(r"mytif.tif - Band_3")==255), 1, 0)

Succeeded at Wed Sep 24 14:28:08 2014 (Elapsed Time: 7,00 seconds)

If you look closely the bands in my TIFF are called differently. I did not add the TIFF to the TOC, but the bands inside the TIFF to the TOC. There is no need to use spaces right around the equal sings (that used to be necessary when using float rasters, but the bands are integer).

Kind regards, Xander

BenLeslie1
Occasional Contributor III

Thanks guys.

Just a note to any other novices reading this - I was testing a concept in Raster Calculator.  When I came to write my batch script the notation is different...

    myRaster = "blah_blah.tif"

    #band notation is a backslash

    redband = myRaster + "\Band_1"

    greenband = myRaster + "\Band_2"

    blueband = myRaster + "\Band_3"

    #need to remind it that each input is a raster

    outRaster = Con((Raster(redband)==255) & (Raster(greenband)==255) & (Raster(blueband)==255),1,0)

    outRaster.save("C:/OutputFolder/" + myRaster)

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Ben,

You're absolutely right, you can use Python for this purpose too and the syntax will be slightly different. It depend on where the code is executed. Your script sample would work as standalone too (after including the import and setting the workspace). The syntax that was returned by the Raster Calculator Tool uses the existence of the TOC and how the rasters are called there (example: "mytif.tif - Band_1").

In your script you use the slash, and that indicates that the TIFF is considered a dataset and the bands are rasters within the dataset. A small script to explain what I mean:

import arcpy

import os

folder = r"D:\Xander\GeoNet\TIFF_CON"

arcpy.env.workspace = folder

lst_ds = arcpy.ListDatasets()

for ds in lst_ds:

    print "dataset: {0}".format(ds)

    arcpy.env.workspace = os.path.join(folder, ds)

    lst_ras = arcpy.ListRasters()

    for ras in lst_ras:

        print "raster: {0}".format(ras)

Results in:

dataset: mytif.tif

raster: Band_1

raster: Band_2

raster: Band_3

raster: Band_4

My folder where the TIFF resides is my initial workspace. When I list the datasets in that workspace it will find the TIFF, setting the TIFF as workspace allows me to list the rasters (the bands inside the TIFF). If you would like to access the rasterbands without setting the workspace to your TIFF you would have to "join" the folder, dataset (TIFF) and each rasterband:

redband = os.path.join(folder, NameOfYourTIFF, "Band_1")

And yes, defining raster objects allows for much easier syntax. For instance I you want to sum 3 rasters without defining the individual rasters as raster objects you would have to use a nested arcpy.sa.Plus tool. If you define the rasters as raster objects you can simply use the + sign to sum them.

Kind regards, Xander

0 Kudos
curtvprice
MVP Esteemed Contributor

> Is it me or is the Raster Calculator help rather vague?

There are many, many raster tools - and unlike 9.x the tool and raster calculator functions and tools have the same syntax - so there is no need to document them twice.

I recommend reading with the help article:  What is Map Algebra?‌ I know there's a lot of detail to wade through, but when you get the hang of it there is a lot of power (and efficiency) in stringing the tools together in map algebra expressions!

Personally, I really like to leave spaces around all operators when working in Python. Both these are legal Python but which would you rather read and debug?

Con(("mytif.tif - Band_1"==255)&("mytif.tif - Band_2"==255)&("mytif.tif - Band_3"==255),1,0)

Con(("mytif.tif - Band_1" == 255) &

    ("mytif.tif - Band_2" == 255) &

    ("mytif.tif - Band_3" == 255), 1, 0)

0 Kudos