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"
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 {
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-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-02-20 13:33:38 +01:00
velopark : veloParking.properties [ key ] ,
2024-02-15 01:07:50 +01:00
} )
}
return {
ref : veloId ,
osmid : osmParking.properties [ "@id" ] ,
2024-02-20 13:33:38 +01:00
distance ,
diffs ,
2024-02-15 01:07:50 +01:00
}
}
async main ( args : string [ ] ) : Promise < void > {
let [ velopark , osm , key ] = args
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
}
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
}
2024-02-20 13:33:38 +01:00
diffs . push ( this . compare ( veloId , parking , veloparking ) )
2024-02-15 01:07:50 +01:00
}
2024-02-20 13:33:38 +01:00
fs . writeFileSync ( "report_diff.json" , JSON . stringify ( diffs ) )
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 ( )