Skip to content

Instantly share code, notes, and snippets.

@chappy84
Last active January 15, 2023 05:25
Show Gist options
  • Save chappy84/8d23f9c3235796dec46ea0749c15d83f to your computer and use it in GitHub Desktop.
Save chappy84/8d23f9c3235796dec46ea0749c15d83f to your computer and use it in GitHub Desktop.
Quick and dirty PHP script to convert .stravactivity file to basic gpx xml
#!/usr/bin/env php
<?php
function dateInFormat($timestamp)
{
return date('Y-m-d\TH:i:s\Z', $timestamp);
}
function latLongInFormat($reference)
{
return round((float) $reference, 6);
}
if ($argc <= 1) {
die("Please provide a file\n");
}
$filename = $argv[1];
if (!preg_match('/\.stravactivity$/', $filename)) {
die("Please provide a stravactivity file\n");
}
$filePath = realpath($filename);
if (empty($filePath)) {
die('Couldn\'t find file: ' . $filename . "\n");
}
if (false === ($fh = @fopen($filePath, 'r'))) {
die('Couldn\'t open ' . $filePath . " , Please check the file permissions\n");
}
date_default_timezone_set('UTC');
$start = null;
$points = array();
while (false !== ($line = fgets($fh))) {
if (0 === strpos($line, 'wp: ')
&& preg_match_all('/(?P<name>[a-z]+)\:(?P<value>[0-9.-]+)(;\s+)??/', $line, $matches)
) {
$point = [];
foreach ($matches['name'] as $key => $name) {
$point[$name] = $matches['value'][$key];
}
if ($start === null && !empty($point['t'])) {
$start = $point['t'];
}
$points[] = $point;
}
}
if (empty($points)) {
die("No points found\n");
}
echo <<<'NOWDOC'
<?xml version="1.0" encoding="UTF-8"?>
<gpx creator="GenericScript" version="1.1" xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3">
<metadata>
<time>
NOWDOC;
echo dateInFormat($start);
echo <<<'NOWDOC'
</time>
</metadata>
<trk>
<name>Generated GPX</name>
<trkseg>
NOWDOC;
foreach ($points as $point) {
echo ' <trkpt lat="', latLongInFormat($point['lat']), '" lon="', latLongInFormat($point['long']), '">
<ele>', round((float) $point['alt'], 1), '</ele>
<time>', dateInFormat($point['t']), '</time>
</trkpt>
';
}
echo <<<'NOWDOC'
</trkseg>
</trk>
</gpx>
NOWDOC;
echo "\n";
@chappy84
Copy link
Author

chappy84 commented Mar 9, 2020

Anyone coming to this, it's worth noting that the Strava App now records in Garmin's .fit format, thus once you've retrieved that from the phone, you can directly upload that to the Strava site without modification. This script is thus redundant for recent activities.

@Williamdb5
Copy link

Hi @chappy84 , I used an older phone and app to record an activity (so not with .fit format). However, Strava failed to upload/sync the activity. I recovered the .stravactivity file and have been trying for ways to fix it on my Mac running Big Sur.

In my latest attempt, I've copied the file in my downloads folder and named it activity.stravactivity
Trying to convert to .gpx for upload on strava website, I then adapted what you've mentioned earlier from:

cd /path/to/download/location/
chmod u+x convert_stravactivity_to_gpx
./convert_stravactivity_to_gpx /path/to/activity.stravactivity > /path/to/activity.gpx

to the following:

cd /Users/gilliam/Downloads/
chmod u+x convert_stravactivity_to_gpx
./convert_stravactivity_to_gpx /Users/gilliam/Downloads/activity.stravactivity > /Users/gilliam/Downloads/activity.gpx

When running this in Terminal, Terminal responds with:

chmod: convert_stravactivity_to_gpx: No such file or directory
zsh: no such file or directory: ./convert_stravactivity_to_gpx

In addition, a new file appears in my downloads folder named activity.gpx, however, the file is empty with zero byte size. (Whilst the original .stravactivity is 704KB).

The response of Terminal is odd to me as when I run the following, Terminal presents the file the same way as what's shown when opening the file in TextEdit.

cat /Users/williamdebry/Downloads/activity.stravactivity

Could it be that MacOS Big Sur is no longer compatible using such instructions or something? I have no IT background whatsoever, so any additional help would be great!

@chappy84
Copy link
Author

@Williamdb5 it's not an issue with Big Sur thankfully.
It can't find a file in your Downloads folder called convert_stravactivity_to_gpx, which means you've either not downloaded the above script, or not downloaded it to your Downloads folder

try this:

cd /Users/gilliam/Downloads/
curl -O https://gist.github.com/chappy84/8d23f9c3235796dec46ea0749c15d83f/raw/00ae732180ee70d9b0d0e9b18e1324329f59ae64/convert_stravactivity_to_gpx
chmod u+x convert_stravactivity_to_gpx
./convert_stravactivity_to_gpx /Users/gilliam/Downloads/activity.stravactivity > /Users/gilliam/Downloads/activity.gpx

The URL with the curl command is the what you get from the "Raw" link at the top of the page, and is from the last edit I made before today
That curl -O command will download the URL to your downloads folder (as long as you run it after the cd command) in a file called convert_stravactivity_to_gpx

Hopefully that helps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment