---
title: "Raster Display"
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

`plot_raster()` displays data using base R `graphics`. The function will read from an open dataset, or use pixel values that have already been read into a vector.

```{r setup}
library(gdalraster)

base_url <- "/vsicurl/https://raw.githubusercontent.com/firelab/gdalraster/main/sample-data/"
```

## Single-band grayscale or color ramp

```{r}
#| fig.alt: >
#|   A plot of an elevation raster for an area of interest around Mt. Hood,
#|   Oregon, USA. Elevation is shown in grayscale with the lowest values dark
#|   gray (minimum 553 m) and the highest values almost white (maximum 3421 m).
f <- paste0(base_url, "lf_elev_220_mt_hood_utm.tif")
ds <- new(GDALRaster, f)

# gray
plot_raster(ds, legend = TRUE, main = "Mount Hood elevation (m)")
```

```{r}
#| fig.alt: >
#|   A plot of an elevation raster for an area of interest around Mt. Hood,
#|   Oregon, USA. A terrain color ramp is used with the lowest values green
#|   (minimum 553 m), intermediate values yellow to orange, and the highest
#|   values almost white (maximum 3421 m).
pal <- c("#00A60E", "#63C600", "#E6E600", "#E9BD3B", "#ECB176", "#EFC2B3",
         "#F2F2F2")

plot_raster(ds, legend = TRUE, col_map_fn = pal,
            main = "Mount Hood elevation (m)")
ds$close()
```

## RGB

```{r}
#| fig.alt: >
#|   A plot of a Landsat image for an area of interest around Mt. Hood,
#|   Oregon, USA. The image is from July 2022. It is shown as a false color
#|   composite of Landsat bands 7, 5 and 4 (SWIR).
f <- paste0(base_url, "landsat_c2ard_sr_mt_hood_jul2022_utm.tif")
ds <- new(GDALRaster, f)

# passing a vector of pixel values rather than the open dataset
r <- read_ds(ds, bands = c(7, 5, 4))
ds$close()

# normalizing to ranges derived from the full Landsat scene (2-98 percentiles)
plot_raster(r, minmax_def = c(7551,7679,7585,14842,24997,12451),
            main = "Mount Hood July 2022 Landsat 7-5-4 (SWIR)")
```

## Color table

```{r}
#| fig.alt: >
#|   A plot of LANDFIRE surface fuel classes for an area of interest around
#|   Mt. Hood, Oregon, USA.
f <- paste0(base_url, "lf_fbfm40_220_mt_hood_utm.tif")
ds <- new(GDALRaster, f)
dm <- ds$dim()
paste("Size is", dm[1], "x",  dm[2], "x", dm[3])

# using the CSV attribute table distributed by LANDFIRE
fbfm_csv <- system.file("extdata/LF20_F40_220.csv", package="gdalraster")
vat <- read.csv(fbfm_csv)
head(vat)
vat <- vat[, c(1,6:8)]

# read at reduced resolution for display
plot_raster(ds, xsize = dm[1] / 2, ysize = dm[2] / 2,
			col_tbl = vat, interpolate = FALSE,
            main = "LANDFIRE surface fuel class (FBFM40)")
ds$close()
```

## Axis labels

```{r}
#| fig.alt: >
#|   A plot of the NASA Earth Observatory Blue Marble image from July 2004.
#|   NASA's Blue Marble: Next Generation images show Earth in true color. The
#|   images show how the surface would look to a human in space if our world
#|   had no clouds and no atmosphere. NASA's Terra satellite collected these
#|   images. There is one Blue Marble image for each month of the year 2004.
f <- paste0(base_url, "bl_mrbl_ng_jul2004_rgb_720x360.tif")
ds <- new(GDALRaster, f)
ds$getProjectionRef() |> srs_is_projected()
r <- read_ds(ds)
ds$close()

plot_raster(r, xlab = "longitude", ylab = "latitude",
            main = "NASA Earth Observatory Blue Marble July 2004")
```

