Skip to content

Commit c67c61a

Browse files
committed
Lesson 3 updated by replacing calls to raster and rgdal packages with calls to terra package. #368 #363
1 parent 231af26 commit c67c61a

File tree

1 file changed

+69
-59
lines changed

1 file changed

+69
-59
lines changed

episodes/03-raster-reproject-in-r.Rmd

Lines changed: 69 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ source("setup.R")
2222
::::::::::::::::::::::::::::::::::::::::::::::::::
2323

2424
```{r load-libraries, echo=FALSE, results="hide", message=FALSE, warning=FALSE}
25-
library(raster)
26-
library(rgdal)
25+
library(terra)
2726
library(ggplot2)
2827
library(dplyr)
2928
```
@@ -33,16 +32,17 @@ library(dplyr)
3332
## Things You'll Need To Complete This Episode
3433

3534
See the [lesson homepage](.) for detailed information about the software,
36-
data, and other prerequisites you will need to work through the examples in this episode.
35+
data, and other prerequisites you will need to work through the examples in
36+
this episode.
3737

3838

3939
::::::::::::::::::::::::::::::::::::::::::::::::::
4040

4141
Sometimes we encounter raster datasets that do not "line up" when plotted or
4242
analyzed. Rasters that don't line up are most often in different Coordinate
43-
Reference Systems (CRS). This episode explains how to deal with rasters in different, known CRSs. It
44-
will walk though reprojecting rasters in R using the `projectRaster()`
45-
function in the `raster` package.
43+
Reference Systems (CRS). This episode explains how to deal with rasters in
44+
different, known CRSs. It will walk though reprojecting rasters in R using
45+
the `project()` function in the `terra` package.
4646

4747
## Raster Projection in R
4848

@@ -57,21 +57,23 @@ far in that the digital surface model (DSM) includes the tops of trees, while
5757
the digital terrain model (DTM) shows the ground level.
5858

5959
We'll be looking at another model (the canopy height model) in
60-
[a later episode](04-raster-calculations-in-r/) and will see how to calculate the CHM from the
61-
DSM and DTM. Here, we will create a map of the Harvard Forest Digital
62-
Terrain Model
63-
(`DTM_HARV`) draped or layered on top of the hillshade (`DTM_hill_HARV`).
64-
The hillshade layer maps the terrain using light and shadow to create a 3D-looking image,
65-
based on a hypothetical illumination of the ground level.
60+
[a later episode](04-raster-calculations-in-r/) and will see how to calculate
61+
the CHM from the DSM and DTM. Here, we will create a map of the Harvard Forest
62+
Digital Terrain Model (`DTM_HARV`) draped or layered on top of the hillshade
63+
(`DTM_hill_HARV`).
64+
The hillshade layer maps the terrain using light and shadow to create a
65+
3D-looking image, based on a hypothetical illumination of the ground level.
6666

6767
![](fig/dc-spatial-raster/lidarTree-height.png){alt='Source: National Ecological Observatory Network (NEON).'}
6868

6969
First, we need to import the DTM and DTM hillshade data.
7070

7171
```{r import-DTM-hillshade}
72-
DTM_HARV <- raster("data/NEON-DS-Airborne-Remote-Sensing/HARV/DTM/HARV_dtmCrop.tif")
72+
DTM_HARV <-
73+
rast("data/NEON-DS-Airborne-Remote-Sensing/HARV/DTM/HARV_dtmCrop.tif")
7374
74-
DTM_hill_HARV <- raster("data/NEON-DS-Airborne-Remote-Sensing/HARV/DTM/HARV_DTMhill_WGS84.tif")
75+
DTM_hill_HARV <-
76+
rast("data/NEON-DS-Airborne-Remote-Sensing/HARV/DTM/HARV_DTMhill_WGS84.tif")
7577
```
7678

7779
Next, we will convert each of these datasets to a dataframe for
@@ -99,8 +101,7 @@ ggplot() +
99101

100102
Our results are curious - neither the Digital Terrain Model (`DTM_HARV_df`)
101103
nor the DTM Hillshade (`DTM_hill_HARV_df`) plotted.
102-
Let's try to
103-
plot the DTM on its own to make sure there are data there.
104+
Let's try to plot the DTM on its own to make sure there are data there.
104105

105106
```{r plot-DTM}
106107
ggplot() +
@@ -123,10 +124,12 @@ geom_raster(data = DTM_hill_HARV_df,
123124
coord_quickmap()
124125
```
125126

126-
If we look at the axes, we can see that the projections of the two rasters are different.
127-
When this is the case, `ggplot` won't render the image. It won't even
128-
throw an error message to tell you something has gone wrong. We can look at Coordinate Reference Systems (CRSs) of the DTM and
129-
the hillshade data to see how they differ.
127+
If we look at the axes, we can see that the projections of the two rasters are
128+
different.
129+
When this is the case, `ggplot` won't render the image. It won't even throw an
130+
error message to tell you something has gone wrong. We can look at Coordinate
131+
Reference Systems (CRSs) of the DTM and the hillshade data to see how they
132+
differ.
130133

