This tutorial will expose you to the fundamentals of focal map algebra operations applied to raster data models. This tutorial was updated using ArcGIS Pro version 3.3. Note that this tutorial makes use of Spatial Analyst. Your installation of ArcGIS Pro license should therefore be configured with the Spatial Analyst extension.
Before tackling this tutorial, you will need to download and install a dataset following these instructions:
Create a folder called MapAlgebraFocal in your course working directory (e.g. C:\Users\yourname\Documents\ArcGISPro_tutorials\MapAlgebraFocal).
Download the data for this exercise then extract the contents of MapAlgebraFocal.zip into your newly created MapAlgebraFocal folder.
You will want to read Chapter 10 of the textbook before completing this lab.
Open the Elevation.aprx map document.
The elevation.img raster represents a small section of the Grand Canyon in Arizona. Its elevation units is in meters.
The elevation labels layer is a polygon layer whose symbology is set to no color. Its purpose is to display the the elevation values for each pixel, but note that this layer is only viewable when zoomed in to a scale larger than 1:200,000. Recall that a scale becomes larger when the denominator becomes smaller (e.g. numerically, 1/24,000 is a larger number than 1/200,000--hence, 1/24,000 is a larger scale than 1/200,000).
In this step, you will create a new raster where each output pixel will reflect the average value of the neighboring input pixels. As such, this type of operation is referred to as a focal operation. In this example, you will compute the average value from a 3 by 3 cell cluster for each output pixel.
Open the Tools toolbox under the Analysis tab.
In the Geoprocessing pane , search of Focal Statistics.
Select the Focal Statistics tools associated with the Spatial Analyst extension.
In the Focal Statistics window, set the Elevation.img layer as the input raster
Name the output file elev_3x3.tif (note the .tif extension and read tip below).
Define the neighborhood as a 3 by 3 rectangle.
Choose the Mean as the statistic type.
Click Run
TIP:
.tif cannot be saved within the geodatabase. Layers with a file extension cannot be saved in a geodatabase.
Select the folder icon next to output raster and navigate to your Project folder and change the output raster's location from Geodatabase to the Project Folder.
Select Project --> select Folders --> select the MapAlgebraFocal folder
The folder should have a House icon. The folder with the house icon is your home project folder.
The output raster is made up of the same number of pixels and has the same pixel size as the input raster. The difference is in the output pixel values.
The left image is the original raster. The image to the right is the resulting smoothed raster. Next, we'll explore one of the output pixel values.
Under the Map tab, click on the Explore pull-down menu and select Visible Layers.
Make sure that all layers (including the Labels layer and the newly created raster layer) are turned on in the Contents pane.
Zoom in on a 3x3 cluster of pixels and extract the pixel value of an elev_3x3.tif cell using the Explore tool.
In this example, the elevation pixel whose value is 1367 is selected. You can find this pixel by selecting its record in the elevation.img attributes table
In the accompanying figure, the output pixel value for the cell shown in a cyan color is 1423.56 . This value is computed from the 3x3 block of the input raster cells as follows:
(898+1413+1418+1505+1367+1505+1379+1389+1938) / 9 = 1423.56
Clear your selection.
The Focal Statistics tool can also be used to compute the summary statistic of pixels using a search radius. In the following example, a 10 km radius is used to define the pixels whose values will be used to compute the output raster pixels. We will also make use of the median in this example as a summary statistic instead of the mean.
Open the Focal Statistics tool.
Set the Elevation.img layer as the input raster
Name the output file elev_10km.tif.
Define the neighborhood as a 10 km circle. Note that the radius is defined using the Map units which are in meters in this example. So make sure to define the radius as 10,000 meters.
Choose the Median as the statistic type.
Click Run.
TIP:
.tif cannot be saved within the geodatabase.
Select the folder icon next to output raster and navigate to your Project folder and change the output raster's location from Geodatabase to the Project Folder.
Double click Project.
Double Click Folders.
Double click the MapAlgebraFocal folder
It must have a House icon. The folder with the house icon is your Project Folder.
For an input raster pixel to be included in the search radius (shown as the blue cyan circle in the adjoining figure, which will not appear on your map and was added for clarity outside of GIS), its center must fall within the search radius. In this example, the input raster pixels whose values are 728, 1694, 1857 and 1147 are not used to compute the output cell value at the center of the 10km radius since their centers fall outside of the search radius.
Here, the output cell centered on the 10 km radius circle has a median value of 1413. Its value is computed from the 21 neighboring cells whose center fall within the search radius. These values include 785 , 1215, 1310, 699, 898, 1413, 1418, 1426, 694, 1505, 1367, 1505, 1835, 1053, 1379, 1389, 1938, 1867, 1425, 1529 and 1874.
The focal operation does not need to be restricted to a pre-defined list of neighbor definitions. For example, you might want to exclude central cells from the output (i.e. the 3x3 neighbor would consist of 8 input pixels instead of 9).
To customize the neighborhood definition, you need to create an ASCII text file. One is already created for this tutorial and is named Kernel1.txt. (Note that a text file differs from a Word file and should therefore be created and saved using a basic text editor such as Windows Notepad.)
To view the contents of the text file, navigate to your project folder using the Windows file manager, right-click on Kernel1.txt and select to have it opened with Notepad via the Open with menu.
The current file consists of 4 lines. The first line defines the width and height of the neighborhood matrix (in pixels) and the next three lines define the neighbor layout with a value of 1 used to define the pixels to include and a value of 0 to define those to exclude.
Close the text editor.
Open the Focal Statistics tool.
Set the Elevation.img layer as the input raster
Name the output file elev_custom.tif.
Define the neighborhood as Irregular. You’ll note that selecting Irregular modifies the tool’s interface.
Select the Kernel1.txt file from your working directory.
Here, we'll use the Mean for the statistical summary.
Click Run
TIP:
.tif cannot be saved within the geodatabase.
Select the folder icon next to output raster and navigate to your Project folder and change the output raster's location from Geodatabase to the Project Folder.
Double click Project.
Double Click Folders.
Double click the MapAlgebraFocal folder
It must have a House icon. The folder with the house icon is your Project Folder.
The output values will be different from those in elev_3x3.img. For example, revisiting the subset of pixels shown in Step 2, we compute a slightly different output pixel value whereby the central input pixel value of 1367 is not used to compute the output pixel value, e.g.:
(898+1413+1418+1505+1367+1505+1379+1389+1938) / 8 = 1430.63.
The kernel file can also be used to define cell weights (but note that only the mean, standard deviation and sum statistics can be computed for such a kernel). For example, a Sobel filter (used for edge detection in image processing) is defined in the text file, then applied to the raster layer. The adjacent figure is that of a horizontal edge detection kernel.
Using the Notepad editor in Windows, modify the Kernel1.txt file to reflect the values shown in the adjacent figure, then save the file as Kernel2.txt.
Save and close the text editor.
Open the Focal Statistics tool.
Set the Elevation.img layer as the input raster.
Name the output file elev_custom2.tif.
Define the neighborhood as a Weight. You’ll note that selecting Weight modifies the tool’s interface.
Select the modified Kernel2.txt file from your working directory.
Choose the Sum as the statistic type.
Click Run
TIP:
.tif cannot be saved within the geodatabase.
Select the folder icon next to output raster and navigate to your Project folder and change the output raster's location from Geodatabase to the Project Folder.
Double click Project.
Double Click Folders.
Double click the MapAlgebraFocal folder
It must have a House icon. The folder with the house icon is your Project Folder.
In the adjoining example, the output value of 1079 is computed from:
(-1) * 898 + (0) * 1413 + (1) * 1418 + (-2) *1505 + (0) * 1367 + (2) *1505 + (-1) * 1379 + (0) * 1389 + (1) * 1938 = 1079
The output of the Sobel filter offers a hillshade-like look to the elevation raster.
ArcGIS has many geoprocesses that fall under the focal operations family. For example, it has a Slope tool that will compute the slope between each pixels' elevation and its surrounding pixels. Note that slope measurement can be sensitive to distortions in distance measurements. For the greatest accuracy in slope computation, you might want to adopt the geodesic method.
If adopting the planar method, the slope is computed by first computing the slope along the x-direction and in the y-direction. It then computes the overall slope by taking the square root of the sum of the squared slopes in both the x and y directions. Note that the center pixel does not take part in the slope calculation.
To learn more about how the slope is computed in geodesic mode, see the tool's help page.
In the geoprocessing search bar, search for the slope tool (choose the Spatial Analyst version).
Select the Elevation.img as the input raster.
Name the output slope.tif.
Set output measurement to Degree.
Click run.
TIP:
.tif cannot be saved within the geodatabase.
Select the folder icon next to output raster and navigate to your Project folder and change the output raster's location from Geodatabase to the Project Folder.
Double click Project.
Double Click Folders.
Double click the MapAlgebraFocal folder
It must have a House icon. The folder with the house icon is your Project Folder.
Note that the tool assumes that the elevation units are in the same units as that of the plane (meters in our example). If, for example, the mapping units were in feet and the elevation units were in meters, you would need to adopt a z-factor of 3.28 to convert from z units to x-y units.
If a geodesic method is selected, you will be presented with a Z unit option field that will allow you to define the raster's elevation values' unit.
Here's an example of the slope's computation for a pixel centered on the elevation value of 1367.
This wraps up this in-class lab activity.