# How Focal Statistics works

The Focal Statistics tool performs a neighborhood operation that computes an output raster where the value for each output cell is a function of the values of all the input cells that are in a specified neighborhood around that location. The function performed on the input is a statistic, such as the maximum, average, or sum of all values encountered in that neighborhood.

Conceptually, on execution, the algorithm visits each cell in the raster and calculates the specified statistic with the identified neighborhood. The cell for which the statistic is being calculated is referred to as the processing cell. The value of the processing cell, as well as all the cell values in the identified neighborhood, is included in the neighborhood statistics calculation.

The neighborhoods can overlap, so that cells in one neighborhood may also be included in the neighborhood of another processing cell.

**Example**

To illustrate the neighborhood processing for Focal Statistics calculating a Sum statistic, consider the processing cell with a value of 5 in the following diagram. A rectangular 3 x 3 cell neighborhood shape is specified. The sum of the values of the neighboring cells (3 + 2 + 3 + 4 + 2 + 1 + 4 = 19) plus the value of the processing cell (5) equals 24 (19 + 5 = 24). So a value of 24 is given to the cell in the output raster in the same location as the processing cell in the input raster.

The above diagram demonstrates how the calculations are performed on a single cell in the input raster. In the following diagram, the results for all the input cells are shown. The cells highlighted in yellow identify the same processing cell and neighborhood as in the example above.

The shape of a neighborhood can be an annulus (a donut), circle, rectangle, or wedge. The possible statistics that can be calculated within a neighborhood are mean, majority, maximum, median, minimum, minority, range, standard deviation, sum, and variety.

The Focal Statistics tool gives control over the neighborhood type and statistic to be calculated.

## Neighborhood types

The shape of a neighborhood can be an annulus (a donut), circle, rectangle, or wedge. By using a kernel file, you can also define a custom neighborhood shape, as well as assign different weights to specific cells in the neighborhood before the statistic is calculated.

Following is a discussion of the different neighborhood shapes and how they are defined:

- Annulus
- The annulus shape comprises one smaller circle within a larger circle (a donut shape). Cells with centers that fall outside the radius of the smaller circle but inside the radius of the larger circle will be included in processing the neighborhood.
- The units for the inner and outer radius can be defined in cells or in map units.

- Circle
- Any cells whose center falls inside the radius of the circle will be included in processing the neighborhood.
- The units for the radius can be defined in cells or in map units.

- Rectangle
- The x,y position for the processing cell within the neighborhood, with respect to the upper left corner of the neighborhood, is determined by the following equations:
x = (width of the neighborhood + 1)/2 y = (height of the neighborhood + 1)/2

- If the input number of cells is even, the x,y coordinates are computed using truncation.
- For example, in a 5 x 5 cell neighborhood, the x- and y-values are 3,3. In a 4 x 4 neighborhood, the x- and y-values are 2,2.
- The units for the width and height values can be defined in cells or in map units.

- The x,y position for the processing cell within the neighborhood, with respect to the upper left corner of the neighborhood, is determined by the following equations:
- Wedge
- Cells with centers that fall within the wedge will be included in processing the neighborhood.
- The start and end angles are integer or floating-point values from 0 to 360. The values of the circle begin with 0 on the positive x-axis (3:00 on a clock) and increase counterclockwise until they return to 0.
- The units for the radius can be defined in cells or in map units.

- Irregular
- Allows you to specify an irregularly shaped neighborhood around the processing cell.
- The irregular kernel file specifies which cell positions should be included within the neighborhood.
- The x,y position for the processing cell within the neighborhood, with respect to the upper left corner of the neighborhood, is determined by the following equations:
x = (width + 1)/2 y = (height + 1)/2

