Reformat all files with prettier

This commit is contained in:
Pieter Vander Vennet 2022-09-08 21:40:48 +02:00
parent e22d189376
commit b541d3eab4
382 changed files with 50893 additions and 35566 deletions

View file

@ -1,11 +1,10 @@
import {appendFileSync, existsSync, readFileSync, writeFileSync} from "fs";
import {GeoOperations} from "../../Logic/GeoOperations";
import ScriptUtils from "../ScriptUtils";
import {Utils} from "../../Utils";
import { appendFileSync, existsSync, readFileSync, writeFileSync } from "fs"
import { GeoOperations } from "../../Logic/GeoOperations"
import ScriptUtils from "../ScriptUtils"
import { Utils } from "../../Utils"
async function main(args: string[]) {
ScriptUtils.fixUtils()
ScriptUtils.fixUtils()
const pointCandidates = JSON.parse(readFileSync(args[0], "utf8"))
const postcodes = JSON.parse(readFileSync(args[1], "utf8"))
const output = args[2] ?? "centralCoordinates.csv"
@ -16,7 +15,7 @@ ScriptUtils.fixUtils()
if (existsSync(output)) {
const lines = readFileSync(output, "UTF8").split("\n")
lines.shift()
lines.forEach(line => {
lines.forEach((line) => {
const postalCode = Number(line.split(",")[0])
alreadyLoaded.add(postalCode)
})
@ -35,7 +34,6 @@ ScriptUtils.fixUtils()
} else {
perPostCode.set(postcode, [boundary])
}
}
for (const postcode of Array.from(perPostCode.keys())) {
@ -48,45 +46,68 @@ ScriptUtils.fixUtils()
continue
}
candidates.push(candidate.geometry.coordinates)
}
}
if (candidates.length === 0) {
console.log("Postcode ", postcode, "has", candidates.length, "candidates, using centerpoint instead")
candidates.push(...boundaries.map(boundary => GeoOperations.centerpointCoordinates(boundary)))
console.log(
"Postcode ",
postcode,
"has",
candidates.length,
"candidates, using centerpoint instead"
)
candidates.push(
...boundaries.map((boundary) => GeoOperations.centerpointCoordinates(boundary))
)
}
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 result = await Utils.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) {
console.error("No result or could not calculate a route")
}
})
)
)
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 result = await Utils.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) {
console.error("No result or could not calculate a route")
}
})))
const centers = boundaries.map(b => GeoOperations.centerpointCoordinates(b))
const centers = boundaries.map((b) => GeoOperations.centerpointCoordinates(b))
const center = GeoOperations.centerpointCoordinates({
type: "Feature",
geometry: {
type: "LineString",
coordinates: centers
}
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))
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")
}
}
let args = [...process.argv]
args.splice(0, 2)
main(args).then(_ => console.log("Done!"))
main(args).then((_) => console.log("Done!"))

View file

