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

@ -9,4 +9,4 @@ const DESTINATION = args[1];
console.log(`Downloading ${FILE_URL} to ${DESTINATION}`)
const file = fs.createWriteStream(DESTINATION);
http.get(FILE_URL, response=>response.pipe(file));
http.get(FILE_URL, response => response.pipe(file));

View file

@ -3,7 +3,7 @@ import {OH} from "../UI/OpeningHours/OpeningHours";
function extractValue(vs: { __value }[]) {
if(vs === undefined){
if (vs === undefined) {
return undefined
}
for (const v of vs) {
@ -16,33 +16,34 @@ function extractValue(vs: { __value }[]) {
}
function extract_oh_block (days) : string{
function extract_oh_block(days): string {
const oh = []
for (const day of days.day) {
const abbr = day.name.substr(0,2)
const abbr = day.name.substr(0, 2)
const block = day.time_block[0]
const from = block.time_from.substr(0,5)
const to = block.time_until.substr(0,5)
const from = block.time_from.substr(0, 5)
const to = block.time_until.substr(0, 5)
const by_appointment = block.by_appointment ? " \"by appointment\"" : ""
oh.push(`${abbr} ${from}-${to}${by_appointment}`)
}
return oh.join("; ")
}
function extract_oh(opening_periods){
function extract_oh(opening_periods) {
const rules = []
if(opening_periods === undefined){
if (opening_periods === undefined) {
return undefined;
}
for (const openingPeriod of opening_periods.opening_period ?? []) {
let rule = extract_oh_block(openingPeriod.days)
if(openingPeriod.name.toLowerCase().indexOf("schoolvakantie") >= 0){
rule = "SH "+rule
if (openingPeriod.name.toLowerCase().indexOf("schoolvakantie") >= 0) {
rule = "SH " + rule
}
rules.push(rule)
}
return OH.simplify( rules.join(";"))
return OH.simplify(rules.join(";"))
}
function rewrite(obj, key) {
if (obj[key] === undefined) {
return
@ -109,12 +110,12 @@ for (const item of data) {
metadata["addr:housenumber"] = addr.number
metadata["phone"] = item.contact_info["telephone"] ?? item.contact_info["mobile"]
metadata["email"] = item.contact_info["email_address"]
const links = item.links?.link?.map(l => l.url) ?? []
metadata["website"] = item.contact_info["website"] ?? links[0]
delete item["links"]
delete item.location_info
delete item.contact_info
delete item.promotional_info
@ -136,25 +137,25 @@ for (const item of data) {
metadata.fee = "yes"
metadata.charge = extractValue(item.price_info?.extra_information?.value)
const methods = item.price_info?.payment_methods?.payment_method
if(methods !== undefined){
if (methods !== undefined) {
methods.map(v => extractValue(v.value)).forEach(method => {
metadata["payment:" + method.toLowerCase()] = "yes"
})
}
delete item.price_info
}else if(item.price_info?.prices?.length === 0){
} else if (item.price_info?.prices?.length === 0) {
delete item.price_info
}
try{
if(item.labels_info?.labels_own?.label[0]?.code === "Billenkar"){
metadata.rental = "quadricycle"
delete item.labels_info
}
}catch(e){
try {
if (item.labels_info?.labels_own?.label[0]?.code === "Billenkar") {
metadata.rental = "quadricycle"
delete item.labels_info
}
} catch (e) {
}
delete item["publishing_channels"]
@ -166,22 +167,20 @@ for (const item of data) {
}
delete item.media
const time_info = item.time_info?.time_info_regular
if(time_info?.permantly_open === true){
if (time_info?.permantly_open === true) {
metadata.opening_hours = "24/7"
}else{
} else {
metadata.opening_hours = extract_oh(time_info?.opening_periods)
}
delete item.time_info
const properties = {}
for (const key in metadata) {
const v = metadata[key]
if(v === null || v === undefined || v === ""){
if (v === null || v === undefined || v === "") {
delete metadata[key]
continue
}

View file

@ -376,29 +376,28 @@ async function main(args: string[]) {
const lat1 = Number(args[5])
const lon1 = Number(args[6])
if(isNaN(lat0)){
if (isNaN(lat0)) {
throw "The first number (a latitude) is not a valid number"
}
if(isNaN(lon0)){
if (isNaN(lon0)) {
throw "The second number (a longitude) is not a valid number"
}
if(isNaN(lat1)){
if (isNaN(lat1)) {
throw "The third number (a latitude) is not a valid number"
}
if(isNaN(lon1)){
if (isNaN(lon1)) {
throw "The first number (a longitude) is not a valid number"
}
const tileRange = Tiles.TileRangeBetween(zoomlevel, lat0, lon0, lat1, lon1)
if(isNaN(tileRange.total)){
if (isNaN(tileRange.total)) {
throw "Something has gone wrong: tilerange is NAN"
}
if (tileRange.total === 0) {
console.log("Tilerange has zero tiles - this is probably an error")
return

View file

@ -33,7 +33,7 @@ function WriteFile(filename, html: BaseUIElement, autogenSource: string[]): void
]).AsMarkdown()
md.replace(/\n\n\n+/g, "\n\n");
writeFileSync(filename, md);
}

View file

@ -32,10 +32,10 @@ function genImages(dryrun = false) {
}
let rawName = name;
if(dryrun){
if (dryrun) {
rawName = "add";
}
module += ` public static ${name} = "${svg}"\n`
module += ` public static ${name}_img = Img.AsImageElement(Svg.${rawName})\n`
module += ` public static ${name}_svg() { return new Img(Svg.${rawName}, true);}\n`

View file

@ -4,10 +4,7 @@ import * as licenses from "../assets/generated/license_info.json"
import {LayoutConfigJson} from "../Models/ThemeConfig/Json/LayoutConfigJson";
import {LayerConfigJson} from "../Models/ThemeConfig/Json/LayerConfigJson";
import Constants from "../Models/Constants";
import {
ValidateLayer,
ValidateThemeAndLayers
} from "../Models/ThemeConfig/Conversion/LegacyJsonConvert";
import {ValidateLayer, ValidateThemeAndLayers} from "../Models/ThemeConfig/Conversion/LegacyJsonConvert";
import {Translation} from "../UI/i18n/Translation";
import {TagRenderingConfigJson} from "../Models/ThemeConfig/Json/TagRenderingConfigJson";
import * as questions from "../assets/tagRenderings/questions.json";
@ -72,14 +69,14 @@ class LayerOverviewUtils {
const dict = new Map<string, TagRenderingConfigJson>();
for (const key in questions["default"]) {
if(key === "id"){
if (key === "id") {
continue
}
questions[key].id = key;
dict.set(key, <TagRenderingConfigJson>questions[key])
}
for (const key in icons["default"]) {
if(key==="id"){
if (key === "id") {
continue
}
if (typeof icons[key] !== "object") {
@ -90,7 +87,7 @@ class LayerOverviewUtils {
}
dict.forEach((value, key) => {
if(key === "id"){
if (key === "id") {
return
}
value.id = value.id ?? key;

View file

@ -105,7 +105,7 @@ async function createManifest(layout: LayoutConfig) {
description: ogDescr,
orientation: "portrait-primary, landscape-primary",
icons: icons,
categories: ["map","navigation"]
categories: ["map", "navigation"]
};
}
@ -180,8 +180,8 @@ async function createLandingPage(layout: LayoutConfig, manifest) {
return output;
}
async function createIndexFor(theme: LayoutConfig){
const filename = "index_"+theme.id+".ts"
async function createIndexFor(theme: LayoutConfig) {
const filename = "index_" + theme.id + ".ts"
writeFileSync(filename, `import * as themeConfig from "./assets/generated/themes/${theme.id}.json"\n`)
appendFileSync(filename, codeTemplate)
}
@ -191,7 +191,7 @@ if (!existsSync(generatedDir)) {
mkdirSync(generatedDir)
}
const blacklist = ["", "test", ".", "..", "manifest", "index", "land", "preferences", "account", "openstreetmap", "custom","theme"]
const blacklist = ["", "test", ".", "..", "manifest", "index", "land", "preferences", "account", "openstreetmap", "custom", "theme"]
// @ts-ignore
const all: LayoutConfigJson[] = all_known_layouts.themes;
for (const i in all) {

View file

@ -99,7 +99,6 @@ knownLicenses.set("twemoji", {
})
function promptLicenseFor(path): SmallLicense {
console.log("License abbreviations:")
knownLicenses.forEach((value, key) => {

View file

@ -19,23 +19,23 @@ function main(args: string[]) {
continue
}
const z = match[1]
if(zoomLevel === undefined){
if (zoomLevel === undefined) {
zoomLevel = z
}else if(zoomLevel !== z){
} else if (zoomLevel !== z) {
throw "Mixed zoomlevels detected"
}
const x = match[2]
const y = match[3]
if(indices[x] === undefined){
if (indices[x] === undefined) {
indices[x] = []
}
indices[x].push(Number(y))
}
indices["zoom"] = zoomLevel;
const match = files[0].match("\(.*\)_\([0-9]*\)_\([0-9]*\)_\([0-9]*\).geojson")
const path = match[1]+"_"+zoomLevel+"_overview.json"
writeFileSync( path, JSON.stringify(indices))
const path = match[1] + "_" + zoomLevel + "_overview.json"
writeFileSync(path, JSON.stringify(indices))
console.log("Written overview for", files.length, " tiles at", path)
}

View file

@ -208,14 +208,14 @@ function compileTranslationsFromWeblate() {
const allTranslations = new TranslationPart()
for (const translationFile of translations) {
try{
const contents = JSON.parse(readFileSync(translationFile, "utf-8"));
let language = translationFile.substring(translationFile.lastIndexOf("/") + 1)
language = language.substring(0, language.length - 5)
allTranslations.add(language, contents)
}catch (e) {
throw "Could not read file "+ translationFile+" due to "+e
try {
const contents = JSON.parse(readFileSync(translationFile, "utf-8"));
let language = translationFile.substring(translationFile.lastIndexOf("/") + 1)
language = language.substring(0, language.length - 5)
allTranslations.add(language, contents)
} catch (e) {
throw "Could not read file " + translationFile + " due to " + e
}
}

View file

@ -2,17 +2,17 @@ import {writeFile} from "fs";
import Translations from "../UI/i18n/Translations";
import * as themeOverview from "../assets/generated/theme_overview.json"
function generateWikiEntry(layout: {hideFromOverview: boolean, id: string, shortDescription: any}) {
function generateWikiEntry(layout: { hideFromOverview: boolean, id: string, shortDescription: any }) {
if (layout.hideFromOverview) {
return "";
}
const languagesInDescr = []
for (const shortDescriptionKey in layout.shortDescription) {
languagesInDescr.push(shortDescriptionKey)
}
const languages = languagesInDescr .map(ln => `{{#language:${ln}|en}}`).join(", ")
const languages = languagesInDescr.map(ln => `{{#language:${ln}|en}}`).join(", ")
let auth = "Yes";
return `{{service_item
|name= [https://mapcomplete.osm.be/${layout.id} ${layout.id}]

View file

@ -10,8 +10,8 @@ import {FixLegacyTheme, UpdateLegacyLayer} from "../Models/ThemeConfig/LegacyJso
const layerFiles = ScriptUtils.getLayerFiles();
for (const layerFile of layerFiles) {
try {
const state : any = undefined; // FIXME
const fixed = new UpdateLegacyLayer().convertStrict(state,layerFile.parsed, "While linting "+layerFile.path);
const state: any = undefined; // FIXME
const fixed = new UpdateLegacyLayer().convertStrict(state, layerFile.parsed, "While linting " + layerFile.path);
writeFileSync(layerFile.path, JSON.stringify(fixed, null, " "))
} catch (e) {
console.error("COULD NOT LINT LAYER" + layerFile.path + ":\n\t" + e)
@ -21,8 +21,8 @@ for (const layerFile of layerFiles) {
const themeFiles = ScriptUtils.getThemeFiles()
for (const themeFile of themeFiles) {
try {
const state : any = undefined; // FIXME
const fixed = new FixLegacyTheme().convertStrict(state,themeFile.parsed, "While linting "+themeFile.path);
const state: any = undefined; // FIXME
const fixed = new FixLegacyTheme().convertStrict(state, themeFile.parsed, "While linting " + themeFile.path);
writeFileSync(themeFile.path, JSON.stringify(fixed, null, " "))
} catch (e) {
console.error("COULD NOT LINT THEME" + themeFile.path + ":\n\t" + e)

View file

@ -1,38 +1,38 @@
import * as fs from "fs";
function main(args){
if(args.length < 2){
function main(args) {
if (args.length < 2) {
console.log("Given a single geojson file, generates the partitions for every found property")
console.log("USAGE: perProperty `file.geojson` `property-key`")
return
}
const path = args[0]
const key = args[1]
const data= JSON.parse(fs.readFileSync(path, "UTF8"))
const data = JSON.parse(fs.readFileSync(path, "UTF8"))
const perProperty = new Map<string, any[]>()
console.log("Partitioning",data.features.length, "features")
console.log("Partitioning", data.features.length, "features")
for (const feature of data.features) {
const v = feature.properties[key]
if(!perProperty.has(v)){
if (!perProperty.has(v)) {
console.log("Found a new category:", v)
perProperty.set(v, [])
}
perProperty.get(v).push(feature)
}
const stripped = path.substr(0, path.length - ".geojson".length)
const stripped = path.substr(0, path.length - ".geojson".length)
perProperty.forEach((features, v) => {
fs.writeFileSync(stripped+"."+v.replace(/[^a-zA-Z0-9_]/g, "_")+".geojson",
fs.writeFileSync(stripped + "." + v.replace(/[^a-zA-Z0-9_]/g, "_") + ".geojson",
JSON.stringify({
type:"FeatureCollection",
type: "FeatureCollection",
features
}))
})
}
let args = [...process.argv]

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

View file

@ -6,7 +6,7 @@ import ScriptUtils from "./ScriptUtils";
import {Utils} from "../Utils";
/**
* This script slices a big newline-delimeted geojson file into tiled geojson
* This script slices a big newline-delimeted geojson file into tiled geojson
* It was used to convert the CRAB-data into geojson tiles
*/
@ -117,7 +117,7 @@ async function main(args: string[]) {
const keysToRemove = ["STRAATNMID", "GEMEENTE", "POSTCODE"]
for (const f of allFeatures) {
if(f.properties === null){
if (f.properties === null) {
console.log("Got a feature without properties!", f)
continue
}