Wire in aspected-routing as calculated tag

This commit is contained in:
Pieter Vander Vennet 2021-07-26 20:21:05 +02:00
parent 829efc5d55
commit e0b71ca53e
10 changed files with 14806 additions and 84 deletions

View file

@ -15,7 +15,7 @@ import {LocalStorageSource} from "./Logic/Web/LocalStorageSource";
import {Utils} from "./Utils"; import {Utils} from "./Utils";
import Svg from "./Svg"; import Svg from "./Svg";
import Link from "./UI/Base/Link"; import Link from "./UI/Base/Link";
import * as personal from "./assets/themes/personalLayout/personalLayout.json" import * as personal from "./assets/themes/personal/personal.json"
import LayoutConfig from "./Customizations/JSON/LayoutConfig"; import LayoutConfig from "./Customizations/JSON/LayoutConfig";
import * as L from "leaflet"; import * as L from "leaflet";
import Img from "./UI/Base/Img"; import Img from "./UI/Base/Img";

View file

@ -6,7 +6,9 @@ import {Utils} from "../Utils";
import BaseUIElement from "../UI/BaseUIElement"; import BaseUIElement from "../UI/BaseUIElement";
import List from "../UI/Base/List"; import List from "../UI/Base/List";
import Title from "../UI/Base/Title"; import Title from "../UI/Base/Title";
import * as AR from "aspected-routing" import {RuleSet} from "aspected-routing"
import {UIEventSourceTools} from "./UIEventSource";
export class ExtraFunction { export class ExtraFunction {
@ -41,9 +43,11 @@ export class ExtraFunction {
private static readonly OverlapFunc = new ExtraFunction( private static readonly OverlapFunc = new ExtraFunction(
"overlapWith", {
"Gives a list of features from the specified layer which this feature (partly) overlaps with. If the current feature is a point, all features that embed the point are given. The returned value is `{ feat: GeoJSONFeature, overlap: number}[]` where `overlap` is the overlapping surface are (in m²) for areas, the overlapping length (in meter) if the current feature is a line or `undefined` if the current feature is a point", name: "overlapWith",
["...layerIds - one or more layer ids of the layer from which every feature is checked for overlap)"], doc: "Gives a list of features from the specified layer which this feature (partly) overlaps with. If the current feature is a point, all features that embed the point are given. The returned value is `{ feat: GeoJSONFeature, overlap: number}[]` where `overlap` is the overlapping surface are (in m²) for areas, the overlapping length (in meter) if the current feature is a line or `undefined` if the current feature is a point",
args: ["...layerIds - one or more layer ids of the layer from which every feature is checked for overlap)"]
},
(params, feat) => { (params, feat) => {
return (...layerIds: string[]) => { return (...layerIds: string[]) => {
const result = [] const result = []
@ -62,9 +66,11 @@ export class ExtraFunction {
} }
) )
private static readonly DistanceToFunc = new ExtraFunction( private static readonly DistanceToFunc = new ExtraFunction(
"distanceTo", {
"Calculates the distance between the feature and a specified point in kilometer. The input should either be a pair of coordinates, a geojson feature or the ID of an object", name: "distanceTo",
["longitude", "latitude"], doc: "Calculates the distance between the feature and a specified point in kilometer. The input should either be a pair of coordinates, a geojson feature or the ID of an object",
args: ["longitude", "latitude"]
},
(featuresPerLayer, feature) => { (featuresPerLayer, feature) => {
return (arg0, lat) => { return (arg0, lat) => {
if (typeof arg0 === "number") { if (typeof arg0 === "number") {
@ -88,9 +94,11 @@ export class ExtraFunction {
) )
private static readonly ClosestObjectFunc = new ExtraFunction( private static readonly ClosestObjectFunc = new ExtraFunction(
"closest", {
"Given either a list of geojson features or a single layer name, gives the single object which is nearest to the feature. In the case of ways/polygons, only the centerpoint is considered.", name: "closest",
["list of features"], doc: "Given either a list of geojson features or a single layer name, gives the single object which is nearest to the feature. In the case of ways/polygons, only the centerpoint is considered.",
args: ["list of features"]
},
(params, feature) => { (params, feature) => {
return (features) => { return (features) => {
if (typeof features === "string") { if (typeof features === "string") {
@ -139,29 +147,40 @@ export class ExtraFunction {
private static readonly Memberships = new ExtraFunction( private static readonly Memberships = new ExtraFunction(
"memberships", {
"Gives a list of `{role: string, relation: Relation}`-objects, containing all the relations that this feature is part of. " + name: "memberships",
"\n\n" + doc: "Gives a list of `{role: string, relation: Relation}`-objects, containing all the relations that this feature is part of. " +
"For example: `_part_of_walking_routes=feat.memberships().map(r => r.relation.tags.name).join(';')`", "\n\n" +
[], "For example: `_part_of_walking_routes=feat.memberships().map(r => r.relation.tags.name).join(';')`",
args: []
},
(params, _) => { (params, _) => {
return () => params.relations ?? []; return () => params.relations ?? [];
} }
) )
private static readonly AspectedRouting = new ExtraFunction( private static readonly AspectedRouting = new ExtraFunction(
"score", {
"Given the path of an aspected routing json file, will calculate the score" + name: "score",
"\n\n" + doc: "Given the path of an aspected routing json file, will calculate the score. This score is wrapped in a UIEventSource, so for further calculations, use `.map(score => ...)`" +
"For example: `_comfort_score=feat.score('https://raw.githubusercontent.com/pietervdvn/AspectedRouting/master/Examples/bicycle/aspects/bicycle.comfort.json')`", "\n\n" +
[], "For example: `_comfort_score=feat.score('https://raw.githubusercontent.com/pietervdvn/AspectedRouting/master/Examples/bicycle/aspects/bicycle.comfort.json')`",
(params, _) => { args: ["path"]
return () => params.relations ?? []; },
(_, feature) => {
return (path) => {
return UIEventSourceTools.downloadJsonCached(path).map(config => {
if (config === undefined) {
return
}
return new RuleSet(config).runProgram(feature.properties)
})
}
} }
) )
private static readonly allFuncs: ExtraFunction[] = [ private static readonly allFuncs: ExtraFunction[] = [
ExtraFunction.DistanceToFunc, ExtraFunction.DistanceToFunc,
ExtraFunction.OverlapFunc, ExtraFunction.OverlapFunc,
ExtraFunction.ClosestObjectFunc, ExtraFunction.ClosestObjectFunc,
ExtraFunction.Memberships, ExtraFunction.Memberships,
@ -170,14 +189,15 @@ export class ExtraFunction {
private readonly _name: string; private readonly _name: string;
private readonly _args: string[]; private readonly _args: string[];
private readonly _doc: string; private readonly _doc: string;
private readonly _async: boolean;
private readonly _f: (params: { featuresPerLayer: Map<string, any[]>, relations: { role: string, relation: Relation }[] }, feat: any) => any; private readonly _f: (params: { featuresPerLayer: Map<string, any[]>, relations: { role: string, relation: Relation }[] }, feat: any) => any;
constructor(name: string, doc: string, args: string[], f: ((params: { featuresPerLayer: Map<string, any[]>, relations: { role: string, relation: Relation }[] }, feat: any) => any)) { constructor(options: { name: string, doc: string, args: string[] },
this._name = name; f: ((params: { featuresPerLayer: Map<string, any[]>, relations: { role: string, relation: Relation }[] }, feat: any) => any)) {
this._doc = doc; this._name = options.name;
this._args = args; this._doc = options.doc;
this._args = options.args;
this._f = f; this._f = f;
console.dir(AR)
} }
public static FullPatchFeature(featuresPerLayer: Map<string, any[]>, relations: { role: string, relation: Relation }[], feature) { public static FullPatchFeature(featuresPerLayer: Map<string, any[]>, relations: { role: string, relation: Relation }[], feature) {
@ -203,7 +223,6 @@ console.dir(AR)
} }
public PatchFeature(featuresPerLayer: Map<string, any[]>, relations: { role: string, relation: Relation }[], feature: any) { public PatchFeature(featuresPerLayer: Map<string, any[]>, relations: { role: string, relation: Relation }[], feature: any) {
feature[this._name] = this._f({featuresPerLayer: featuresPerLayer, relations: relations}, feature)
feature[this._name] = this._f({featuresPerLayer: featuresPerLayer, relations: relations}, feature);
} }
} }

View file

@ -27,8 +27,8 @@ export default class MetaTagging {
relations: Map<string, { role: string, relation: Relation }[]>, relations: Map<string, { role: string, relation: Relation }[]>,
layers: LayerConfig[], layers: LayerConfig[],
includeDates = true) { includeDates = true) {
if(features === undefined || features.length === 0){ if (features === undefined || features.length === 0) {
return; return;
} }
@ -79,14 +79,10 @@ export default class MetaTagging {
} }
} }
}) })
} }
@ -115,6 +111,17 @@ export default class MetaTagging {
const f = (featuresPerLayer, feature: any) => { const f = (featuresPerLayer, feature: any) => {
try { try {
let result = func(feature); let result = func(feature);
if(result instanceof UIEventSource){
result.addCallbackAndRunD(d => {
if (typeof d !== "string") {
// Make sure it is a string!
d = JSON.stringify(d);
}
feature.properties[key] = d;
})
result = result.data
}
if (result === undefined || result === "") { if (result === undefined || result === "") {
return; return;
} }
@ -124,11 +131,11 @@ export default class MetaTagging {
} }
feature.properties[key] = result; feature.properties[key] = result;
} catch (e) { } catch (e) {
if(MetaTagging. errorPrintCount < MetaTagging.stopErrorOutputAt){ if (MetaTagging.errorPrintCount < MetaTagging.stopErrorOutputAt) {
console.warn("Could not calculate a calculated tag defined by " + code + " due to " + e + ". This is code defined in the theme. Are you the theme creator? Doublecheck your code. Note that the metatags might not be stable on new features", e) console.warn("Could not calculate a calculated tag defined by " + code + " due to " + e + ". This is code defined in the theme. Are you the theme creator? Doublecheck your code. Note that the metatags might not be stable on new features", e)
MetaTagging. errorPrintCount ++; MetaTagging.errorPrintCount++;
if(MetaTagging. errorPrintCount == MetaTagging.stopErrorOutputAt){ if (MetaTagging.errorPrintCount == MetaTagging.stopErrorOutputAt) {
console.error("Got ",MetaTagging.stopErrorOutputAt," errors calculating this metatagging - stopping output now") console.error("Got ", MetaTagging.stopErrorOutputAt, " errors calculating this metatagging - stopping output now")
} }
} }
} }

View file

@ -166,4 +166,21 @@ export class UIEventSource<T> {
} }
}) })
} }
}
export class UIEventSourceTools {
private static readonly _download_cache = new Map<string, UIEventSource<any>>()
public static downloadJsonCached(url: string): UIEventSource<any>{
const cached = UIEventSourceTools._download_cache.get(url)
if(cached !== undefined){
return cached;
}
const src = new UIEventSource<any>(undefined)
UIEventSourceTools._download_cache.set(url, src)
Utils.downloadJson(url).then(r => src.setData(r))
return src;
}
} }

View file

@ -100,7 +100,7 @@ export default class State {
public readonly featureSwitchFakeUser: UIEventSource<boolean>; public readonly featureSwitchFakeUser: UIEventSource<boolean>;
public readonly featurePipeline: FeaturePipeline; public featurePipeline: FeaturePipeline;
/** /**

View file

@ -1,6 +1,6 @@
import State from "../../State"; import State from "../../State";
import ThemeIntroductionPanel from "./ThemeIntroductionPanel"; import ThemeIntroductionPanel from "./ThemeIntroductionPanel";
import * as personal from "../../assets/themes/personalLayout/personalLayout.json"; import * as personal from "../../assets/themes/personal/personal.json";
import PersonalLayersPanel from "./PersonalLayersPanel"; import PersonalLayersPanel from "./PersonalLayersPanel";
import Svg from "../../Svg"; import Svg from "../../Svg";
import Translations from "../i18n/Translations"; import Translations from "../i18n/Translations";

View file

@ -51,6 +51,9 @@
] ]
} }
}, },
"calculatedTags": [
"_comfort_score=feat.score('https://raw.githubusercontent.com/pietervdvn/AspectedRouting/master/Examples/bicycle/aspects/bicycle.comfort.json')"
],
"title": { "title": {
"render": { "render": {
"en": "Cycleways", "en": "Cycleways",
@ -1072,6 +1075,9 @@
] ]
} }
}, },
"calculatedTags": [
"_comfort_score=feat.score('https://raw.githubusercontent.com/pietervdvn/AspectedRouting/master/Examples/bicycle/aspects/bicycle.comfort.json')"
],
"minzoom": 14, "minzoom": 14,
"wayHandling": 0, "wayHandling": 0,
"title": { "title": {

14746
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -58,7 +58,7 @@
"@types/leaflet.markercluster": "^1.4.3", "@types/leaflet.markercluster": "^1.4.3",
"@types/lz-string": "^1.3.34", "@types/lz-string": "^1.3.34",
"@types/prompt-sync": "^4.1.0", "@types/prompt-sync": "^4.1.0",
"aspected-routing": "^0.1.0", "aspected-routing": "^0.2.0",
"autoprefixer": "^9.8.6", "autoprefixer": "^9.8.6",
"country-language": "^0.1.7", "country-language": "^0.1.7",
"email-validator": "^2.0.4", "email-validator": "^2.0.4",

View file

@ -1,6 +1,5 @@
import {lstatSync, readdirSync, readFileSync} from "fs"; import {lstatSync, readdirSync, readFileSync} from "fs";
import {Utils} from "../Utils"; import {Utils} from "../Utils";
Utils.runningFromConsole = true Utils.runningFromConsole = true
import * as https from "https"; import * as https from "https";
import {LayerConfigJson} from "../Customizations/JSON/LayerConfigJson"; import {LayerConfigJson} from "../Customizations/JSON/LayerConfigJson";