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")
}

View file

@ -29,9 +29,9 @@ async function main(args: string[]) {
let i = 0;
let failed = 0
let createdFiles : string [] = []
if(!perPostalCode){
let createdFiles: string [] = []
if (!perPostalCode) {
fs.writeFileSync(outputFile, "")
}
// @ts-ignore
@ -63,21 +63,21 @@ async function main(args: string[]) {
let targetFile = outputFile
if (perPostalCode) {
if(parsed["postcode"] === ""){
if (parsed["postcode"] === "") {
continue
}
if(isNaN(Number(parsed["postcode"]))){
if (isNaN(Number(parsed["postcode"]))) {
continue;
}
targetFile = outputFile + "-" + parsed["postcode"] + ".geojson"
let isFirst = false
if(!existsSync(targetFile)){
if (!existsSync(targetFile)) {
writeFileSync(targetFile, '{ "type":"FeatureCollection", "features":[')
createdFiles.push(targetFile)
isFirst = true
}
if(!isFirst){
if (!isFirst) {
fs.appendFileSync(targetFile, ",\n")
}
@ -89,20 +89,20 @@ async function main(args: string[]) {
coordinates: [lon, lat]
}
}))
}else{
fs.appendFileSync(outputFile, JSON.stringify({
type: "Feature",
properties: parsed,
geometry: {
type: "Point",
coordinates: [lon, lat]
}
}) + "\n")
} else {
fs.appendFileSync(outputFile, JSON.stringify({
type: "Feature",
properties: parsed,
geometry: {
type: "Point",
coordinates: [lon, lat]
}
}) + "\n")
}
}
@ -112,7 +112,7 @@ async function main(args: string[]) {
ScriptUtils.erasableLog("Closing ", createdFile, "and creating convex hull")
fs.appendFileSync(createdFile, "]}")
}
console.log("Done! Converted ", i, "features (of which ", failed, "features don't have a coordinate)")
}