mirror of
https://forge.chapril.org/tykayn/wololo
synced 2025-06-20 01:34:42 +02:00
split utils, separate IRVE guess
This commit is contained in:
parent
b4c28335b2
commit
a732edc228
12 changed files with 460 additions and 363 deletions
|
@ -1,7 +1,7 @@
|
|||
/**
|
||||
* csv_to_geojson.ts
|
||||
*
|
||||
* Convertir un fichier CSV en GeoJSON
|
||||
* Convertir un fichier CSV en GeoJSON, en précisant les colonnes latitude et longitude
|
||||
*
|
||||
* Utilisation:
|
||||
*
|
||||
|
@ -10,170 +10,33 @@
|
|||
*/
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import csvParser from 'csv-parser';
|
||||
import minimist from 'minimist';
|
||||
import { Feature, FeatureCollection, Point } from 'geojson';
|
||||
import { csvToGeoJSON, checkFile, countGeoJSONFeatures } from './csv_to_geojson.utils';
|
||||
|
||||
interface Options {
|
||||
interface CSVConversionOptions {
|
||||
dir: string;
|
||||
file: string;
|
||||
latColumn: string;
|
||||
lonColumn: string;
|
||||
hasHeaders: boolean;
|
||||
}
|
||||
let counter = 0;
|
||||
let counter_features = 0;
|
||||
let counter_missing_lat = 0;
|
||||
let counter_missing_lon = 0;
|
||||
function csvToGeoJSON(options: Options): FeatureCollection<Point> {
|
||||
const { dir, file, latColumn, lonColumn, hasHeaders } = options;
|
||||
|
||||
console.log(`options latColumn: ${latColumn}`);
|
||||
console.log(`options lonColumn: ${lonColumn}`);
|
||||
|
||||
const filePath = path.join(dir, file);
|
||||
const features: Feature<Point>[] = [];
|
||||
let geoJSON: FeatureCollection<Point> = {
|
||||
type: 'FeatureCollection',
|
||||
features,
|
||||
};
|
||||
|
||||
let headers: string[] = [];
|
||||
let headersFound = false;
|
||||
|
||||
let limitOffset = 100000000;
|
||||
|
||||
fs.createReadStream(filePath)
|
||||
.pipe(csvParser({ headers: hasHeaders }))
|
||||
.on('data', (row) => {
|
||||
counter += 1;
|
||||
if (!headersFound && hasHeaders) {
|
||||
let keys = Object.keys(row);
|
||||
keys.forEach((key) => {
|
||||
headers.push(row[key]);
|
||||
});
|
||||
headersFound = true;
|
||||
console.log(`headers: ${headers}`);
|
||||
}
|
||||
if (counter > limitOffset) {
|
||||
return;
|
||||
}
|
||||
if (headers.indexOf(latColumn) && headers.indexOf(lonColumn)) {
|
||||
const lat = parseFloat(row['_' + headers.indexOf(latColumn)]);
|
||||
const lon = parseFloat(row['_' + headers.indexOf(lonColumn)]);
|
||||
|
||||
// Si pas d'entêtes, utiliser des noms de colonnes génériques
|
||||
if (!hasHeaders) {
|
||||
const properties: { [key: string]: any } = {};
|
||||
Object.keys(row).forEach((key, index) => {
|
||||
properties[headers[index]] = row[key];
|
||||
});
|
||||
row = properties;
|
||||
} else {
|
||||
let keys = Object.keys(row);
|
||||
// Utiliser les entêtes comme noms de propriétés
|
||||
const properties: { [key: string]: any } = {};
|
||||
headers.forEach((header, index) => {
|
||||
|
||||
properties[header] = row['_' + index];
|
||||
});
|
||||
row = properties;
|
||||
}
|
||||
|
||||
// filtrer la ligne du header si présente
|
||||
if (lon && lat) {
|
||||
if (hasHeaders && counter > 1 || !hasHeaders || counter > limitOffset) {
|
||||
features.push({
|
||||
type: 'Feature',
|
||||
geometry: {
|
||||
type: 'Point',
|
||||
coordinates: [lon, lat],
|
||||
},
|
||||
properties: row,
|
||||
});
|
||||
counter_features += 1;
|
||||
}
|
||||
}
|
||||
if (headers.indexOf(latColumn) === -1) {
|
||||
console.log('!!! no latColumn', row);
|
||||
counter_missing_lat += 1;
|
||||
}
|
||||
if (headers.indexOf(lonColumn) === -1) {
|
||||
console.log('!!! no lonColumn', row);
|
||||
counter_missing_lon += 1;
|
||||
}
|
||||
}
|
||||
})
|
||||
.on('end', () => {
|
||||
geoJSON = {
|
||||
type: 'FeatureCollection',
|
||||
features,
|
||||
};
|
||||
fs.writeFileSync(`${dir}/${file}.geojson`, JSON.stringify(geoJSON, null, 2));
|
||||
console.log(`GeoJSON créé avec succès : ${file}.geojson`);
|
||||
console.log(`geoJSON lines: ${counter}`);
|
||||
console.log(`geoJSON lines missing lat: ${counter_missing_lat}`);
|
||||
console.log(`geoJSON lines missing lon: ${counter_missing_lon}`);
|
||||
console.log(`geoJSON lines features: ${counter_features}`);
|
||||
});
|
||||
|
||||
return geoJSON;
|
||||
}
|
||||
|
||||
const args = minimist<Options>(process.argv.slice(2), {
|
||||
// config des arguments de conversion
|
||||
const args = minimist<CSVConversionOptions>(process.argv.slice(2), {
|
||||
alias: {
|
||||
dir: 'd',
|
||||
file: 'f',
|
||||
latColumn: 'lat',
|
||||
lonColumn: 'lon',
|
||||
hasHeaders: 'h',
|
||||
dir: 'd', // dossier source
|
||||
file: 'f', // fichier source
|
||||
// infos pour un fichier CSV en source au lieu d'un fichier geojson
|
||||
latColumn: 'lat', // colonne latitude
|
||||
lonColumn: 'lon', // colonne longitude
|
||||
hasHeaders: 'h', // headers présents
|
||||
},
|
||||
default: {
|
||||
hasHeaders: true,
|
||||
},
|
||||
});
|
||||
|
||||
function checkFile(args: Options) {
|
||||
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)}`);
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
function countGeoJSONFeatures(args: Options) {
|
||||
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;
|
||||
}
|
||||
|
||||
checkFile(args);
|
||||
csvToGeoJSON(args);
|
||||
// Appeler la fonction après la création du GeoJSON
|
||||
countGeoJSONFeatures(args);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue