mapcomplete/scripts/velopark/compare.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

121 lines
4.3 KiB
TypeScript
Raw Normal View History

2024-02-15 01:07:50 +01:00
import Script from "../Script"
import fs from "fs"
import { Feature, FeatureCollection } from "geojson"
import { GeoOperations } from "../../src/Logic/GeoOperations"
// vite-node scripts/velopark/compare.ts -- velopark_all.geojson osm_with_velopark_link_.geojson
2024-02-15 01:07:50 +01:00
class Compare extends Script {
2024-02-20 13:33:38 +01:00
compare(
veloId: string,
osmParking: Feature,
veloParking: Feature
): {
distance: number
ref: string
osmid: string
diffs: {
osm: string
velopark: string
key: string
}[]
} {
2024-02-15 01:07:50 +01:00
const osmCenterpoint = GeoOperations.centerpointCoordinates(osmParking)
const veloparkCenterpoint = GeoOperations.centerpointCoordinates(veloParking)
2024-02-20 13:33:38 +01:00
const distance = Math.round(
GeoOperations.distanceBetween(osmCenterpoint, veloparkCenterpoint)
)
const diffs: { osm: string; velopark: string; key: string }[] = []
2024-02-15 01:07:50 +01:00
2024-02-20 13:33:38 +01:00
const allKeys = new Set<string>(
Object.keys(osmParking.properties).concat(Object.keys(veloParking.properties))
)
2024-02-15 01:07:50 +01:00
for (const key of allKeys) {
2024-04-13 02:40:21 +02:00
if (["name", "numberOfLevels"].indexOf(key) >= 0) {
continue // We don't care about these tags
}
2024-02-20 13:33:38 +01:00
if (osmParking.properties[key] === veloParking.properties[key]) {
2024-02-15 01:07:50 +01:00
continue
}
2024-02-20 13:33:38 +01:00
if (Number(osmParking.properties[key]) === veloParking.properties[key]) {
2024-02-15 01:07:50 +01:00
continue
}
2024-02-20 13:33:38 +01:00
if (veloParking.properties[key] === undefined) {
2024-02-15 01:07:50 +01:00
continue
}
diffs.push({
key,
osm: osmParking.properties[key],
2024-04-13 02:40:21 +02:00
velopark: veloParking.properties[key],
2024-02-15 01:07:50 +01:00
})
}
2024-04-13 02:40:21 +02:00
let osmid =
osmParking.properties["@id"] ??
osmParking["id"] /*Not in the properties, that is how overpass returns it*/
if (!osmid.startsWith("http")) {
osmid = "https://openstreetmap.org/" + osmid
}
2024-02-15 01:07:50 +01:00
return {
ref: veloId,
osmid,
2024-02-20 13:33:38 +01:00
distance,
2024-04-13 02:40:21 +02:00
diffs,
2024-02-15 01:07:50 +01:00
}
}
2024-02-15 01:07:50 +01:00
async main(args: string[]): Promise<void> {
let [velopark, osm, key] = args
if(velopark === undefined || osm === undefined){
console.log("Needed argument: velopark.geojson osm.geojson [key]\nThe key is optional and will be `ref:velopark` by default\nUse overpass to get a geojson with ref:velopark")
return
}
2024-02-15 01:07:50 +01:00
key ??= "ref:velopark"
const veloparkData: FeatureCollection = JSON.parse(fs.readFileSync(velopark, "utf-8"))
2024-02-20 13:33:38 +01:00
const osmData: FeatureCollection = JSON.parse(fs.readFileSync(osm, "utf-8"))
2024-02-15 01:07:50 +01:00
2024-02-20 13:33:38 +01:00
const veloparkById: Record<string, Feature> = {}
2024-02-15 01:07:50 +01:00
for (const parking of veloparkData.features) {
veloparkById[parking.properties[key] ?? parking.properties.url] = parking
2024-02-15 01:07:50 +01:00
}
const diffs = []
for (const parking of osmData.features) {
const veloId = parking.properties[key]
const veloparking = veloparkById[veloId]
2024-02-20 13:33:38 +01:00
if (veloparking === undefined) {
2024-02-15 01:07:50 +01:00
console.error("No velopark entry found for", veloId)
continue
}
console.log("Veloparking is", veloparking)
2024-02-20 13:33:38 +01:00
diffs.push(this.compare(veloId, parking, veloparking))
2024-02-15 01:07:50 +01:00
}
2024-04-13 02:40:21 +02:00
console.log(
"Found ",
diffs.length,
" items with differences between OSM and the provided data"
)
2024-04-13 02:40:21 +02:00
const maxDistance = Math.max(...diffs.map((d) => d.distance))
const distanceBins = []
const binSize = 5
2024-04-13 02:40:21 +02:00
for (let i = 0; i < Math.ceil(maxDistance / binSize); i++) {
distanceBins.push(0)
}
for (const diff of diffs) {
const bin = Math.floor(diff.distance / binSize)
distanceBins[bin] += 1
}
fs.writeFileSync("report_diff.json", JSON.stringify({ diffs, distanceBins }, null, " "))
console.log("Written report_diff.json")
2024-02-15 01:07:50 +01:00
}
2024-02-15 01:07:50 +01:00
constructor() {
2024-02-20 13:33:38 +01:00
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"
)
2024-02-15 01:07:50 +01:00
}
}
new Compare().run()