mirror of
https://forge.chapril.org/tykayn/mapillary_download
synced 2025-10-04 17:04:53 +02:00
67 lines
1.6 KiB
TypeScript
67 lines
1.6 KiB
TypeScript
import fs from 'fs';
|
|
import path from 'path';
|
|
import csvParser from 'csv-parser';
|
|
import minimist from 'minimist';
|
|
import { Feature, FeatureCollection, Point } from 'geojson';
|
|
|
|
interface Options {
|
|
dir: string;
|
|
file: string;
|
|
latColumn: string;
|
|
lonColumn: string;
|
|
hasHeaders: boolean;
|
|
}
|
|
|
|
/**
|
|
* @name csvToGeoJSON
|
|
* @description conversion de csv vers geojson
|
|
*
|
|
* Utilisation:
|
|
* node csv_to_geojson.ts -d ./etalab_data/panneaux -f mon_fichier.csv -lat GPSLatitude -lon GPSLongitude -h
|
|
*
|
|
**/
|
|
function csvToGeoJSON(options: Options): FeatureCollection<Point> {
|
|
const { dir, file, latColumn, lonColumn, hasHeaders } = options;
|
|
const filePath = path.join(dir, file);
|
|
const features: Feature<Point>[] = [];
|
|
|
|
fs.createReadStream(filePath)
|
|
.pipe(csvParser({ headers: hasHeaders }))
|
|
.on('data', (row) => {
|
|
const lat = parseFloat(row[latColumn]);
|
|
const lon = parseFloat(row[lonColumn]);
|
|
features.push({
|
|
type: 'Feature',
|
|
geometry: {
|
|
type: 'Point',
|
|
coordinates: [lon, lat],
|
|
},
|
|
properties: row,
|
|
});
|
|
})
|
|
.on('end', () => {
|
|
const geoJSON: FeatureCollection<Point> = {
|
|
type: 'FeatureCollection',
|
|
features,
|
|
};
|
|
fs.writeFileSync(`${file}.geojson`, JSON.stringify(geoJSON, null, 2));
|
|
console.log(`GeoJSON créé avec succès : ${file}.geojson`);
|
|
});
|
|
|
|
return features;
|
|
}
|
|
|
|
const args = minimist<Options>(process.argv.slice(2), {
|
|
alias: {
|
|
dir: 'd',
|
|
file: 'f',
|
|
latColumn: 'lat',
|
|
lonColumn: 'lon',
|
|
hasHeaders: 'h',
|
|
},
|
|
default: {
|
|
hasHeaders: true,
|
|
},
|
|
});
|
|
|
|
csvToGeoJSON(args);
|