2025-04-11 15:53:23 +02:00
|
|
|
import { FeatureCollection, Feature, Point } from "geojson";
|
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
|
|
|
import csvParser from 'csv-parser';
|
|
|
|
|
|
|
|
|
|
|
|
let counter = 0;
|
|
|
|
let counter_features = 0;
|
|
|
|
let counter_missing_lat = 0;
|
|
|
|
let counter_missing_lon = 0;
|
|
|
|
|
2025-04-17 17:34:39 +02:00
|
|
|
export interface CSVConversionOptions {
|
|
|
|
dir: string;
|
|
|
|
file: string;
|
|
|
|
latColumn?: string;
|
|
|
|
lonColumn?: string;
|
|
|
|
hasHeaders?: boolean;
|
|
|
|
}
|
2025-04-11 15:53:23 +02:00
|
|
|
|
2025-04-17 17:34:39 +02:00
|
|
|
function parseCoordinates(value: string): { lat: number; lon: number } | null {
|
|
|
|
if (!value) return null;
|
2025-04-11 15:53:23 +02:00
|
|
|
|
2025-04-17 17:34:39 +02:00
|
|
|
const parts = value.split(';');
|
|
|
|
if (parts.length >= 2) {
|
|
|
|
const lat = parseFloat(parts[0]);
|
|
|
|
const lon = parseFloat(parts[1]);
|
|
|
|
if (!isNaN(lat) && !isNaN(lon)) {
|
|
|
|
return { lat, lon };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2025-04-11 15:53:23 +02:00
|
|
|
|
2025-04-17 17:34:39 +02:00
|
|
|
function csvToGeoJSON(options: CSVConversionOptions): FeatureCollection<Point> {
|
|
|
|
const { dir, file, latColumn, lonColumn, hasHeaders } = options;
|
2025-04-11 15:53:23 +02:00
|
|
|
const filePath = path.join(dir, file);
|
|
|
|
|
2025-04-17 17:34:39 +02:00
|
|
|
if (!fs.existsSync(filePath)) {
|
|
|
|
console.error(`Le fichier ${filePath} n'existe pas`);
|
|
|
|
return { type: 'FeatureCollection', features: [] };
|
|
|
|
}
|
2025-04-11 15:53:23 +02:00
|
|
|
|
2025-04-17 17:34:39 +02:00
|
|
|
const csvContent = fs.readFileSync(filePath, 'utf-8');
|
|
|
|
const lines = csvContent.split('\n');
|
|
|
|
const features: Feature<Point>[] = [];
|
2025-04-11 15:53:23 +02:00
|
|
|
|
2025-04-17 17:34:39 +02:00
|
|
|
const startIndex = hasHeaders ? 1 : 0;
|
|
|
|
const headers = hasHeaders ? lines[0].split(';') : null;
|
|
|
|
|
|
|
|
for (let i = startIndex; i < lines.length; i++) {
|
|
|
|
const line = lines[i].trim();
|
|
|
|
if (!line) continue;
|
|
|
|
|
|
|
|
const values = line.split(';');
|
|
|
|
const row: { [key: string]: any } = {};
|
|
|
|
|
|
|
|
if (headers) {
|
|
|
|
headers.forEach((header, index) => {
|
|
|
|
row[header] = values[index];
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
values.forEach((value, index) => {
|
|
|
|
row[`column${index}`] = value;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let coordinates: { lat: number; lon: number } | null = null;
|
|
|
|
if (latColumn && lonColumn) {
|
|
|
|
const lat = parseFloat(row[latColumn]);
|
|
|
|
const lon = parseFloat(row[lonColumn]);
|
|
|
|
if (!isNaN(lat) && !isNaN(lon)) {
|
|
|
|
coordinates = { lat, lon };
|
2025-04-11 15:53:23 +02:00
|
|
|
}
|
2025-04-17 17:34:39 +02:00
|
|
|
} else if (row['geo_point_2d']) {
|
|
|
|
coordinates = parseCoordinates(row['geo_point_2d']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (coordinates) {
|
|
|
|
features.push({
|
|
|
|
type: 'Feature',
|
|
|
|
geometry: {
|
|
|
|
type: 'Point',
|
|
|
|
coordinates: [coordinates.lon, coordinates.lat]
|
|
|
|
},
|
|
|
|
properties: row
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2025-04-11 15:53:23 +02:00
|
|
|
|
2025-04-17 17:34:39 +02:00
|
|
|
return {
|
|
|
|
type: 'FeatureCollection',
|
|
|
|
features
|
|
|
|
};
|
2025-04-11 15:53:23 +02:00
|
|
|
}
|
|
|
|
|
2025-04-17 17:34:39 +02:00
|
|
|
function checkFile(args: CSVConversionOptions) {
|
2025-04-11 15:53:23 +02:00
|
|
|
let filePath = path.join(args.dir, args.file);
|
|
|
|
let lineCount = 0;
|
|
|
|
|
|
|
|
// Vérifier si le fichier existe
|
|
|
|
if (!fs.existsSync(filePath)) {
|
|
|
|
throw new Error(`Le fichier CSV ${filePath} n'existe pas`);
|
|
|
|
} else {
|
|
|
|
console.log(`Le fichier CSV ${filePath} existe`);
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.createReadStream(filePath)
|
|
|
|
.on('data', () => {
|
|
|
|
lineCount++;
|
|
|
|
})
|
|
|
|
.on('end', () => {
|
|
|
|
console.log(`Nombre de lignes dans le fichier CSV : ${Math.floor(lineCount)}`);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2025-04-17 17:34:39 +02:00
|
|
|
function countGeoJSONFeatures(args: CSVConversionOptions) {
|
2025-04-11 15:53:23 +02:00
|
|
|
const filePath = path.join(args.dir, `${args.file}.geojson`);
|
|
|
|
|
|
|
|
// Vérifier si le fichier GeoJSON existe
|
|
|
|
if (!fs.existsSync(filePath)) {
|
|
|
|
console.log(`Le fichier GeoJSON ${filePath} n'existe pas`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lire et parser le fichier GeoJSON
|
|
|
|
const geoJSON = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
|
|
|
|
|
|
// Compter le nombre de features
|
|
|
|
const featureCount = geoJSON.features?.length || 0;
|
|
|
|
|
|
|
|
console.log(`Nombre de features dans le GeoJSON : ${featureCount}`);
|
|
|
|
return featureCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
csvToGeoJSON,
|
|
|
|
checkFile,
|
|
|
|
countGeoJSONFeatures
|
|
|
|
};
|