title: r params$track_title author: r params$author date: r params$date


knitr::opts_chunk$set(echo = FALSE,
                      warning = FALSE)

library("dplyr", warn.conflicts = FALSE)
library("ggplot2")
library("sf")
library("extrafont")
library("ggplot2")
library("ggspatial")
library("sf")
library("ggrepel")
#extrafont::loadfonts()

library("gpx2pdf")
library("sf")


track_points_sf <- params$track_points_sf

tracks_sf <- params$tracks_sf

# track_points_sf <- read_track_points(path)
# 
# tracks_sf <- read_tracks(path)

register_tile_source(Thunderforest_outdoor_API = source_from_url_format(
  url_format = stringr::str_c(c('http://a.tile.thunderforest.com/outdoors/${z}/${x}/${y}.png?apikey=',
                 'http://b.tile.thunderforest.com/outdoors/${z}/${x}/${y}.png?apikey=',
                 'http://c.tile.thunderforest.com/outdoors/${z}/${x}/${y}.png?apikey='),
                 api_key),
  attribution = "More on Thunderforest at http://www.thunderforest.com/",
  extension = "png"
))

register_tile_source(OpenTopoMap = source_from_url_format(
  url_format = stringr::str_c(c('http://a.tile.opentopomap.org/${z}/${x}/${y}.png',
                 'http://b.tile.opentopomap.org/${z}/${x}/${y}.png',
                 'http://c.tile.opentopomap.org/${z}/${x}/${y}.png'),
                 api_key),
  attribution = "opentopomap.org CC-BY-SA",
  extension = "png"
))
gpx_df <- create_distance_elevation_df(track_points_sf = track_points_sf)
labels_df <- create_labels_df(distance_elevation_df = gpx_df,
                              track_points_sf = track_points_sf)
if (params$url!="") {
  create_qr(url = params$url)
}
library("gt")
tibble::tibble(Lunghezza = scales::number(x = max(gpx_df$distance),
                                          accuracy = 0.1,
                                          scale = 0.001,
                                          suffix = " km"), 
               `Altitudine alla partenza` = gpx_df$elevation[1] %>%
                 scales::number(accuracy = 1,
                                suffix = " m"),
               `Altitudine all'arrivo` = gpx_df$elevation[length(gpx_df$elevation)] %>%
                 scales::number(accuracy = 1,
                                suffix = " m"),
               `Altitudine massima` = max(gpx_df$elevation) %>% scales::number(accuracy = 1, suffix = " m"),
               `Altitudine minima` = min(gpx_df$elevation) %>% scales::number(accuracy = 1, suffix = " m"),
               `Dislivello` = (max(gpx_df$elevation)-min(gpx_df$elevation))  %>% scales::number(accuracy = 1, suffix = " m"), 
               `Dislivello cumulativo in salita` = diff(gpx_df$elevation) %>% 
                 ifelse(.>0, yes = ., no = 0) %>% 
                 sum() %>% 
                 scales::number(accuracy = 1, suffix = " m"),
               `Dislivello cumulativo in discesa` = diff(gpx_df$elevation) %>% 
                 ifelse(.<0, yes = ., no = 0) %>% 
                 sum() %>% 
                 scales::number(accuracy = 1, suffix = " m")) %>% 
  tidyr::pivot_longer(cols = dplyr::everything(), names_to = "x", values_to = "y") %>% 
  gt::gt()  %>%
    tab_header(
    #  title = md(paste0("**", params$track_title, "**")), 
      title = md("*Dati riassuntivi sul percorso*")
    ) %>% 
  cols_label(x = "",y = "") %>% 
  opt_table_font(font = "Roboto Condensed")
gpx_df %>% 
  dplyr::distinct(distance, .keep_all = TRUE) %>% 
  ggplot(mapping = aes(x = distance, y = elevation, ymax = elevation)) +
  geom_ribbon(fill = "lightgrey",
              mapping = aes(ymin = c(min(gpx_df$elevation)*0.85)),
              alpha = 0.6) +
  geom_line() +
  scale_x_continuous(name = "", 
                     labels = function(x) scales::number(x = x,
                                                         accuracy = 0.1,
                                                         scale = 0.001,
                                                         suffix =  " km"))  +
  scale_y_continuous(name = "", labels = function(x) scales::number(x = x,
                                                                    suffix = " m"),
                     limits = c(min(gpx_df$elevation)*0.85, max(gpx_df$elevation)*1.10),
                     expand = expansion(mult = 0,
                                        add = 0)
  ) +
  ggrepel::geom_label_repel(data = labels_df,
                            mapping = aes(x = distance,
                                          y = elevation,
                                          label = id),
                            nudge_y = 30,
                            min.segment.length = 0.1,
                            family =  "Roboto Condensed") +
  labs(#title = params$track_title, 
       title = "Profilo altimetrico del percorso"
       # , 
       # caption = "Fonte: Archivio della Memoria dell'Ecomuseo della Valle dei Laghi
       # https://archiviomemoria.ecomuseovalledeilaghi.it/"
       ) +
  theme_minimal(base_family = "Roboto Condensed") +
  theme(panel.background = element_rect(fill = "white", colour = NA), 
        plot.background = element_rect(fill = "white", colour = NA)) 
