From 8be41571fa1371494bbbb977f5a6ad321ec16d1c Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Thu, 15 Feb 2024 01:07:50 +0100 Subject: [PATCH] Velopark: add report generating script --- scripts/velopark/compare.ts | 72 +++++++++++++++++++++++++++ scripts/velopark/veloParkToGeojson.ts | 29 +++++++---- 2 files changed, 90 insertions(+), 11 deletions(-) create mode 100644 scripts/velopark/compare.ts diff --git a/scripts/velopark/compare.ts b/scripts/velopark/compare.ts new file mode 100644 index 000000000..4bab31dc4 --- /dev/null +++ b/scripts/velopark/compare.ts @@ -0,0 +1,72 @@ +import Script from "../Script" +import fs from "fs" +import { Feature, FeatureCollection } from "geojson" +import { GeoOperations } from "../../src/Logic/GeoOperations" +import * as os from "os" +// vite-node scripts/velopark/compare.ts -- scripts/velopark/velopark_all_2024-02-14T12\:18\:41.772Z.geojson ~/Projecten/OSM/Fietsberaad/2024-02-02\ Fietsenstallingen_OSM_met_velopark_ref.geojson +class Compare extends Script { + + compare(veloId: string, osmParking: Feature, veloParking: Feature): {distance: number, ref: string, osmid: string, diffs: { + osm: string, velopark: string, key: string + }[] }{ + const osmCenterpoint = GeoOperations.centerpointCoordinates(osmParking) + const veloparkCenterpoint = GeoOperations.centerpointCoordinates(veloParking) + const distance = Math.round(GeoOperations.distanceBetween(osmCenterpoint, veloparkCenterpoint)) + const diffs: { osm: string, velopark: string, key: string}[] = [] + + const allKeys = new Set(Object.keys(osmParking.properties).concat(Object.keys(veloParking.properties))) + for (const key of allKeys) { + if(osmParking.properties[key] === veloParking.properties[key]){ + continue + } + if(Number(osmParking.properties[key]) === veloParking.properties[key]){ + continue + } + if(veloParking.properties[key] === undefined){ + continue + } + diffs.push({ + key, + osm: osmParking.properties[key], + velopark: veloParking.properties[key] + }) + } + return { + ref: veloId, + osmid: osmParking.properties["@id"], + distance, diffs + } + } + async main(args: string[]): Promise { + let [velopark, osm, key] = args + key ??= "ref:velopark" + const veloparkData: FeatureCollection = JSON.parse(fs.readFileSync(velopark, "utf-8")) + const osmData : FeatureCollection = JSON.parse(fs.readFileSync(osm, "utf-8")) + + const veloparkById : Record = {} + for (const parking of veloparkData.features) { + veloparkById[parking.properties[key]] = parking + } + + const diffs = [] + for (const parking of osmData.features) { + const veloId = parking.properties[key] + const veloparking = veloparkById[veloId] + if(veloparking === undefined){ + console.error("No velopark entry found for", veloId) + continue + } + diffs.push(this.compare(veloId, parking, veloparking)) + } + + fs.writeFileSync("report_diff.json",JSON.stringify(diffs)) + + + } + constructor() { + super("Compares a velopark geojson with OSM geojson. Usage: `compare velopark.geojson osm.geojson [key-to-compare-on]`. If key-to-compare-on is not given, `ref:velopark` will be used") + } + +} + +new Compare().run() diff --git a/scripts/velopark/veloParkToGeojson.ts b/scripts/velopark/veloParkToGeojson.ts index 0157e9b1f..dcd200a32 100644 --- a/scripts/velopark/veloParkToGeojson.ts +++ b/scripts/velopark/veloParkToGeojson.ts @@ -15,6 +15,20 @@ class VeloParkToGeojson extends Script { ) } + exportTo(filename: string, features){ + fs.writeFileSync( + filename+"_" + new Date().toISOString() + ".geojson", + JSON.stringify( + { + type: "FeatureCollection", + features, + }, + null, + " " + ) + ) + } + async main(args: string[]): Promise { console.log("Downloading velopark data") // Download data for NIS-code 1000. 1000 means: all of belgium @@ -38,12 +52,15 @@ class VeloParkToGeojson extends Script { ) console.log("OpenStreetMap contains", seenIds.size, "bicycle parkings with a velopark ref") const allVelopark = data.map((f) => VeloparkLoader.convert(f)) + this.exportTo("velopark_all", allVelopark) + const features = allVelopark.filter((f) => !seenIds.has(f.properties["ref:velopark"])) const allProperties = new Set() for (const feature of features) { Object.keys(feature.properties).forEach((k) => allProperties.add(k)) } + this.exportTo("velopark_noncynced",features) allProperties.delete("ref:velopark") for (const feature of features) { allProperties.forEach((k) => { @@ -51,17 +68,7 @@ class VeloParkToGeojson extends Script { }) } - fs.writeFileSync( - "velopark_id_only_export_" + new Date().toISOString() + ".geojson", - JSON.stringify( - { - type: "FeatureCollection", - features, - }, - null, - " " - ) - ) + this.exportTo("velopark_nonsynced_id_only", features) } }