79749639

Date: 2025-08-28 21:55:13
Score: 1
Natty:
Report link

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
Reasons:
  • Blacklisted phrase (0.5): Thank you
  • Blacklisted phrase (1): Cheers
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: Amanda Goldberg