---
params:
  updateProgress: updateProgress
  path: path
---

\newpage
\phantom{invisible}


```{r calendar-{{i}}, dev='pdf',echo=F, include=F, fig.path=paste0(params$path,"fig_test/")}

if(is.function(params$updateProgress)) params$updateProgress()
data.frame(date = as.Date(stations_enu[[{{i}}]]$date),value = 1) %>% 
  dplyr::count(date) %>% 
  dplyr::rename(value = n) %>%
  plot_heatmap() %>%
        layout(width=800,height=200) %>%
  config(displayModeBar = F)
    
       
```
  

```{r local_map_q-{{i}},echo=FALSE, dev="png", out.width=600,out.height=450, include=FALSE, fig.path=paste0(params$path,"fig_test/")}

s <- stations[{{i}},]
        scale <- (s$v_q0*500)+30
        if(is.null(scale))scale<-0
        scale <- sapply(scale,min,100)
        l <- leaflet(width = 600,height = 450,options = leafletOptions(zoomAnimation = F, fadeAnimation = F )) %>%
          registerPlugin(rotatedMarker) %>%
          addTiles("https://wxs.ign.fr/choisirgeoportail/geoportail/wmts?REQUEST=GetTile&SERVICE=WMTS&VERSION=1.0.0&LAYER=ORTHOIMAGERY.ORTHOPHOTOS&STYLE=normal&TILEMATRIXSET=PM&FORMAT=image/jpeg&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}") %>%
          setView(lng = s$lon ,lat = s$lat, zoom = 20) %>%
          addMarkers(lng = s$lon,
                     lat = s$lat,
                     label = s$sta_code,
                     layerId = s$sta_code,
                     icon = makeIcon(iconUrl= "arrow_up.svg",
                                     iconWidth = scale,
                                     iconHeight = scale,
                                     iconAnchorX = 0.5*scale,
                                     iconAnchorY = scale,
                                     className = paste0("q_",s$sta_code),
                     ),
                     options = markerOptions(rotationAngle = s$heading_q0*180/pi))  %>%
          leaflet::addLegend(colors = speed2col(seq(0,0.3,0.05)),
                             labels = seq(0,0.3,0.05)*100,
                             title = "3D Velocity in cm/yr") %>%
                    prependContent(tags$style(paste0(custom_css_q,".leaflet-tile {
                                                                    filter: grayscale(40%) brightness(0.95);
                                                                }")))
        l
```

```{r d_xy_q-{{i}}, echo=FALSE, out.width = 230, include=FALSE, fig.path=paste0(params$path,"fig_test/")}

# browser()
p <- tryCatch({
  data <- stations_enu[[{{i}}]] %>%
    filter(date > (date_start))

  # Ensure data is not empty after filtering
  if (nrow(data) == 0) {
    stop("No data after filtering")
  }

  ggplot_serie_option_2d(data, 3, F)
}, error = function(e) {
  p <- ggplot() +
    annotate("text", x = 0.5, y = 0.5, label = "No data", 
             size = 5, hjust = 0.5, vjust = 0.5)
  return(list(p,p))
})
p[[1]]
```

```{r d_z_q-{{i}}, echo=FALSE, out.width= 230,fig.height=2, include=FALSE, fig.path=paste0(params$path,"fig_test/")}

p[[2]]
```

