forked from MapComplete/MapComplete
I should have commited sooner...
This commit is contained in:
parent
2685b6e734
commit
16612b10ef
35 changed files with 570 additions and 177 deletions
|
@ -8,23 +8,33 @@ import {TagRenderingConfigJson} from "./TagRenderingConfigJson";
|
|||
import {Translation} from "../../UI/i18n/Translation";
|
||||
import {Img} from "../../UI/Img";
|
||||
import Svg from "../../Svg";
|
||||
import {SubstitutedTranslation} from "../../UI/SpecialVisualizations";
|
||||
import {Utils} from "../../Utils";
|
||||
import Combine from "../../UI/Base/Combine";
|
||||
import {Browser} from "leaflet";
|
||||
|
||||
export default class LayerConfig {
|
||||
|
||||
|
||||
id: string;
|
||||
|
||||
name: Translation
|
||||
|
||||
description: Translation;
|
||||
overpassTags: TagsFilter;
|
||||
doNotDownload: boolean;
|
||||
|
||||
passAllFeatures: boolean;
|
||||
|
||||
minzoom: number;
|
||||
|
||||
title: TagRenderingConfig;
|
||||
title?: TagRenderingConfig;
|
||||
|
||||
titleIcons: TagRenderingConfig[];
|
||||
|
||||
icon: TagRenderingConfig;
|
||||
iconSize: TagRenderingConfig;
|
||||
rotation: TagRenderingConfig;
|
||||
color: TagRenderingConfig;
|
||||
width: TagRenderingConfig;
|
||||
dashArray: TagRenderingConfig;
|
||||
|
@ -53,10 +63,11 @@ export default class LayerConfig {
|
|||
this.name = Translations.T(json.name);
|
||||
this.description = Translations.T(json.name);
|
||||
this.overpassTags = FromJSON.Tag(json.overpassTags, context + ".overpasstags");
|
||||
this.doNotDownload = json.doNotDownload ?? false,
|
||||
this.passAllFeatures = json.passAllFeatures ?? false;
|
||||
this.minzoom = json.minzoom;
|
||||
this.wayHandling = json.wayHandling ?? 0;
|
||||
this.hideUnderlayingFeaturesMinPercentage = json.hideUnderlayingFeaturesMinPercentage ?? 0;
|
||||
this.title = new TagRenderingConfig(json.title);
|
||||
this.presets = (json.presets ?? []).map(pr =>
|
||||
({
|
||||
title: Translations.T(pr.title),
|
||||
|
@ -93,7 +104,10 @@ export default class LayerConfig {
|
|||
|
||||
function tr(key, deflt) {
|
||||
const v = json[key];
|
||||
if (v === undefined) {
|
||||
if (v === undefined || v === null) {
|
||||
if (deflt === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
return new TagRenderingConfig(deflt);
|
||||
}
|
||||
if (typeof v === "string") {
|
||||
|
@ -107,11 +121,19 @@ export default class LayerConfig {
|
|||
}
|
||||
|
||||
|
||||
this.title = tr("title", "");
|
||||
this.title = tr("title", undefined);
|
||||
this.icon = tr("icon", Img.AsData(Svg.bug));
|
||||
const iconPath = this.icon.GetRenderValue({id: "node/-1"}).txt;
|
||||
if (iconPath.startsWith(Utils.assets_path)) {
|
||||
const iconKey = iconPath.substr(Utils.assets_path.length);
|
||||
if (Svg.All[iconKey] === undefined) {
|
||||
throw "Builtin SVG asset not found: " + iconPath
|
||||
}
|
||||
}
|
||||
this.iconSize = tr("iconSize", "40,40,center");
|
||||
this.color = tr("color", "#0000ff");
|
||||
this.width = tr("width", "7");
|
||||
this.rotation = tr("rotation", "0");
|
||||
this.dashArray = tr("dashArray", "");
|
||||
|
||||
|
||||
|
@ -121,13 +143,16 @@ export default class LayerConfig {
|
|||
public GenerateLeafletStyle(tags: any):
|
||||
{
|
||||
color: string;
|
||||
icon: { popupAnchor: [number, number]; iconAnchor: [number, number]; iconSize: [number, number]; iconUrl: string }; weight: number; dashArray: number[]
|
||||
icon: {
|
||||
iconUrl: string,
|
||||
popupAnchor: [number, number];
|
||||
iconAnchor: [number, number];
|
||||
iconSize: [number, number];
|
||||
html: string;
|
||||
rotation: number;
|
||||
};
|
||||
weight: number; dashArray: number[]
|
||||
} {
|
||||
const iconUrl = this.icon?.GetRenderValue(tags)?.txt;
|
||||
const iconSize = (this.iconSize?.GetRenderValue(tags)?.txt ?? "40,40,center").split(",");
|
||||
|
||||
|
||||
const dashArray = this.dashArray.GetRenderValue(tags)?.txt.split(" ").map(Number);
|
||||
|
||||
function num(str, deflt = 40) {
|
||||
const n = Number(str);
|
||||
|
@ -137,6 +162,33 @@ export default class LayerConfig {
|
|||
return n;
|
||||
}
|
||||
|
||||
function rendernum(tr: TagRenderingConfig, deflt: number) {
|
||||
const str = Number(render(tr, "" + deflt));
|
||||
const n = Number(str);
|
||||
if (isNaN(n)) {
|
||||
return deflt;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
function render(tr: TagRenderingConfig, deflt?: string) {
|
||||
const str = (tr?.GetRenderValue(tags)?.txt ?? deflt);
|
||||
return SubstitutedTranslation.SubstituteKeys(str, tags);
|
||||
}
|
||||
|
||||
const iconUrl = render(this.icon);
|
||||
const iconSize = render(this.iconSize, "40,40,center").split(",");
|
||||
const dashArray = render(this.dashArray).split(" ").map(Number);
|
||||
let color = render(this.color, "#00f");
|
||||
|
||||
if (color.startsWith("--")) {
|
||||
color = getComputedStyle(document.body).getPropertyValue("--catch-detail-color")
|
||||
}
|
||||
|
||||
const weight = rendernum(this.width, 5);
|
||||
const rotation = rendernum(this.rotation, 0);
|
||||
|
||||
|
||||
const iconW = num(iconSize[0]);
|
||||
const iconH = num(iconSize[1]);
|
||||
const mode = iconSize[2] ?? "center"
|
||||
|
@ -157,16 +209,22 @@ export default class LayerConfig {
|
|||
anchorH = iconH;
|
||||
}
|
||||
|
||||
|
||||
const color = this.color?.GetRenderValue(tags)?.txt ?? "#00f";
|
||||
let weight = num(this.width?.GetRenderValue(tags)?.txt, 5);
|
||||
let html = `<img src="${iconUrl}" style="width:100%;height:100%;rotate:${rotation}deg;display:block;" />`;
|
||||
if (iconUrl.startsWith(Utils.assets_path)) {
|
||||
const key = iconUrl.substr(Utils.assets_path.length);
|
||||
html = new Combine([
|
||||
(Svg.All[key] as string).replace(/stop-color:#000000/g, 'stop-color:' + color)
|
||||
]).SetStyle(`width:100%;height:100%;rotate:${rotation}deg;display:block;`).Render();
|
||||
}
|
||||
return {
|
||||
icon:
|
||||
{
|
||||
iconUrl: iconUrl,
|
||||
html: html,
|
||||
iconSize: [iconW, iconH],
|
||||
iconAnchor: [anchorW, anchorH],
|
||||
popupAnchor: [0, 3 - anchorH]
|
||||
popupAnchor: [0, 3 - anchorH],
|
||||
rotation: rotation,
|
||||
iconUrl: iconUrl
|
||||
},
|
||||
color: color,
|
||||
weight: weight,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue