-
Notifications
You must be signed in to change notification settings - Fork 0
/
Session3.R
421 lines (315 loc) · 11.1 KB
/
Session3.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# Load packages ----
library(tidyverse) # Meta package for data science
library(here) # Working with paths
library(raster) # Accessing and manipulating raster data
library(rgdal) # Interface to the GDAL utility
# 1. Intro to raster data ----
## View Raster File Attributes
GDALinfo(here("data","tud-dsm.tif"))
## Open a Raster
DSM_TUD <- raster(here("data","tud-dsm.tif"))
DSM_TUD
### More summary statistics
summary(DSM_TUD)
summary(DSM_TUD, maxsamp = ncell(DSM_TUD))
### Visualise the rater
DSM_TUD_df <- as.data.frame(DSM_TUD, xy = TRUE)
str(DSM_TUD_df)
ggplot() +
geom_raster(data = DSM_TUD_df , aes(x = x, y = y, fill = tud.dsm)) +
scale_fill_viridis_c() + # remember, the this color palette was introduced in the first lesson
coord_quickmap()
#### Or quick preview
plot(DSM_TUD)
## View raster CRS
crs(DSM_TUD)
## Calculate Min and Max values
minValue(DSM_TUD)
maxValue(DSM_TUD)
DSM_TUD <- raster::setMinMax(DSM_TUD)
minValue(DSM_TUD)
maxValue(DSM_TUD)
## Raster bands
nlayers(DSM_TUD)
## Creating a histogram of raster values
ggplot() +
geom_histogram(data = DSM_TUD_df, aes(tud.dsm))
ggplot() +
geom_histogram(data = DSM_TUD_df, aes(tud.dsm), bins = 40)
### Challenge 1 (2 minutes)
# Use `GDALinfo()` to determine the following about the `tud-dsm-hill.tif` file:
#
# 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?
GDALinfo(here("data","tud-dsm-hill.tif"))
# 2. Plot raster data ----
DSM_TUD_df <- DSM_TUD_df %>%
mutate(fct_elevation = cut(tud.dsm, breaks = 3))
ggplot() +
geom_bar(data = DSM_TUD_df, aes(fct_elevation))
unique(DSM_TUD_df$fct_elevation)
DSM_TUD_df %>%
group_by(fct_elevation) %>%
count()
## Customize cutoff values
custom_bins <- c(-10, 0, 5, 100)
head(DSM_TUD_df)
DSM_TUD_df <- DSM_TUD_df %>%
mutate(fct_elevation_cb = cut(tud.dsm, breaks = custom_bins))
head(DSM_TUD_df)
unique(DSM_TUD_df$fct_elevation_cb)
ggplot() +
geom_bar(data = DSM_TUD_df, aes(fct_elevation_cb))
DSM_TUD_df %>%
group_by(fct_elevation_cb) %>%
count()
ggplot() +
geom_raster(data = DSM_TUD_df , aes(x = x, y = y, fill = fct_elevation_cb)) +
coord_quickmap()
## Customize colors
terrain.colors(3)
ggplot() +
geom_raster(data = DSM_TUD_df , aes(x = x, y = y,
fill = fct_elevation_cb)) +
scale_fill_manual(values = terrain.colors(3)) +
coord_quickmap()
### Or store colors in a variable
my_col <- terrain.colors(3)
ggplot() +
geom_raster(data = DSM_TUD_df , aes(x = x, y = y,
fill = fct_elevation_cb)) +
scale_fill_manual(values = my_col, name = "Elevation") +
coord_quickmap()
### Challenge 2 (5 minutes)
# Create a plot of the TU Delft Digital Surface Model (`DSM_TUD`) that has:
#
# 1. Six classified ranges of values (break points) that are evenly divided among the range of pixel values.
# 2. Axis labels.
# 3. A plot title.
DSM_TUD_df <- DSM_TUD_df %>%
mutate(fct_elevation_6 = cut(tud.dsm, breaks = 6))
unique(DSM_TUD_df$fct_elevation_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_quickmap() +
xlab("X") +
ylab("Y") +
labs(title = "Elevation Classes of the Digital Surface Model (DSM)")
## Layering rasters
DSM_hill_TUD <- raster(here("data","tud-dsm-hill.tif"))
DSM_hill_TUD
DSM_hill_TUD_df <- as.data.frame(DSM_hill_TUD, xy = TRUE)
str(DSM_hill_TUD_df)
ggplot() +
geom_raster(data = DSM_hill_TUD_df,
aes(x = x, y = y, alpha = tud.dsm.hill)) +
scale_alpha(range = c(0.15, 0.65), guide = "none") +
coord_quickmap()
ggplot() +
geom_raster(data = DSM_TUD_df ,
aes(x = x, y = y,
fill = tud.dsm)) +
geom_raster(data = DSM_hill_TUD_df,
aes(x = x, y = y,
alpha = tud.dsm.hill)) +
scale_fill_viridis_c() +
scale_alpha(range = c(0.15, 0.65), guide = "none") +
ggtitle("Elevation with hillshade") +
coord_quickmap()
### Challenge 3 (8 minutes)
# Use the `tud-dtm.tif` and `tud-dtm-hill.tif` files from the `data` directory to create a Digital Terrain Model map of the TU Delft area.
#
# Make sure to:
#
# - include hillshade in the maps,
# - label axes,
# - include a title for each map,
# - experiment with various alpha values and color palettes to represent the data.
# import DTM
DTM_TUD <- raster(here("data","tud-dtm.tif"))
DTM_TUD_df <- as.data.frame(DTM_TUD, xy = TRUE)
# DTM Hillshade
DTM_hill_TUD <- raster(here("data","tud-dtm-hill.tif"))
DTM_hill_TUD_df <- as.data.frame(DTM_hill_TUD, xy = TRUE)
ggplot() +
geom_raster(data = DTM_TUD_df ,
aes(x = x, y = y,
fill = tud.dtm,
alpha = 2.0)
) +
geom_raster(data = DTM_hill_TUD_df,
aes(x = x, y = y,
alpha = tud.dtm.hill)
) +
scale_fill_viridis_c() +
guides(fill = guide_colorbar()) +
scale_alpha(range = c(0.4, 0.7), guide = "none") +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank()) +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank()) +
ggtitle("DTM with Hillshade") +
coord_quickmap()
# 3. Reproject raster data ----
DTM_TUD <- raster(here("data","tud-dtm.tif"))
DTM_hill_TUD <- raster(here("data","tud-dtm-hill-ETRS89.tif"))
DTM_TUD_df <- as.data.frame(DTM_TUD, xy = TRUE)
DTM_hill_TUD_df <- as.data.frame(DTM_hill_TUD, xy = TRUE)
# try to plot them together
ggplot() +
geom_raster(data = DTM_TUD_df ,
aes(x = x, y = y,
fill = tud.dtm)) +
geom_raster(data = DTM_hill_TUD_df,
aes(x = x, y = y,
alpha = tud.dtm.hill.ETRS89)) +
scale_fill_gradientn(name = "Elevation", colors = terrain.colors(10)) +
coord_quickmap()
# examine the rasters separately
ggplot() +
geom_raster(data = DTM_TUD_df,
aes(x = x, y = y,
fill = tud.dtm)) +
scale_fill_gradientn(name = "Elevation", colors = terrain.colors(10)) +
coord_quickmap()
ggplot() +
geom_raster(data = DTM_hill_TUD_df,
aes(x = x, y = y,
alpha = tud.dtm.hill.ETRS89)) +
coord_quickmap()
### Challenge 4 (2 minutes)
# View the CRS for each of these two datasets. What projection does each use?
crs(DTM_TUD)
crs(DTM_hill_TUD)
## Reproject rasters
DTM_hill_EPSG28992_TUD <- projectRaster(DTM_hill_TUD,
crs = crs(DTM_TUD))
crs(DTM_hill_EPSG28992_TUD)
crs(DTM_hill_TUD)
extent(DTM_hill_EPSG28992_TUD)
extent(DTM_hill_TUD)
### Question
# Why do you think the two extents differ?
## Dealing with raster resolution
res(DTM_hill_EPSG28992_TUD)
res(DTM_TUD)
DTM_hill_EPSG28992_TUD <- projectRaster(DTM_hill_TUD,
crs = crs(DTM_TUD),
res = res(DTM_TUD))
res(DTM_hill_EPSG28992_TUD)
res(DTM_TUD)
### Plot the two layers
DTM_hill_TUD_2_df <- as.data.frame(DTM_hill_EPSG28992_TUD, xy = TRUE)
ggplot() +
geom_raster(data = DTM_TUD_df ,
aes(x = x, y = y,
fill = tud.dtm)) +
geom_raster(data = DTM_hill_TUD_2_df,
aes(x = x, y = y,
alpha = tud.dtm.hill.ETRS89)) +
scale_fill_gradientn(name = "Elevation", colors = terrain.colors(10)) +
coord_quickmap()
# 4. Raster calculations ----
## See slides with explanation of DSM and DTM
GDALinfo(here("data","tud-dtm.tif"))
GDALinfo(here("data","tud-dsm.tif"))
ggplot() +
geom_raster(data = DTM_TUD_df ,
aes(x = x, y = y, fill = tud.dtm)) +
scale_fill_gradientn(name = "Elevation", colors = terrain.colors(10)) +
coord_quickmap()
ggplot() +
geom_raster(data = DSM_TUD_df ,
aes(x = x, y = y, fill = tud.dsm)) +
scale_fill_gradientn(name = "Elevation", colors = terrain.colors(10)) +
coord_quickmap()
## Raster math and Canopy Height Models
CHM_TUD <- DSM_TUD - DTM_TUD
CHM_TUD_df <- as.data.frame(CHM_TUD, xy = TRUE)
ggplot() +
geom_raster(data = CHM_TUD_df ,
aes(x = x, y = y, fill = layer)) +
scale_fill_gradientn(name = "Canopy Height", colors = terrain.colors(10)) +
coord_quickmap()
ggplot(CHM_TUD_df) +
geom_histogram(aes(layer))
### Challenge 5 (5 minutes)
# It’s often a good idea to explore the range of values in a raster dataset just like we might
# explore a dataset that we collected in the field.
#
# 1. What is the min and maximum value for the Canopy Height Model `CHM_TUD` that we just created?
# 2. What are two ways you can check this range of data for `CHM_TUD`?
# 3. What is the distribution of all the pixel values in the CHM?
# 4. Plot a histogram with 6 bins instead of the default and change the color of the histogram.
# 5. Plot the CHM_HARV raster using breaks that make sense for the data. Include an appropriate
# color palette for the data, plot title and no axes ticks / labels.
min(CHM_TUD_df$layer, na.rm = TRUE)
max(CHM_TUD_df$layer, na.rm = TRUE)
ggplot(CHM_TUD_df) +
geom_histogram(aes(layer))
ggplot(CHM_TUD_df) +
geom_histogram(aes(layer), colour="black",
fill="darkgreen", bins = 6)
custom_bins <- c(0, 10, 20, 30, 100)
CHM_TUD_df <- CHM_TUD_df %>%
mutate(canopy_discrete = cut(layer, breaks = custom_bins))
ggplot() +
geom_raster(data = CHM_TUD_df , aes(x = x, y = y,
fill = canopy_discrete)) +
scale_fill_manual(values = terrain.colors(4)) +
coord_quickmap()
## Export a GeoTIFF
writeRaster(CHM_TUD, here("fig_output","CHM_TUD.tiff"),
format="GTiff",
overwrite=TRUE,
NAflag=-9999)
# 5. Work with multi-band rasters ----
RGB_band1_TUD <- raster(here("data","tudlib-rgb.tif"))
RGB_band1_TUD_df <- as.data.frame(RGB_band1_TUD, xy = TRUE)
ggplot() +
geom_raster(data = RGB_band1_TUD_df,
aes(x = x, y = y, alpha = tudlib.rgb)) +
coord_quickmap() # use `coord_equal()` instead
RGB_band1_TUD
nbands(RGB_band1_TUD)
RGB_band2_TUD <- raster(here("data","tudlib-rgb.tif"), band = 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)) +
coord_equal()
## Raster stacks
RGB_stack_TUD <- stack(here("data","tudlib-rgb.tif"))
RGB_stack_TUD
RGB_stack_TUD@layers
RGB_stack_TUD[[2]]
RGB_stack_TUD_df <- as.data.frame(RGB_stack_TUD, xy = TRUE)
str(RGB_stack_TUD_df)
ggplot() +
geom_histogram(data = RGB_stack_TUD_df, aes(tudlib.rgb.1))
ggplot() +
geom_raster(data = RGB_stack_TUD_df,
aes(x = x, y = y, alpha = tudlib.rgb.2)) +
coord_equal()
## Create a three-band image
plotRGB(RGB_stack_TUD,
r = 1, g = 2, b = 3)
plotRGB(RGB_stack_TUD,
r = 1, g = 2, b = 3,
scale = 800,
stretch = "lin")
plotRGB(RGB_stack_TUD,
r = 1, g = 2, b = 3,
scale = 800,
stretch = "hist")
## RasterStack vs. RasterBrick
object.size(RGB_stack_TUD)
RGB_brick_TUD <- brick(RGB_stack_TUD)
object.size(RGB_brick_TUD)
plotRGB(RGB_brick_TUD)