2024-06-14 01:01:41 +02:00
|
|
|
import Script from "../Script"
|
|
|
|
import { readFileSync, writeFileSync } from "fs"
|
|
|
|
import { OsmId } from "../../src/Models/OsmFeature"
|
|
|
|
import { Utils } from "../../src/Utils"
|
|
|
|
|
|
|
|
interface DiffItem {
|
|
|
|
/**
|
|
|
|
* Velopark-id
|
|
|
|
*/
|
2024-06-16 16:06:26 +02:00
|
|
|
ref: string
|
|
|
|
osmid: OsmId
|
|
|
|
distance: number
|
|
|
|
diffs: {
|
|
|
|
key: string
|
2024-06-14 01:01:41 +02:00
|
|
|
/**
|
|
|
|
* The value in OpenStreetMap
|
|
|
|
* Might be undefined if OSM doesn't have an appropriate value
|
|
|
|
*/
|
2024-06-16 16:06:26 +02:00
|
|
|
osm?: string
|
|
|
|
velopark: string | number
|
|
|
|
}[]
|
2024-06-14 01:01:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export class DiffToCsv extends Script {
|
|
|
|
constructor() {
|
2024-06-16 16:06:26 +02:00
|
|
|
super(
|
|
|
|
"Converts a 'report.diff' to a CSV file for people who prefer LibreOffice Calc (or other Spreadsheet Software)"
|
|
|
|
)
|
2024-06-14 01:01:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async main(args: string[]): Promise<void> {
|
|
|
|
const file = args[0] ?? "report_diff.json"
|
2024-06-16 16:06:26 +02:00
|
|
|
const json = <{ diffs: DiffItem[]; distanceBinds: number[] }>(
|
|
|
|
JSON.parse(readFileSync(file, "utf8"))
|
|
|
|
)
|
2024-06-14 01:01:41 +02:00
|
|
|
const diffs = json.diffs
|
2024-06-16 16:06:26 +02:00
|
|
|
const allKeys = Utils.Dedup(diffs.flatMap((item) => item.diffs.map((d) => d.key)))
|
2024-06-14 01:01:41 +02:00
|
|
|
allKeys.sort()
|
|
|
|
|
2024-06-16 16:06:26 +02:00
|
|
|
const header = [
|
|
|
|
"osm_id",
|
|
|
|
"velopark_id",
|
|
|
|
"distance",
|
|
|
|
...allKeys.flatMap((k) => ["osm:" + k, "velopark:" + k]),
|
|
|
|
]
|
2024-06-14 01:01:41 +02:00
|
|
|
const lines = [header]
|
|
|
|
for (const diffItem of diffs) {
|
|
|
|
const line = []
|
|
|
|
lines.push(line)
|
|
|
|
line.push(diffItem.osmid)
|
|
|
|
line.push(diffItem.ref)
|
|
|
|
line.push(diffItem.distance)
|
|
|
|
|
|
|
|
const d = diffItem.diffs
|
|
|
|
for (const k of allKeys) {
|
2024-06-16 16:06:26 +02:00
|
|
|
const found = d.find((i) => i.key === k)
|
|
|
|
if (!found) {
|
|
|
|
line.push("", "")
|
2024-06-14 01:01:41 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
line.push(found.osm, found.velopark)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const path = "report_diff.csv"
|
2024-06-16 16:06:26 +02:00
|
|
|
writeFileSync(path, lines.map((l) => l.join(",")).join("\n"), "utf8")
|
2024-06-14 01:01:41 +02:00
|
|
|
console.log("Written", path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
new DiffToCsv().run()
|