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";
@kylerphillips
Copy link

Do you have a hosted implementation of this - having trouble getting the code to work with a .stravactivity file I have.

@chappy84
Copy link
Author

Apologies for the very delayed response @kylerphillips, it appears I missed your comments, as github doesn't send notifications for gists. I have now signed up to this service to try and avoid missing others in the future: https://github.com/tightenco/giscus

Anyway, this is intended to be run on the unix command line, with php installed, and available in the environment.
It can be run using the following:

./convert_stravactivity_to_gpx /path/to/activity.stravactivity

It will output the xml to stdout which can be copied and saved to a gpx file.

You can do this easier using the following:

./convert_stravactivity_to_gpx /path/to/activity.stravactivity > /path/to/activity.gpx

@stevevls
Copy link

This is awesome...the Strava app was failing to upload my ride today, and I was able to lift the .stravactivity file from my phone and use this script to convert it. Thanks!

@RobiDubplate
Copy link

RobiDubplate commented Feb 5, 2018

Hi @stevevls, yesterday I have the same problem than you, but I don´t know how do this conversion. Could you help me? I have not the knowledge needed to do this conversion. I attach the link to my stravactivity file to convert into gpx https://wetransfer.com/downloads/9fc4cf18f5571519dc3f160b0a992a9c20180205122211/b367610ac0dc9c41585767f01f4f507920180205122211/7dac04
Thanks in advance ;)

@chappy84
Copy link
Author

chappy84 commented Feb 5, 2018

@RobiDubplate It depends on what operating system you're running on your main computer? (Windows? Mac? Linux?)
From there you can progress (Windows / Linux will need either PHP or Docker installing, macOS comes with PHP built in)

Linux, you can install it via the operating system's package manager (yum, apt, dnf, etc.)
On Mac / Linux, you can then download this file and run the following in a terminal:

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

If you don't want to install php, but have docker, you can do the following instead
You'll need the strava activity file in the same folder as this converter script

cd /path/to/download/location/
chmod u+x convert_stravactivity_to_gpx
docker run --rm -it -v "`pwd`":/mnt php:cli-alpine /mnt/convert_stravactivity_to_gpx /mnt/activity.stravactivity > activity.gpx

Windows will need the above script modifying slightly to remove the 1st line

#!/usr/bin/env php

After which, once you've installed php, you should be able to run it using the following:

php C:\path\to\convert_stravactivity_to_gpx C:\path\to\activity.stravactivity

after which you can just copy / paste the output into a notepad, and save it as any filename with a .gpx extension

You will have to forgive me however, as my Windows foo is rather rusty

In all of these, activity.stravactivity should be replaced with what your strava activity filename actually is. activity.gpx can be whatever you want it to be, including activity.gpx if you so wish.

I also apologise, but I'm afraid I won't download un-solicited files onto my local machine for conversion, especially ones whose filename is obfuscated, hence why I haven't converted this for you.

@kayoubi
Copy link

kayoubi commented Mar 28, 2018

👍

@killmosquito
Copy link

killmosquito commented Apr 7, 2018

Thanks in advance for the effort in making this converter.

I'm trying to use it because my strava app can't upload my last activity and their support service doesn't seem very interested in helping me out.

I followed @chappy84 instructions closely but I'm receiving this error:

Warning: fopen(): Filename cannot be empty in /Users/me/Desktop/convert_stravactivity_to_gpx on line 26
Couldnt open /Users/me/Desktop/test/test.stravactivity

I'm on macOS high sierra.

I tried it with a good stravactivity file that uploaded correctly last week, but I'm receiving the same error, so it's not because my stravactivity file is corrupt.

Can anybody help?

@chappy84
Copy link
Author

chappy84 commented Apr 7, 2018

@killmosquito can you show us the full command you’re running?
I’m presuming the file path is correct and it exists, thus I suspect this is down to either using the Mac path rather than the docker mount path with docker, or a file with the wrong permissions when running directly on MacOS. It depends which method you’re trying to use.

@killmosquito
Copy link

Thanks for the super fast reply! I drag-dropped the file positions into the terminal, this is the command I've run

./convert_stravactivity_to_gpx /Users/me/Desktop/test/test.stravactivity > /Users/me/Desktop/dest/activity.gpx

I have very little experience with programming, so not sure if I have a docker or how to use it...

@chappy84
Copy link
Author

chappy84 commented Apr 7, 2018

@killmosquito Well you're not using docker with the command you're running, so I suspect it's either the file path being incorrect, or permissions on the file
If you can run cat /Users/me/Desktop/test/test.stravactivity and not get a response something along the lines of: cat: /Users/me/Desktop/test/test.stravactivity: No such file or directory, then the file path definitely exists. At this point I'd simply recommend running chmod +r /Users/me/Desktop/test/test.stravactivity to add read permissions for all users on the computer to the file, then try running it again.
If it's still failing after that, could you post the output of ls -la /Users/me/Desktop/test/test.stravactivity so I can see the permissions on the file?
As the file will have been transferred from your phone, rather than downloaded from the internet, I don't suspect MacOS is blocking access to it.

@killmosquito
Copy link

@chappy84 I ran the "cat" command and it printed the file content, and I also ran the chmod to change the permission on the stravactivity file, but it the conversion still gives the same error message.

here's the permissions for the file -rw-rw-rw-@ 1 me staff 2984774 7 Apr 12:13 /Users/me/Desktop/test/test.stravactivity

Could it be because the stravactivity file is corrupt? Or the conversion doesn't even go into that?

@chappy84
Copy link
Author

chappy84 commented Apr 9, 2018

@killmosquito Apologies, the issue is because the script is expecting a relative path for the .stravactivity file, not an absolute path as you've provided. This is my documentation's fault and something I forgot about when writing my comment on the 5th Feb. The realpath function returns an empty string when it can't find a file in the provided path, and it's combining to the directory the script is in, and /Users/me/Desktop/test/test.stravactivity, which won't exist as a file, and thus fopen can't open an empty path as a string, which is the error you're getting. It was a quick and dirty script and thus I knew it may have bugs, this being one. Easiest quick fix your side is to move the .stravactivity file to the same dir as the convert_stravactivity_to_gpx then run ./convert_stravactivity_to_gpx test.stravactivity > activity.gpx. This that should produce a file you can upload. I've sorted the code to deal with this situation long term.

As a side, I am surprised it's letting you run it, as the execute flag isn't set for anyone, thus ./convert_stravactivity_to_gpx should in theory return a Permission denied message, and not get as far as reading the fopen in the php code. You can add execution permissions with a chmod u+x convert_stravactivity_to_gpx (this will give just the me user permissions to execute this file as a script, which should be the user it's running the script as, provided that's who you're logged in as)

@killmosquito
Copy link

Thank you @chappy84, moving all three files in the same location worked, and I now I have a working gpx file!
I remember one of the first attempt to convert was with all three files in the same folder, but I wasn't aware of the permission requirement yet.

Ironically, as I was about to upload the gpx, the Strava team has uploaded my performance from their side, so now I can't upload my gpx as it would be a double...

But this converter is a very handy tool to have anyway and you were of great support, I'm sure it will help others!
Thanks again

@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