mapcomplete/Models/ThemeConfig/LayerConfig.ts

445 lines
18 KiB
TypeScript
Raw Normal View History

2021-07-23 15:56:22 +02:00
import {Translation} from "../../UI/i18n/Translation";
import SourceConfig from "./SourceConfig";
import TagRenderingConfig from "./TagRenderingConfig";
import PresetConfig, {PreciseInput} from "./PresetConfig";
import {LayerConfigJson} from "./Json/LayerConfigJson";
import Translations from "../../UI/i18n/Translations";
import {TagUtils} from "../../Logic/Tags/TagUtils";
2021-07-22 11:29:09 +02:00
import FilterConfig from "./FilterConfig";
import {Unit} from "../Unit";
import DeleteConfig from "./DeleteConfig";
import MoveConfig from "./MoveConfig";
import PointRenderingConfig from "./PointRenderingConfig";
import WithContextLoader from "./WithContextLoader";
import LineRenderingConfig from "./LineRenderingConfig";
import PointRenderingConfigJson from "./Json/PointRenderingConfigJson";
import LineRenderingConfigJson from "./Json/LineRenderingConfigJson";
2021-10-22 18:53:07 +02:00
import {TagRenderingConfigJson} from "./Json/TagRenderingConfigJson";
import {UIEventSource} from "../../Logic/UIEventSource";
import BaseUIElement from "../../UI/BaseUIElement";
2021-11-08 02:36:01 +01:00
import Combine from "../../UI/Base/Combine";
import Title from "../../UI/Base/Title";
import List from "../../UI/Base/List";
import Link from "../../UI/Base/Link";
2021-11-08 03:00:58 +01:00
import {Utils} from "../../Utils";
import {TagsFilter} from "../../Logic/Tags/TagsFilter";
import Table from "../../UI/Base/Table";
2021-04-10 23:53:13 +02:00
2021-10-22 18:53:07 +02:00
export default class LayerConfig extends WithContextLoader {
2021-07-23 15:56:22 +02:00
public readonly id: string;
public readonly name: Translation;
public readonly description: Translation;
public readonly source: SourceConfig;
public readonly calculatedTags: [string, string, boolean][];
public readonly doNotDownload: boolean;
public readonly passAllFeatures: boolean;
public readonly isShown: TagRenderingConfig;
public minzoom: number;
public minzoomVisible: number;
public readonly maxzoom: number;
public readonly title?: TagRenderingConfig;
public readonly titleIcons: TagRenderingConfig[];
2021-10-22 18:53:07 +02:00
public readonly mapRendering: PointRenderingConfig[]
public readonly lineRendering: LineRenderingConfig[]
2021-07-23 15:56:22 +02:00
public readonly units: Unit[];
public readonly deletion: DeleteConfig | null;
public readonly allowMove: MoveConfig | null
public readonly allowSplit: boolean
public readonly shownByDefault: boolean;
/**
* In seconds
*/
public readonly maxAgeOfCache: number
2021-07-23 15:56:22 +02:00
public readonly presets: PresetConfig[];
2020-11-17 02:22:48 +01:00
public readonly tagRenderings: TagRenderingConfig[];
public readonly filters: FilterConfig[];
2021-07-22 11:29:09 +02:00
2021-07-23 15:56:22 +02:00
constructor(
json: LayerConfigJson,
context?: string,
official: boolean = true
) {
context = context + "." + json.id;
super(json, context)
2021-07-23 15:56:22 +02:00
this.id = json.id;
if (json.source === undefined) {
throw "Layer " + this.id + " does not define a source section (" + context + ")"
}
if (json.source.osmTags === undefined) {
throw "Layer " + this.id + " does not define a osmTags in the source section - these should always be present, even for geojson layers (" + context + ")"
}
2021-07-23 15:56:22 +02:00
if (json.id.toLowerCase() !== json.id) {
throw `${context}: The id of a layer should be lowercase: ${json.id}`
}
if (json.id.match(/[a-z0-9-_]/) == null) {
throw `${context}: The id of a layer should match [a-z0-9-_]*: ${json.id}`
}
2021-07-23 15:56:22 +02:00
this.maxAgeOfCache = json.source.maxCacheAge ?? 24 * 60 * 60 * 30
2021-07-23 15:56:22 +02:00
const osmTags = TagUtils.Tag(
json.source.osmTags,
context + "source.osmTags"
);
if (json.source["geoJsonSource"] !== undefined) {
throw context + "Use 'geoJson' instead of 'geoJsonSource'";
}
if (json.source["geojson"] !== undefined) {
throw context + "Use 'geoJson' instead of 'geojson' (the J is a capital letter)";
}
this.source = new SourceConfig(
{
osmTags: osmTags,
geojsonSource: json.source["geoJson"],
geojsonSourceLevel: json.source["geoJsonZoomLevel"],
overpassScript: json.source["overpassScript"],
isOsmCache: json.source["isOsmCache"],
mercatorCrs: json.source["mercatorCrs"]
},
json.id
);
this.allowSplit = json.allowSplit ?? false;
this.name = Translations.T(json.name, context + ".name");
this.units = (json.units ?? []).map(((unitJson, i) => Unit.fromJson(unitJson, `${context}.unit[${i}]`)))
if (json.description !== undefined) {
if (Object.keys(json.description).length === 0) {
json.description = undefined;
}
}
this.description = Translations.T(
json.description,
context + ".description"
);
2021-07-23 15:56:22 +02:00
this.calculatedTags = undefined;
if (json.calculatedTags !== undefined) {
if (!official) {
console.warn(
`Unofficial theme ${this.id} with custom javascript! This is a security risk`
);
}
2021-07-23 15:56:22 +02:00
this.calculatedTags = [];
for (const kv of json.calculatedTags) {
const index = kv.indexOf("=");
let key = kv.substring(0, index);
const isStrict = key.endsWith(':')
if (isStrict) {
key = key.substr(0, key.length - 1)
}
2021-07-23 15:56:22 +02:00
const code = kv.substring(index + 1);
try {
new Function("feat", "return " + code + ";");
} catch (e) {
throw `Invalid function definition: code ${code} is invalid:${e} (at ${context})`
}
this.calculatedTags.push([key, code, isStrict]);
2020-10-27 01:01:34 +01:00
}
2021-07-23 15:56:22 +02:00
}
2021-07-23 15:56:22 +02:00
this.doNotDownload = json.doNotDownload ?? false;
this.passAllFeatures = json.passAllFeatures ?? false;
this.minzoom = json.minzoom ?? 0;
2021-07-27 19:39:57 +02:00
this.minzoomVisible = json.minzoomVisible ?? this.minzoom;
this.shownByDefault = json.shownByDefault ?? true;
if (json.presets !== undefined && json.presets?.map === undefined) {
throw "Presets should be a list of items (at " + context + ")"
2021-09-22 20:44:53 +02:00
}
this.presets = (json.presets ?? []).map((pr, i) => {
let preciseInput: PreciseInput = {
2021-10-16 00:43:53 +02:00
preferredBackground: ["photo"],
snapToLayers: undefined,
maxSnapDistance: undefined
};
if (pr.preciseInput !== undefined) {
if (pr.preciseInput === true) {
pr.preciseInput = {
preferredBackground: undefined
}
}
let snapToLayers: string[];
if (typeof pr.preciseInput.snapToLayer === "string") {
snapToLayers = [pr.preciseInput.snapToLayer]
} else {
snapToLayers = pr.preciseInput.snapToLayer
}
let preferredBackground: string[]
if (typeof pr.preciseInput.preferredBackground === "string") {
preferredBackground = [pr.preciseInput.preferredBackground]
} else {
preferredBackground = pr.preciseInput.preferredBackground
}
preciseInput = {
preferredBackground: preferredBackground,
snapToLayers,
maxSnapDistance: pr.preciseInput.maxSnapDistance ?? 10
}
}
const config: PresetConfig = {
title: Translations.T(pr.title, `${context}.presets[${i}].title`),
tags: pr.tags.map((t) => TagUtils.SimpleTag(t)),
description: Translations.T(pr.description, `${context}.presets[${i}].description`),
preciseInput: preciseInput,
2021-07-24 02:32:33 +02:00
}
return config;
2021-07-24 02:32:33 +02:00
});
2021-07-23 15:56:22 +02:00
2021-10-22 18:53:07 +02:00
if (json.mapRendering === undefined) {
throw "MapRendering is undefined in " + context
2021-10-21 01:26:20 +02:00
}
2021-11-09 18:22:05 +01:00
if (json.mapRendering === null) {
this.mapRendering = []
this.lineRendering = []
} else {
2021-11-09 19:45:26 +01:00
this.mapRendering = Utils.NoNull(json.mapRendering)
2021-11-09 18:22:05 +01:00
.filter(r => r["location"] !== undefined)
.map((r, i) => new PointRenderingConfig(<PointRenderingConfigJson>r, context + ".mapRendering[" + i + "]"))
2021-11-09 19:45:26 +01:00
this.lineRendering = Utils.NoNull(json.mapRendering)
2021-11-09 18:22:05 +01:00
.filter(r => r["location"] === undefined)
.map((r, i) => new LineRenderingConfig(<LineRenderingConfigJson>r, context + ".mapRendering[" + i + "]"))
const hasCenterRendering = this.mapRendering.some(r => r.location.has("centroid") || r.location.has("start") || r.location.has("end"))
if (this.lineRendering.length === 0 && this.mapRendering.length === 0) {
console.log(json.mapRendering)
throw("The layer " + this.id + " does not have any maprenderings defined and will thus not show up on the map at all. If this is intentional, set maprenderings to 'null' instead of '[]'")
} else if (!hasCenterRendering && this.lineRendering.length === 0) {
throw "The layer " + this.id + " might not render ways. This might result in dropped information"
}
}
const missingIds = json.tagRenderings?.filter(tr => typeof tr !== "string" && tr["builtin"] === undefined && tr["id"] === undefined && tr["rewrite"] === undefined) ?? [];
2021-11-08 03:00:58 +01:00
if (missingIds?.length > 0 && official) {
console.error("Some tagRenderings of", this.id, "are missing an id:", missingIds)
throw "Missing ids in tagrenderings"
}
2021-12-21 18:35:31 +01:00
this.tagRenderings = (json.tagRenderings ?? []).map((tr, i) => new TagRenderingConfig(<TagRenderingConfigJson>tr, this.id + ".tagRenderings[" + i + "]"))
2021-11-08 03:00:58 +01:00
2021-07-23 15:56:22 +02:00
this.filters = (json.filter ?? []).map((option, i) => {
return new FilterConfig(option, `${context}.filter-[${i}]`)
});
2021-11-08 03:00:58 +01:00
{
2021-12-21 18:35:31 +01:00
const duplicateIds = Utils.Dupiclates(this.filters.map(f => f.id))
2021-11-08 03:00:58 +01:00
if (duplicateIds.length > 0) {
throw `Some filters have a duplicate id: ${duplicateIds} (at ${context}.filters)`
}
}
if (json["filters"] !== undefined) {
throw "Error in " + context + ": use 'filter' instead of 'filters'"
}
2021-07-23 15:56:22 +02:00
2020-11-16 01:59:30 +01:00
2021-12-21 18:35:31 +01:00
this.titleIcons = this.ParseTagRenderings((<TagRenderingConfigJson[]> json.titleIcons), {
readOnlyMode: true
});
this.title = this.tr("title", undefined);
this.isShown = this.tr("isShown", "yes");
2021-10-22 18:53:07 +02:00
2021-07-23 15:56:22 +02:00
this.deletion = null;
if (json.deletion === true) {
json.deletion = {};
}
if (json.deletion !== undefined && json.deletion !== false) {
this.deletion = new DeleteConfig(json.deletion, `${context}.deletion`);
}
this.allowMove = null
if (json.allowMove === false) {
this.allowMove = null;
} else if (json.allowMove === true) {
this.allowMove = new MoveConfig({}, context + ".allowMove")
} else if (json.allowMove !== undefined && json.allowMove !== false) {
this.allowMove = new MoveConfig(json.allowMove, context + ".allowMove")
}
2021-07-23 15:56:22 +02:00
if (json["showIf"] !== undefined) {
throw (
"Invalid key on layerconfig " +
this.id +
": showIf. Did you mean 'isShown' instead?"
);
}
}
2021-11-07 16:34:51 +01:00
public defaultIcon(): BaseUIElement | undefined {
if (this.mapRendering === undefined || this.mapRendering === null) {
2021-11-09 19:45:26 +01:00
return undefined;
}
const mapRendering = this.mapRendering.filter(r => r.location.has("point"))[0]
if (mapRendering === undefined) {
return undefined
}
const defaultTags = new UIEventSource(TagUtils.changeAsProperties(this.source.osmTags.asChange({id: "node/-1"})))
return mapRendering.GenerateLeafletStyle(defaultTags, false, {noSize: true}).html
}
public GenerateDocumentation(usedInThemes: string[], layerIsNeededBy: Map<string, string[]>, dependencies: {
context?: string;
reason: string;
neededLayer: string;
}[], addedByDefault = false, canBeIncluded = true): BaseUIElement {
2021-11-08 02:36:01 +01:00
const extraProps = []
if (canBeIncluded) {
2021-11-09 18:22:05 +01:00
if (addedByDefault) {
extraProps.push("**This layer is included automatically in every theme. This layer might contain no points**")
2021-11-08 14:18:45 +01:00
}
if (this.shownByDefault === false) {
extraProps.push('This layer is not visible by default and must be enabled in the filter by the user. ')
}
2021-11-08 02:36:01 +01:00
if (this.title === undefined) {
extraProps.push("This layer cannot be toggled in the filter view. If you import this layer in your theme, override `title` to make this toggleable.")
}
if (this.title === undefined && this.shownByDefault === false) {
extraProps.push("This layer is not visible by default and the visibility cannot be toggled, effectively resulting in a fully hidden layer. This can be useful, e.g. to calculate some metatags. If you want to render this layer (e.g. for debugging), enable it by setting the URL-parameter layer-<id>=true")
2021-11-08 02:36:01 +01:00
}
if (this.name === undefined) {
extraProps.push("Not visible in the layer selection by default. If you want to make this layer toggable, override `name`")
}
if (this.mapRendering.length === 0) {
extraProps.push("Not rendered on the map by default. If you want to rendering this on the map, override `mapRenderings`")
}
if (this.source.geojsonSource !== undefined) {
extraProps.push("<img src='../warning.svg' height='1rem'/> This layer is loaded from an external source, namely `" + this.source.geojsonSource + "`")
}
2021-11-08 02:36:01 +01:00
} else {
extraProps.push("This layer can **not** be included in a theme. It is solely used by [special renderings](SpecialRenderings.md) showing a minimap with custom data.")
}
let usingLayer: BaseUIElement[] = []
if (usedInThemes?.length > 0 && !addedByDefault) {
usingLayer = [new Title("Themes using this layer", 4),
new List((usedInThemes ?? []).map(id => new Link(id, "https://mapcomplete.osm.be/" + id)))
]
}
for (const dep of dependencies) {
extraProps.push(new Combine(["This layer will automatically load ", new Link(dep.neededLayer, "./" + dep.neededLayer + ".md"), " into the layout as it depends on it: ", dep.reason, "(" + dep.context + ")"]))
}
for (const revDep of layerIsNeededBy?.get(this.id) ?? []) {
extraProps.push(new Combine(["This layer is needed as dependency for layer", new Link(revDep, "#" + revDep)]))
}
const icon = Array.from(this.mapRendering[0]?.icon?.ExtractImages(true) ?? [])[0]
let iconImg = ""
if (icon !== undefined) {
iconImg = `<img src='https://mapcomplete.osm.be/${icon}' height="100px"> `
}
let neededTags: TagsFilter[] = [this.source.osmTags]
if (this.source.osmTags["and"] !== undefined) {
neededTags = this.source.osmTags["and"]
}
let tableRows = Utils.NoNull(this.tagRenderings.map(tr => tr.FreeformValues())
.map(values => {
if (values == undefined) {
return undefined
}
const embedded: (Link | string)[] = values.values?.map(v => Link.OsmWiki(values.key, v, true)) ?? ["_no preset options defined, or no values in them_"]
return [
new Combine([
new Link(
"<img src='https://mapcomplete.osm.be/assets/svg/statistics.svg' height='18px'>",
"https://taginfo.openstreetmap.org/keys/"+values.key+"#values"
),Link.OsmWiki(values.key)
]),
values.type === undefined ? "Multiple choice" : new Link(values.type, "../SpecialInputElements.md#" + values.type),
new Combine(embedded)
];
}))
let quickOverview: BaseUIElement = undefined;
if (tableRows.length > 0) {
quickOverview = new Combine([
"**Warning** This quick overview is incomplete",
new Table(["attribute", "type", "values which are supported by this layer"], tableRows)
]).SetClass("flex-col flex")
}
2021-11-08 02:36:01 +01:00
return new Combine([
new Combine([
new Title(this.id, 1),
iconImg,
this.description,
"\n"
]).SetClass("flex flex-col"),
new List(extraProps),
...usingLayer,
2021-11-09 18:22:05 +01:00
2021-11-08 14:18:45 +01:00
new Link("Go to the source code", `../assets/layers/${this.id}/${this.id}.json`),
new Title("Basic tags for this layer", 2),
"Elements must have the all of following tags to be shown on this layer:",
new List(neededTags.map(t => t.asHumanString(true, false, {}))),
new Title("Supported attributes", 2),
quickOverview,
...this.tagRenderings.map(tr => tr.GenerateDocumentation())
]).SetClass("flex-col")
2021-11-08 02:36:01 +01:00
}
2021-07-23 15:56:22 +02:00
public CustomCodeSnippets(): string[] {
if (this.calculatedTags === undefined) {
return [];
}
return this.calculatedTags.map((code) => code[1]);
2021-07-22 11:29:09 +02:00
}
AllTagRenderings(): TagRenderingConfig[] {
return Utils.NoNull([...this.tagRenderings, ...this.titleIcons, this.title, this.isShown])
}
2021-07-23 15:56:22 +02:00
public ExtractImages(): Set<string> {
const parts: Set<string>[] = [];
parts.push(...this.tagRenderings?.map((tr) => tr.ExtractImages(false)));
parts.push(...this.titleIcons?.map((tr) => tr.ExtractImages(true)));
for (const preset of this.presets) {
parts.push(new Set<string>(preset.description?.ExtractImages(false)));
2020-11-16 01:59:30 +01:00
}
for (const pointRenderingConfig of this.mapRendering) {
parts.push(pointRenderingConfig.ExtractImages())
}
2021-07-23 15:56:22 +02:00
const allIcons = new Set<string>();
for (const part of parts) {
part?.forEach(allIcons.add, allIcons);
}
2021-07-23 15:56:22 +02:00
return allIcons;
}
2021-10-22 18:53:07 +02:00
public isLeftRightSensitive(): boolean {
return this.lineRendering.some(lr => lr.leftRightSensitive)
}
}