added first version of CartoDB import script for Garmin Training Center databases (tex)
This commit is contained in:
parent
fb9ad5eaad
commit
b2ba0cc1d4
2 changed files with 133 additions and 0 deletions
92
cartodb/import_tcx.sh
Executable file
92
cartodb/import_tcx.sh
Executable file
|
@ -0,0 +1,92 @@
|
|||
#!/bin/bash
|
||||
#
|
||||
# Import your sport activities from tapiriik.com to cartoco.com.
|
||||
#
|
||||
# Prerequisistes:
|
||||
# - rclone
|
||||
# - curl
|
||||
# - jq
|
||||
# - xsqlproc
|
||||
#
|
||||
# Author: Steffen Vogel <post@steffenvogel.de>
|
||||
# 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}
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<xsl:stylesheet version="1.0"
|
||||
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
|
||||
xmlns:tcx="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2">
|
||||
<xsl:output method="text" />
|
||||
<xsl:template match="/tcx:TrainingCenterDatabase/tcx:Activities/tcx:Activity">
|
||||
<xsl:if test="count(//tcx:LongitudeDegrees) > 0">
|
||||
<xsl:text>q=INSERT INTO laps (number, starttime, averageheartratebpm, maximumheartratebpm, calories, distancemeters, intensity, totaltimeseconds, the_geom) VALUES</xsl:text>
|
||||
<xsl:for-each select="tcx:Lap">
|
||||
<xsl:if test="//tcx:LongitudeDegrees">
|
||||
(
|
||||
<xsl:value-of select="position()" />,
|
||||
TIMESTAMP '<xsl:value-of select="@StartTime" />',
|
||||
<xsl:value-of select="tcx:AverageHeartRateBpm/tcx:Value" />,
|
||||
<xsl:value-of select="tcx:MaximumHeartRateBpm/tcx:Value" />,
|
||||
<xsl:value-of select="tcx:Calories" />,
|
||||
<xsl:value-of select="tcx:DistanceMeters" />,
|
||||
'<xsl:value-of select="tcx:Intensity" />',
|
||||
<xsl:value-of select="tcx:TotalTimeSeconds" />,
|
||||
ST_SetSRID(ST_GeomFromText('MULTILINESTRING((<xsl:for-each select="tcx:Track/tcx:Trackpoint">
|
||||
<xsl:if test="tcx:Position/tcx:LongitudeDegrees">
|
||||
<xsl:value-of select="tcx:Position/tcx:LongitudeDegrees" /><xsl:text> </xsl:text>
|
||||
<xsl:value-of select="tcx:Position/tcx:LatitudeDegrees" />
|
||||
<xsl:if test="position() != last()">,</xsl:if>
|
||||
</xsl:if>
|
||||
</xsl:for-each>))'), 4326)
|
||||
)<xsl:if test="position() != last()">,</xsl:if>
|
||||
</xsl:if>
|
||||
</xsl:for-each>;
|
||||
</xsl:if>
|
||||
</xsl:template>
|
||||
<xsl:template match="text()"/>
|
||||
</xsl:stylesheet>
|
||||
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}
|
41
cartodb/tcx2gpx.sh
Executable file
41
cartodb/tcx2gpx.sh
Executable file
|
@ -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
|
Loading…
Add table
Reference in a new issue