Initial testing for Trash Tracker

Mapping
Supporting Activism
Tracking Litter using a phone app, and mapping the results
Author

Alan Jackson

Published

August 14, 2024

Test reading file and posting on map

# setup

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
path <- "/home/ajackson/Dropbox/Rprojects/TrashTracker/data/"

Read in all the files

Read files in, and split into path and collection points Run some simple stats on collection points Project the data Create a 25 m corridor around the path Are there collection points outside that corridor? If so create 10 meter circles around them, and do a union with corridor Create a 5 m grid in the area Using 20 m mean filter, populate grid Contour and plot

#   Read in the files

gpx_parsed <- XML::htmlTreeParse(file = paste0(path, "20240620.gpx"), useInternalNodes = TRUE)

coords <- XML::xpathSApply(doc = gpx_parsed, path = "//trkpt", fun = XML::xmlAttrs)
ways <- XML::xpathSApply(doc = gpx_parsed, path = "//wpt", fun = XML::xmlAttrs)
wpt_type <- XML::xpathSApply(doc = gpx_parsed, path = "//wpt/name", fun = XML::xmlValue)
elevation <- XML::xpathSApply(doc = gpx_parsed, path = "//wpt/ele", fun = XML::xmlValue)
# elevation <- XML::xpathSApply(doc = gpx_parsed, path = "//trkpt/ele", fun = XML::xmlValue)

df <- data.frame(
  lat = as.numeric(ways["lat", ]),
  lon = as.numeric(ways["lon", ]),
  waypt = wpt_type,
  elevation = as.numeric(elevation)
)

plot it

library(leaflet)

leaflet() %>%
  addTiles() %>%
  addPolylines(data = df, lat = ~lat, lng = ~lon, color = "#000000", opacity = 0.8, weight = 3) %>% 
  addMarkers(data=df, lat = ~lat, lng = ~lon )

Better plot

df2 <- df %>% 
  sf::st_as_sf(., coords=c("lon","lat"),crs=4326)


tmap::tmap_options(basemaps="OpenStreetMap")
tmap::tmap_mode("view") # set mode to interactive plots
tmap mode set to interactive viewing
  tmap::tm_shape(df2) + 
  tmap::tm_dots(size=0.05) +
  tmap::tm_shape(df2) + 
  tmap::tm_dots(col="red")