You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: labs/interpolation/01-overview-of-the-approach.Rmd
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
#### GISC 422 T1 2021
1
+
#### GISC 422 T2 2023
2
2
3
3
# The overall approach to interpolation
4
4
The *R* ecosystem's approach to spatial interpolation seems pretty complicated at first. It's not exactly simple, although the overall concept is straightforward enough and some of the apparent complexity has more to do with making different data types interact with one another successfully.
Copy file name to clipboardExpand all lines: labs/interpolation/01-overview-of-the-approach.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
#### GISC 422 T1 2021
1
+
#### GISC 422 T2 2023
2
2
3
3
# The overall approach to interpolation
4
4
The *R* ecosystem's approach to spatial interpolation seems pretty complicated at first. It's not exactly simple, although the overall concept is straightforward enough and some of the apparent complexity has more to do with making different data types interact with one another successfully.
Copy file name to clipboardExpand all lines: labs/interpolation/02-example-dataset.Rmd
+3-3Lines changed: 3 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -1,19 +1,19 @@
1
-
#### GISC 422 T1 2021
1
+
#### GISC 422 T2 2023
2
2
# Basics of working with raster data
3
3
First load some libraries
4
4
5
5
```{r}
6
6
library(sf)
7
7
library(tmap)
8
-
library(raster)
8
+
library(terra)
9
9
```
10
10
The new(ish) to us kid on the block here is `raster` for handling gridded raster datasets. One thing to be very aware of is that `raster` masks the `select` function from `dplyr` so you have to specify `dplyr::select` when using the `select` to tidy up datasets during data preparation.
11
11
12
12
## Read a raster dataset
13
13
We are using a simple example of the elevation of Maungawhau (Mt Eden) in Auckland to demonstrate the interpolation methods. There is a version of this dataset available as standard in *R* but I made raster version of it for us to work with. So load this with the raster package:
14
14
15
15
```{r}
16
-
volcano <- raster("data/maungawhau.tif")
16
+
volcano <- rast("data/maungawhau.tif")
17
17
```
18
18
19
19
Confusingly, when you read in a raster dataset it names the associated numerical data using the filename, so we rename that to `height` which is more appropriate for our purposes.
Copy file name to clipboardExpand all lines: labs/interpolation/02-example-dataset.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,19 +1,19 @@
1
-
#### GISC 422 T1 2021
1
+
#### GISC 422 T2 2023
2
2
# Basics of working with raster data
3
3
First load some libraries
4
4
5
5
```{r}
6
6
library(sf)
7
7
library(tmap)
8
-
library(raster)
8
+
library(terra)
9
9
```
10
10
The new(ish) to us kid on the block here is `raster` for handling gridded raster datasets. One thing to be very aware of is that `raster` masks the `select` function from `dplyr` so you have to specify `dplyr::select` when using the `select` to tidy up datasets during data preparation.
11
11
12
12
## Read a raster dataset
13
13
We are using a simple example of the elevation of Maungawhau (Mt Eden) in Auckland to demonstrate the interpolation methods. There is a version of this dataset available as standard in *R* but I made raster version of it for us to work with. So load this with the raster package:
14
14
15
15
```{r}
16
-
volcano <- raster("data/maungawhau.tif")
16
+
volcano <- rast("data/maungawhau.tif")
17
17
```
18
18
19
19
Confusingly, when you read in a raster dataset it names the associated numerical data using the filename, so we rename that to `height` which is more appropriate for our purposes.
Copy file name to clipboardExpand all lines: labs/interpolation/03-preparing-for-interpolation.Rmd
+14-9Lines changed: 14 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,14 @@
1
-
#### GISC 422 T1 2021
1
+
#### GISC 422 T2 2023
2
2
3
3
# Preparing for interpolation
4
4
Run this first to make sure all the data and packages you need are loaded:
5
5
```{r}
6
6
library(sf)
7
7
library(tmap)
8
-
library(raster)
8
+
library(terra)
9
9
library(dplyr)
10
10
11
-
volcano <- raster("data/maungawhau.tif")
11
+
volcano <- rast("data/maungawhau.tif")
12
12
names(volcano) <- "height"
13
13
```
14
14
@@ -34,14 +34,19 @@ interp_ext <- controls %>%
34
34
## Control points
35
35
For the demonstration data, we already know the result.
36
36
37
-
Normally, we would have a set of control points in some spatial format and would simply read them with `sf::st_read`. Here, we will make set of random control points to work with in the other steps of these instructions when we are using the Maungawhau data. We start from the interpolation extent, and use `st_sample` to get a specified random number of points in the extent, then convert it to an `sf` dataset. Finally we use the `raster::extract` function to extract values from the raster dataset and assign their values to a height attribute of the `sf` dataset.
37
+
Normally, we would have a set of control points in some spatial format and would simply read them with `sf::st_read`. Here, we will make set of random control points to work with in the other steps of these instructions when we are using the Maungawhau data. We start from the interpolation extent, and use `st_sample` to get a specified random number of points in the extent, then convert it to an `sf` dataset. Finally we use the `terra::extract` function to extract values from the raster dataset and assign their values to a height attribute of the `sf` dataset.
38
38
39
39
```{r}
40
40
controls <- interp_ext %>%
41
41
st_sample(size = 250) %>%
42
42
st_sf() %>%
43
-
st_set_crs(st_crs(interp_ext)) %>%
44
-
mutate(height = raster::extract(volcano, .))
43
+
st_set_crs(st_crs(interp_ext))
44
+
45
+
heights <- controls %>%
46
+
extract(x = volcano)
47
+
48
+
controls <- controls %>%
49
+
mutate(height = heights$height)
45
50
```
46
51
47
52
Every time you run the above you will get a different random set of the specified number of control point locations. It is useful to map them on top of the underlying data and think about how many you might need to get a reasonable representation of the height map of Maungawhau.
Remember that if you change the control points, you should also change this file to keep them matching.
67
72
68
73
## Make a set of locations to interpolate
69
-
Unlike the previous step which may not be necessary when you are provided with control points to interpolate directly, this step is always required. Basically, *R* wants a raster layer to *interpolate into*. We'll call this `sites` and make `sf`, `raster` and simple xyz versions of these.
74
+
Unlike the previous step which may not be necessary when you are provided with control points to interpolate directly, this step is always required. Basically, *R* wants a raster layer to *interpolate into*. We'll call this `sites` and make `sf`, `terra` and simple xyz versions of these.
70
75
71
76
```{r}
72
77
sites_sf <- interp_ext %>% # start with the extent
@@ -75,12 +80,12 @@ sites_sf <- interp_ext %>% # start with the extent
Copy file name to clipboardExpand all lines: labs/interpolation/03-preparing-for-interpolation.md
+14-9Lines changed: 14 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,14 @@
1
-
#### GISC 422 T1 2021
1
+
#### GISC 422 T2 2023
2
2
3
3
# Preparing for interpolation
4
4
Run this first to make sure all the data and packages you need are loaded:
5
5
```{r}
6
6
library(sf)
7
7
library(tmap)
8
-
library(raster)
8
+
library(terra)
9
9
library(dplyr)
10
10
11
-
volcano <- raster("data/maungawhau.tif")
11
+
volcano <- rast("data/maungawhau.tif")
12
12
names(volcano) <- "height"
13
13
```
14
14
@@ -34,14 +34,19 @@ interp_ext <- controls %>%
34
34
## Control points
35
35
For the demonstration data, we already know the result.
36
36
37
-
Normally, we would have a set of control points in some spatial format and would simply read them with `sf::st_read`. Here, we will make set of random control points to work with in the other steps of these instructions when we are using the Maungawhau data. We start from the interpolation extent, and use `st_sample` to get a specified random number of points in the extent, then convert it to an `sf` dataset. Finally we use the `raster::extract` function to extract values from the raster dataset and assign their values to a height attribute of the `sf` dataset.
37
+
Normally, we would have a set of control points in some spatial format and would simply read them with `sf::st_read`. Here, we will make set of random control points to work with in the other steps of these instructions when we are using the Maungawhau data. We start from the interpolation extent, and use `st_sample` to get a specified random number of points in the extent, then convert it to an `sf` dataset. Finally we use the `terra::extract` function to extract values from the raster dataset and assign their values to a height attribute of the `sf` dataset.
38
38
39
39
```{r}
40
40
controls <- interp_ext %>%
41
41
st_sample(size = 250) %>%
42
42
st_sf() %>%
43
-
st_set_crs(st_crs(interp_ext)) %>%
44
-
mutate(height = raster::extract(volcano, .))
43
+
st_set_crs(st_crs(interp_ext))
44
+
45
+
heights <- controls %>%
46
+
extract(x = volcano)
47
+
48
+
controls <- controls %>%
49
+
mutate(height = heights$height)
45
50
```
46
51
47
52
Every time you run the above you will get a different random set of the specified number of control point locations. It is useful to map them on top of the underlying data and think about how many you might need to get a reasonable representation of the height map of Maungawhau.
Remember that if you change the control points, you should also change this file to keep them matching.
67
72
68
73
## Make a set of locations to interpolate
69
-
Unlike the previous step which may not be necessary when you are provided with control points to interpolate directly, this step is always required. Basically, *R* wants a raster layer to *interpolate into*. We'll call this `sites` and make `sf`, `raster` and simple xyz versions of these.
74
+
Unlike the previous step which may not be necessary when you are provided with control points to interpolate directly, this step is always required. Basically, *R* wants a raster layer to *interpolate into*. We'll call this `sites` and make `sf`, `terra` and simple xyz versions of these.
70
75
71
76
```{r}
72
77
sites_sf <- interp_ext %>% # start with the extent
@@ -75,12 +80,12 @@ sites_sf <- interp_ext %>% # start with the extent
Copy file name to clipboardExpand all lines: labs/interpolation/04-nn-and-idw.Rmd
+9-9Lines changed: 9 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -1,20 +1,20 @@
1
-
#### GISC 422 T1 2021
1
+
#### GISC 422 T2 2023
2
2
# Near neighbour and inverse-distance weighted interpolation
3
3
Run this first to make sure all the data and packages you need are loaded. If any data are missing you probably didn't make them in one of the previous instruction pages.
4
4
5
5
```{r}
6
6
library(sf)
7
7
library(tmap)
8
-
library(raster)
8
+
library(terra)
9
9
library(dplyr)
10
10
library(gstat)
11
11
12
-
volcano <- raster("data/maungawhau.tif")
12
+
volcano <- rast("data/maungawhau.tif")
13
13
names(volcano) <- "data/height"
14
14
15
15
controls <- st_read("data/controls.gpkg")
16
16
sites_sf <- st_read("data/sites-sf.gpkg")
17
-
sites_raster <- raster("data/sites-raster.tif")
17
+
sites_raster <- rast("data/sites-raster.tif")
18
18
```
19
19
20
20
## Inverse-distance weighted interpolation
@@ -23,9 +23,9 @@ These two methods are very similar, and IDW is actually *more general* so we'll
23
23
As with all the `gstat` methods we use the `gstat::gstat` function to make a statistical model, and then apply it using the `raster::interpolate` function.
24
24
25
25
```{r}
26
-
fit_IDW <- gstat( # makes a model
27
-
formula = height ~ 1, # The column `height` is what we are interested in
28
-
data = as(controls, "Spatial"), # using sf but converting to sp, which is required
26
+
fit_IDW <- gstat( # makes a model
27
+
formula = height ~ 1, # The column `height` is what we are interested in
28
+
data = as(controls, "Spatial"), # using sf but converting to sp, which is required
29
29
set = list(idp = 2),
30
30
# nmax = 12, maxdist = 100 # you can experiment with these options later...
31
31
)
@@ -37,7 +37,7 @@ Having made the model (called `fit_IDW`) we pass it to the `predict` function to
Copy file name to clipboardExpand all lines: labs/interpolation/04-nn-and-idw.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,20 +1,20 @@
1
-
#### GISC 422 T1 2021
1
+
#### GISC 422 T2 2023
2
2
# Near neighbour and inverse-distance weighted interpolation
3
3
Run this first to make sure all the data and packages you need are loaded. If any data are missing you probably didn't make them in one of the previous instruction pages.
4
4
5
5
```{r}
6
6
library(sf)
7
7
library(tmap)
8
-
library(raster)
8
+
library(terra)
9
9
library(dplyr)
10
10
library(gstat)
11
11
12
-
volcano <- raster("data/maungawhau.tif")
12
+
volcano <- rast("data/maungawhau.tif")
13
13
names(volcano) <- "data/height"
14
14
15
15
controls <- st_read("data/controls.gpkg")
16
16
sites_sf <- st_read("data/sites-sf.gpkg")
17
-
sites_raster <- raster("data/sites-raster.tif")
17
+
sites_raster <- rast("data/sites-raster.tif")
18
18
```
19
19
20
20
## Inverse-distance weighted interpolation
@@ -23,9 +23,9 @@ These two methods are very similar, and IDW is actually *more general* so we'll
23
23
As with all the `gstat` methods we use the `gstat::gstat` function to make a statistical model, and then apply it using the `raster::interpolate` function.
24
24
25
25
```{r}
26
-
fit_IDW <- gstat( # makes a model
27
-
formula = height ~ 1, # The column `height` is what we are interested in
28
-
data = as(controls, "Spatial"), # using sf but converting to sp, which is required
26
+
fit_IDW <- gstat( # makes a model
27
+
formula = height ~ 1, # The column `height` is what we are interested in
28
+
data = as(controls, "Spatial"), # using sf but converting to sp, which is required
29
29
set = list(idp = 2),
30
30
# nmax = 12, maxdist = 100 # you can experiment with these options later...
31
31
)
@@ -37,7 +37,7 @@ Having made the model (called `fit_IDW`) we pass it to the `predict` function to
Copy file name to clipboardExpand all lines: labs/interpolation/05-trend-surfaces-and-kriging.Rmd
+6-6Lines changed: 6 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
#### GISC 422 T1 2021
1
+
#### GISC 422 T2 2023
2
2
3
3
# Trend surfaces and kriging
4
4
@@ -7,16 +7,16 @@ Run this first to make sure all the data and packages you need are loaded. If an
7
7
```{r}
8
8
library(sf)
9
9
library(tmap)
10
-
library(raster)
10
+
library(terra)
11
11
library(dplyr)
12
12
library(gstat)
13
13
14
-
volcano <- raster("data/maungawhau.tif")
14
+
volcano <- rast("data/maungawhau.tif")
15
15
names(volcano) <- "data/height"
16
16
17
17
controls <- st_read("data/controls.gpkg")
18
18
sites_sf <- st_read("data/sites-sf.gpkg")
19
-
sites_raster <- raster("data/sites-raster.tif")
19
+
sites_raster <- rast("data/sites-raster.tif")
20
20
```
21
21
22
22
There are many different styles of kriging. We'll work here with universal kriging which models variation in the data with two components a *trend surface* and a *variogram* which models how control points vary with distance from one another. So... to perform kriging we have to consider each of these elements in turn.
@@ -38,7 +38,7 @@ The form of the trend surface function is specified by the `degree` parameter an
Copy file name to clipboardExpand all lines: labs/interpolation/05-trend-surfaces-and-kriging.md
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
#### GISC 422 T1 2021
1
+
#### GISC 422 T2 2023
2
2
3
3
# Trend surfaces and kriging
4
4
@@ -7,16 +7,16 @@ Run this first to make sure all the data and packages you need are loaded. If an
7
7
```{r}
8
8
library(sf)
9
9
library(tmap)
10
-
library(raster)
10
+
library(terra)
11
11
library(dplyr)
12
12
library(gstat)
13
13
14
-
volcano <- raster("data/maungawhau.tif")
14
+
volcano <- rast("data/maungawhau.tif")
15
15
names(volcano) <- "data/height"
16
16
17
17
controls <- st_read("data/controls.gpkg")
18
18
sites_sf <- st_read("data/sites-sf.gpkg")
19
-
sites_raster <- raster("data/sites-raster.tif")
19
+
sites_raster <- rast("data/sites-raster.tif")
20
20
```
21
21
22
22
There are many different styles of kriging. We'll work here with universal kriging which models variation in the data with two components a *trend surface* and a *variogram* which models how control points vary with distance from one another. So... to perform kriging we have to consider each of these elements in turn.
@@ -38,7 +38,7 @@ The form of the trend surface function is specified by the `degree` parameter an
0 commit comments