sports-alliance / sports-lib

A Library for processing GPX, TCX, FIT and JSON files from services such as Strava, Movescount, Garmin, Polar etc
GNU Affero General Public License v3.0
149 stars 21 forks source link

FIT to GPX - Code Sample #63

Closed guy-keller closed 3 years ago

guy-keller commented 3 years ago

Hey guys,

I thought I'd contribute with a little code sample. Hope this helps someone / somehow.


filename: fit-converter.js

'use strict';

import fs from 'fs';
import sportsLibPkg from '@sports-alliance/sports-lib';
import exporterPkg from '@sports-alliance/sports-lib/lib/events/adapters/exporters/exporter.gpx.js'

const { SportsLib } = sportsLibPkg;
const { EventExporterGPX } = exporterPkg;

// Input and output file path
const inputFilePath = '/tmp/test.fit';
const outputGpxFilePath = '/tmp/test.gpx';

// reads the FIT file into memory
const inputFile = fs.readFileSync(inputFilePath, null);
if (inputFile && inputFile.buffer) {
    const inputFileBuffer = inputFile.buffer;
    // uses lib to read the FIT file
    SportsLib.importFromFit(inputFileBuffer).then((event) => {
        // convert to gpx
        const gpxPromise = new EventExporterGPX().getAsString(event);
        gpxPromise.then((gpxString) => {
            // writes the gpx to file
            fs.writeFileSync(outputGpxFilePath, gpxString, (wError) => {
                if (error) {
                    console.error('Ooops, something went wrong while saving the GPX file, see details below.');
                    console.error(JSON.stringify(wError));
                }
            });
            // all done, celebrate!
            console.log('Converted FIT file to GPX successfully!');
            console.log('GPX file saved here: ' + outputGpxFilePath);
        }).catch((cError) => {
            console.error('Ooops, something went wrong while converting the FIT file, see details below');
            console.error(JSON.stringify(cError));
        });
    });
} else {
    console.error('Ooops, could not read the inputFile or it does not exists, see details below');
    console.error(JSON.stringify(inputFilePath));
}

to run it:

node fit-converter.js

requirements: a "/tmp" folder with a valid test.fit file in it.


Many thanks for the easy to use lib!

jimmykane commented 3 years ago

That's great! Thanks so much

jimmykane commented 3 years ago

Is it ok if I include this example to the Readme?

guy-keller commented 3 years ago

Go for it! It would be nice to get a mention. However, if it is too much, it is all good :D

jimmykane commented 3 years ago

Alright should be up now :-D

jimmykane commented 3 years ago

Did a little modification, I tend to like to exit early do identation is little.

guy-keller commented 3 years ago

Looks good; also thanks for the mention!

You might want to fix this here though - from 'error' to 'wError':

        // writes the gpx to file
        fs.writeFileSync(outputGpxFilePath, gpxString, (wError) => {
            if (wError) {
                console.error('Ooops, something went wrong while saving the GPX file, see details below.');
                console.error(JSON.stringify(wError));
            }
        });