This repository has been archived by the owner on Jan 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
05_Raster.Rmd
628 lines (413 loc) · 12.3 KB
/
05_Raster.Rmd
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
---
title: "Introduction to Raster Package"
---
```{r, echo=FALSE, message=FALSE, results='hide', purl=FALSE}
## This chunk automatically generates a text .R version of this script when running within knitr. You do not need to run this...
input = knitr::current_input() # filename of input document
output = paste(tools::file_path_sans_ext(input), 'R', sep = '.')
knitr::purl(input,output,documentation=2,quiet=T)
source("knitr_header.R")
knitr::opts_chunk$set(eval=F)
```
This file is available as a [<i class="fa fa-file-text" aria-hidden="true"></i> R script here](`r output`). Download this file and open it (or copy-paste into a new script) with RStudio so you can follow along.
## Libraries
```{r message=F,warning=FALSE}
library(dplyr)
library(tidyr)
library(sp)
library(ggplot2)
# New libraries
library(raster)
library(rasterVis) #visualization library for raster
```
# Raster Package
## `getData()`
Raster package includes access to some useful (vector and raster) datasets with `getData()`:
* Elevation (SRTM 90m resolution raster)
* World Climate (Tmin, Tmax, Precip, BioClim rasters)
* Countries from CIA factsheet (vector!)
* Global Administrative boundaries (vector!)
`getData()` steps for GADM:
1. _Select Dataset_: ‘GADM’ returns the global administrative boundaries.
2. _Select country_: Country name of the boundaries using its ISO A3 country code
3. _Specify level_: Level of of administrative subdivision (0=country, 1=first level subdivision).
## GADM: Global Administrative Areas
Administrative areas in this database are countries and lower level subdivisions.
<img src="assets/gadm25.png" alt="alt text" width="70%">
Divided by country (see website for full dataset). Explore country list:
```{r}
getData("ISO3")%>%
as.data.frame%>%
filter(NAME=="South Africa")
```
Download data for South Africa
```{r}
za=getData('GADM', country='ZAF', level=1)
```
```{r}
plot(za)
```
Danger: `plot()` works, but can be slow for complex polygons.
### Check out attribute table
```{r}
za@data
```
```{r}
za=subset(za,NAME_1!="Prince Edward Islands")
plot(za)
```
## Your turn
Use the method above to download and plot the boundaries for a country of your choice.
```{r}
getData("ISO3")%>%
as.data.frame%>%
filter(NAME=="Tunisia")
tun=getData('GADM', country='TUN', level=1)
plot(tun)
```
# Raster Data
## Raster introduction
Spatial data structure dividing region ('grid') into rectangles (’cells’ or ’pixels’) storing one or more values each.
<small> Some examples from the [Raster vignette](http://cran.r-project.org/web/packages/raster/vignettes/Raster.pdf) by Robert J. Hijmans. </small>
* `rasterLayer`: 1 band
* `rasterStack`: Multiple Bands
* `rasterBrick`: Multiple Bands of _same_ thing.
```{r}
x <- raster()
x
```
```{r}
str(x)
```
```{r}
x <- raster(ncol=36, nrow=18, xmn=-1000, xmx=1000, ymn=-100, ymx=900)
res(x)
res(x) <- 100
res(x)
ncol(x)
```
```{r}
# change the numer of columns (affects resolution)
ncol(x) <- 18
ncol(x)
res(x)
```
## Raster data storage
```{r}
r <- raster(ncol=10, nrow=10)
ncell(r)
```
But it is an empty raster
```{r}
hasValues(r)
```
Use `values()` function:
```{r}
values(r) <- 1:ncell(r)
hasValues(r)
values(r)[1:10]
```
## Your Turn
Create and then plot a new raster with:
1. 100 rows
2. 50 columns
3. Fill it with random values (`rnorm()`)
```{r}
x=raster(nrow=100,ncol=50,vals=rnorm(100*50))
# OR
x= raster(nrow=100,ncol=50)
values(x)= rnorm(5000)
plot(x)
```
Raster memory usage
```{r}
inMemory(r)
```
> You can change the memory options using the `maxmemory` option in `rasterOptions()`
## Raster Plotting
Plotting is easy (but slow) with `plot`.
```{r}
plot(r, main='Raster with 100 cells')
```
### ggplot and rasterVis
rasterVis package has `gplot()` for plotting raster data in the `ggplot()` framework.
```{r}
gplot(r,maxpixels=50000)+
geom_raster(aes(fill=value))
```
Adjust `maxpixels` for faster plotting of large datasets.
```{r}
gplot(r,maxpixels=10)+
geom_raster(aes(fill=value))
```
Can use all the `ggplot` color ramps, etc.
```{r}
gplot(r)+geom_raster(aes(fill=value))+
scale_fill_distiller(palette="OrRd")
```
## Spatial Projections
Raster package uses standard [coordinate reference system (CRS)](http://www.spatialreference.org).
For example, see the projection format for the [_standard_ WGS84](http://www.spatialreference.org/ref/epsg/4326/).
```{r}
projection(r)
```
## Warping rasters
Use `projectRaster()` to _warp_ to a different projection.
`method=` `ngb` (for categorical) or `bilinear` (continuous)
```{r}
r2=projectRaster(r,crs="+proj=sinu +lon_0=0",method = )
par(mfrow=c(1,2));plot(r);plot(r2)
```
# WorldClim
## Overview of WorldClim
Mean monthly climate and derived variables interpolated from weather stations on a 30 arc-second (~1km) grid.
See [worldclim.org](http://www.worldclim.org/methods)
## Bioclim variables
<small>
Variable Description
- -
BIO1 Annual Mean Temperature
BIO2 Mean Diurnal Range (Mean of monthly (max temp – min temp))
BIO3 Isothermality (BIO2/BIO7) (* 100)
BIO4 Temperature Seasonality (standard deviation *100)
BIO5 Max Temperature of Warmest Month
BIO6 Min Temperature of Coldest Month
BIO7 Temperature Annual Range (BIO5-BIO6)
BIO8 Mean Temperature of Wettest Quarter
BIO9 Mean Temperature of Driest Quarter
BIO10 Mean Temperature of Warmest Quarter
BIO11 Mean Temperature of Coldest Quarter
BIO12 Annual Precipitation
BIO13 Precipitation of Wettest Month
BIO14 Precipitation of Driest Month
BIO15 Precipitation Seasonality (Coefficient of Variation)
BIO16 Precipitation of Wettest Quarter
BIO17 Precipitation of Driest Quarter
BIO18 Precipitation of Warmest Quarter
BIO19 Precipitation of Coldest Quarter
</small>
## Download climate data
Download the data:
```{r, eval=F}
clim=getData('worldclim', var='bio', res=10)
```
`res` is resolution (0.5, 2.5, 5, and 10 minutes of a degree)
### Gain and Offset
```{r}
clim
```
Note the min/max of the raster. What are the units? Always check metadata, the [WorldClim temperature dataset](http://www.worldclim.org/formats) has a `gain` of 0.1, meaning that it must be multipled by 0.1 to convert back to degrees Celsius. Precipitation is in mm, so a gain of 0.1 would turn that into cm.
```{r}
gain(clim)=0.1
```
### Plot with `plot()`
```{r}
plot(clim)
```
## Faceting in ggplot
Or use `rasterVis` methods with gplot
```{r}
gplot(clim[[13:19]])+geom_raster(aes(fill=value))+
facet_wrap(~variable)+
scale_fill_gradientn(colours=c("brown","red","yellow","darkgreen","green"),trans="log10")+
coord_equal()
```
Let's dig a little deeper into the data object:
```{r}
## is it held in RAM?
inMemory(clim)
## How big is it?
object.size(clim)
## can we work with it directly in RAM?
canProcessInMemory(clim)
```
## Subsetting and spatial cropping
Use `[[1:3]]` to select raster layers from raster stack.
```{r}
r1 <- crop(clim[[1]], bbox(za))
## crop to a latitude/longitude box
r1 <- crop(clim[[1]], extent(10,35,-35,-20))
```
```{r}
r1
plot(r1)
```
## Spatial aggregation
```{r}
## aggregate using a function
aggregate(r1, 3, fun=mean) %>%
plot()
```
## Your turn
Create a new raster by aggregating to the minimum (`min`) value of `r1` within a 10 pixel window
```{r}
aggregate(r1, 10, fun=min) %>%
plot()
```
## Focal ("moving window")
```{r}
## apply a function over a moving window
focal(r1, w=matrix(1,3,3), fun=mean) %>%
plot()
```
```{r}
## apply a function over a moving window
rf_min <- focal(r1, w=matrix(1,11,11), fun=min)
rf_max <- focal(r1, w=matrix(1,11,11), fun=max)
rf_range=rf_max-rf_min
## or just use the range function
rf_range2 <- focal(r1, w=matrix(1,11,11), fun=range)
plot(rf_range2)
```
## Your turn
Plot the focal standard deviation of `r1` over a 3x3 window.
```{r}
focal(r1,w=matrix(1,3,3),fun=sd)%>%
plot()
```
## Raster calculations
the `raster` package has many options for _raster algebra_, including `+`, `-`, `*`, `/`, logical operators such as `>`, `>=`, `<`, `==`, `!` and functions such as `abs`, `round`, `ceiling`, `floor`, `trunc`, `sqrt`, `log`, `log10`, `exp`, `cos`, `sin`, `max`, `min`, `range`, `prod`, `sum`, `any`, `all`.
So, for example, you can
```{r}
cellStats(r1,range)
## add 10
s = r1 + 10
cellStats(s,range)
```
```{r}
## take the square root
s = sqrt(r1)
cellStats(s,range)
# round values
r = round(r1)
cellStats(r,range)
# find cells with values less than 15 degrees C
r = r1 < 15
plot(r)
```
### Apply algebraic functions
```{r}
# multiply s times r and add 5
s = s * r1 + 5
cellStats(s,range)
```
## Extracting Raster Data
* points
* lines
* polygons
* extent (rectangle)
* cell numbers
Extract all intersecting values OR apply a summarizing function with `fun`.
### Point data
```{r}
## define which species to query
library(spocc)
sp='Protea repens'
## run the query and convert to data.frame()
d = occ(query=sp, from='gbif',limit = 1000, has_coords=T) %>% occ2df()
coordinates(d)=c("longitude","latitude")
projection(d)="+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
dc=raster::extract(clim[[1:4]],d,df=T)
head(dc)
```
> Use `package::function` to avoid confusion with similar functions.
```{r}
gplot(r1)+geom_raster(aes(fill=value))+
geom_point(data=as.data.frame(d),
aes(x=longitude,y=latitude),col="red")+
coord_equal()
```
```{r,warning=F}
d2=dc%>%
gather(ID)
colnames(d2)[1]="cell"
ggplot(d2,aes(x=value))+
geom_density()+
facet_wrap(~ID,scales="free")
```
### Lines
Extract values along a transect.
```{r}
transect = SpatialLinesDataFrame(
SpatialLines(list(Lines(list(Line(
cbind(c(19, 26),c(-33.5, -33.5)))), ID = "ZAF"))),
data.frame(Z = c("transect"), row.names = c("ZAF")))
gplot(r1)+geom_tile(aes(fill=value))+
geom_line(aes(x=long,y=lat),data=fortify(transect))
```
### Plot Transect
```{r}
trans=raster::extract(clim,transect,along=T,cellnumbers=T)%>%
as.data.frame()
trans[,c("lon","lat")]=coordinates(clim)[trans$cell]
trans$order=as.integer(rownames(trans))
transl=group_by(trans,lon,lat)%>%
gather(variable, value, -lon, -lat, -cell, -order)
ggplot(transl,aes(x=lon,y=value,
colour=variable,group=variable,
order=order))+
geom_line()
```
### _Zonal_ statistics
```{r}
rsp=raster::extract(r1,za,mean,sp=T)
spplot(rsp,zcol="bio1")
```
## Your turn
1. Download the Maximum Temperature dataset using `getData()`
2. Crop it to the country you downloaded (or ZA?)
2. Calculate the overall range for each variable with `cellStats()`
3. Calculate the focal median with an 11x11 window with `focal()`
4. Create a transect across the region and extract the temperature data.
## Example
1. Download the Maximum Temperature dataset using `getData()`
2. Crop it to the country you downloaded (or ZA?)
2. Calculate the overall range for each variable with `cellStats()`
```{r}
tun=getData('GADM', country='TUN', level=1)
tmax=getData('worldclim', var='tmax', res=10)
gain(tmax)=0.1
tmax_tun=crop(tmax,tun)
cellStats(tmax_tun,"range")
```
3. Calculate the focal median with an 11x11 window with `focal()`
4. Create a transect across the region and extract the temperature data.
```{r}
tmax_tunf=list()
for(i in 1:nlayers(tmax_tun))
tmax_tunf[[i]]=focal(tmax_tun[[i]],w=matrix(1,11,11),fun=median)
tmax_tunf=stack(tmax_tunf)
# Transect
transect = SpatialLinesDataFrame(
SpatialLines(list(Lines(list(Line(
cbind(c(8, 10),c(36, 36)))), ID = "TUN"))),
data.frame(Z = c("transect"), row.names = c("TUN")))
```
```{r}
gplot(tmax_tun)+geom_tile(aes(fill=value))+
facet_wrap(~variable)+
geom_path(data=fortify(tun),
mapping=aes(x=long,y=lat,
group=group,order=order))+
geom_line(aes(x=long,y=lat),
data=fortify(transect),col="red")+
coord_equal()
```
```{r}
trans=raster::extract(tmax_tun,transect,along=T,cellnumbers=T)%>%
as.data.frame()
trans[,c("lon","lat")]=coordinates(clim)[trans$cell]
trans$order=as.integer(rownames(trans))
transl=group_by(trans,lon,lat)%>%
gather(variable, value, -lon, -lat, -cell, -order)
ggplot(transl,aes(x=lon,y=value,
colour=variable,group=variable,
order=order))+
geom_line()
```
## Raster Processing
Things to consider:
* RAM limitations
* Disk space and temporary files
* Use of external programs (e.g. GDAL)
* Use of external GIS viewer (e.g. QGIS)