Full code cleanup

This commit is contained in:
Pieter Vander Vennet 2022-01-26 21:40:38 +01:00
parent 3a4a2a2016
commit fa971ffbbf
300 changed files with 16352 additions and 19284 deletions

View file

@ -11,22 +11,22 @@ async function main(args: string[]) {
const output = args[2] ?? "centralCoordinates.csv"
const perPostCode = new Map<string, any[]>()
const alreadyLoaded = new Set<number>()
if(existsSync(output)){
const lines = readFileSync(output, "UTF8").split("\n")
if (existsSync(output)) {
const lines = readFileSync(output, "UTF8").split("\n")
lines.shift()
lines.forEach(line => {
const postalCode = Number( line.split(",")[0])
const postalCode = Number(line.split(",")[0])
alreadyLoaded.add(postalCode)
})
}else{
writeFileSync(output,"postal_code,lon,lat\n","UTF-8")
} else {
writeFileSync(output, "postal_code,lon,lat\n", "UTF-8")
}
for (const boundary of postcodes.features) {
const postcode = boundary.properties.nouveau_PO
if(alreadyLoaded.has(Number(postcode))){
if (alreadyLoaded.has(Number(postcode))) {
console.log("Skipping ", postcode, "as already loaded")
continue
}
@ -58,29 +58,29 @@ async function main(args: string[]) {
const url = "https://staging.anyways.eu/routing-api/v1/routes?access_token=postal_code_script&turn_by_turn=false&format=geojson&language=en"
const depPoints :[number,number][] = Utils.NoNull( await Promise.all(candidates.map(async candidate => {
try{
const depPoints: [number, number][] = Utils.NoNull(await Promise.all(candidates.map(async candidate => {
try {
const result = await ScriptUtils.DownloadJSON(url + "&loc=" + candidate.join("%2C") + "&loc=3.22000%2C51.21577&profile=car.short")
const depPoint = result.features.filter(f => f.geometry.type === "LineString")[0].geometry.coordinates[0]
return <[number,number]>[depPoint[0], depPoint[1]] // Drop elevation
}catch(e){
return <[number, number]>[depPoint[0], depPoint[1]] // Drop elevation
} catch (e) {
console.error("No result or could not calculate a route")
}
})))
const centers = boundaries.map(b => GeoOperations.centerpointCoordinates(b))
const center = GeoOperations.centerpointCoordinates({
type:"Feature",
geometry:{
type:"LineString",
type: "Feature",
geometry: {
type: "LineString",
coordinates: centers
}
})
depPoints.sort((c0, c1) => GeoOperations.distanceBetween(c0, center) - GeoOperations.distanceBetween(c1, center))
console.log("Sorted departure point candidates for ",postcode," are ", JSON.stringify(depPoints))
appendFileSync(output,[postcode, ...depPoints[0]].join(", ")+"\n","UTF-8")
console.log("Sorted departure point candidates for ", postcode, " are ", JSON.stringify(depPoints))
appendFileSync(output, [postcode, ...depPoints[0]].join(", ") + "\n", "UTF-8")
}