Geo 880 Exercise 5

Author

Jan Krummenacher

Functions

Task 1

bmi <- function(weight, height){
 result <- weight / height
 print(result)
  }

F <- function(Celcius){
  result <- (Celcius*9/5)+32
  print(result)
}

E_distance <- function(x1,x2,y1,y2){
  result <- ((x2-x1)^2+(y2-y1)^2)^0.5
  print(result)
}
  
  # test
bmi (55, 160)
[1] 0.34375
F (0)
[1] 32
E_distance(13,0,25,40)
[1] 19.84943

Task 2

library(readr)
library(dplyr)
wildschwein <- read_delim("Data/wildschwein_BE_2056.csv", ",")

supsset <- filter(wildschwein, TierName == "Sabi" | TierName == "Rosa")  |> 
  filter(DatetimeUTC >= as.POSIXct("2015-04-01", tz = "UTC") & DatetimeUTC <= as.POSIXct("2015-04-16", tz = "UTC"))

Task 3

# Rond date to quarters
library(lubridate)
supsset$DatetimeUTC <- as.POSIXct(supsset$DatetimeUTC, format = "%Y-%m-%d %H:%M:%S", tz = "UTC") 

supsset <- supsset |> 
  mutate(Round = round_date(DatetimeUTC, unit = "15 mins"))

Task 4

We use left join because Sabi has 20 more observations, so we want only timestamps where we have data for both animals!

# Split dataframe for each animal
Sabi_df <- filter(supsset, TierName == "Sabi") 
Rosa_df <- filter(supsset, TierName == "Rosa")

# Combine them again (Join) 
joined_df <- left_join(Rosa_df, Sabi_df , by = "Round", suffix = c(".rosa", ".sabi")) 
analysis <- select(joined_df, Round, E.sabi, N.sabi, E.rosa, N.rosa)

analysis <- mutate(analysis, distance = E_distance(E.sabi, E.rosa, N.sabi, N.rosa))

# close enough?
analysis <- mutate(analysis, close = distance < 100)

Task 5

library(sf)
# Prepare Mapping

map_sabi <- st_as_sf(Sabi_df, coords = c("E", "N"), crs = 2056)
map_rosa <- st_as_sf(Rosa_df,coords = c("E", "N"), crs = 2056)

# Filter meets
map_meets <- filter(analysis, close == TRUE)

coords <- cbind(map_meets$E.sabi, map_meets$N.sabi, map_meets$E.rosa, map_meets$N.rosa)

multipoint_geom <- st_sfc(st_multipoint(coords), crs = 2056)

# multipoint_geom <- st_sfc(map_meets, st_multipoint(coords = c("E.sabi","N.sabi","E.rosa","N.rosa")), crs = 2056)
#Umwandeln der Liste von Multipoints in ein sf-Objekt
multipoint_sf <- st_sfc(multipoint_geom, crs = 2056)

# Entfernen der Z/M-Koordinaten (Z- und M-Komponenten entfernen)
multipoint_sf <- st_zm(multipoint_sf)

# Erstellen eines DataFrames mit Multipoint-Geometrien
df <- data.frame(
  id = 1:length(multipoint_sf),  # ID für jedes Multipoint
  geometry = multipoint_sf
)

# Umwandeln in ein sf-Objekt
sf_df <- st_as_sf(df)
library(ggplot2)

# Mapping

ggplot() +
  geom_sf(data = map_sabi, aes(color = "Sabi"), size = 1) +
  theme_minimal() +
  geom_sf(data = map_rosa, aes(color = "Rosa"), size = 1) +
  theme_minimal() +
  geom_sf(data = sf_df , aes(color = "Meet"), size = 1.5) +
  labs(title = "Sabi & Rosa meet?", color = "Legend")