```{r local_map-{{i}},echo=FALSE, dev="png", out.width=600,out.height=450, include=FALSE, fig.path=paste0(params$path,"fig_test/")}
s <- stations[{{i}},]
        scale <- (s$v*500)+30
        scale <- sapply(scale,min,100)
        l <- leaflet(width = 600,height = 450,options = leafletOptions(zoomAnimation = F, fadeAnimation = F )) %>%
          registerPlugin(rotatedMarker) %>%
          addTiles("https://wxs.ign.fr/choisirgeoportail/geoportail/wmts?REQUEST=GetTile&SERVICE=WMTS&VERSION=1.0.0&LAYER=ORTHOIMAGERY.ORTHOPHOTOS&STYLE=normal&TILEMATRIXSET=PM&FORMAT=image/jpeg&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}") %>%
          setView(lng = s$lon ,lat = s$lat, zoom = 20) %>%
          addMarkers(lng = s$lon,
                     lat = s$lat,
                     label = s$sta_code,
                     layerId = s$sta_code,
                     icon = makeIcon(iconUrl= "arrow_up.svg",
                                     iconWidth = scale,
                                     iconHeight = scale,
                                     iconAnchorX = 0.5*scale,
                                     iconAnchorY = scale,
                                     className = paste0("all_",s$sta_code),
                     ),
                     options = markerOptions(rotationAngle = s$heading*180/pi))  %>%
          leaflet::addLegend(colors = speed2col(seq(0,0.3,0.05)),
                             labels = seq(0,0.3,0.05)*100,
                             title = "3D Velocity in cm/yr") %>%
                    prependContent(tags$style(paste0(custom_css,".leaflet-tile {
                                                                    filter: grayscale(40%) brightness(0.95);
                                                                }")))
        l
```

```{r d_xy-{{i}},echo=FALSE, out.width = 230, include=FALSE, fig.path=paste0(params$path,"fig_test/")}

p <- stations_enu[[{{i}}]] %>%
  ggplot_serie_option_2d(3,F)
p[[1]]
```

```{r d_z-{{i}}, echo=FALSE, out.width= 230,fig.height=2, include=FALSE, fig.path=paste0(params$path,"fig_test/")}

p[[2]]
```



```{r , echo=FALSE, results='asis'}
cat(paste0("\\setstaticframe{",8*({{i}}-1)+1,"}{valign=b}\n"))
cat(paste0("\\setstaticframe{",8*({{i}}-1)+2,"}{valign=t}\n"))

cat(paste0("\\setstaticcontents{",8*({{i}}-1)+1,"}{\\textbf{Cible ",stations[{{i}},'sta_code'],"}\\centering}\n"))
cat(paste0("\\setstaticcontents{",8*({{i}}-1)+2,"}{\\includegraphics[height=\\textheight]{",params$path,"fig_test/calendar-",{{i}},"-1.pdf}\\centering}\n"))
cat(paste0("\\setstaticcontents{",8*({{i}}-1)+3,"}{Déplacement du ",as.Date(date_start,tz="")," au ",as.Date(date_end,tz=""),"\\\\\\\\\\includegraphics[height=0.22\\textheight]{",params$path,"fig_test/local_map_q-",{{i}},"-1.png}\\centering}\n"))
cat(paste0("\\setstaticcontents{",8*({{i}}-1)+4,"}{Déplacement 3D Total\\\\\\\\\\includegraphics[height=0.22\\textheight]{",params$path,"fig_test/local_map-",{{i}},"-1.png}\\centering}\n"))
cat(paste0("\\setstaticcontents{",8*({{i}}-1)+5,"}{\\includegraphics[height=0.3\\textheight]{",params$path,"fig_test/d_xy_q-",{{i}},"-1.pdf}\\centering}\n"))
cat(paste0("\\setstaticcontents{",8*({{i}}-1)+6,"}{\\includegraphics[height=0.3\\textheight]{",params$path,"fig_test/d_xy-",{{i}},"-1.pdf}\\centering}\n"))
cat(paste0("\\setstaticcontents{",8*({{i}}-1)+7,"}{\\includegraphics[height=0.15\\textheight]{",params$path,"fig_test/d_z_q-",{{i}},"-1.pdf}\\centering}\n"))
cat(paste0("\\setstaticcontents{",8*({{i}}-1)+8,"}{\\includegraphics[height=0.15\\textheight]{",params$path,"fig_test/d_z-",{{i}},"-1.pdf}\\centering}\n"))
```

