Mejoras generales
This commit is contained in:
69
csv2json.js
Normal file
69
csv2json.js
Normal file
@@ -0,0 +1,69 @@
|
||||
// Importamos módulos necesarios
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import readline from 'node:readline';
|
||||
import { dataLineToObject } from './libs/utils.js';
|
||||
|
||||
// Ruta del archivo CSV: se pasa como argumento o usa 'logs/datos.csv' por defecto
|
||||
const inputPath = process.argv[2] || path.join('.', 'datos.csv');
|
||||
|
||||
// Arrays para acumular datos
|
||||
const dataObjects = [];
|
||||
const geoFeatures = [];
|
||||
|
||||
// Creamos interfaz de lectura de líneas
|
||||
const rl = readline.createInterface({
|
||||
input: fs.createReadStream(inputPath),
|
||||
crlfDelay: Infinity
|
||||
});
|
||||
|
||||
rl.on('line', (line) => {
|
||||
const obj = dataLineToObject(line);
|
||||
if (obj) {
|
||||
dataObjects.push(obj);
|
||||
// Solo añadimos puntos válidos
|
||||
if (obj.latitud != null && obj.longitud != null) {
|
||||
geoFeatures.push({
|
||||
type: 'Feature',
|
||||
properties: {
|
||||
paquete: obj.paquetes,
|
||||
temperatura: obj.temperatura,
|
||||
presion: obj.presion,
|
||||
altitud: obj.altitudMetros,
|
||||
velocidad: obj.velocidadKmph,
|
||||
numSatelites: obj.numSatelites,
|
||||
datetimeGPS: obj.datetimeGPS
|
||||
},
|
||||
geometry: {
|
||||
type: 'Point',
|
||||
coordinates: [obj.longitud, obj.latitud]
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
rl.on('close', () => {
|
||||
// Escribimos JSON
|
||||
const jsonPath = path.join(path.dirname(inputPath), 'datos.json');
|
||||
fs.writeFileSync(jsonPath, JSON.stringify(dataObjects, null, 2), 'utf8');
|
||||
console.log(`Archivo JSON creado en ${jsonPath}`);
|
||||
|
||||
// Escribimos GeoJSON
|
||||
const geojson = {
|
||||
type: 'FeatureCollection',
|
||||
features: geoFeatures
|
||||
};
|
||||
|
||||
geojson.features.push({
|
||||
type: 'Feature',
|
||||
properties: {},
|
||||
geometry: {
|
||||
type: 'LineString',
|
||||
coordinates: geoFeatures.map(feature => feature.geometry.coordinates)
|
||||
}
|
||||
});
|
||||
const geoPath = path.join(path.dirname(inputPath), 'datos.geojson');
|
||||
fs.writeFileSync(geoPath, JSON.stringify(geojson, null, 2), 'utf8');
|
||||
console.log(`Archivo GeoJSON creado en ${geoPath}`);
|
||||
});
|
||||
Reference in New Issue
Block a user