131134
::::::::::::::::::::::::::::::::::::::: challenge
132135

@@ -141,10 +144,10 @@ does each use?
141144

142145
```{r explore-crs}
143146
# view crs for DTM
144-
crs(DTM_HARV)
147+
crs(DTM_HARV, parse = TRUE)
145148
146149
# view crs for hillshade
147-
crs(DTM_hill_HARV)
150+
crs(DTM_hill_HARV, parse = TRUE)
148151
```
149152

150153
`DTM_HARV` is in the UTM projection, with units of meters.
@@ -158,12 +161,12 @@ crs(DTM_hill_HARV)
158161
::::::::::::::::::::::::::::::::::::::::::::::::::
159162

160163
Because the two rasters are in different CRSs, they don't line up when plotted
161-
in R. We need to reproject (or change the projection of) `DTM_hill_HARV` into the UTM CRS. Alternatively,
162-
we could reproject `DTM_HARV` into WGS84.
164+
in R. We need to reproject (or change the projection of) `DTM_hill_HARV` into
165+
the UTM CRS. Alternatively, we could reproject `DTM_HARV` into WGS84.
163166

164167
## Reproject Rasters
165168

166-
We can use the `projectRaster()` function to reproject a raster into a new CRS.
169+
We can use the `project()` function to reproject a raster into a new CRS.
167170
Keep in mind that reprojection only works when you first have a defined CRS
168171
for the raster object that you want to reproject. It cannot be used if no
169172
CRS is defined. Lucky for us, the `DTM_hill_HARV` has a defined CRS.
@@ -172,46 +175,46 @@ CRS is defined. Lucky for us, the `DTM_hill_HARV` has a defined CRS.
172175

173176
## Data Tip
174177

175-
When we reproject a raster, we
176-
move it from one "grid" to another. Thus, we are modifying the data! Keep this
177-
in mind as we work with raster data.
178+
When we reproject a raster, we move it from one "grid" to another. Thus, we are
179+
modifying the data! Keep this in mind as we work with raster data.
178180

179181

180182
::::::::::::::::::::::::::::::::::::::::::::::::::
181183

182-
To use the `projectRaster()` function, we need to define two things:
184+
To use the `project()` function, we need to define two things:
183185

184186
1. the object we want to reproject and
185187
2. the CRS that we want to reproject it to.
186188

187-
The syntax is `projectRaster(RasterObject, crs = CRSToReprojectTo)`
189+
The syntax is `project(RasterObject, crs)`
188190

189191
We want the CRS of our hillshade to match the `DTM_HARV` raster. We can thus
190-
assign the CRS of our `DTM_HARV` to our hillshade within the `projectRaster()`
191-
function as follows: `crs = crs(DTM_HARV)`.
192-
Note that we are using the `projectRaster()` function on the raster object,
192+
assign the CRS of our `DTM_HARV` to our hillshade within the `project()`
193+
function as follows: `crs(DTM_HARV)`.
194+
Note that we are using the `project()` function on the raster object,
193195
not the `data.frame()` we use for plotting with `ggplot`.
194196

195-
First we will reproject our `DTM_hill_HARV` raster data to match the `DTM_HARV` raster CRS:
197+
First we will reproject our `DTM_hill_HARV` raster data to match the `DTM_HARV`
198+
raster CRS:
196199

197200
```{r reproject-raster}
198-
DTM_hill_UTMZ18N_HARV <- projectRaster(DTM_hill_HARV,
199-
crs = crs(DTM_HARV))
201+
DTM_hill_UTMZ18N_HARV <- project(DTM_hill_HARV,
202+
crs(DTM_HARV))
200203
```
201204

202-
Now we can compare the CRS of our original DTM hillshade
203-
and our new DTM hillshade, to see how they are different.
205+
Now we can compare the CRS of our original DTM hillshade and our new DTM
206+
hillshade, to see how they are different.
204207

205208
```{r}
206-
crs(DTM_hill_UTMZ18N_HARV)
207-
crs(DTM_hill_HARV)
209+
crs(DTM_hill_UTMZ18N_HARV, parse = TRUE)
210+
crs(DTM_hill_HARV, parse = TRUE)
208211
```
209212

210213
We can also compare the extent of the two objects.
211214

212215
```{r}
213-
extent(DTM_hill_UTMZ18N_HARV)
214-
extent(DTM_hill_HARV)
216+
ext(DTM_hill_UTMZ18N_HARV)
217+
ext(DTM_hill_HARV)
215218
```
216219

