Skip to content

Instantly share code, notes, and snippets.

@jwm-art-net
Created May 19, 2020 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwm-art-net/836fac6de2f4df2f3903618eb663b50e to your computer and use it in GitHub Desktop.
Save jwm-art-net/836fac6de2f4df2f3903618eb663b50e to your computer and use it in GitHub Desktop.
Pull .fit files off my Garmin Edge 510 and put into a year folder and convert to gpx.
#!/bin/bash
function mountpoint_from_vendor_id
{
VENDORID="$1"
iserial=$(lsusb -v -d "$VENDORID": 2> /dev/null | grep iSerial -m 1 | cut -d ' ' -f 3)
}
SRCDIR="/run/media/sirrom/GARMIN/Garmin/Activities"
#SRCDIR=$(mountpoint_from_vendor_id
NAME="*.fit"
USER="James"
while [[ $# -gt 0 ]]; do
case $1 in
-s|--srcdir) SRCDIR="${2%%/}"; shift ;;
-n|--name) NAME="${2}"; shift ;;
-u|--user) USER="${2}"; shift ;;
-h|--help) usage;
esac
shift
done
DEST="${HOME}/Maps/Rides/${USER}"
function usage
{
printf "%s\n\n" $(basename $0)
printf "%s\n\n" "Process Garmin .fit files contained on Garmin device, and convert to GPX."
printf "%s\n\n" "All files (including intermediary TCX) are achived to 'user' folder (unrelated to \$HOME) and by year."
printf "%s\n\n" "OPTIONS"
printf "%s\t\t%s\n" "-s|--srcdir" "source directory of .fit files to process. default: USB Garmin device."
printf "%s\t\t%s\n" "-n|--name" "glob pattern to find .fit files must be single quoted. default: '*.fit'."
printf "%s\t\t%s\n" "-u|--user" "change user name for archiving files. default: 'James'."
printf "%s\t\t%s\n" "-h|--help" "this help."
exit -1
}
function mkdirorexit
{
if [ ! -d "$1" ]; then
mkdir $V "$1"
if [ $? -ne 0 ]; then
echo "Failed to create directory '$1'";
exit 1
fi
fi
}
mkdirorexit "$DEST"
V="-v"
unset V
N="-n"
# fit files
FITPATH="${DEST}/fit"
FITEXT=".fit"
mkdirorexit "$FITPATH"
# tcx files
TCXPATH="${DEST}/tcx"
TCXEXT=".tcx"
mkdirorexit "$TCXPATH"
# gpx files
GPXPATH="${DEST}/gpx"
GPXEXT=".gpx"
mkdirorexit "$GPXPATH"
# PNG thumbnails
PNGPATH="${DEST}/thumbs"
PNGEXT=".png"
#PNGOPTS="-s 320x240"
PNGOPTS="-b 2"
mkdirorexit "$PNGPATH"
while read -r FILE
do
if [ ! -e "$FILE" ]; then
echo "Cannot find Garmin FIT files: '${FILES}'"
exit 1
fi
FN=$(basename -s $FITEXT "${FILE}")
echo $N "${FN}${FITEXT} "
YEAR=${FN:0:4}
#------------------------------------------------
# 1) transfer fit file to archive folder
#------------------------------------------------
FITDIR="${FITPATH}/${YEAR}"
mkdirorexit "$FITDIR"
FITFN="${FITDIR}/${FN}${FITEXT}"
if [[ "$FITFN" != "$FILE" ]]; then # source file resides in dest!
if [ -e "$FITFN" ]; then
echo $N ">FIT exists< "
else
mv $V "$FILE" "$FITFN"
if [ $? -ne 0 ]; then
echo
echo "Failed to archive $FITFN"
exit 1
fi
echo $N "--> FIT "
fi
fi
#------------------------------------------------
# 2) convert fit file to intermediary tcx format
#------------------------------------------------
TCXDIR="${TCXPATH}/${YEAR}"
mkdirorexit "$TCXDIR"
TCXFN="${TCXDIR}/${FN}${TCXEXT}"
if [ -e "$TCXFN" ]; then
echo $N ">TCX exists< "
else
fittotcx "$FITFN" > "$TCXFN"
if [ $? -ne 0 ]; then
echo
echo "Failed to convert to tcx"
exit 1
fi
echo $N "--> TCX "
fi
#------------------------------------------------
# 3) convert intermediary tcx to final gpx format
#------------------------------------------------
GPXDIR="${GPXPATH}/${YEAR}"
mkdirorexit "$GPXDIR"
GPXFN="${GPXDIR}/${FN}${GPXEXT}"
if [ -e "$GPXFN" ]; then
echo $N ">GPX exists< "
else
gpsbabel -i gtrnctr -o gpx "$TCXFN" "$GPXFN"
if [ $? -ne 0 ]; then
echo
echo "Failed to convert to gpx"
exit 1
fi
echo "--> GPX "
fi
done < <(find "${SRCDIR}" -mindepth 1 \
-maxdepth 1 \
-type f \
-name "${NAME}" ) # \
exit
#-------------------------------------------------------------------
PNGDIR="${PNGPATH}/${YEAR}"
echo "PNGDIR:$PNGDIR"
mkdirorexit "$PNGDIR"
PNGFN="${PNGDIR}/${FN}${PNGEXT}"
if [ -e "$PNGFN" ]; then
echo $N ">PNG exists<."
else
#gpx2png -g "${GPXFN}" $PNGOPTS -o "${PNGFN}"
/home/sirrom/Maps/Rides/James/gpx2png.pl $PNGOPTS -o "${PNGFN}" -q "${GPXFN}"
if [ $? -ne 0 ]; then
echo
echo "Failed to convert to PNG"
fi
echo "--> PNG."
fi
done < <(find "${SRCDIR}" -mindepth 1 \
-maxdepth 1 \
-type f \
-name "${NAME}" ) # \
#-printf '%f\0')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment