library("readr")
library("sf")
library("tidyverse")
library("terra")
library("forcats")Exercise 6
Libraries
Tasks 1: Import and visualize spatial data
wildschwein_BE <- read_delim("Datasets/wildschwein_BE_2056.csv", ",") |>
st_as_sf(coords = c("E", "N"), crs = 2056, remove = FALSE)Importing Feldaufnahmen_Fanel Dataset
feldaufnahmen <- read_sf("Datasets/Feldaufnahmen_Fanel.gpkg")This dataset has 3 variables: the ID of the field; Crop type and the coordinates in polygon format.
Exploring variables:
str(feldaufnahmen)sf [975 × 3] (S3: sf/tbl_df/tbl/data.frame)
$ FieldID: num [1:975] 1 0 0 2 3 5 6 4 7 0 ...
$ Frucht : chr [1:975] "Roggen" NA NA "Wiese" ...
$ geom :sfc_POLYGON of length 975; first list element: List of 1
..$ : num [1:12, 1:2] 2570914 2570917 2570985 2571294 2571719 ...
..- attr(*, "class")= chr [1:3] "XY" "POLYGON" "sfg"
- attr(*, "sf_column")= chr "geom"
- attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA
..- attr(*, "names")= chr [1:2] "FieldID" "Frucht"
FieldID: numerical
Frucht: character
geom: sfc_POLYGON
Task 2: Annotate Trajectories from vector data
We would like to know what crop was visited by which wild boar, and at what time. Since the crop data is most relevant in summer, we filter the wild boar data to the months may to june first and save the output to a new variable. Then, filtered dataset is overlayed with fanel data, in order to verify the spatial overlap.
wildboars_summer <- wildschwein_BE |>
mutate(DateMonth = round_date(DatetimeUTC, unit = "month")) |>
filter(
DateMonth >= "2015-04-01" & DateMonth < "2015-04-15"
)
joined_wildboars_feld <- st_join(wildboars_summer, feldaufnahmen)Task 3: Explore annotated trajectories
Visually exploring the spatio-temporal patterns of wild boar in relation to the crops.
wildboars_summary <- joined_wildboars_feld |>
st_drop_geometry() |>
mutate(
hour = hour(round_date(DatetimeUTC, "hour")),
Frucht = fct_lump(Frucht, 4)
) |>
group_by(TierName, hour, Frucht) |>
summarise(n = n()) |>
mutate(
perc = n/sum(n)
)
wildboars_summary |>
ggplot(aes(hour, perc, fill = Frucht)) +
geom_col() +
facet_wrap(~TierName)Task 4: Import and visualize vegetationindex (raster data)
veg <- rast("Datasets/vegetationshoehe_LFI.tif")
plot(veg)Task 5: Annotate Trajectories from raster data
wildboars_vegetation <- extract(veg, vect(wildschwein_BE))
# Checking number of rows
nrow(wildboars_vegetation)[1] 51246
nrow(wildschwein_BE)[1] 51246
wildschwein_BE$veg <- wildboars_vegetation$vegetationshoehe_LFI
wildschwein_BESimple feature collection with 51246 features and 7 fields
Geometry type: POINT
Dimension: XY
Bounding box: xmin: 2568153 ymin: 1202306 xmax: 2575154 ymax: 1207609
Projected CRS: CH1903+ / LV95
# A tibble: 51,246 × 8
TierID TierName CollarID DatetimeUTC E N
* <chr> <chr> <dbl> <dttm> <dbl> <dbl>
1 002A Sabi 12275 2014-08-22 21:00:12 2570409. 1204752.
2 002A Sabi 12275 2014-08-22 21:15:16 2570402. 1204863.
3 002A Sabi 12275 2014-08-22 21:30:43 2570394. 1204826.
4 002A Sabi 12275 2014-08-22 21:46:07 2570379. 1204817.
5 002A Sabi 12275 2014-08-22 22:00:22 2570390. 1204818.
6 002A Sabi 12275 2014-08-22 22:15:10 2570390. 1204825.
7 002A Sabi 12275 2014-08-22 22:30:13 2570387. 1204831.
8 002A Sabi 12275 2014-08-22 22:45:11 2570381. 1204840.
9 002A Sabi 12275 2014-08-22 23:00:27 2570316. 1204935.
10 002A Sabi 12275 2014-08-22 23:15:41 2570393. 1204815.
# ℹ 51,236 more rows
# ℹ 2 more variables: geometry <POINT [m]>, veg <dbl>