wee4_excercise A

# install.packages("pacman")
library("pacman")

p_install("dplyr", force = FALSE)
Package is already on your system.
p_install("ggplot2", force = FALSE)
Package is already on your system.
p_install("readr", force = FALSE)
Package is already on your system.
p_install("tidyr", force = FALSE)
Package is already on your system.
p_install("sf", force = FALSE)
Package is already on your system.
p_install("terra", force = FALSE)
Package is already on your system.
p_install("tmap", force = FALSE)
Package is already on your system.
p_install("zoo", force = FALSE)
Package is already on your system.
p_install("units", force = FALSE)
Package is already on your system.
p_install("plotly", force = FALSE)
Package is already on your system.
p_install("patchwork", force = FALSE)
Package is already on your system.
library("ggplot2")
library("dplyr")

Attache Paket: 'dplyr'
Die folgenden Objekte sind maskiert von 'package:stats':

    filter, lag
Die folgenden Objekte sind maskiert von 'package:base':

    intersect, setdiff, setequal, union
library("readr")
library("sf")
Linking to GEOS 3.13.0, GDAL 3.10.1, PROJ 9.5.1; sf_use_s2() is TRUE
library("readr")
library("sf")
library(lubridate)

Attache Paket: 'lubridate'
Die folgenden Objekte sind maskiert von 'package:base':

    date, intersect, setdiff, union

Task 1: Import data

library(sf)
library(jsonlite)
library(dplyr)
library(tidyr)
records_json <- jsonlite::read_json("Zeitachse_mw.json",simplifyVector = TRUE)

records <- records_json[[2]]
records <- records$position
colnames(records)
[1] "LatLng"               "accuracyMeters"       "altitudeMeters"      
[4] "source"               "timestamp"            "speedMetersPerSecond"
# inspired by the following SO-answer
# https://gis.stackexchange.com/a/319067/40929
records <- records |>
  na.omit() |> 
  separate_wider_delim(LatLng, delim = ", ", names = c("Lat", "Long"))

records$Long <- sub('°', '', records$Long)
records$Lat <- sub('°', '', records$Lat)


records <- records |> 
  mutate(Long = as.numeric(Long)) |> 
  mutate(Lat = as.numeric(Lat))

records_sf <- st_as_sf(records, coords = c("Lat", "Long"), crs = 4326, remove = FALSE)