Skip to content

Commit

Permalink
Update raster slides
Browse files Browse the repository at this point in the history
  • Loading branch information
cforgaci committed Jan 31, 2025
1 parent 6e2c520 commit f80c132
Show file tree
Hide file tree
Showing 23 changed files with 517 additions and 39 deletions.
335 changes: 310 additions & 25 deletions instructors/3-raster-slides.html

Large diffs are not rendered by default.

131 changes: 129 additions & 2 deletions instructors/3-raster-slides.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,24 @@ knitr::opts_chunk$set(
warning = FALSE,
error = FALSE
)
library(tidyverse)
library(terra)
```

```{r echo = FALSE}
DSM_TUD <- rast("../episodes/data/tud-dsm-5m.tif")
DSM_TUD_df <- as.data.frame(DSM_TUD, xy = TRUE)
DTM_TUD <- rast("../episodes/data/tud-dtm-5m.tif")
DTM_TUD_df <- as.data.frame(DTM_TUD, xy = TRUE)
CHM_TUD <- DSM_TUD - DTM_TUD
CHM_TUD_df <- as.data.frame(CHM_TUD, xy = TRUE)
DTM_hill_TUD <- rast("../episodes/data/tud-dtm-5m-hill-WGS84.tif")
DTM_hill_TUD_df <- as.data.frame(DTM_hill_TUD, xy = TRUE)
```

## Outline

Expand All @@ -46,7 +62,7 @@ knitr::opts_chunk$set(

## Challenge 1: **2 mins**

Use `describe()` to determine the following about the `tud-dsm-hill.tif` file:
Use `describe()` to determine the following about the `tud-dsm-5m-hill.tif` file:

1. Does this file have the same CRS as `DSM_TUD`?
2. What is resolution of the raster data?
Expand All @@ -59,6 +75,34 @@ Use `describe()` to determine the following about the `tud-dsm-hill.tif` file:
countdown::countdown(minutes = 2)
```

## Challenge 1 - Solution {.smaller}

:::: {.columns}

::: {.column}

::: {.nonincremental}

1. Does this file have the same CRS as `DSM_TUD`?
2. What is resolution of the raster data?
3. How large would a 5x5 pixel area be on the Earth’s surface?
4. Is the file a multi- or single-band raster?

:::

:::

::: {.column}

```{r}
# Replace with your own path
describe("../episodes/data/tud-dsm-5m-hill.tif")
```

:::

::::

# Plotting raster data

## Challenge 2: **5 mins**
Expand All @@ -74,6 +118,22 @@ Create a plot of the TU Delft Digital Surface Model (`DSM_TUD`) that has:
countdown::countdown(minutes = 5)
```

## Challenge 2 - Solution

```{r}
DSM_TUD_df <- DSM_TUD_df %>%
mutate(fct_elevation_6 = cut(`tud-dsm-5m`, breaks = 6))
my_col <- terrain.colors(6)
ggplot() +
geom_raster(data = DSM_TUD_df, aes(x = x, y = y,
fill = fct_elevation_6)) +
scale_fill_manual(values = my_col, name = "Elevation") +
coord_equal() +
labs(title = "Elevation Classes of the Digital Surface Model (DSM)")
```

# Reprojecting raster data

## Challenge 3: **2 mins**
Expand All @@ -86,6 +146,18 @@ View the CRS for each of these two datasets. What projection does each use?
countdown::countdown(minutes = 2)
```

## Challenge 3 - Solution

```{r dtm-crs}
crs(DTM_TUD, parse = TRUE) %>% head()
```

```{r hill-crs}
crs(DTM_hill_TUD, parse = TRUE) %>% head()
```

`DTM_TUD` is in the Amersfoort / RD New projection, whereas `DTM_hill_TUD` is in WGS 84.

# Raster calculations

## Challenge 4: **10 mins**
Expand All @@ -94,12 +166,67 @@ It’s often a good idea to explore the range of values in a raster dataset just

1. What is the min and max value for the Canopy Height Model `CHM_TUD` that we just created?
3. What is the distribution of all the pixel values in the CHM?
5. Plot the `CHM_TUD` raster using breaks that make sense for the data. Include an appropriate color palette for the data, plot title and no axes ticks / labels.
5. Plot the `CHM_TUD` raster using breaks that make sense for the data.

``` {r}
#| echo: false
#| cache: false
countdown::countdown(minutes = 10)
```

## Challenge 4 - Solution part 1

```{r chm-challenge-1}
min(CHM_TUD_df$`tud-dsm-5m`, na.rm = TRUE)
max(CHM_TUD_df$`tud-dsm-5m`, na.rm = TRUE)
```

## Challenge 4 - Solution part 2

```{r chm-challenge-2}
ggplot(CHM_TUD_df) +
geom_histogram(aes(`tud-dsm-5m`))
```

## Challenge 4 - Solution part 3

```{r chm-challenge-3}
custom_bins <- c(-5, 0, 10, 20, 30, 100)
CHM_TUD_df <- CHM_TUD_df |>
mutate(canopy_discrete = cut(`tud-dsm-5m`, breaks = custom_bins))
ggplot() +
geom_raster(data = CHM_TUD_df , aes(x = x, y = y,
fill = canopy_discrete)) +
scale_fill_manual(values = terrain.colors(5)) +
coord_quickmap()
```


# Working with multi-band rasters

## Challenge 5: **5 mins**

We can use the `rast()` function to import specific bands in our raster object by specifying which band we want with `lyrs = N` (N represents the band number we want to work with). To import the green band, we would use `lyrs = 2`. We can convert this data to a data frame and plot the same way we plotted the red band.

Import and plot the green band.

``` {r}
#| echo: false
#| cache: false
countdown::countdown(minutes = 5)
```

## Challenge 5 - Solution

```{r}
# replace path with your data folder
RGB_band2_TUD <- rast("../episodes/data/tudlib-rgb.tif", lyrs = 2)
RGB_band2_TUD_df <- as.data.frame(RGB_band2_TUD, xy = TRUE)
ggplot() +
geom_raster(data = RGB_band2_TUD_df,
aes(x = x, y = y, alpha = `tudlib-rgb_2`)) +
coord_equal()
```
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ var PdfExport = ( function( _Reveal ){
Reveal = _Reveal;
install();
};
Plugin.togglePdfExport = function () {
togglePdfExport();
};
}

