-
Notifications
You must be signed in to change notification settings - Fork 0
/
eia_uav_params.Rmd
168 lines (138 loc) · 5.89 KB
/
eia_uav_params.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
---
title: "Analysis of Drone Data of Small Elephant Impact Sites"
author: "Sunniva McKeever, Isabella Metz and Maximilian Merzdorf"
date: "Internship Kruger WS23/24"
output: pdf_document
---
## Data Import
Load the required libraries.
```{r libraries, warning=FALSE, message=FALSE}
library(terra)
library(ggplot2)
library(lidR)
library(mapview)
library(sf)
library(knitr)
```
Set working directory and hyperparameters and import drone data.
```{r data import, warning=FALSE, message=FALSE}
setwd("C://Users/avinn/Documents/Master/Semester3/ElephantTransects/")
## (only once!!!) create empty data frame
params_df <- data.frame(matrix(ncol = 7, nrow = 0))
colnames(params_df) <- c("name",
"numtrees",
"treedens",
"treeheight_min",
"treeheight_max",
"treeheight_mean",
"canopyarea"
)
params_df_indices <- data.frame(matrix(ncol = 4, nrow = 0))
colnames(params_df_indices) <- c("ndvi_mean",
"evi_mean",
"gci_mean",
"lai_mean")
# hyperparameters
mw_size <- 15
crs_epsg <- "epsg:32736"
area <- 0.11 # area of each EIA in km2
EIA_name <- "EIA2 C3"
# load drone data
# EIA2C3
DSM <- terra::rast("./ElephantTransectSites/Pix4d/20230810_EIA2_C3/20230810_EIA2C3_dsm.tif")
DTM <- terra::rast("./ElephantTransectSites/Pix4d/20230810_EIA2_C3/20230810_EIA2C3_dtm.tif")
Ortho <- terra::rast(c(
"./ElephantTransectSites/Pix4d/20230810_EIA2_C3/20230810_EIA2C3_transparent_mosaic_group1.tif",
"./ElephantTransectSites/Pix4d/20230810_EIA2_C3/20230810_EIA2C3_transparent_mosaic_green.tif",
"./ElephantTransectSites/Pix4d/20230810_EIA2_C3/20230810_EIA2C3_transparent_mosaic_red.tif",
"./ElephantTransectSites/Pix4d/20230810_EIA2_C3/20230810_EIA2C3_transparent_mosaic_red edge.tif",
"./ElephantTransectSites/Pix4d/20230810_EIA2_C3/20230810_EIA2C3_transparent_mosaic_nir.tif"),
lyrs = c(1,2,3,5,7,9,11)
)
# rename bands
names(Ortho) <- c("red", "green", "blue", "MSgreen", "MSred", "MSrededge", "MSnir")
# load other data
aoi <- st_read("./data/other/polygons.shp")
```
## Data Preprocessing
The data needs to be reprojected (as lidR package requires a projection in meters) and cropped to the extent of the EIA. Then, an absolute tree height raster is calculated by subtracting the DTM from the DSM.
```{r data preprocessing, warning = FALSE, message=FALSE}
## DATA PREPROCESSING
# reproject
# lidR package requires projection in m
DSM <- terra::project(DSM, crs_epsg)
DTM <- terra::project(DTM, crs_epsg)
aoi <- sf::st_transform(aoi, crs = crs_epsg)
# crop
DSM <- terra::mask(DSM, aoi[aoi$FieldID == EIA_name,])
DTM <- terra::mask(DTM, aoi[aoi$FieldID == EIA_name,])
Ortho <- terra::mask(Ortho, aoi[aoi$FieldID == EIA_name,])
# calculate Canopy Height Model (CHM) from DSM and DTM
DSM <- resample(DSM, DTM)
CHM <- DSM - DTM
CHM <- aggregate(CHM, 10) # lower resolution to limit computational time
# plot
par(mfrow = c(1,3))
plot(DSM, main = "DSM")
plot(DTM, main = "DTM")
plot(CHM, main = "CHM")
```
## Analysis
Basic indices are calculated to compare the structure of different EIAs among each other. Using the lidR package, individual trees are then detected and segmented. Tree tops can be detected by applying a Local Maximum Filter (LMF) on the loaded data set.For a given point, the algorithm analyzes neighborhood points, checking if the processed point is the highest. The size of the moving window determines the size of the analysed neighborhood.
```{r analysis, warning=FALSE, include=TRUE, message=FALSE}
# calculate indices
ndvi <- (Ortho$MSnir - Ortho$red) / (Ortho$MSnir + Ortho$red)
evi <- 2.5 * ((Ortho$MSnir - Ortho$red) / (Ortho$MSnir + 6 * Ortho$red - 7.5 * Ortho$blue + 1))
gci <- (Ortho$MSnir / Ortho$MSgreen) - 1
lai <- 3.618 * evi - 0.118
# locate tree tops
# a tree in savannah is everything > 1.5m
ttops <- locate_trees(CHM, lmf(ws = mw_size, hmin = 1.5))
# segment trees
algo <- lidR::dalponte2016(CHM, ttops)
crowns <- algo()
# calculate parameters
numtrees <- round(nrow(ttops), digits = 2)
treedens <- round(numtrees/area, digits = 2) # number of trees per km2
treeheight_min <- round(min(ttops$Z), digits = 2)
treeheight_max <- round(max(ttops$Z), digits = 2)
treeheight_mean <- round(mean(ttops$Z), digits = 2)
canopyarea <- terra::expanse(crowns) # crown area in m2
ndvi_mean <- terra::global(ndvi, 'mean', na.rm = T)
evi_mean <- terra::global(evi, 'mean', na.rm = T)
gci_mean <- terra::global(gci, 'mean', na.rm = T)
lai_mean <- terra::global(lai, 'mean', na.rm = T)
canopyarea <- round(canopyarea, digits = 2)
ndvi_mean <- round(ndvi_mean, digits = 2)
evi_mean <- round(evi_mean, digits = 2)
gci_mean <- round(gci_mean, digits = 2)
lai_mean <- round(lai_mean, digits = 2)
```
## Results
```{r results, warning=FALSE, include=TRUE, message=FALSE}
# plot indices
par(mfrow = c(2,2))
plot(ndvi, main = "ndvi")
plot(evi, main = "evi")
plot(gci, main = "gci")
plot(lai, main = "lai")
# plot tree tops
par(mfrow = c(1,2))
plot(CHM, col = height.colors(50), main = "Tree Tops")
plot(sf::st_geometry(ttops), add = TRUE, pch = 3)
plot(crowns, col = pastel.colors(200), legend = FALSE, main = "Tree Segmentation")
# save all data in one df
params_df[nrow(params_df) + 1,] <- c(EIA_name,
numtrees,
treedens,
treeheight_min,
treeheight_max,
treeheight_mean,
canopyarea$area)
params_df_indices[nrow(params_df_indices) + 1,] <- c(ndvi_mean,
evi_mean,
gci_mean,
lai_mean)
kable(params_df)
kable(params_df_indices)
```