add config maxspeed and Echirolles, up script get datasets, start conflation of geojsons

This commit is contained in:
Tykayn 2025-03-31 13:47:28 +02:00 committed by tykayn
parent 9b5baab032
commit aa35803a0b
5 changed files with 518 additions and 82 deletions

View file

@ -22,6 +22,9 @@ interface Options {
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;
@ -38,13 +41,12 @@ function csvToGeoJSON(options: Options): FeatureCollection<Point> {
let headers: string[] = [];
let headersFound = false;
let limitOffset = 100;
let limitOffset = 30000000;
console.log(`hasHeaders: ${hasHeaders}`);
fs.createReadStream(filePath)
.pipe(csvParser({ headers: hasHeaders }))
.on('data', (row) => {
counter++;
counter += 1;
if (!headersFound && hasHeaders) {
let keys = Object.keys(row);
keys.forEach((key) => {
@ -56,11 +58,7 @@ function csvToGeoJSON(options: Options): FeatureCollection<Point> {
if (counter > limitOffset) {
return;
}
if (headers.indexOf(latColumn)) {
console.log(`latColumn: ${latColumn}`);
console.log(`headers latColumn: ${headers.indexOf(latColumn)}`);
console.log(`headers.indexOf(latColumn): `, headers.indexOf(latColumn));
console.log('row', row);
if (headers.indexOf(latColumn) && headers.indexOf(lonColumn)) {
const lat = parseFloat(row['_' + headers.indexOf(latColumn)]);
const lon = parseFloat(row['_' + headers.indexOf(lonColumn)]);
@ -83,21 +81,28 @@ function csvToGeoJSON(options: Options): FeatureCollection<Point> {
}
// filtrer la ligne du header si présente
if (hasHeaders && counter > 1 || !hasHeaders || counter > limitOffset) {
features.push({
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [lon, lat],
},
properties: row,
});
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;
}
}
else {
console.log('!!! no latColumn', row);
}
})
.on('end', () => {
geoJSON = {
@ -106,9 +111,12 @@ function csvToGeoJSON(options: Options): FeatureCollection<Point> {
};
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}`);
});
console.log(`geoJSON lines: ${counter}`);
return geoJSON;
}
@ -146,7 +154,26 @@ function checkFile(args: Options) {
}
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;
}
console.log(`args: `, args);
checkFile(args);
csvToGeoJSON(args);
csvToGeoJSON(args);
// Appeler la fonction après la création du GeoJSON
countGeoJSONFeatures(args);