Cleaning GPS data

Assembling provided GPS data into cleaned files, and a single large dataset

Author

David O’Sullivan

Published

July 17, 2024

Date Changes
2024-07-19 Rerun to add turn angles.
2024-07-17 Initial post.

Run this code once only to make clean and aggregated GPS datasets.

library(tidyr)
library(stringr)
library(here)
library(sf)

source(str_glue("{here()}/scripts/gps-data-utils.R"))

Read raw files, clean and write any that still have data remaining

src_folder <- str_glue("{here()}/_data/GPS-1516Season-MiersValley")
tgt_folder <- str_glue("{here()}/_data/cleaned-gps-data")

if (!file.exists(tgt_folder)) {
  dir.create(tgt_folder)
}
persons <- dir(src_folder, pattern = ".csv") |>
  lapply(str_split_i, "-", i = 1) |>
  unlist()
dfs <- list()
i <- 0
for (p in persons) {
  i <- i + 1
  df <- get_cleaned_gps_data(str_glue("{src_folder}/{p}-FINAL.csv"), p) 
  if (nrow(df) > 0) {
    df |> write.csv(str_glue("{tgt_folder}/{p}-cleaned.csv"))
    dfs[[i]] <- df
  }
}
bind_rows(dfs) |>
  write.csv(str_glue("{tgt_folder}/all-gps-traces.csv"))

Write to individual GPKG files and a single all traces file

files <- str_c(tgt_folder, "/", dir(path = tgt_folder, pattern = ".csv"))
for (f in files) {
  get_gps_data_as_sf(f) |> 
    st_write(str_replace(f, "csv", "gpkg"), delete_dsn = TRUE)
}
Deleting source `/Users/david/Documents/work/mwlr-tpm-antarctica/antarctica/_data/cleaned-gps-data/all-gps-traces.gpkg' using driver `GPKG'
Writing layer `all-gps-traces' to data source 
  `/Users/david/Documents/work/mwlr-tpm-antarctica/antarctica/_data/cleaned-gps-data/all-gps-traces.gpkg' using driver `GPKG'
Writing 112216 features with 26 fields and geometry type Point.
Deleting source `/Users/david/Documents/work/mwlr-tpm-antarctica/antarctica/_data/cleaned-gps-data/AVC-cleaned.gpkg' using driver `GPKG'
Writing layer `AVC-cleaned' to data source 
  `/Users/david/Documents/work/mwlr-tpm-antarctica/antarctica/_data/cleaned-gps-data/AVC-cleaned.gpkg' using driver `GPKG'
Writing 5069 features with 26 fields and geometry type Point.
Deleting source `/Users/david/Documents/work/mwlr-tpm-antarctica/antarctica/_data/cleaned-gps-data/Ben-cleaned.gpkg' using driver `GPKG'
Writing layer `Ben-cleaned' to data source 
  `/Users/david/Documents/work/mwlr-tpm-antarctica/antarctica/_data/cleaned-gps-data/Ben-cleaned.gpkg' using driver `GPKG'
Writing 6918 features with 26 fields and geometry type Point.
Deleting source `/Users/david/Documents/work/mwlr-tpm-antarctica/antarctica/_data/cleaned-gps-data/Charlie-cleaned.gpkg' using driver `GPKG'
Writing layer `Charlie-cleaned' to data source 
  `/Users/david/Documents/work/mwlr-tpm-antarctica/antarctica/_data/cleaned-gps-data/Charlie-cleaned.gpkg' using driver `GPKG'
Writing 5851 features with 26 fields and geometry type Point.
Deleting source `/Users/david/Documents/work/mwlr-tpm-antarctica/antarctica/_data/cleaned-gps-data/Craig-cleaned.gpkg' using driver `GPKG'
Writing layer `Craig-cleaned' to data source 
  `/Users/david/Documents/work/mwlr-tpm-antarctica/antarctica/_data/cleaned-gps-data/Craig-cleaned.gpkg' using driver `GPKG'
Writing 1668 features with 26 fields and geometry type Point.
Deleting source `/Users/david/Documents/work/mwlr-tpm-antarctica/antarctica/_data/cleaned-gps-data/Fraser-cleaned.gpkg' using driver `GPKG'
Writing layer `Fraser-cleaned' to data source 
  `/Users/david/Documents/work/mwlr-tpm-antarctica/antarctica/_data/cleaned-gps-data/Fraser-cleaned.gpkg' using driver `GPKG'
Writing 7216 features with 26 fields and geometry type Point.
Deleting source `/Users/david/Documents/work/mwlr-tpm-antarctica/antarctica/_data/cleaned-gps-data/Gemma-cleaned.gpkg' using driver `GPKG'
Writing layer `Gemma-cleaned' to data source 
  `/Users/david/Documents/work/mwlr-tpm-antarctica/antarctica/_data/cleaned-gps-data/Gemma-cleaned.gpkg' using driver `GPKG'
