-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathR_code_EBVs.r
More file actions
155 lines (87 loc) · 3 KB
/
Copy pathR_code_EBVs.r
File metadata and controls
155 lines (87 loc) · 3 KB
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
########
#1st Day
# Download the data from the iol website (image is aggregated, 10 times)
# Set working directory
# setwd("~/lab/")
# setwd("/Users/utente/lab") #mac
setwd("C:/lab/") # windows
# require libraries
library(raster)
library(RStoolbox)
# import the downloaded satellite image by the brick function
snt <- brick("snt_r10.tif")
snt # to see the characteristics of the image
# plot the image
plot(snt)
# Bands
#B1: blue
#B2: green
#B3: red
#B4: NIR - Near Infrared
# plot rgb R3, G2, B1
plotRGB(snt,3,2,1, stretch="lin")
# put the NIR band on top of the red
plotRGB(snt,4,3,2, stretch="lin")
# relation between the bands
pairs(snt)
### PCA analysis
sntpca <- rasterPCA(snt)
sntpca
summary(sntpca$model) # 1st band has the highest amount of information, 70%
plot(sntpca$map)
plotRGB(sntpca$map, 1,2,3, stretch="lin")
# set the moving window
window <- matrix(1, nrow = 5, ncol = 5)
window
sd_snt <- focal(sntpca$map$PC1, w=window, fun=sd) #w is the window that we are using, sd is the standard deviation
cl <- colorRampPalette(c("dark blue", "green", "orange", "red"))(100) # create colorramppalette
plot(sd_snt, col=cl)
dev.off()
par(mfrow=c(1,2))
plotRGB(snt,4,3,2, stretch="lin", main="the original image")
plot(sd_snt, col=cl, main="diversity")
# more diversity is observed on the borders of the different ecosystems
########
#2nd Day
#Download the cladonia_stellaris_calaita.JPG from the IOL website to the lab folder
# Set working directory
# setwd("~/lab/")
# setwd("/Users/utente/lab") #mac
setwd("C:/lab/") # windows
# require libraries
library(raster)
library(RStoolbox)
# brick the cladonia_stellaris_calaita.JPG and assign a name
clad <- brick("cladonia_stellaris_calaita.JPG")
#plot the image by using plotRGB function
plotRGB(clad, 1, 2, 3, stretch="lin")
# use pairs to the relation
pairs(clad)
# PCA Analysis for the cladonia
cladpca <- rasterPCA(clad)
# see the information of the pca analysis
cladpca
# see the summary of the pca analysis
summary(cladpca$model) #98% by 1st component
# plot the map of cladpca
plotRGB(cladpca$map, 1, 2, 3, stretch="lin")
#put the focal function on top of the image to measure biodiversity
#build the moving window (3x3 matrix and set value for the pixel, in this case it is 1)
window <- matrix(1, nrow = 3, ncol = 3)
# check the window
window
# standard deviation of the clad
sd_clad <- focal(cladpca$map$PC1, w=window, fun=sd)
# you can also aggregate the cladpca and then do the calculation of the standard deviation to accelerate the process
PC1_agg <- aggregate(cladpca$map$PC1, fact=10)
sd_clad_agg <- focal(PC1_agg, w=window, fun=sd)
# create a colorramppalette
cl <- colorRampPalette(c("yellow", "violet", "black"))(100)
# plot the calculations
par(mfrow=c(1,2))
plot(sd_clad, col=cl)
plot(sd_clad_agg, col=cl) # all of the microvariations in the structure of the cladonia can be seen in these images.
# For comparison
par(mfrow=c(1,2))
plotRGB(clad, 1,2,3, stretch="lin")
plot(sd_clad, col=cl) #or/ plot(sd_clad_agg, col=cl)