mapcomplete/Models/ThemeConfig/Conversion/LegacyJsonConvert.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

199 lines
7.1 KiB
TypeScript
Raw Normal View History

2022-09-08 21:40:48 +02:00
import { LayoutConfigJson } from "../Json/LayoutConfigJson"
import { Utils } from "../../../Utils"
import LineRenderingConfigJson from "../Json/LineRenderingConfigJson"
import { LayerConfigJson } from "../Json/LayerConfigJson"
import { DesugaringStep, Each, Fuse, On } from "./Conversion"
export class UpdateLegacyLayer extends DesugaringStep<
LayerConfigJson | string | { builtin; override }
> {
2021-12-21 18:35:31 +01:00
constructor() {
2022-09-08 21:40:48 +02:00
super(
"Updates various attributes from the old data format to the new to provide backwards compatibility with the formats",
["overpassTags", "source.osmtags", "tagRenderings[*].id", "mapRendering"],
2022-09-08 21:40:48 +02:00
"UpdateLegacyLayer"
)
2021-12-21 18:35:31 +01:00
}
2022-09-08 21:40:48 +02:00
convert(
json: LayerConfigJson,
context: string
): { result: LayerConfigJson; errors: string[]; warnings: string[] } {
2021-12-21 18:35:31 +01:00
const warnings = []
if (typeof json === "string" || json["builtin"] !== undefined) {
// Reuse of an already existing layer; return as-is
2022-09-08 21:40:48 +02:00
return { result: json, errors: [], warnings: [] }
2021-12-21 18:35:31 +01:00
}
2022-09-08 21:40:48 +02:00
let config = { ...json }
if (config["overpassTags"]) {
2022-06-19 22:37:31 +02:00
config.source = config.source ?? {
2022-09-08 21:40:48 +02:00
osmTags: config["overpassTags"],
2022-06-19 22:37:31 +02:00
}
config.source.osmTags = config["overpassTags"]
delete config["overpassTags"]
}
if (config.tagRenderings !== undefined) {
2022-09-08 21:40:48 +02:00
let i = 0
for (const tagRendering of config.tagRenderings) {
2022-09-08 21:40:48 +02:00
i++
if (
typeof tagRendering === "string" ||
tagRendering["builtin"] !== undefined ||
tagRendering["rewrite"] !== undefined
) {
2021-12-21 19:56:04 +01:00
continue
}
if (tagRendering["id"] === undefined) {
if (tagRendering["#"] !== undefined) {
tagRendering["id"] = tagRendering["#"]
delete tagRendering["#"]
} else if (tagRendering["freeform"]?.key !== undefined) {
tagRendering["id"] = config.id + "-" + tagRendering["freeform"]["key"]
} else {
tagRendering["id"] = "tr-" + i
}
}
}
}
2021-11-21 03:48:05 +01:00
if (config.mapRendering === undefined) {
config.mapRendering = []
// This is a legacy format, lets create a pointRendering
let location: ("point" | "centroid")[] = ["point"]
let wayHandling: number = config["wayHandling"] ?? 0
if (wayHandling !== 0) {
location = ["point", "centroid"]
}
2021-12-21 18:35:31 +01:00
if (config["icon"] ?? config["label"] !== undefined) {
const pointConfig = {
icon: config["icon"],
iconBadges: config["iconOverlays"],
label: config["label"],
iconSize: config["iconSize"],
location,
2022-09-08 21:40:48 +02:00
rotation: config["rotation"],
2021-12-21 18:35:31 +01:00
}
config.mapRendering.push(pointConfig)
}
if (wayHandling !== 1) {
const lineRenderConfig = <LineRenderingConfigJson>{
color: config["color"],
width: config["width"],
2022-09-08 21:40:48 +02:00
dashArray: config["dashArray"],
}
if (Object.keys(lineRenderConfig).length > 0) {
config.mapRendering.push(lineRenderConfig)
}
}
2021-12-21 18:35:31 +01:00
if (config.mapRendering.length === 0) {
2022-09-08 21:40:48 +02:00
throw (
"Could not convert the legacy theme into a new theme: no renderings defined for layer " +
config.id
)
}
}
delete config["color"]
delete config["width"]
delete config["dashArray"]
delete config["icon"]
delete config["iconOverlays"]
delete config["label"]
delete config["iconSize"]
delete config["rotation"]
delete config["wayHandling"]
delete config["hideUnderlayingFeaturesMinPercentage"]
2021-12-21 18:35:31 +01:00
2022-09-08 21:40:48 +02:00
for (const mapRenderingElement of config.mapRendering ?? []) {
if (mapRenderingElement["iconOverlays"] !== undefined) {
mapRenderingElement["iconBadges"] = mapRenderingElement["iconOverlays"]
}
for (const overlay of mapRenderingElement["iconBadges"] ?? []) {
if (overlay["badge"] !== true) {
2021-12-21 18:35:31 +01:00
warnings.push("Warning: non-overlay element for ", config.id)
}
delete overlay["badge"]
}
}
2021-12-21 18:35:31 +01:00
return {
result: config,
errors: [],
2022-09-08 21:40:48 +02:00
warnings,
}
2021-12-21 18:35:31 +01:00
}
}
class UpdateLegacyTheme extends DesugaringStep<LayoutConfigJson> {
constructor() {
2022-09-08 21:40:48 +02:00
super("Small fixes in the theme config", ["roamingRenderings"], "UpdateLegacyTheme")
2021-12-21 18:35:31 +01:00
}
2022-09-08 21:40:48 +02:00
convert(
json: LayoutConfigJson,
context: string
): { result: LayoutConfigJson; errors: string[]; warnings: string[] } {
const oldThemeConfig = { ...json }
2022-08-22 14:46:55 +02:00
if (oldThemeConfig.socialImage === "") {
2022-02-19 17:40:51 +01:00
delete oldThemeConfig.socialImage
}
2022-08-22 14:46:55 +02:00
2021-12-21 18:35:31 +01:00
if (oldThemeConfig["roamingRenderings"] !== undefined) {
if (oldThemeConfig["roamingRenderings"].length == 0) {
delete oldThemeConfig["roamingRenderings"]
} else {
return {
result: null,
2022-09-08 21:40:48 +02:00
errors: [
context +
": The theme contains roamingRenderings. These are not supported anymore",
],
warnings: [],
2021-12-21 18:35:31 +01:00
}
}
}
2022-01-24 16:43:50 +01:00
oldThemeConfig.layers = Utils.NoNull(oldThemeConfig.layers)
delete oldThemeConfig["language"]
2022-08-22 14:46:55 +02:00
delete oldThemeConfig["version"]
if (oldThemeConfig["maintainer"] !== undefined) {
2022-09-08 21:40:48 +02:00
console.log(
"Maintainer: ",
oldThemeConfig["maintainer"],
"credits: ",
oldThemeConfig["credits"]
)
2022-08-22 14:46:55 +02:00
if (oldThemeConfig.credits === undefined) {
oldThemeConfig["credits"] = oldThemeConfig["maintainer"]
delete oldThemeConfig["maintainer"]
} else if (oldThemeConfig["maintainer"].toLowerCase().trim() === "mapcomplete") {
delete oldThemeConfig["maintainer"]
} else if (oldThemeConfig["maintainer"].toLowerCase().trim() === "") {
delete oldThemeConfig["maintainer"]
}
}
2021-12-21 18:35:31 +01:00
return {
errors: [],
warnings: [],
2022-09-08 21:40:48 +02:00
result: oldThemeConfig,
2021-12-21 18:35:31 +01:00
}
}
2021-12-21 18:35:31 +01:00
}
2021-12-21 18:35:31 +01:00
export class FixLegacyTheme extends Fuse<LayoutConfigJson> {
constructor() {
super(
"Fixes a legacy theme to the modern JSON format geared to humans. Syntactic sugars are kept (i.e. no tagRenderings are expandend, no dependencies are automatically gathered)",
new UpdateLegacyTheme(),
2022-08-22 14:46:55 +02:00
new On("layers", new Each(new UpdateLegacyLayer()))
2022-09-08 21:40:48 +02:00
)
2021-12-21 18:35:31 +01:00
}
}