# Nodata issues in map algebra

A dummy question. I need to change all values in my rasters into ones. I've been able to do this fine in the Rater Calculator just by multiplying the layer by zero and adding one (my_layer*0+1), but when I'm doing the same in arcpy, for some reason it works for a part of my raster but for some it changes all the nodata cells to ones as well. The nodata values in different rasters are different because my rasters have been generated from different source data. Does it not recognize some nodata values? If so, this is odd as it displays them correctly when I just import them on the mxd. Any ideas on why this works sometimes, but sometimes not?

Here's the exact script:

``````bin_test = arcpy.Raster("F:/datasets/map1.img")*0+1
``````
• I'd check your nodata values. I don't know what type of data you're working with, but sometimes a nodata value can just be 0 rather than "NoData." In this case, 0*0+1 = 1 rather than anything calculated with NoData = NoData. – Wes Jun 17 '14 at 17:07
• Wes, my nodata values are all numbers, but for some values it does work (and for displaying the maps it works as well as in raster calculator regardless of the values). For example, when my nodata are 65536 (checking from layer properties when displayed in the mxd) it doesn't work but with another layer nodata value is 127 and it works fine. I don't even know how else nodata could be expressed in a a raster other than as a specific number? – dummy Jun 17 '14 at 17:19
• Sounds like you're using many different bit depth rasters. Look into how NoData is defined in your environment settings. – Wes Jun 17 '14 at 17:24
• @dummy did any of the answers below get you want you wanted? – DPSSpatial Oct 24 '14 at 17:40

My favourite way to deal with this would be to use a Con(IsNull) statement...

Basically, using a Con statement allows you to define a condition; if it is true, do this, if it is false, do this - including leaving the values as they were.

So in this case, I would be tempted to do something like

``````Con(IsNull(myRaster), myRaster, 1)
``````

Which basically states:

• Set up a condition to test cells in myRaster for NoData (IsNull)
• If it is NoData, leave it as NoData
• Otherwise, convert the cell value to 1

Some documentation:

...I don't know, however, why you're NoData cells are being multiplied into something besides NoData...

NoData is defined different ways depending on the raster source, including numerically. (You can look it up in raster properties if you are curious what the actual value is for your particular datasets.)

I suggest instead using Reclassify instead of Raster Calculator, which can either explicitly ignore NoData values (leaving them NoData) or explicitly include them (changing them to 1). It isn't entirely clear to me whether you do or don't want those NoData values respected, but Reclassify works for either scenario.

• Thanks, but reclassifying is also problematic because I have >500 layers that contain different and often many number values, so I'm not sure if that could be scripted effectively. All I want is for any actual value to become 1 and nodata to remain nodata. Very easy to do in raster calculator, just not for >500 layers... – dummy Jun 17 '14 at 17:23
• You can reclassify ranges, e.g. "1 - 9999999 -> 1", "NoData -> NoData". – Erica Jun 17 '14 at 17:25
• Perhaps alternatively look into manually setting the NoData value for each of your rasters before doing any processing -- this risks messing with actual data values, though, depending on just what the range of the raster values you have is. – Erica Jun 17 '14 at 17:40
• Reclassifying ranges was the closest I could get to a fast solution, it appears to have worked. Still puzzled though why this became a problem in the first place and why only recassifying ranges worked and none of the other options... Thanks anyway for all the suggestions! – dummy Oct 25 '14 at 20:56

Just so you know there are a million ways to do this. But, that said, multiplying is a very indirect method, these methods are more straightforward and efficient, even using the raster calculator. Going at it this way instead of multiplying by zero is probably safer with different bit depths / data formats...

These three calculations should give you the same answer:

``````dataraster = Con(IsNull(myRaster), myRaster, 1)
dataraster = SetNull(IsNull(myRaster), 1)
dataraster = Con(~ IsNull(myRaster), 1)
``````