# Rasters

## Raster algebra

Raster algebra (or raster map algebra) is probably the most general way of manipulating rasters which is accessible to the user.

### if statement

Let's determine forested areas which are higher then certain elevation. Set computational region (both extent and resolution) to the landclass raster: ```g.region rast=landclass96 ``` See what are the land classes (raster categories) in the raster: ```r.report map=landclass96 units=p ``` See also univariate statistics for the elevation raster: ```r.univar map=elevation ``` Use raster algebra to select areas higher than chosen elevation and with forest land class: ```r.mapcalc "forest_high = if(elevation > 120 && landclass96 == 5, 1, null())" ```

### `eval` function
If the output of the computation should be only one map but the expression is so complex that it is better to split it to several expressions, the eval function can be used: ```r.mapcalc "eval(elev_200 = elevation - 200, elev_5 = 5 * elevation, elev_p = pow(elev_5, 2)); elevation_result = (0.5 * elev_200) + 0.8 * elev_p" ```
### neighborhood operator `[ , ]`
Apply low pass filter (smoothing) on a Landsat image using r.mapcalc: ```r.mapcalc "lsat7_2002_10_smooth = (lsat7_2002_10[-1,-1] + lsat7_2002_10[-1,0] + lsat7_2002_10[1,1] + lsat7_2002_10[0,-1] + lsat7_2002_10[0,0] + lsat7_2002_10[0,1] + lsat7_2002_10[1,-1] + lsat7_2002_10[1,0] + lsat7_2002_10[1,1]) / 9" ``` Set the same color table as the original raster map has: ```r.colors map=lsat7_2002_10_smooth raster=lsat7_2002_10 ``` Use Map Swipe to compare original and resulting map. Map Swipe is available from File menu or using: ```g.gui.mapswipe first=lsat7_2002_10 second=lsat7_2002_10_smooth ``` Note that in this case the same operation which was done using r.mapcalc could be done using r.neighbors module.