217220
Notice in the output above that the `crs()` of `DTM_hill_UTMZ18N_HARV` is now
@@ -228,8 +231,9 @@ Why do you think the two extents differ?
228231

229232
## Answers
230233

231-
The extent for DTM\_hill\_UTMZ18N\_HARV is in UTMs so the extent is in meters. The extent for DTM\_hill\_HARV is in lat/long so the extent is expressed
232-
in decimal degrees.
234+
The extent for DTM\_hill\_UTMZ18N\_HARV is in UTMs so the extent is in meters.
235+
The extent for DTM\_hill\_HARV is in lat/long so the extent is expressed in
236+
decimal degrees.
233237

234238

235239

@@ -239,31 +243,36 @@ in decimal degrees.
239243

240244
## Deal with Raster Resolution
241245

242-
Let's next have a look at the resolution of our reprojected hillshade versus our original data.
246+
Let's next have a look at the resolution of our reprojected hillshade versus
247+
our original data.
243248

244249
```{r view-resolution}
245250
res(DTM_hill_UTMZ18N_HARV)
246251
res(DTM_HARV)
247252
```
248253

249-
These two resolutions are different, but they're representing the same data. We can tell R to force our
250-
newly reprojected raster to be 1m x 1m resolution by adding a line of code
251-
`res=1` within the `projectRaster()` function. In the example below, we ensure a resolution match by using `res(DTM_HARV)` as a variable.
254+
These two resolutions are different, but they're representing the same data. We
255+
can tell R to force our newly reprojected raster to be 1m x 1m resolution by
256+
adding a line of code `res=1` within the `project()` function. In the
257+
example below, we ensure a resolution match by using `res(DTM_HARV)` as a
258+
variable.
252259

253260
```{r reproject-assign-resolution}
254-
DTM_hill_UTMZ18N_HARV <- projectRaster(DTM_hill_HARV,
255-
crs = crs(DTM_HARV),
256-
res = res(DTM_HARV))
261+
DTM_hill_UTMZ18N_HARV <- project(DTM_hill_HARV,
262+
crs(DTM_HARV),
263+
res = res(DTM_HARV))
257264
```
258265

259-
Now both our resolutions and our CRSs match, so we can plot these two data sets together. Let's double-check our resolution to be sure:
266+
Now both our resolutions and our CRSs match, so we can plot these two data sets
267+
together. Let's double-check our resolution to be sure:
260268

261269
```{r}
262270
res(DTM_hill_UTMZ18N_HARV)
263271
res(DTM_HARV)
264272
```
265273

266-
For plotting with `ggplot()`, we will need to create a dataframe from our newly reprojected raster.
274+
For plotting with `ggplot()`, we will need to create a dataframe from our newly
275+
reprojected raster.
267276

268277
```{r make-df-projected-raster}
269278
DTM_hill_HARV_2_df <- as.data.frame(DTM_hill_UTMZ18N_HARV, xy = TRUE)
@@ -302,15 +311,16 @@ Reproject the data as necessary to make things line up!
302311

303312
```{r challenge-code-reprojection, echo=TRUE}
304313
# import DSM
305-
DSM_SJER <- raster("data/NEON-DS-Airborne-Remote-Sensing/SJER/DSM/SJER_dsmCrop.tif")
314+
DSM_SJER <-
315+
rast("data/NEON-DS-Airborne-Remote-Sensing/SJER/DSM/SJER_dsmCrop.tif")
306316
# import DSM hillshade
307317
DSM_hill_SJER_WGS <-
308-
raster("data/NEON-DS-Airborne-Remote-Sensing/SJER/DSM/SJER_DSMhill_WGS84.tif")
318+
rast("data/NEON-DS-Airborne-Remote-Sensing/SJER/DSM/SJER_DSMhill_WGS84.tif")
309319
310320
# reproject raster
311-
DTM_hill_UTMZ18N_SJER <- projectRaster(DSM_hill_SJER_WGS,
312-
crs = crs(DSM_SJER),
313-
res = 1)
321+
DTM_hill_UTMZ18N_SJER <- project(DSM_hill_SJER_WGS,
322+
crs(DSM_SJER),
323+
res = 1)
314324
315325
# convert to data.frames
316326
DSM_SJER_df <- as.data.frame(DSM_SJER, xy = TRUE)
@@ -355,7 +365,7 @@ is this one was reprojected from WGS84 to UTM prior to plotting.
355365
:::::::::::::::::::::::::::::::::::::::: keypoints
356366

357367
- In order to plot two raster data sets together, they must be in the same CRS.
358-
- Use the `projectRaster()` function to convert between CRSs.
368+
- Use the `project()` function to convert between CRSs.
359369

360370
::::::::::::::::::::::::::::::::::::::::::::::::::
361371

0 commit comments

Comments
 (0)