forked from MapComplete/MapComplete
Add conversion script for ZHV
This commit is contained in:
parent
124376470a
commit
b3bea7503a
1 changed files with 47 additions and 0 deletions
47
scripts/importscripts/zhv.ts
Normal file
47
scripts/importscripts/zhv.ts
Normal 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()
|
Loading…
Add table
Add a link
Reference in a new issue