scripts/kartaview_exif_mapper/main.ts

151 lines
4.1 KiB
TypeScript
Raw Normal View History

2023-08-13 23:12:01 +02:00
/**
conversion de données gpx
conversion de données exif
**/
import * as fs from 'node:fs';
2023-08-14 09:39:49 +02:00
// @ts-ignore
import path from "node:path";
import minimist from 'minimist';
2023-08-13 23:12:01 +02:00
2023-08-14 09:39:49 +02:00
// configs
let sequence_name: string = '3596977'
const dossier_photo: string = "/home/poule/encrypted/stockage-syncable/photos/imagerie kartaview carto tel/kartaview_export_storage/share2tykayn/photo/" + sequence_name
let dossier_gpx: string = "."
const file_gpx: string = "/home/poule/encrypted/stockage-syncable/photos/imagerie kartaview carto tel/kartaview_export_storage/share2tykayn/metadata_file/" + sequence_name + "/3596249_d875a_60a0f9bf38f99.txt"
2023-08-13 23:12:01 +02:00
let gpxData: any = {}
2023-08-14 09:39:49 +02:00
let tableKartaviewTrace: any = []
2023-08-13 23:12:01 +02:00
2023-08-14 09:39:49 +02:00
let mini_arguments :any = minimist(process.argv.slice(2))
console.log('mini_arguments', mini_arguments)
if(mini_arguments['sequence']){
sequence_name = mini_arguments['sequence']
}
2023-08-13 23:12:01 +02:00
function makeGpxFromKartaview() {
2023-08-14 09:39:49 +02:00
let track_points: string = '';
2023-08-13 23:12:01 +02:00
// TODO build track points
tableKartaviewTrace.forEach((elem: any) => {
2023-08-14 09:39:49 +02:00
track_points = `${track_points}<trkpt lat="${elem[0]}" lon="${elem[1]}">
</trkpt>
`
2023-08-13 23:12:01 +02:00
})
2023-08-14 09:39:49 +02:00
let content = `<?xml version='1.0' encoding='UTF-8'?>
<gpx version="1.1" creator="JOSM GPX export" 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">
<metadata>
<desc>trace_gpx_de_demo</desc>
<author>
<name>somebody</name>
</author>
<copyright author="somebody">
<year>2023</year>
<license>https://creativecommons.org/licenses/by-sa/2.5</license>
</copyright>
<keywords>nada</keywords>
</metadata>
<trk>
<trkseg>
${track_points}
</trkseg>
</trk>
</gpx>`
2023-08-13 23:12:01 +02:00
return content;
}
function openGPX(filepath: any) {
fs.readFile(filepath, 'utf8', (err, data) => {
if (err) {
throw err;
}
const gpx_content = data;
let lines = gpx_content.split('\n')
console.log('lines.length', lines.length)
2023-08-14 09:39:49 +02:00
// loop on all lines, only take the lines that contain :g:
2023-08-13 23:12:01 +02:00
lines.forEach((elem: any) => {
if (elem.indexOf(":g:") > -1) {
2023-08-14 09:39:49 +02:00
// do stuff on gpxData to enrich it
2023-08-13 23:12:01 +02:00
let boom = elem.split(':')
let gpsmodel = boom[2]
let gps = gpsmodel.split(';')
tableKartaviewTrace.push(gps)
let date = new Date(boom[0] * 1000)
console.log('*', date, gps[0], gps[1])
}
})
let content_gpx = makeGpxFromKartaview()
2023-08-14 09:39:49 +02:00
writeFile('' + sequence_name + '_trace.gpx', content_gpx)
2023-08-13 23:12:01 +02:00
// console.log('gpx_content', gpx_content)
})
}
2023-08-14 09:39:49 +02:00
let cwd = path.dirname(process.cwd()) + '/' + path.basename(process.cwd())
let expandedFileList: any = []
/**
* get a list of all files in a path
* @param folderPath
*/
function getAllFilesInFolder(folderPath: string) {
let filesList: any = []
fs.readdir(folderPath, (err, filesList) => {
if (err) throw err
console.log('readSubdirectories - files', filesList)
filesList.forEach((subDirOrFile) => {
const newFullPath = cwd + '/' + subDirOrFile
if (fs.existsSync(newFullPath)) {
const s = fs.statSync(newFullPath)
if (s.isFile()) {
expandedFileList.push(cwd + '/' + subDirOrFile)
}
}
})
return filesList;
})
2023-08-13 23:12:01 +02:00
}
function mapExifDataOnListOfFilesFromGpxData(listOfFiles: any, gpxData: any) {
listOfFiles.forEach((elem: any) => {
// find corresponding timestamp
// add exif info
// save file
})
}
2023-08-14 09:39:49 +02:00
function writeFile(fileName: string, fileContent: any) {
console.log('write file', fileName)
return fs.writeFile(
`${dossier_gpx}/${fileName}`,
fileContent,
'utf8',
(err) => {
if (err) {
console.log(`Error writing file: ${err}`)
}
}
)
}
2023-08-13 23:12:01 +02:00
openGPX(file_gpx)
let listOfFiles: any = getAllFilesInFolder(dossier_photo)
2023-08-14 09:39:49 +02:00
2023-08-13 23:12:01 +02:00
console.log('listOfFiles', listOfFiles)