Skip to content
This repository has been archived by the owner on Jan 13, 2021. It is now read-only.

Commit

Permalink
add raster presentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam M. Wilson committed Oct 2, 2018
1 parent 389f6a4 commit af47864
Show file tree
Hide file tree
Showing 54 changed files with 2,726 additions and 6 deletions.
16 changes: 15 additions & 1 deletion 05_Raster.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,21 @@ title: "Introduction to Raster Package"
source("knitr_header.R")
```

[<i class="fa fa-file-code-o fa-3x" aria-hidden="true"></i> The R Script associated with this page is available here](`r output`). Download this file and open it (or copy-paste into a new script) with RStudio so you can follow along.
<div class="extraswell">
<button data-toggle="collapse" class="btn btn-link" data-target="#pres">View Presentation </button> [Open presentation in a new tab](presentations/day_10_raster.html){target="_blank"}
<div id="pres" class="collapse">
<div class="embed-responsive embed-responsive-16by9">
<iframe class="embed-responsive-item" src="presentations/day_10_raster.html" allowfullscreen></iframe>
_Click on presentation and then use the space bar to advance to the next slide or escape key to show an overview._
</div>
</div>
</div>


## Download

| [<i class="fas fa-code fa-2x" aria-hidden="true"></i><br> R Script](`r output_nocomment`) | [<i class="fa fa-file-code-o fa-2x"></i> <br> Commented R Script](`r output`) | [<i class="far fa-file-alt fa-2x"></i> <br> Rmd Script](`r fullinput`)|
|:--:|:-:|:-:|

## Libraries

Expand Down
16 changes: 15 additions & 1 deletion 05_Raster.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,21 @@ title: "Introduction to Raster Package"



[<i class="fa fa-file-code-o fa-3x" aria-hidden="true"></i> The R Script associated with this page is available here](scripts/05_Raster.R). Download this file and open it (or copy-paste into a new script) with RStudio so you can follow along.
<div class="extraswell">
<button data-toggle="collapse" class="btn btn-link" data-target="#pres">View Presentation </button> [Open presentation in a new tab](presentations/day_10_raster.html){target="_blank"}
<div id="pres" class="collapse">
<div class="embed-responsive embed-responsive-16by9">
<iframe class="embed-responsive-item" src="presentations/day_10_raster.html" allowfullscreen></iframe>
_Click on presentation and then use the space bar to advance to the next slide or escape key to show an overview._
</div>
</div>
</div>


## Download

| [<i class="fas fa-code fa-2x" aria-hidden="true"></i><br> R Script](scripts/05_Raster_nocomments.R) | [<i class="fa fa-file-code-o fa-2x"></i> <br> Commented R Script](scripts/05_Raster.R) | [<i class="far fa-file-alt fa-2x"></i> <br> Rmd Script](scripts/05_Raster.Rmd)|
|:--:|:-:|:-:|

## Libraries

Expand Down
2 changes: 1 addition & 1 deletion CS_05.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Your final result should look something like this:
```{r purl=F, echo=F, message=FALSE, warning=FALSE}
canada <- world %>%
st_transform(crs=albers) %>%
rename(geometry=geom) %>%
st_set_geometry("geom")%>%
filter(name_long=="Canada") %>%
st_buffer(10000)
Expand Down
26 changes: 25 additions & 1 deletion docs/05_Raster.html
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,31 @@ <h1 class="title toc-ignore">Introduction to Raster Package</h1>
</div>


<p><a href="scripts/05_Raster.R"><i class="fa fa-file-code-o fa-3x" aria-hidden="true"></i> The R Script associated with this page is available here</a>. Download this file and open it (or copy-paste into a new script) with RStudio so you can follow along.</p>
<div class="extraswell">
<button data-toggle="collapse" class="btn btn-link" data-target="#pres">
View Presentation
</button>
<a href="presentations/day_10_raster.html" target="_blank">Open presentation in a new tab</a>
<div id="pres" class="collapse">
<div class="embed-responsive embed-responsive-16by9">
<p><iframe class="embed-responsive-item" src="presentations/day_10_raster.html" allowfullscreen></iframe> <em>Click on presentation and then use the space bar to advance to the next slide or escape key to show an overview.</em></p>
</div>
</div>
</div>
<div id="download" class="section level2">
<h2>Download</h2>
<table>
<thead>
<tr class="header">
<th align="center"><a href="scripts/05_Raster_nocomments.R"><i class="fas fa-code fa-2x" aria-hidden="true"></i><br> R Script</a></th>
<th align="center"><a href="scripts/05_Raster.R"><i class="fa fa-file-code-o fa-2x"></i> <br> Commented R Script</a></th>
<th align="center"><a href="scripts/05_Raster.Rmd"><i class="far fa-file-alt fa-2x"></i> <br> Rmd Script</a></th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<div id="libraries" class="section level2">
<h2>Libraries</h2>
<pre class="r"><code>library(dplyr)
Expand Down
189 changes: 189 additions & 0 deletions docs/presentations/day_10_raster.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
---
title: "Raster Analyses"
---

```{r echo=F, eval=T, message=F,warning=F}
library(sf)
library(spData)
library(viridis)
library(tidyverse)
library(raster)
```

---

# Spatial Data in R

## Available Packages

* `sp` First major spatial data package/format
* `rgdal` reading and writing spatial data
* `rgeos` Interface to open-source geometry engine (GEOS)
* `sf` Spatial Features in the 'tidyverse'
* `raster` gridded data (like satellite imagery)
* and a few others...

## Common Simple Feature (SF) types

![](https://geocompr.robinlovelace.net/figures/sf-classes.png)

## What about grids?
```{r echo=F, message=F,warning=F}
library(raster)
library(spData)
data(elev) #load fake data from spData package
plot(elev)
```


# Raster Data

## Raster Data Types
Many types including:

* Thematic (classified/thematic) data: land-use or soils data.
* Continuous data (temperature, elevation, spectral)
* Imagery / Pictures (scanned maps, drawings, etc.)

Most Common:

* Satellite Imagery and derived products
* Earth System Models (e.g. climate models)
* Other models


## Raster data in the tidyverse

Raster data is not yet closely connected to the **tidyverse**, however:

- Some functions from `raster` work well in `pipes`
- Convert vector data to `Spatial*` form using `as(my_vector, "Spatial")` for raster-vector interactions
- Some early efforts to bring raster data into the **tidyverse**, including [tabularaster](https://github.com/hypertidy/tabularaster), [sfraster](https://github.com/mdsumner/sfraster), [fasterize](https://github.com/ecohealthalliance/fasterize), and [stars](https://github.com/r-spatial/stars) (multidimensional, large datasets).

## Raster Package
```{r message=F,warning=F}
library(raster)
library(spData)
data(elev) #load fake data from spData package
plot(elev) #load fake data from spData package
```



## Raster Data Structure
```{r}
elev
```

---
```{r}
str(elev)
```

---

### Arrays with metadata
```{r}
as.array(elev)
```
Rasters are just arrays / matricies with metadata.

---

### Cell Stats

```{r}
cellStats(elev, stat = mean, na.rm=T)
cellStats(elev, stat = quantile, na.rm=T)
```

---

### Raster Data Memory
```{r}
inMemory(elev)
canProcessInMemory(elev)
```
Raster does not try to load or work with large datasets in RAM. It creates temporary files and processes them in the background.

## Map Algebra

Convert feet to meters
```{r}
elev_m <- elev*0.3048
plot(elev_m)
```

---

### Simple Filter
```{r}
high_ground <- elev_m>8
plot(high_ground)
```


---

### Focal
```{r}
m <- matrix(1,nrow=3,ncol=3)
m
elev_smooth <- focal(elev_m,m,mean)
```
---
```{r}
par(mfrow=c(1,2))
plot(elev_m)
plot(elev_smooth)
```

## Arbitrary functions

```{r}
NA16=function(x) ifelse(x == 16,1,NA)
one_cell <- calc(elev,fun = NA16)
plot(one_cell)
```

## Distances to non-NA cells
```{r}
distance(one_cell)%>%
plot()
```

Distance unit is meters if RasterLayer is `+proj=longlat`, map units (typically also meters) otherwise.

## And much more

* aggregate (to coarser resolution)
* area (calculate cell area)
* buffer
* contour
* crop
* interpolate
* Moran's I


# Visualization

## ggplot

```{r, message=F}
library(rasterVis)
gplot(elev)+
geom_raster(aes(fill=value))
```

---

### All ggplot functionality available

```{r}
gplot(elev)+
geom_raster(aes(fill=value))+
coord_equal()+
scale_fill_viridis_c()
```


Loading

0 comments on commit af47864

Please sign in to comment.