- If the input number of cells is even, the x- and y-coordinates are computed using truncation.
- Kernel file (for irregular neighborhood)
- The irregular kernel file is an ASCII text file that defines the values and shape of an irregular neighborhood.
- The first line specifies the width and height of the neighborhood (the number of cells in the x direction, followed by a space, and the number of cells in the y direction).
- The subsequent lines give the values of each position in the neighborhood. The values are input in the same configuration as appears in the neighborhood they represent. A space between each value is necessary.
- The values in the kernel file should be either 0 (zero) or 1 (one). However, any value not equal to 0 will be interpreted as 1.
- A value of 0 (not a blank space) for a cell position indicates that the cell is not a member of the neighborhood and will not be used for processing. A value of 1 indicates that its corresponding cell (and value) is a member of the neighborhood.

- An example of an ASCII irregular kernel file and the neighborhood it represents follows:

- Weight
- Allows you to define an irregular neighborhood around the processing cell and specify which weights will be used to multiply the cell values on an input raster.
- The weight kernel file specifies which cell positions should be included within the neighborhood and the weights that will be applied to them.
- The x,y position for the processing cell within the neighborhood, with respect to the upper left corner of the neighborhood, is determined by the following equations:
x = (width + 1)/2 y = (height + 1)/2

- If the input number of cells is even, the x- and y-coordinates are computed using truncation.
- The weight neighborhood is only available for the mean, standard deviation (STD), and sum statistics types.
- Kernel file (for weighted neighborhood)
- The weight kernel file is an ASCII text file that defines the values and shape of a weight neighborhood.
- The first line specifies the width and height of the neighborhood (the number of cells in the x direction, followed by a space, and the number of cells in the y direction).
- The subsequent lines give the weight values of each position in the neighborhood. The values are input in the same configuration as appears in the neighborhood they represent. Positive, negative, and decimal values are all valid options to use as a weight. A space between each value is necessary.
- For locations in the neighborhood that are not to be part of the calculation, use a value of 0 at the corresponding location in the kernel file.

- An example of an ASCII weighted kernel file and the neighborhood it represents follows:

## Statistics type

The available Focal Statistics statistics are majority, maximum, mean, median, minimum, minority, range, standard deviation, and sum. The default statistics type is mean.

- Majority
- If the input raster is integer, the values on the output raster will be integer; if the values on the input are floating point, the values on the output will be floating point.
- When there is more than one majority value within a neighborhood, the processing cell location will receive NoData on the output.

- Maximum
- If the input raster is integer, the values on the output raster will be integer; if the values on the input are floating point, the values on the output will be floating point.

- Mean
- The output raster will always be floating point.
- The mean statistic can be used with the weight neighborhood type.

- Median
- If the input raster is integer, the values on the output raster will be integer; if the values on the input are floating point, the values on the output will be floating point.
- When the number of valid cell values in the neighborhood is odd, the median value is calculated by ranking the values and selecting the middle value. If the number of values in a neighborhood is even, the values will be ranked and the middle two values will be averaged.

- Minimum
- Minority
- When there is more than one minority value within a neighborhood, the processing cell location will receive NoData on the output.

- Range
- The values for each cell location on the output grid are determined on a cell-by-cell basis by applying this simple formula:
Focal Range = Focal Maximum – Focal Minimum

- STD
- The output raster will always be floating point.
- The STD statistic can be used with the weight neighborhood type.

- Sum
- The STD statistic can be used with the weight neighborhood type.

- Variety
- The output raster will always be integer.

## Processing cells of NoData

The Ignore NoData in calculations option controls how NoData cells withing the neighbourhood window are handled. When this option is checked (the DATA option), any cells in the neighbourhood that are NoData will be ignored in the calculation of the output cell value. When unchecked (the NODATA option), if any cell in the neighbourhood is NoData, the output cell will be NoData.

If the processing cell itself is NoData, with the Ignore NoData option selected, the output value for the cell will be calculated based on the other cells in the neighbourhood that have a valid value. Of course, if all of the cells in the neighbourhood are NoData, the output will be NoData, regardless of the setting for this parameter.