Writing 8428 features with 26 fields and geometry type Point.
Deleting source `/Users/david/Documents/work/mwlr-tpm-antarctica/antarctica/_data/cleaned-gps-data/Georgia-cleaned.gpkg' using driver `GPKG'
Writing layer `Georgia-cleaned' to data source 
  `/Users/david/Documents/work/mwlr-tpm-antarctica/antarctica/_data/cleaned-gps-data/Georgia-cleaned.gpkg' using driver `GPKG'
Writing 4685 features with 26 fields and geometry type Point.
Deleting source `/Users/david/Documents/work/mwlr-tpm-antarctica/antarctica/_data/cleaned-gps-data/IanHawes-cleaned.gpkg' using driver `GPKG'
Writing layer `IanHawes-cleaned' to data source 
  `/Users/david/Documents/work/mwlr-tpm-antarctica/antarctica/_data/cleaned-gps-data/IanHawes-cleaned.gpkg' using driver `GPKG'
Writing 6593 features with 26 fields and geometry type Point.
Deleting source `/Users/david/Documents/work/mwlr-tpm-antarctica/antarctica/_data/cleaned-gps-data/IanMcD-cleaned.gpkg' using driver `GPKG'
Writing layer `IanMcD-cleaned' to data source 
  `/Users/david/Documents/work/mwlr-tpm-antarctica/antarctica/_data/cleaned-gps-data/IanMcD-cleaned.gpkg' using driver `GPKG'
Writing 8861 features with 26 fields and geometry type Point.
Deleting source `/Users/david/Documents/work/mwlr-tpm-antarctica/antarctica/_data/cleaned-gps-data/Jayne-cleaned.gpkg' using driver `GPKG'
Writing layer `Jayne-cleaned' to data source 
  `/Users/david/Documents/work/mwlr-tpm-antarctica/antarctica/_data/cleaned-gps-data/Jayne-cleaned.gpkg' using driver `GPKG'
Writing 6448 features with 26 fields and geometry type Point.
Deleting source `/Users/david/Documents/work/mwlr-tpm-antarctica/antarctica/_data/cleaned-gps-data/Kurt-cleaned.gpkg' using driver `GPKG'
Writing layer `Kurt-cleaned' to data source 
  `/Users/david/Documents/work/mwlr-tpm-antarctica/antarctica/_data/cleaned-gps-data/Kurt-cleaned.gpkg' using driver `GPKG'
Writing 6178 features with 26 fields and geometry type Point.
Deleting source `/Users/david/Documents/work/mwlr-tpm-antarctica/antarctica/_data/cleaned-gps-data/Marwan-cleaned.gpkg' using driver `GPKG'
Writing layer `Marwan-cleaned' to data source 
  `/Users/david/Documents/work/mwlr-tpm-antarctica/antarctica/_data/cleaned-gps-data/Marwan-cleaned.gpkg' using driver `GPKG'
Writing 10385 features with 26 fields and geometry type Point.
Deleting source `/Users/david/Documents/work/mwlr-tpm-antarctica/antarctica/_data/cleaned-gps-data/Paul-cleaned.gpkg' using driver `GPKG'
Writing layer `Paul-cleaned' to data source 
  `/Users/david/Documents/work/mwlr-tpm-antarctica/antarctica/_data/cleaned-gps-data/Paul-cleaned.gpkg' using driver `GPKG'
Writing 7320 features with 26 fields and geometry type Point.
Deleting source `/Users/david/Documents/work/mwlr-tpm-antarctica/antarctica/_data/cleaned-gps-data/Peyman-cleaned.gpkg' using driver `GPKG'
Writing layer `Peyman-cleaned' to data source 
  `/Users/david/Documents/work/mwlr-tpm-antarctica/antarctica/_data/cleaned-gps-data/Peyman-cleaned.gpkg' using driver `GPKG'
Writing 8390 features with 26 fields and geometry type Point.
Deleting source `/Users/david/Documents/work/mwlr-tpm-antarctica/antarctica/_data/cleaned-gps-data/Pierre-cleaned.gpkg' using driver `GPKG'
Writing layer `Pierre-cleaned' to data source 
  `/Users/david/Documents/work/mwlr-tpm-antarctica/antarctica/_data/cleaned-gps-data/Pierre-cleaned.gpkg' using driver `GPKG'
Writing 8615 features with 26 fields and geometry type Point.
Deleting source `/Users/david/Documents/work/mwlr-tpm-antarctica/antarctica/_data/cleaned-gps-data/Tim-cleaned.gpkg' using driver `GPKG'
Writing layer `Tim-cleaned' to data source 
  `/Users/david/Documents/work/mwlr-tpm-antarctica/antarctica/_data/cleaned-gps-data/Tim-cleaned.gpkg' using driver `GPKG'
Writing 9591 features with 26 fields and geometry type Point.