diff --git a/scripts/importscripts/zhv.ts b/scripts/importscripts/zhv.ts new file mode 100644 index 000000000..e608196bc --- /dev/null +++ b/scripts/importscripts/zhv.ts @@ -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 { + 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 = { + 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()