14 Raster Analysis
By the end of this chapter you should be able to:
- Describe a raster by its cells, cell size, extent, and NoData values.
- Combine rasters with map algebra, and predict how NoData passes through.
- Line up rasters before analysis by resampling, and by setting the cell size, extent, and snap raster.
- Classify a raster function by its scope: local, neighborhood, zonal, or global.
- Use mathematical, logical, reclassification, and conditional functions.
- Compute neighborhood and zonal statistics, and aggregate a raster to a coarser cell size.
Chapter 13 ended with a glimpse of raster overlay. This chapter takes rasters on their own terms. A raster stores the world as a grid, and almost every raster tool does the same thing: it repeats one operation cell by cell. What changes from tool to tool is which input cells feed each output cell, and what is done with them. Once you see that, the long list of raster tools in ArcGIS Pro and QGIS becomes much easier to understand.
14.1 Raster basics
14.1.1 Cells, cell size, and extent
A raster represents the world as a regular grid of cells (also called pixels, short for picture elements). Each cell holds a value, such as an elevation, a temperature, or a land-cover code. Two numbers describe the grid (Figure 14.1):
- Cell size, also called resolution, is the ground length of one side of a cell, such as 30 m or 1 km.
- Extent is the ground area the whole grid covers, from its lower-left to its upper-right corner.
The software does not store coordinates for every cell. It stores the location of one corner and the cell size, and works out any cell’s position from its row and column. So a raster 100 cells wide with 30 m cells has an extent 3 km wide. It also explains why the cell size matters so much: change it, and every cell’s footprint changes.
14.1.2 What’s in a cell?
A cell can hold a whole number (such as a land-cover code), a decimal (such as rainfall in inches), or, in some formats, text. Most rasters hold one value per cell. Multiband rasters, such as satellite images, hold one value per band in each cell, for example red, green, blue, and near-infrared.
Cell values are either categorical (also called discrete or thematic), where numbers are codes for classes such as land cover or soil type, or continuous, where numbers are measurements, such as elevation or temperature. Many choices in this chapter depend on which kind you have.
14.1.3 NoData
A cell is never truly empty. A cell without a measurement holds a special code, NoData (Null in some software). There are two rules for NoData:
- NoData in, NoData out. In map algebra, any operation that touches a NoData cell writes NoData to the output: 5 + NoData = NoData.
- Statistics skip NoData. The mean of 2, 4, and NoData is 3, not 2.
The difference between NoData and 0 matters. In an elevation raster, 0 means sea level; NoData means “unknown.” If missing values are stored as 0, every mean that includes them is too low, and the map is wrong.
14.2 Combining rasters
14.2.1 Map algebra
Map algebra treats rasters as variables in an equation, and evaluates it cell by cell (Tomlin 1990). In Figure 14.2, each cell of raster C is the sum of the cells at the same position in A and B. The top-left cell is 1 + (−1) = 0. The bottom-right cell of B is NoData, so the bottom-right cell of C is NoData too, whatever A holds.
An operation with one input raster, such as A × 2 or the square root of A, is unary. One with two input rasters, such as A + B, is binary. (A constant such as 2 is not a raster, so it doesn’t count as an input.)
14.2.2 When the grids don’t line up
Map algebra assumes the rasters line up exactly: the same cell size, the same extent, and cells in the same places. Real data rarely do (Figure 14.3).
- Mismatched cell sizes. A coarse cell sits over parts of many fine cells. Which of them should it be added to? There is no one answer.
- Different extents. Outside the overlap, one input has no cells at all, so the output there can only be NoData.
14.2.3 Resampling
Resampling rebuilds a raster on a new grid, so that every layer shares one cell size and one set of cell positions. For each new cell, it decides what value the cell should get. There are three standard methods (Table 14.1):
| Method | How it picks the new value | Use it for |
|---|---|---|
| Nearest neighbor | Copies the value of the closest input cell center | Categories: land cover, soils, zones |
| Bilinear | Distance-weighted mean of the 4 nearest cells | Continuous surfaces: elevation, temperature |
| Cubic convolution | A smooth curve through the 16 nearest cells | Continuous surfaces where smoothness matters |
Nearest neighbor never invents values, so it is the only safe choice for categories: you can’t average forest and water. Bilinear and cubic blend nearby cells, which gives smoother surfaces for continuous data, but creates values that were never measured. On a land-cover raster, they produce nonsense such as class 2.25 (Figure 14.4).
14.2.4 Cell size, extent, and snap raster
Three settings make the grids line up before any map algebra runs. In ArcGIS Pro they are environment settings, used by every raster tool; QGIS asks for them in each tool’s dialog.
- Cell size. Choose one output cell size, usually the coarsest input. Resampling to finer cells adds no real detail.
- Extent. Intersection keeps only the area where every input has data; union keeps everything, with NoData in the gaps. Intersection is the safer default.
- Snap raster. Even with the same cell size, two grids can be offset by part of a cell. Snapping aligns the output’s cell edges to one input’s grid.
The output can only be as detailed as its coarsest input (Figure 14.5). If you add a 30 m land-use raster to a 1 km elevation raster, the result is a blocky 1 km grid, and choosing 30 m output cells would only copy each 1 km value into a thousand smaller cells. If you need 30 m results, find a better elevation source, such as a 10 m or 30 m digital elevation model from USGS 3DEP.
14.3 The scope of raster functions
Scope is the set of input cells that feed each output cell. Chapter 13 introduced local, neighborhood, and global scope; rasters add a fourth, zonal (Figure 14.6):
- Local functions use only the input cell at the same location. Adding 4 to every cell is local: 10 becomes 14.
- Neighborhood (focal) functions use a window of cells around each location, usually 3 × 3. The window’s result is written to its center cell, and then the window moves.
- Zonal functions use every cell in the same zone, where zones come from a second layer. Each cell in zone a gets the mean of all cells in zone a.
- Global functions can use every cell in the raster. The global maximum is written to every cell; more useful global functions, such as distance to the nearest road, give a different answer for each cell but still need to look everywhere.
Notice the edge cells in the neighborhood example: a window centered on a corner holds only four cells, so edge cells always have fewer neighbors.
Is each of these local, neighborhood, zonal, or global? Converting elevation from feet to meters; smoothing a noisy elevation surface; computing the mean elevation of each county; finding the distance from every cell to the nearest hospital.
14.4 Local functions
14.4.1 Mathematical functions
Mathematical functions are the most common map algebra. You write rasters as letters in an expression, such as A * B, (A * 2) / B, or A + B + C, and the tool evaluates it cell by cell. You can also apply functions to a single raster: absolute value, exponents and logarithms, sine and cosine, rounding, square roots, and powers. In ArcGIS Pro, the Raster Calculator tool evaluates expressions; in QGIS, it is also called the Raster Calculator.
Watch two things. Trigonometric functions expect radians in most software, so convert slope or aspect in degrees first. And when you combine variables with different units, one can dominate. Multiplying elevation (100 to 1,000 m) by slope (0 to 31 degrees) gives a result driven mostly by elevation. Rescale both to the same range, such as 0 to 1, first (Figure 14.7).
14.4.2 Logical functions
Relational operators compare two rasters, or a raster and a constant, cell by cell: less than, greater than, equal, and not equal. They write 1 where the comparison is true and 0 where it is false. Boolean operators, such as AND, OR, and NOT, combine those 0s and 1s (Figure 14.8).
Typical uses:
- Find buildable land:
Slope < 15. - Find land-cover change:
LC2010 != LC2020gives 1 wherever the class changed. - Find cells between two values:
(Slope > 5) AND (Slope < 15).
For continuous data, avoid testing equality: 1.30000 and 1.30001 are not equal. Test whether the difference is small instead.
ISNULL is unary: it writes 1 where a cell is NoData and 0 everywhere else. It is the one test that never outputs NoData, which makes it the tool for finding gaps. (You can’t find NoData with A = NoData, since any comparison with NoData gives NoData.)
14.4.3 Reclassification
Reclassification replaces each cell’s value with a new value from a rule you supply (Figure 14.9). It is local and unary, and one of the most used raster tools.
- By table, each exact value maps to a new value. Use this for categorical data, such as converting land-cover codes to a suitability score.
- By ranges, every value within a range maps to one new value. Use this for continuous data, such as slope.
Watch the boundaries: is a slope of exactly 5 in class a or b? Software has a rule, and you should check it. Write ranges so that every value falls into exactly one class.
14.4.4 Conditional functions
The conditional function, CON in ArcGIS Pro, is the raster version of if-then-else. It takes a condition, a value or raster to use where the condition is true, and one to use where it is false. In Figure 14.10, the expression is Con(A == 1, B, C): where A is 1, copy B; everywhere else, copy C. A decides, but its values never appear in the output.
The output’s bottom-right cell is NoData because it comes from C, which is NoData there. B has a value in that cell, but B wasn’t chosen. A common pattern fills gaps: Con(IsNull(A), 0, A) replaces NoData with 0 and keeps every other value.
14.5 Beyond local
14.5.1 Neighborhood functions
A neighborhood function computes a statistic from a window of cells and writes it to the center cell (Figure 14.11). The window is usually 3 × 3, but can be larger, a circle, or an annulus. In ArcGIS Pro, the tool is Focal Statistics; in QGIS, it is r.neighbors (from GRASS).
Choose the statistic to fit the data. Mean and median smooth continuous surfaces; maximum and minimum find peaks and pits; majority suits categories, since a mean of land-cover codes is meaningless. Slope and aspect are neighborhood functions too: they fit a surface through the window and report how steep it is and which way it faces.
14.5.2 The moving window
Neighborhood functions run by sliding the window across the raster one cell at a time, left to right and top to bottom. A majority filter uses this to clean up classified satellite images, which often have single misclassified cells, called “salt and pepper” noise (Figure 14.12). Each cell takes the value shared by most of its neighbors; if there’s no majority, it keeps its value.
Filters have side effects. Here, two other cells changed as well, including a water cell on the edge of a small water body. That is why filters are used with care, and checked afterward.
14.5.3 Aggregation
Aggregation (rescaling to a coarser cell size) groups blocks of cells and writes one value per block (Figure 14.13). It is the cleanest way to bring a fine raster to a coarser one’s cell size. In ArcGIS Pro, the tool is Aggregate; in QGIS, set the resolution and a resampling method such as average in the Align Rasters tool.
Use the mean or sum for continuous data, such as rainfall or population, and the majority for categories.
14.5.4 Zonal functions
Zonal functions compute a statistic for every cell in a zone. Every neighborhood statistic (mean, sum, maximum, majority, and so on) can be computed by zone instead of by window. Zones can come from a raster, as in Figure 14.6, or from polygons, such as counties or watersheds. ArcGIS Pro’s Zonal Statistics writes the result to a raster; Zonal Statistics as Table writes one row per zone. QGIS’s Zonal statistics tool adds the results as fields in a polygon layer.
When the zones are polygons, the software converts them to cells behind the scenes. A polygon smaller than a cell may get no cells at all, and drop out of the results. Use a cell size that is small relative to your smallest polygon.
14.5.5 Global functions: distance
The most useful global functions measure distance (Figure 14.14). Euclidean distance gives every cell its straight-line distance to the nearest source cell, such as a river or a road. To know which source is nearest, the tool has to consider all of them, so the scope is global. Cost distance works the same way, but accumulates a cost, such as travel time across terrain, rather than meters.
Distance rasters are often reclassified (into bands such as 0–100 m and 100–500 m) and combined with other layers in a suitability model, the raster version of the buffer and overlay workflow in Chapter 13.
14.6 Check your understanding
A raster is 200 cells wide with a 30 m cell size. How wide is its extent, in meters?
What is 5 + NoData in map algebra?
Which resampling method should you use for a land-cover raster?
You add a 30 m land-use raster to a 1 km elevation raster. Which output cell size should you usually choose?
Computing the distance from every cell to the nearest river has which scope?
What is the mean of the 3 × 3 window 1, 2, 3 / 3, 6, 6 / 3, 9, 2, to one decimal?
In
Con(A == 1, B, C), where A is 4, the output takes the value from:ISNULL can output NoData.
14.7 Summary
- A raster is a grid of cells. The cell size and the corner of the extent locate every cell. Values can be categorical or continuous.
- NoData means “no value.” It passes through map algebra (NoData in, NoData out), and statistics skip it. Don’t store missing data as 0.
- Map algebra combines rasters cell by cell. Unary operations have one input raster; binary ones have two.
- Before combining rasters, line up the grids: resample to one cell size (nearest neighbor for categories, bilinear or cubic for surfaces), choose an extent, and set a snap raster. The output is only as detailed as its coarsest input.
- Raster functions have four scopes: local (one cell), neighborhood (a moving window), zonal (all cells in a zone), and global (every cell).
- Local functions include math, logical (relational and Boolean), reclassification, and conditional (CON) functions.
- Neighborhood functions compute statistics in a moving window, such as a majority filter. Aggregation rescales to coarser cells. Zonal functions summarize by zone. Distance is the most common global function.
14.8 Exercises
- Extent. A DEM has 3,601 columns and 3,601 rows of 1-arc-second cells. About how far across is it in degrees? At the equator, about how many kilometers?
- Map algebra by hand. For A and B in Figure 14.2, compute A − B and A × B. Where does NoData appear?
- Resampling. In QGIS or ArcGIS Pro, download an NLCD land-cover raster for your county from the MRLC viewer, and resample it to 300 m twice, once with nearest neighbor and once with bilinear. Open the attribute tables or histograms. What happened to the classes?
- Suitability. Using a DEM for Orange County, NC, from USGS 3DEP or NC OneMap, compute slope, then find the cells with a slope under 10 degrees and an elevation above 150 m. Write the expression you used.
- Fill the gaps. Write a CON expression that replaces NoData in a rainfall raster with the raster’s global mean, and leaves every other value unchanged.
- Zonal statistics. Compute the mean elevation of each county in North Carolina with Zonal Statistics as Table. Which county is highest, and which is lowest?
14.9 Further reading and online resources
14.9.1 Books
- GIS Fundamentals, by Paul Bolstad (Bolstad 2022). The chapter on raster analyses covers map algebra, scope, and resampling.
- Geographic Information Systems and Cartographic Modeling, by C. Dana Tomlin (Tomlin 1990), the source of map algebra and its local, focal, and zonal functions.
- GIS for Environmental Applications, by Xuan Zhu, has clear chapters on raster analysis and suitability modeling.
- Intro to GIS and Spatial Analysis, by Manuel Gimond, free online, includes chapters on raster data and map algebra: https://mgimond.github.io/Spatial/.
14.9.2 ArcGIS Pro
- Esri, “Understanding Map Algebra”: https://pro.arcgis.com/en/pro-app/latest/help/analysis/spatial-analyst/mapalgebra/what-is-map-algebra.htm.
- Esri, “Raster Calculator” tool reference: https://pro.arcgis.com/en/pro-app/latest/tool-reference/spatial-analyst/raster-calculator.htm.
- Esri, “Con” tool reference: https://pro.arcgis.com/en/pro-app/latest/tool-reference/spatial-analyst/con-.htm.
- Esri, “Focal Statistics”: https://pro.arcgis.com/en/pro-app/latest/tool-reference/spatial-analyst/focal-statistics.htm.
- Esri, “Zonal Statistics as Table”: https://pro.arcgis.com/en/pro-app/latest/tool-reference/spatial-analyst/zonal-statistics-as-table.htm.
- Esri, “Cell size and resampling in analysis”: https://pro.arcgis.com/en/pro-app/latest/help/analysis/spatial-analyst/performing-analysis/cell-size-and-resampling-in-analysis.htm.
14.9.3 QGIS
- QGIS User Guide, “Raster Calculator”: https://docs.qgis.org/latest/en/docs/user_manual/working_with_raster/raster_analysis.html.
- QGIS User Guide, “Raster analysis” algorithms (Reclassify by table, Zonal statistics): https://docs.qgis.org/latest/en/docs/user_manual/processing_algs/qgis/rasteranalysis.html.
- QGIS Training Manual, “Raster analysis” lessons: https://docs.qgis.org/latest/en/docs/training_manual/rasters/index.html.
14.9.4 Data and other resources
- Multi-Resolution Land Characteristics Consortium (NLCD): https://www.mrlc.gov.
- USGS 3D Elevation Program (3DEP): https://www.usgs.gov/3d-elevation-program.
- USGS EarthExplorer, for Landsat and elevation data: https://earthexplorer.usgs.gov.
- The GIS&T Body of Knowledge, “Map Algebra” and “Raster Data Models”: https://www.ucgis.org/site/gis-t-body-of-knowledge.