mirror of
https://forge.chapril.org/tykayn/mapillary_download
synced 2025-10-09 17:02:46 +02:00
up
This commit is contained in:
parent
cdd4d6e549
commit
a080ab844f
15 changed files with 392 additions and 80 deletions
67
csv_to_geojson.ts
Normal file
67
csv_to_geojson.ts
Normal file
|
@ -0,0 +1,67 @@
|
|||
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);
|
Loading…
Add table
Add a link
Reference in a new issue