return Plugin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ window.QuartoLineHighlight = function () {
divSourceCode.forEach((el) => {
if (el.hasAttribute(kCodeLineNumbersAttr)) {
const codeLineAttr = el.getAttribute(kCodeLineNumbersAttr);
el.removeAttribute("data-code-line-numbers");
el.removeAttribute(kCodeLineNumbersAttr);
if (handleLinesSelector(deck, codeLineAttr)) {
// Only process if attr is a string to select lines to highlights
// e.g "1|3,6|8-11"
Expand Down Expand Up @@ -165,17 +165,17 @@ window.QuartoLineHighlight = function () {
if (typeof highlight.last === "number") {
spanToHighlight = [].slice.call(
codeBlock.querySelectorAll(
":scope > span:nth-child(n+" +
":scope > span:nth-of-type(n+" +
highlight.first +
"):nth-child(-n+" +
"):nth-of-type(-n+" +
highlight.last +
")"
)
);
} else if (typeof highlight.first === "number") {
spanToHighlight = [].slice.call(
codeBlock.querySelectorAll(
":scope > span:nth-child(" + highlight.first + ")"
":scope > span:nth-of-type(" + highlight.first + ")"
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
max-height: 2.2rem;
height: 100%;
width: auto;
z-index: 2;
}

.reveal .footer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ window.QuartoSupport = function () {
return /print-pdf/gi.test(window.location.search);
}

// helper for theme toggling
function toggleBackgroundTheme(el, onDarkBackground, onLightBackground) {
if (onDarkBackground) {
el.classList.add('has-dark-background')
} else {
el.classList.remove('has-dark-background')
}
if (onLightBackground) {
el.classList.add('has-light-background')
} else {
el.classList.remove('has-light-background')
}
}

// implement controlsAudo
function controlsAuto(deck) {
const config = deck.getConfig();
Expand Down Expand Up @@ -111,8 +125,19 @@ window.QuartoSupport = function () {
}
}

// add footer text
function addFooter(deck) {
// tweak slide-number element
function tweakSlideNumber(deck) {
deck.on("slidechanged", function (ev) {
const revealParent = deck.getRevealElement();
const slideNumberEl = revealParent.querySelector(".slide-number");
const onDarkBackground = Reveal.getSlideBackground(ev.indexh, ev.indexv).classList.contains('has-dark-background');
const onLightBackground = Reveal.getSlideBackground(ev.indexh, ev.indexv).classList.contains('has-light-background');
toggleBackgroundTheme(slideNumberEl, onDarkBackground, onLightBackground);
})
}

// add footer text
function addFooter(deck) {
const revealParent = deck.getRevealElement();
const defaultFooterDiv = document.querySelector(".footer-default");
if (defaultFooterDiv) {
Expand All @@ -127,13 +152,17 @@ window.QuartoSupport = function () {
prevSlideFooter.remove();
}
const currentSlideFooter = ev.currentSlide.querySelector(".footer");
const onDarkBackground = Reveal.getSlideBackground(ev.indexh, ev.indexv).classList.contains('has-dark-background')
const onLightBackground = Reveal.getSlideBackground(ev.indexh, ev.indexv).classList.contains('has-light-background')
if (currentSlideFooter) {
defaultFooterDiv.style.display = "none";
const slideFooter = currentSlideFooter.cloneNode(true);
handleLinkClickEvents(deck, slideFooter);
deck.getRevealElement().appendChild(slideFooter);
toggleBackgroundTheme(slideFooter, onDarkBackground, onLightBackground)
} else {
defaultFooterDiv.style.display = "block";
toggleBackgroundTheme(defaultFooterDiv, onDarkBackground, onLightBackground)
}
});
}
Expand Down Expand Up @@ -272,6 +301,23 @@ window.QuartoSupport = function () {
}
}

function handleWhiteSpaceInColumns(deck) {
for (const outerDiv of window.document.querySelectorAll("div.columns")) {
// remove all whitespace text nodes
// whitespace nodes cause the columns to be misaligned
// since they have inline-block layout
//
// Quarto emits no whitespace nodes, but third-party tooling
// has bugs that can cause whitespace nodes to be emitted.
// See https://github.com/quarto-dev/quarto-cli/issues/8382
for (const node of outerDiv.childNodes) {
if (node.nodeType === 3 && node.nodeValue.trim() === "") {
outerDiv.removeChild(node);
}
}
}
}

return {
id: "quarto-support",
init: function (deck) {
Expand All @@ -280,11 +326,13 @@ window.QuartoSupport = function () {
fixupForPrint(deck);
applyGlobalStyles(deck);
addLogoImage(deck);
tweakSlideNumber(deck);
addFooter(deck);
addChalkboardButtons(deck);
handleTabbyClicks();
handleSlideChanges(deck);
workaroundMermaidDistance(deck);
handleWhiteSpaceInColumns(deck);
},
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,15 @@ console.warn( "toggleNotesButton is deprecated, use customcontrols plugin instea
} );
a.href = window.URL.createObjectURL( blob );
} catch ( error ) {
// https://stackoverflow.com/a/6234804
// escape data for proper handling of quotes and line breaks
// in case malicious gets a chance to craft the exception message
error = String(error).replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;");

a.innerHTML += ' (' + error + ')';
}
a.click();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,7 @@ window.RevealMenuToolHandlers = {
downloadDrawings: revealMenuToolHandler(function () {
RevealChalkboard.download();
}),
togglePdfExport: revealMenuToolHandler(function () {
PdfExport.togglePdfExport();
}),
};

0 comments on commit f80c132

Please sign in to comment.