Skip to content

Instantly share code, notes, and snippets.

@natelowry
Last active December 7, 2021 23:10
Show Gist options
  • Save natelowry/d5dc92d981c520488682bc32f3db0bf3 to your computer and use it in GitHub Desktop.
Save natelowry/d5dc92d981c520488682bc32f3db0bf3 to your computer and use it in GitHub Desktop.
/*jshint esversion: 6 */
const exiftool = require("exiftool-vendored").exiftool; //yarn add exiftool-vendored
const gpxParser = require("gpxparser"); //yarn add gpxparser
const fs = require("fs");
var gpx = new gpxParser();
gpx.parse(fs.readFileSync("c:/data/Geo.gpx"));
var mainTrack = gpx.tracks[0];
console.log("let's go!");
//TODO: handle the async better
const dirName = "c:/data/";
fs.readdir(dirName, (err, fileNames) => {
fileNames.filter(s => !s.endsWith(".gpx")).forEach((fileName) => {
exiftool
.read(dirName + fileName)
.then((tags) => {
var dateTaken = tags.DateTimeOriginal.toDate();
//the dst timing is already done
//if (tags.DaylightSavings === "Yes") {
// dateTaken.setHours(dateTaken.getHours() - 1);
//}
const firstGpxTrackBefore = mainTrack.points
.filter(p => p.time < dateTaken && dateTaken - p.time < 1000 * 60)
.slice(-1)
.pop();
const firstGpxTrackAfter = mainTrack.points.filter(
p => p.time > dateTaken && p.time - dateTaken < 1000 * 60
)[0];
var finalPoint = {};
if (firstGpxTrackBefore && firstGpxTrackAfter) {
//TODO: interpolate instead of average
finalPoint = {
lat: (firstGpxTrackBefore.lat + firstGpxTrackAfter.lat) / 2.0,
lon: (firstGpxTrackBefore.lon + firstGpxTrackAfter.lon) / 2.0,
ele: (firstGpxTrackBefore.ele + firstGpxTrackAfter.ele) / 2.0,
};
} else {
finalPoint = firstGpxTrackBefore || firstGpxTrackAfter || finalPoint;
}
if (finalPoint.lat && finalPoint.lon && finalPoint.ele) {
exiftool
.write(
dirName + fileName,
{
GPSLatitude: finalPoint.lat,
GPSLatitudeRef: finalPoint.lat,
GPSLongitude: finalPoint.lon,
GPSLongitudeRef: finalPoint.lon,
GPSAltitude: finalPoint.ele,
},
["-overwrite_original"]
)
.then(() => {
console.log(`finished: ${fileName}`);
})
.catch((err) => {
console.log(err);
});
} else {
console.log(`no trackpoint that matches ${fileName}`);
}
})
.catch((err) => {
console.log(err);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment