Add conversion script for ZHV

This commit is contained in:
Robin van der Linde 2025-07-25 00:01:31 +02:00
parent 124376470a
commit b3bea7503a

View file

@ -0,0 +1,47 @@
import { readFileSync, writeFileSync } from "fs"
import Script from "../Script"
import { parse } from "papaparse"
import { FeatureCollection, Point } from "geojson"
class ZHVcsv2GeoJson extends Script {
constructor() {
super("Converts a CSV file with ZHV data to GeoJSON format. Usage: csv-file output-file")
}
async main(args: string[]): Promise<void> {
const csvFile = args[0]
const outputFile = args[1]
if (!csvFile || !outputFile) {
console.error("Usage: csv-file output-file")
return
}
// Read the CSV file and parse it using PapaParse
const text = readFileSync(csvFile, "utf-8")
const parsed = parse(text, {
header: true,
dynamicTyping: true,
})
console.log(`Parsed ${parsed.data.length} rows from ${csvFile}`)
// Convert the parsed data to GeoJSON format
const geoJson: FeatureCollection<Point> = {
type: "FeatureCollection",
features: parsed.data.map((row: any) => ({
type: "Feature",
geometry: {
type: "Point",
coordinates: [row.longitude, row.latitude],
},
properties: row,
})),
}
// Write the GeoJSON output to the specified file
writeFileSync(outputFile, JSON.stringify(geoJson), "utf-8")
}
}
new ZHVcsv2GeoJson().run()