diff --git a/cartodb/import_tcx.sh b/cartodb/import_tcx.sh new file mode 100755 index 0000000..859d3d5 --- /dev/null +++ b/cartodb/import_tcx.sh @@ -0,0 +1,92 @@ +#!/bin/bash +# +# Import your sport activities from tapiriik.com to cartoco.com. +# +# Prerequisistes: +# - rclone +# - curl +# - jq +# - xsqlproc +# +# Author: Steffen Vogel +# Copyright: 2016, Steffen Vogel +# License: GPLv3 + +CARTODB_API_KEY=$(pass apis/cartodb) +CARTODB_USER=stv0g +CARTODB_EP="https://${CARTODB_USER}.carto.com/api/v2/sql?api_key=${CARTODB_API_KEY}" + +TCXDIR=~/Tracks + +STYLESHEET=$(mktemp) +JQFILTER=$(mktemp) + +cat << EOF > ${JQFILTER} +if has("error") then + "Error: " + .error[0] +else + "Success: Rows added: " + (.total_rows|tostring) +end +EOF + +cat << EOF > ${STYLESHEET} + + + + + + q=INSERT INTO laps (number, starttime, averageheartratebpm, maximumheartratebpm, calories, distancemeters, intensity, totaltimeseconds, the_geom) VALUES + + + ( + , + TIMESTAMP '', + , + , + , + , + '', + , + ST_SetSRID(ST_GeomFromText('MULTILINESTRING(( + + + + , + + ))'), 4326) + ), + + ; + + + + +EOF + +FILES_BEFORE=$(ls -1 -d ${TCXDIR}/*.tcx) + +echo "##### Starting download from Dropbox #####" +rclone sync drpbx:/Apps/tapiriik/ ${TCXDIR} + +FILES_AFTER=$(ls -1 -d ${TCXDIR}/*.tcx) +FILES_NEW=$(comm -23 <(echo "${FILES_AFTER}") <(echo "${FILES_BEFORE}")) + +echo +echo "##### Starting import to CartoCB / PostGIS #####" +echo "${FILES_NEW}" | while read FILE; do + TEMPFILE=$(mktemp) + + xsltproc -o "${TEMPFILE}" "${STYLESHEET}" "${FILE}" + + printf "%s %-64s" "$(date +'%Y/%m/%d %H:%M:%S')" "$(basename "${FILE}"):" + + if [ -s ${TEMPFILE} ]; then + curl -sSX POST --data @${TEMPFILE} ${CARTODB_EP} | jq -rf ${JQFILTER} + else + echo "Note: There are no trackpoints. Skipped" + fi +done + +rm ${STYLESHEET} ${JQFILTER} \ No newline at end of file diff --git a/cartodb/tcx2gpx.sh b/cartodb/tcx2gpx.sh new file mode 100755 index 0000000..f840861 --- /dev/null +++ b/cartodb/tcx2gpx.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +SRC=${1:-${DROPBOX}/Apps/tapiriik} +DEST=${2:-${DROPBOX}/Apps/cartodb} + +SPORTS="" + +# Convert all TXC into GPX files +for FILE in ${SRC}/*.tcx +do + BASE=$(basename "${FILE// /_}" .tcx) + INPUT="${FILE}" + OUTPUT="${BASE}.gpx" + + SPORT="${BASE##*_}" + SPORT="${SPORT%% *}" + + SPORTS="$SPORTS $SPORT" + + echo "Converting $INPUT to $OUTPUT of Sport $SPORT" + + mkdir -p "${DEST}/${SPORT}" + + ${BABEL} -t -r -w -i gtrnctr -f "${INPUT}" -x track,speed -o gpx -F "${DEST}/${SPORT}/${OUTPUT}" +done + +SPORTS=$(echo $SPORTS | tr ' ' '\n' | sort -u | tr '\n' ' ') + +# Merge all activities per sport +for SPORT in ${SPORTS} +do + FILES="" + + for FILE in ${DEST}/${SPORT}/*.gpx; do + FILES="$FILES -f $FILE" + done + + echo "Merging into $SPORT.gpx" + + ${BABEL} -t -r -w -i gpx ${FILES} -o gpx -F ${DEST}/${SPORT}.gpx +done \ No newline at end of file