@ -1,49 +1,70 @@
import * as fs from "fs";
import {existsSync, writeFileSync} from "fs";
import * as readline from "readline";
import ScriptUtils from "../ScriptUtils";
import * as fs from "fs"
import { existsSync, writeFileSync } from "fs"
import * as readline from "readline"
import ScriptUtils from "../ScriptUtils"
/**
* Converts an open-address CSV file into a big geojson file
*/
async function main(args: string[]) {
const inputFile = args[0]
const outputFile = args[1]
const fileStream = fs.createReadStream(inputFile);
const fileStream = fs.createReadStream(inputFile)
const perPostalCode = args[2] == "--per-postal-code"
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity
});
crlfDelay: Infinity,
})
// Note: we use the crlfDelay option to recognize all instances of CR LF
// ('\r\n') in input.txt as a single line break.
const fields = [
"EPSG:31370_x", "EPSG:31370_y", "EPSG:4326_lat", "EPSG:4326_lon",
"address_id", "box_number",
"house_number", "municipality_id", "municipality_name_de", "municipality_name_fr", "municipality_name_nl", "postcode", "postname_fr",
"postname_nl", "street_id", "streetname_de", "streetname_fr", "streetname_nl", "region_code", "status"
"EPSG:31370_x",
"EPSG:31370_y",
"EPSG:4326_lat",
"EPSG:4326_lon",
"address_id",
"box_number",
"house_number",
"municipality_id",
"municipality_name_de",
"municipality_name_fr",
"municipality_name_nl",
"postcode",
"postname_fr",
"postname_nl",
"street_id",
"streetname_de",
"streetname_fr",
"streetname_nl",
"region_code",
"status",
]
let i = 0;
let i = 0
let failed = 0
let createdFiles: string [] = []
let createdFiles: string[] = []
if (!perPostalCode) {
fs.writeFileSync(outputFile, "")
}
// @ts-ignore
for await (const line of rl) {
i++;
i++
if (i % 10000 == 0) {
ScriptUtils.erasableLog("Converted ", i, "features (of which ", failed, "features don't have a coordinate)")
ScriptUtils.erasableLog(
"Converted ",
i,
"features (of which ",
failed,
"features don't have a coordinate)"
)
}
const data = line.split(",")
const parsed: any = {}
for (let i = 0; i < fields.length; i++) {
const field = fields[i];
const field = fields[i]
parsed[field] = data[i]
}
const lat = Number(parsed["EPSG:4326_lat"])
@ -67,7 +88,7 @@ async function main(args: string[]) {
continue
}
if (isNaN(Number(parsed["postcode"]))) {
continue;
continue
}
targetFile = outputFile + "-" + parsed["postcode"] + ".geojson"
let isFirst = false
@ -81,29 +102,30 @@ async function main(args: string[]) {
fs.appendFileSync(targetFile, ",\n")
}
fs.appendFileSync(targetFile, JSON.stringify({
type: "Feature",
properties: parsed,
geometry: {
type: "Point",
coordinates: [lon, lat]
}
}))
fs.appendFileSync(
targetFile,
JSON.stringify({
type: "Feature",
properties: parsed,
geometry: {
type: "Point",
coordinates: [lon, lat],
},
})
)
} else {
fs.appendFileSync(outputFile, JSON.stringify({
type: "Feature",
properties: parsed,
geometry: {
type: "Point",
coordinates: [lon, lat]
}
}) + "\n")
fs.appendFileSync(
outputFile,
JSON.stringify({
type: "Feature",
properties: parsed,
geometry: {
type: "Point",
coordinates: [lon, lat],
},
}) + "\n"
)
}
}
console.log("Closing files...")
@ -113,8 +135,13 @@ async function main(args: string[]) {
fs.appendFileSync(createdFile, "]}")
}
console.log("Done! Converted ", i, "features (of which ", failed, "features don't have a coordinate)")
console.log(
"Done! Converted ",
i,
"features (of which ",
failed,
"features don't have a coordinate)"
)
}
let args = [...process.argv]
@ -123,5 +150,5 @@ args.splice(0, 2)
if (args.length == 0) {
console.log("USAGE: input-csv-file output.newline-delimited-geojson.json [--per-postal-code]")
} else {
main(args).catch(e => console.error(e))
main(args).catch((e) => console.error(e))
}

View file

@ -1,22 +1,22 @@
import * as fs from "fs";
import {writeFileSync} from "fs";
import ScriptUtils from "../ScriptUtils";
import * as fs from "fs"
import { writeFileSync } from "fs"
import ScriptUtils from "../ScriptUtils"
function handleFile(file: string, postalCode: number) {
const geojson = JSON.parse(fs.readFileSync(file, "UTF8"))
geojson.properties = {
type: "boundary",
"boundary": "postal_code",
"postal_code": postalCode + ""
boundary: "postal_code",
postal_code: postalCode + "",
}
return geojson
}
function getKnownPostalCodes(): number[] {
return fs.readFileSync("./scripts/postal_code_tools/knownPostalCodes.csv", "UTF8").split("\n")
.map(line => Number(line.split(",")[1]))
return fs
.readFileSync("./scripts/postal_code_tools/knownPostalCodes.csv", "UTF8")
.split("\n")
.map((line) => Number(line.split(",")[1]))
}
function main(args: string[]) {
@ -28,27 +28,36 @@ function main(args: string[]) {
for (const file of files) {
const nameParts = file.split("-")
const postalCodeStr = nameParts[nameParts.length - 1]
const postalCode = Number(postalCodeStr.substr(0, postalCodeStr.length - ".geojson.convex.geojson".length))
const postalCode = Number(
postalCodeStr.substr(0, postalCodeStr.length - ".geojson.convex.geojson".length)
)
if (isNaN(postalCode)) {
console.error("Not a number: ", postalCodeStr)
continue
}
if (knownPostals.has(postalCode)) {
skipped.push(postalCode)
ScriptUtils.erasableLog("Skipping boundary for ", postalCode, "as it is already known - skipped ", skipped.length, "already")
ScriptUtils.erasableLog(
"Skipping boundary for ",
postalCode,
"as it is already known - skipped ",
skipped.length,
"already"
)
continue
}
allFiles.push(handleFile(file, postalCode))
}
writeFileSync("all_postal_codes_filtered.geojson", JSON.stringify({
type: "FeatureCollection",
features: allFiles
}))
writeFileSync(
"all_postal_codes_filtered.geojson",
JSON.stringify({
type: "FeatureCollection",
features: allFiles,
})
)
}
let args = [...process.argv]
args.splice(0, 2)
main(args)
main(args)