Skip to content

Instantly share code, notes, and snippets.

@n8fr8
Created January 20, 2023 03:53
Show Gist options
  • Save n8fr8/52f14fcfc1ba2de2da3c1052b84c6291 to your computer and use it in GitHub Desktop.
Save n8fr8/52f14fcfc1ba2de2da3c1052b84c6291 to your computer and use it in GitHub Desktop.
var fs = require('fs');
var path = require('path');
// In newer Node.js versions where process is already global this isn't necessary.
var process = require("process");
var geojson = require('geojson');
const tokml = require('tokml');
var proofDir = process.argv[2]
const proofPoints = [];
var lastPoint;
function getProof (targetDir) {
// Loop through all the files in the temp directory
fs.readdir(targetDir, function (err, files) {
if (err) {
console.error("Could not list the directory.", err);
process.exit(1);
}
files.forEach(function (file, index) {
// Make one pass and make the file complete
var fromPath = path.join(targetDir, file);
if (fromPath.endsWith(".proof.json"))
{
console.log("============================================");
console.log("Proof: '%s'", fromPath);
let rawdata = fs.readFileSync(fromPath);
let proof = JSON.parse(rawdata);
console.log("Proof Generated: %s",proof["File Modified"])
console.log("Location: %s,%s",proof["Location.Longitude"],proof["Location.Latitude"])
var thisPoint = {name: proof["File Modified"], latitude: parseFloat(proof["Location.Latitude"]), longitude: parseFloat(proof["Location.Longitude"])}
proofPoints.push(thisPoint);
if (lastPoint)
proofPoints.push({line: [[lastPoint.longitude, lastPoint.latitude], [thisPoint.longitude, thisPoint.latitude]]})
lastPoint = thisPoint;
}
else if (fs.lstatSync(fromPath).isDirectory())
{
getProof(fromPath)
}
if (Object.is(files.length - 1, index)) {
// execute last item logic
generateKml(proofPoints);
}
});
});
}
function generateKml (points) {
const geojsonObject = geojson.parse(points, { Point: ['latitude', 'longitude'], LineString: 'line' })
console.log(JSON.stringify(geojsonObject));
const response = tokml(geojsonObject);
console.log((response));
}
function proofKml () {
getProof(proofDir);
}
proofKml();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment