UNPKG

@gooin/fit2spatial

Version:

Convert Garmin FIT files to GeoJSON, SHP, KML, GPX

56 lines (55 loc) 2.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.exportToGeoJSONLineString = exports.eventToGeoJSON = void 0; const _ = require("lodash"); const file_1 = require("./file"); const constant_1 = require("./constant"); const eventToGeoJSON = (event, output) => { const json = (0, exports.exportToGeoJSONLineString)(event); (0, file_1.writeToFile)(output, JSON.stringify(json)); }; exports.eventToGeoJSON = eventToGeoJSON; // export to a collection with a single feature of type LineString const exportToGeoJSONLineString = (event) => { var _a, _b; const resultJSON = { type: 'FeatureCollection', features: [ { type: 'Feature', geometry: { type: 'LineString', coordinates: [], }, properties: {}, }, ], }; const activity = event.getActivities()[0]; const streams = activity.getAllStreams(); const lonData = (_a = _.find(streams, stream => stream.type === 'Longitude')) === null || _a === void 0 ? void 0 : _a.getData(); const latData = (_b = _.find(streams, stream => stream.type === 'Latitude')) === null || _b === void 0 ? void 0 : _b.getData(); _.each(lonData, (lon, index) => { const lat = latData[index]; if (!lon || !lat) { return; } resultJSON.features[0].geometry.coordinates.push([lon, lat]); }); const stats = event.getStats(); // add average stats to properties for (const [key, stat] of stats.entries()) { const field = _.camelCase(key); if (_.includes(constant_1.DataStatFields, field)) { let value = stat.getValue(); if (_.isArray(value)) { value = _.last(value); } resultJSON.features[0].properties[field] = _.isNumber(value) ? _.round(value, 2) : value; } } // add date to properties const date = event.startDate; resultJSON.features[0].properties.date = date.toISOString(); return resultJSON; }; exports.exportToGeoJSONLineString = exportToGeoJSONLineString;