# Recalculating raster based in number of cells?

I am working with binary rasters of fish species distributions (1=presence, 2=absence). I am looking to recalculate each raster in this way:

1. Count the number of cells with value 1.
2. Divide: 1/Number of cells with value 1.
3. Replace each cell with value 1 with the value resulting from the division.

I think that should be some way to do it with raster calculator in QGIS or ArcGIS for Desktop.

• Open attribute table of your raster. You'll see count of 1s. Use raster calculator Con("original"==1, 1/n). This will create NODATA elsewhere, if this is what you want Commented Apr 11, 2016 at 20:40

I sm no arcGIS expert, but I recently had a similar problem, and did the following: In arcGIS, first add an extra field to your attribute table, and give it a name, say NAME. (If your raster does not have an attribute table, first build one.)

Then use select by attribute in the attribute table. Specify that only the value 1 should be chosen. At the bottom of your table, the number of selected rows will shown, being your count.

Use the Field calculator to specify that the new field should be equal to 1/number of ones (NAME=1/number). Only the selected rows will be given the new value.

Switch selection, and use the field calculator to specify that the rest of the numbers in NAME=2 (or whatever other value you might want it to be).

If you have spatial analyst for ArcMap you could do this rather easily. I'll refer to your raster as FishRaster in my following example:

Geoprocessing Tools> ... (I'll use the below format to refer to the path to tools): Geoprocessing Tools> Spatial Analyst> Map Algebra> Raster Calculator

First step: We need to get your raster in 1's and 0's to target the 1's for zonal stats. FishRaster < 2 =FisRasterB This creates a raster of ones and zeros, ones where the value you wanted divided is located only.

...Spatial Analyst> Zonal> Zonal Statistics Input Raster = FishRaster Zone field= Value Input value Raster = FishRaster Output raster = output1 Statistics type = SUM

This will create a raster which will have the sum of all 1 cell's value replacing the 1s that were there. Read the value, and we'll use that (MaxValue) for the next step.

The next step could be done using the raster calculator, but it will give you an integer output unless you force it into a floating point raster: output1/(MaxValue) ... should result in all zeros.

...math> Float: output1 output = float1

then to raster calculator, divide float1 by (yourNumber)

This will leave you with 0's and fractions. You mentioned you wanted those 0's to be 2's; you can use Con or Reclassify. Reclassify may force everything to integers, however, and Con statements are a pain to figure out.

At least this will get you a distinguishable list of values which you could do a study with. I can only assume you're doing some sort of density study.