Mapping

Read Garmin FIT file and extract the necessary data for mapping

Author

Kizen Sasaki

First version

2023 December 05

Modified

2023 December 06

1. Install devtools package fist before installing ReadFit package from GitHub.
Code
if(!requireNamespace("remotes")) {
  install.packages("remotes")
}
remotes::install_github("grimbough/FITfileR")
library("FITfileR")
library(tidyverse)
2. Read the Garmin FIT file.
Select the fit file from data folder
Code
TargetDir <- paste(as.character(tkchooseDirectory(title = "Select the fit data folder"),sep = "", collapse =""))

library("tcltk")



FitNameList <- list.files(path = TargetDir) %>%
  str_extract("^.*fit*$") %>%
  na.omit()

#FitNameList # check
3. Read fit to ReadFit

If the FIT file is the third, give 3 to FileNo. FileNo <- 2

Code
setwd(TargetDir)
FileNo <- 65
ReadFit <- readFitFile(FitNameList[FileNo])
ReadFit # check
Fit File
├╴File created: 2023-10-15 06:41:42
├╴Device: garmin NA
└╴Number of data messages: 13676
4. Check the message type
Code
listMessageTypes(ReadFit) # check
 [1] "file_id"                 "device_settings"        
 [3] "user_profile"            "zones_target"           
 [5] "sport"                   "session"                
 [7] "lap"                     "record"                 
 [9] "event"                   "device_info"            
[11] "activity"                "file_creator"           
[13] "device_aux_battery_info"

“records” often contain data such as heart rat, latitude and longtitude, etc.

Code
# Read the record message
RecodeData <- getMessagesByType(ReadFit,
                                message_type = "record")
#RecodeData #check

# Merge all the message together into a single tibble.(dplyr)
MasterRecord <- RecodeData %>%
  bind_rows() %>% arrange(timestamp)

#日付けデータを変換する
MasterRecord <-
  MasterRecord %>%
  mutate_if(is.POSIXt, ~with_tz(., tz = "Asia/Tokyo"))

#MasterRecordを時間で並び替え
MasterRecord <- MasterRecord %>%
  arrange(timestamp)
5. Mapping (load leaflet package 運動強度を可視化できないか。)
Code
coords <- MasterRecord %>% 
  select(position_long, position_lat) 
Code
library(leaflet)

# Leafletを使用して地図を描画
m_leaflet <- coords %>% as.matrix() %>% 
  leaflet(  ) %>% 
  addTiles() %>% 
  addPolylines( )
m_leaflet