Thank you everyone for your help. I agree that it's too much hassle to worry about the nitpicky small segments so I'm going to skip that for now. Cheers.
library(sf)
library(dplyr)
library(lubridate)
library(geosphere)
process_gpx <- function(gpx_path, polygons) {
gpx <- read_sf(gpx_path, layer = "track_points") %>%
st_transform(st_crs(polygons)) %>%
arrange(time)
gpx_joined <- st_join(gpx, polygons, join = st_within)
gpx_joined <- gpx_joined %>%
group_by(SiteName) %>% # Replace with your polygon ID column
mutate(
next_time = lead(time),
next_geom = lead(geometry),
time_diff = as.numeric(difftime(next_time, time, units = "secs")),
dist_m = geosphere::distHaversine(
st_coordinates(geometry),
st_coordinates(next_geom)
)
) %>%
filter(time_diff <= 90) %>%
summarise(
total_time_sec = sum(time_diff, na.rm = TRUE),
total_distance_m = sum(dist_m, na.rm = TRUE),
.groups = "drop"
)
gpx_joined$file <- basename(gpx_path)
return(gpx_joined)
}
# Load your polygons
polygons <- st_read("try1/polygons_combo.shp")
# List all GPX files
gpx_files <- list.files("try1/gpx/", pattern = "\\.gpx$", full.names = TRUE)
# Process each file
results <- lapply(gpx_files, process_gpx, polygons = polygons)
# Combine into one data frame
final_summary <- bind_rows(results)
final_summary