cat(paste0("[", params$url, "](", params$url, ")"))
selected_points <- track_points_sf %>% 
  dplyr::filter(is.na(name)==FALSE) %>% 
  dplyr::transmute(name,
            description = desc,
            number = row_number()) %>% 
  dplyr::mutate(description = ifelse(is.na(description), "", description)) %>% 
  dplyr::transmute(name, 
            text = stringr::str_c(
   # "<h2>", number, ". ", name, "</h2>",
     "\n### ", number, ". ", name, "\n",
  #  "<img src='", img, "' width = '128'>",
    "<p>", description, "</p>"
    ))
if (params$map_style=="Thunderforest_outdoor_API") {
  caption_text <- "Maps © www.thunderforest.com, Data © www.osm.org/copyright"
} else if (params$map_style=="OpenTopoMap") {
   caption_text <- "Maps © opentopomap.org (CC-BY-SA), Data © www.osm.org/copyright"
} else {
  caption_text <- "© OpenStreetMap contributors"
}


route_points_numbered <- selected_points %>% 
  dplyr::select(name) %>% 
  dplyr::mutate(number = dplyr::row_number())


route_points_coords <- selected_points %>% 
  sf::st_coordinates() %>% 
  as.data.frame() %>% 
  tibble::as_tibble() %>% 
  dplyr::transmute(lon = X,
                   lat = Y,
                   name = route_points_numbered$name,
                   number = dplyr::row_number())


map_osmgrayscale <- ggplot() +
  #geom_sf(data=route_points_numbered, aes(colour=name)) +
  #geom_sf(data = route_points_numbered, mapping = aes(fill = number)) +
  annotation_map_tile(
    type = params$map_style,
    zoomin = 0,
    progress = "none",
    quiet = TRUE) +
  geom_sf(data = tracks_sf, colour = "black", size = 3) +
  geom_sf(data = tracks_sf, colour = "white", size = 2) +
   geom_label_repel(data = route_points_numbered,
                    mapping = aes(geometry = geometry,
                                  label = number),
                    colour = "black",
                    fill = "white",
                    size = 4,
                    stat = "sf_coordinates",
                    min.segment.length = 0) +
  theme_void() +
  ggplot2::labs(caption = caption_text)

map_osmgrayscale
for (i in seq_along(selected_points$text)) {
   cat("\n") 
  cat("\n") 
  cat(selected_points$text[i])
   cat("\n") 
   cat("\n") 

}
gpx_df %>% 
  dplyr::distinct(distance, .keep_all = TRUE) %>% 
  ggplot(mapping = aes(x = distance, y = elevation, ymax = elevation)) +
  geom_ribbon(fill = "lightgrey", 
              mapping = aes(ymin = c(min(gpx_df$elevation)*0.80)), alpha = 0.6) +
  geom_line() +
  scale_x_continuous(name = "", 
               labels = function(x) scales::number(x = x,
                                                   accuracy = 1,
                                                   scale = 0.001,
                                                   suffix =  " km"))  +
  scale_y_continuous(name = "", labels = function(x) scales::number(x = x,
                                                                    suffix = " m"),
                     limits = c(min(gpx_df$elevation)*0.80,
                                max(gpx_df$elevation)*1.10),
                     expand = expansion(mult = 0, add = 0)
                     ) +
  labs(title = params$track_title, 
       subtitle = "Profilo altimetrico del percorso", 
       caption = "Fonte: Archivio della Memoria dell'Ecomuseo della Valle dei Laghi
       https://archiviomemoria.ecomuseovalledeilaghi.it/") +
  theme_minimal(base_family = "Roboto Condensed") +

  theme_minimal(base_family = "Roboto Condensed") +
  theme(panel.background = element_rect(fill = "white", colour = NA), 
        plot.background = element_rect(fill = "white", colour = NA)) 



#ggsave(filename = "profilo_altimetrico.png" , width = 7, height = 5)


giocomai/gpx2pdf documentation built on July 7, 2022, 9:53 p.m.